Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ajaxGetImportTemplate to find instrument import templates in addons #1349

Merged
merged 10 commits into from
May 20, 2019
62 changes: 40 additions & 22 deletions bika/lims/exportimport/dataimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
# Copyright 2018-2019 by it's authors.
# Some rights reserved, see README and LICENSE.

import json
import os.path
import plone
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from bika.lims import bikaMessageFactory as _
from bika.lims.browser import BrowserView
from bika.lims.exportimport import instruments
from bika.lims.exportimport.instruments import get_instrument_import_interfaces
Expand All @@ -29,11 +31,9 @@
from Products.Archetypes.public import DisplayList
from Products.CMFCore.utils import getToolByName
from zope.interface import implements
from pkg_resources import *
from pkg_resources import resource_filename
from pkg_resources import resource_listdir
from zope.component import getAdapters
import json

import plone


class SetupDataSetList:
Expand All @@ -46,7 +46,7 @@ def __init__(self, context):
def __call__(self, projectname="bika.lims"):
datasets = []
for f in resource_listdir(projectname, 'setupdata'):
fn = f+".xlsx"
fn = f + ".xlsx"
try:
if fn in resource_listdir(projectname, 'setupdata/%s' % f):
datasets.append({"projectname": projectname, "dataset": f})
Expand Down Expand Up @@ -107,26 +107,35 @@ def __call__(self):

def getInstruments(self):
bsc = getToolByName(self, 'bika_setup_catalog')
items = [('', '...Choose an Instrument...')] + [(o.UID, o.Title) for o in
bsc(portal_type = 'Instrument',
is_active = True)]
brains = bsc(portal_type='Instrument', is_active=True)
items = [('', '...Choose an Instrument...')]
for item in brains:
items.append((item.UID, item.Title))
items.sort(lambda x, y: cmp(x[1].lower(), y[1].lower()))
return DisplayList(list(items))


class ajaxGetImportTemplate(BrowserView):

def __call__(self):
plone.protect.CheckAuthenticator(self.request)
exim = self.request.get('exim').replace(".", "/")
exim = self.request.get('exim')
core_instrument = self.is_exim_in_core(exim)
exim = exim.replace(".", "/")
# If a specific template for this instrument doesn't exist yet,
# use the default template for instrument results file import located
# at bika/lims/exportimport/instruments/instrument.pt
import os.path
instrpath = os.path.join("exportimport", "instruments")
templates_dir = resource_filename("bika.lims", instrpath)
fname = "%s/%s_import.pt" % (templates_dir, exim)
# if exim.startswith('senaite/instruments'):
if core_instrument:
instrpath = os.path.join("exportimport", "instruments")
templates_dir = resource_filename("bika.lims", instrpath)
fname = "%s/%s_import.pt" % (templates_dir, exim)
else:
instrpath = '/'.join(exim.split('/')[2:-2])
templates_dir = resource_filename("senaite.instruments", instrpath)
fname = "{}/{}_import.pt".format(templates_dir, exim.split('/')[-1])
if os.path.isfile(fname):
return ViewPageTemplateFile("instruments/%s_import.pt" % exim)(self)
return ViewPageTemplateFile(fname)(self)
else:
return ViewPageTemplateFile("instruments/instrument.pt")(self)

Expand All @@ -136,12 +145,21 @@ def getAnalysisServicesDisplayList(self):
text to be displayed.
'''
bsc = getToolByName(self, 'bika_setup_catalog')
items = [('', '')] + [(o.getObject().Keyword, o.Title) for o in
bsc(portal_type = 'AnalysisService',
is_active = True)]
brains = bsc(portal_type='AnalysisService')
items = []
for item in brains:
items.append((item.getKeyword, item.Title))
items.sort(lambda x, y: cmp(x[1].lower(), y[1].lower()))
return DisplayList(list(items))

def is_exim_in_core(self, exim):
portal_tool = plone.api.portal.get_tool('portal_setup')
profiles = portal_tool.listProfileInfo()
for profile in profiles:
if exim.startswith(profile['product']):
return False
return True


class ajaxGetImportInterfaces(BrowserView):
""" Returns a json list with the interfaces assigned to the instrument
Expand All @@ -157,11 +175,11 @@ def __call__(self):
except Forbidden:
return json.dumps(interfaces)

from bika.lims.exportimport import instruments
bsc = getToolByName(self, 'bika_setup_catalog')
instrument=bsc(portal_type='Instrument',
UID=self.request.get('instrument_uid', ''),
is_active=True,)
instrument = bsc(
portal_type='Instrument',
UID=self.request.get('instrument_uid', ''),
is_active=True,)
if instrument and len(instrument) == 1:
instrument = instrument[0].getObject()
for i in instrument.getImportDataInterface():
Expand Down