From 0cc5c08c4dad1d67ebbc92461aa7c516bf18b15e Mon Sep 17 00:00:00 2001 From: Mike Metcalfe Date: Mon, 29 Apr 2019 16:40:51 +0200 Subject: [PATCH 1/6] PEP8 changes only --- bika/lims/exportimport/dataimport.py | 36 +++++++++++++++------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/bika/lims/exportimport/dataimport.py b/bika/lims/exportimport/dataimport.py index 9c6add5cf3..9c11a12c01 100644 --- a/bika/lims/exportimport/dataimport.py +++ b/bika/lims/exportimport/dataimport.py @@ -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 @@ -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: @@ -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}) @@ -107,12 +107,14 @@ 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): @@ -121,7 +123,6 @@ def __call__(self): # 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) @@ -136,9 +137,10 @@ 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.UID, item.Title)) items.sort(lambda x, y: cmp(x[1].lower(), y[1].lower())) return DisplayList(list(items)) @@ -157,11 +159,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(): From 8d13e06eee007f25e30e30cd87954bfd18949026 Mon Sep 17 00:00:00 2001 From: Mike Metcalfe Date: Mon, 29 Apr 2019 17:10:57 +0200 Subject: [PATCH 2/6] Refactor ajaxGetImportTemplate to find instrument import templates in addons --- bika/lims/exportimport/dataimport.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/bika/lims/exportimport/dataimport.py b/bika/lims/exportimport/dataimport.py index 9c11a12c01..c5b0750099 100644 --- a/bika/lims/exportimport/dataimport.py +++ b/bika/lims/exportimport/dataimport.py @@ -118,16 +118,23 @@ def getInstruments(self): class ajaxGetImportTemplate(BrowserView): def __call__(self): + import pdb; pdb.set_trace() plone.protect.CheckAuthenticator(self.request) exim = self.request.get('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 - 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'): + #TODO find a better way to see check if the instrument is in core + instrpath = '/'.join(exim.split('/')[2:-2]) + templates_dir = resource_filename("senaite.instruments", instrpath) + fname = "{}/{}_import.pt".format(templates_dir, exim.split('/')[-1]) + else: + instrpath = os.path.join("exportimport", "instruments") + templates_dir = resource_filename("bika.lims", instrpath) + fname = "%s/%s_import.pt" % (templates_dir, exim) if os.path.isfile(fname): - return ViewPageTemplateFile("instruments/%s_import.pt" % exim)(self) + return ViewPageTemplateFile(fname)(self) else: return ViewPageTemplateFile("instruments/instrument.pt")(self) From 3f7982392e897e718d2cec5658ef285ed0445043 Mon Sep 17 00:00:00 2001 From: Mike Metcalfe Date: Mon, 29 Apr 2019 17:16:47 +0200 Subject: [PATCH 3/6] Removed pdb --- bika/lims/exportimport/dataimport.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bika/lims/exportimport/dataimport.py b/bika/lims/exportimport/dataimport.py index c5b0750099..b175ee1326 100644 --- a/bika/lims/exportimport/dataimport.py +++ b/bika/lims/exportimport/dataimport.py @@ -118,7 +118,6 @@ def getInstruments(self): class ajaxGetImportTemplate(BrowserView): def __call__(self): - import pdb; pdb.set_trace() plone.protect.CheckAuthenticator(self.request) exim = self.request.get('exim').replace(".", "/") # If a specific template for this instrument doesn't exist yet, From 28ed4f75ca9823ae892fc8fa683642c372592ecc Mon Sep 17 00:00:00 2001 From: Mike Metcalfe Date: Mon, 13 May 2019 12:57:41 +0200 Subject: [PATCH 4/6] Introduced is_exim_in_core method to find appropriate import instrument template --- bika/lims/exportimport/dataimport.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/bika/lims/exportimport/dataimport.py b/bika/lims/exportimport/dataimport.py index b175ee1326..ac53a67ee9 100644 --- a/bika/lims/exportimport/dataimport.py +++ b/bika/lims/exportimport/dataimport.py @@ -119,19 +119,21 @@ class ajaxGetImportTemplate(BrowserView): def __call__(self): plone.protect.CheckAuthenticator(self.request) - exim = self.request.get('exim').replace(".", "/") + exim = self.request.get('exim') + core_instrument = 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 - if exim.startswith('senaite/instruments'): - #TODO find a better way to see check if the instrument is in core - instrpath = '/'.join(exim.split('/')[2:-2]) - templates_dir = resource_filename("senaite.instruments", instrpath) - fname = "{}/{}_import.pt".format(templates_dir, exim.split('/')[-1]) - else: + # 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(fname)(self) else: @@ -150,6 +152,14 @@ def getAnalysisServicesDisplayList(self): items.sort(lambda x, y: cmp(x[1].lower(), y[1].lower())) return DisplayList(list(items)) + def is_exim_in_core(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 From 2231b8999d4eebd3f52b443c25629c59d20d8be5 Mon Sep 17 00:00:00 2001 From: Mike Metcalfe Date: Mon, 13 May 2019 13:28:33 +0200 Subject: [PATCH 5/6] Fixed bug introduced in previous commit --- bika/lims/exportimport/dataimport.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bika/lims/exportimport/dataimport.py b/bika/lims/exportimport/dataimport.py index ac53a67ee9..e8fd630a3b 100644 --- a/bika/lims/exportimport/dataimport.py +++ b/bika/lims/exportimport/dataimport.py @@ -120,7 +120,7 @@ class ajaxGetImportTemplate(BrowserView): def __call__(self): plone.protect.CheckAuthenticator(self.request) exim = self.request.get('exim') - core_instrument = is_exim_in_core(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 @@ -152,7 +152,7 @@ def getAnalysisServicesDisplayList(self): items.sort(lambda x, y: cmp(x[1].lower(), y[1].lower())) return DisplayList(list(items)) - def is_exim_in_core(exim): + def is_exim_in_core(self, exim): portal_tool = plone.api.portal.get_tool('portal_setup') profiles = portal_tool.listProfileInfo() for profile in profiles: From f5fd0923799f416fd583b7f47933d9097a2e2703 Mon Sep 17 00:00:00 2001 From: Mike Metcalfe Date: Mon, 20 May 2019 11:18:08 +0200 Subject: [PATCH 6/6] Removed bug in getAnalysisServicesDisplayList introduced in previous commit --- bika/lims/exportimport/dataimport.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bika/lims/exportimport/dataimport.py b/bika/lims/exportimport/dataimport.py index e8fd630a3b..763acd98cc 100644 --- a/bika/lims/exportimport/dataimport.py +++ b/bika/lims/exportimport/dataimport.py @@ -148,7 +148,7 @@ def getAnalysisServicesDisplayList(self): brains = bsc(portal_type='AnalysisService') items = [] for item in brains: - items.append((item.UID, item.Title)) + items.append((item.getKeyword, item.Title)) items.sort(lambda x, y: cmp(x[1].lower(), y[1].lower())) return DisplayList(list(items))