Skip to content

Commit

Permalink
Copied extentension fields from senaite.core-lunga-timeseries branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Lunga001 committed Sep 10, 2024
1 parent 88d1d13 commit 2ef94f0
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 0 deletions.
Empty file.
106 changes: 106 additions & 0 deletions src/senaite/timeseries/extenders/baseanalysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# -*- coding: utf-8 -*-

from Products.Archetypes.Widget import StringWidget
from Products.Archetypes.utils import DisplayList
from archetypes.schemaextender.interfaces import IBrowserLayerAwareExtender
from archetypes.schemaextender.interfaces import ISchemaExtender
from zope.component import adapts
from zope.interface import implementer

from .fields import ExtStringField, ExtRecordsField
from bika.lims.browser.widgets.recordswidget import RecordsWidget
from bika.lims.interfaces import IBaseAnalysis
from senaite.timeseries.config import _
from senaite.timeseries.interfaces import ISenaiteTimeseriesLayer


graph_title_field = ExtStringField(
"GraphTitle",
schemata="Result Options",
widget=StringWidget(
label=_("Graph Title"),
description=_("Title that appears above the time series graph"),
),
)
graph_x_axis_title_field = ExtStringField(
"GraphXAxisTitle",
schemata="Result Options",
widget=StringWidget(
label=_("Graph X-Axis Title"),
description=_("Title that appears on the X-Axis of the time series graph"),
),
)
graph_y_axis_title_field = ExtStringField(
"GraphYAxisTitle",
schemata="Result Options",
widget=StringWidget(
label=_("Graph Y-Axis Title"),
description=_("Title that appears on the Y-Axis of the time series graph"),
),
)
time_series_columns_field = ExtRecordsField(
"TimeSeriesColumns",
schemata="Result Options",
subfields=("ColumnType", "ColumnTitle", "ColumnDataType",),
subfield_labels={
"ColumnType": _("Column Type"),
"ColumnTitle": _("Column Title"),
"ColumnDataType": _("Column Data Type"),
},
subfield_validators={},
subfield_types={
"ColumnType": "string",
"ColumnTitle": "string",
"ColumnDataType": "string",
},
subfield_sizes={
"ColumnType": 1,
# 'ColumnTitle': 25,
"ColumnDataType": 1,
},
subfield_maxlength={
"ColumnType": 1,
"ColumnTitle": 25,
"ColumnDataType": 1},
subfield_vocabularies={
"ColumnType": DisplayList(
(("index", _("Index")),
("data", _("Data")),
("average", _("Average")),)
),
"ColumnDataType": DisplayList(
(("float", _("Float")),
("number", _("Number")),
("date", _("Date")),)
),
},
widget=RecordsWidget(
label=_("Time Series Columns"),
description=_(
"List of possible final results. When set, no custom result is "
"allowed on results entry and user has to choose from these values"
),
),
)


@implementer(ISchemaExtender, IBrowserLayerAwareExtender)
class AnalysisRequestSchemaExtender(object):
adapts(IBaseAnalysis)
layer = ISenaiteTimeseriesLayer

fields = [
graph_title_field,
graph_x_axis_title_field,
graph_y_axis_title_field,
time_series_columns_field,
]

def __init__(self, context):
self.context = context

def getOrder(self, schematas):
return schematas

def getFields(self):
return self.fields
10 changes: 10 additions & 0 deletions src/senaite/timeseries/extenders/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<configure xmlns:browser="http://namespaces.zope.org/browser"
xmlns="http://namespaces.zope.org/zope"
i18n_domain="bika.aquaculture">

<include package="archetypes.schemaextender"/>
<adapter name='bika.aquaculture.analysisrequest'
provides="archetypes.schemaextender.interfaces.ISchemaExtender"
factory=".analysisrequest.AnalysisRequestSchemaExtender"/>

</configure>
129 changes: 129 additions & 0 deletions src/senaite/timeseries/extenders/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# -*- coding: utf-8 -*-
#
# This file is part of SENAITE.CORE.
#
# SENAITE.CORE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright 2018-2021 by it's authors.
# Some rights reserved, see README and LICENSE.

import six
from archetypes.schemaextender.interfaces import IExtensionField
from Products.Archetypes import public
from senaite.core.browser.fields.datetime import DateTimeField
from senaite.core.browser.fields.record import RecordField
from senaite.core.browser.fields.records import RecordsField
from bika.lims.browser.fields import UIDReferenceField
from zope.interface import implements
from zope.site.hooks import getSite


class ExtensionField(object):
"""Mix-in class to make Archetypes fields not depend on generated
accessors and mutators, and use AnnotationStorage by default.
"""

implements(IExtensionField)
storage = public.AnnotationStorage()

def __init__(self, *args, **kwargs):
super(ExtensionField, self).__init__(*args, **kwargs)
self.args = args
self.kwargs = kwargs

def getAccessor(self, instance):
def accessor():
if self.getType().endswith("ReferenceField"):
return self.get(instance.__of__(getSite()))
else:
return self.get(instance)

return accessor

def getEditAccessor(self, instance):
def edit_accessor():
if self.getType().endswith("ReferenceField"):
return self.getRaw(instance.__of__(getSite()))
else:
return self.getRaw(instance)

return edit_accessor

def getMutator(self, instance):
def mutator(value, **kw):
if self.getType().endswith("ReferenceField"):
self.set(instance.__of__(getSite()), value)
else:
self.set(instance, value)

return mutator

def getIndexAccessor(self, instance):
name = getattr(self, "index_method", None)
if name is None or name == "_at_accessor":
return self.getAccessor(instance)
elif name == "_at_edit_accessor":
return self.getEditAccessor(instance)
elif not isinstance(name, six.string_types):
raise ValueError("Bad index accessor value: %r", name)
else:
return getattr(instance, name)


class ExtBooleanField(ExtensionField, public.BooleanField):
"Field extender"


class ExtComputedField(ExtensionField, public.ComputedField):
"Field extender"


class ExtDateTimeField(ExtensionField, DateTimeField):
"Field extender"


class ExtFloatField(ExtensionField, public.FloatField):
"Field extender"


class ExtIntegerField(ExtensionField, public.IntegerField):
"Field extender"


class ExtLinesField(ExtensionField, public.LinesField):
"Field extender"


class ExtRecordField(ExtensionField, RecordField):
"Field extender"


class ExtRecordsField(ExtensionField, RecordsField):
"Field extender"


class ExtReferenceField(ExtensionField, public.ReferenceField):
"Field extender"


class ExtStringField(ExtensionField, public.StringField):
"Field extender"


class ExtTextField(ExtensionField, public.TextField):
"Field extender"


class ExtUIDReferenceField(ExtensionField, UIDReferenceField):
"Field extender"

0 comments on commit 2ef94f0

Please sign in to comment.