forked from senaite/senaite.core
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to edit the analysis service sort keys in the services listing (s…
…enaite#2131) * Added datamanager for analysis services * Allow to edit the service sortkey in the listing * Changelog updated * Set refetch flag * Added column help text * Change help key to be equal to the colum key Co-authored-by: Jordi Puiggené <jp@naralabs.com>
- Loading branch information
Showing
5 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
|
||
# -*- coding: utf-8 -*- | ||
|
||
from AccessControl import Unauthorized | ||
from bika.lims import api | ||
from bika.lims.interfaces import IAnalysisService | ||
from Products.Archetypes.utils import mapply | ||
from senaite.core import logger | ||
from senaite.core.datamanagers import DataManager | ||
from zope.component import adapter | ||
|
||
|
||
@adapter(IAnalysisService) | ||
class AnalysisServiceDataManager(DataManager): | ||
"""Data Manager for Analysis Services | ||
""" | ||
|
||
@property | ||
def fields(self): | ||
return api.get_fields(self.context) | ||
|
||
def is_field_readable(self, field): | ||
"""Checks if the field is readable | ||
""" | ||
return field.checkPermission("get", self.context) | ||
|
||
def is_field_writeable(self, field, context=None): | ||
"""Checks if the field is writeable | ||
""" | ||
if context is None: | ||
context = self.context | ||
return field.checkPermission("set", context) | ||
|
||
def get_field_by_name(self, name): | ||
"""Get the field by name | ||
""" | ||
field = self.fields.get(name) | ||
|
||
# try to fetch the field w/o the `get` prefix | ||
# this might be the case is some listings | ||
if field is None: | ||
# ensure we do not have the field setter as column | ||
name = name.split("get", 1)[-1] | ||
field = self.fields.get(name) | ||
|
||
return field | ||
|
||
def get(self, name): | ||
"""Get sample field | ||
""" | ||
# get the schema field | ||
field = self.get_field_by_name(name) | ||
|
||
# check if the field exists | ||
if field is None: | ||
raise AttributeError("Field '{}' not found".format(name)) | ||
|
||
# Check the permission of the field | ||
if not self.is_field_readable(field): | ||
raise Unauthorized("Field '{}' not readable!".format(name)) | ||
|
||
# return the value with the field accessor | ||
if hasattr(field, "getAccessor"): | ||
accessor = field.getAccessor(self.context) | ||
return accessor() | ||
else: | ||
# Set the value on the field directly | ||
return field.get(self.context) | ||
|
||
def set(self, name, value): | ||
"""Set sample field or analysis result | ||
""" | ||
# set of updated objects | ||
updated_objects = set() | ||
|
||
# get the schema field | ||
field = self.get_field_by_name(name) | ||
|
||
if field is None: | ||
raise AttributeError("Field '{}' not found".format(name)) | ||
|
||
# Check the permission of the field | ||
if not self.is_field_writeable(field): | ||
logger.error("Field '{}' not writeable!".format(name)) | ||
return [] | ||
# get the field mutator (works only for AT content types) | ||
if hasattr(field, "getMutator"): | ||
mutator = field.getMutator(self.context) | ||
mapply(mutator, value) | ||
else: | ||
# Set the value on the field directly | ||
field.set(self.context, value) | ||
|
||
updated_objects.add(self.context) | ||
|
||
# return a unified list of the updated objects | ||
return list(updated_objects) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters