#-*- mode: python; mode: fold -*- from cgi import escape from Globals import InitializeClass ##from Products.CPSSchemas.Widget import CPSWidgetType from Products.CMFCore.utils import getToolByName from Products.CPSSchemas.BasicWidgets import CPSWidget, CPSStringWidget, CPSEmailWidget from Products.CPSSchemas.BasicWidgets import renderHtmlTag,CPSSelectWidget, CPSStringWidget from Products.CPSSchemas.ExtendedWidgets import CPSDateTimeWidget from Products.CPSSchemas.Widget import widgetRegistry ##from Products.CPSSchemas.WidgetTypesTool import WidgetTypeRegistry from DateTime.DateTime import DateTime from AccessControl import getSecurityManager from re import compile from zLOG import LOG, DEBUG class CPSSelectWidgetForRecord(CPSSelectWidget): ###( """Select widget. with record names""" meta_type = 'Select Widget for Records' field_types = ('CPS String Field',) field_inits = ({'is_searchabletext': 1,},) _properties = CPSSelectWidget._properties + ( {'id': 'record_id', 'type': 'string', 'mode': 'w', 'label': 'Record Id', 'is_required' : 1}, ) def render(self, mode, datastructure, **kw): """Render in mode from datastructure.""" value = datastructure[self.getWidgetId()] vocabulary = self._getVocabulary(datastructure) portal = getToolByName(self, 'portal_url').getPortalObject() cpsmcat = portal.translation_service if mode == 'view': if self.translated: return escape(cpsmcat(vocabulary.getMsgid(value, value)).encode('ISO-8859-15', 'ignore')) else: return escape(vocabulary.get(value, value)) elif mode == 'edit': html_widget_id = self.getHtmlWidgetId() res = renderHtmlTag('select', name='%s.%s:records' % (self.record_id,html_widget_id), id=html_widget_id) in_selection = 0 for k, v in vocabulary.items(): if self.translated: kw = {'value': k, 'contents': cpsmcat(vocabulary.getMsgid(k, k)).encode('ISO-8859-15', 'ignore') } else: kw = {'value': k, 'contents': v} if value == k: kw['selected'] = 'selected' in_selection = 1 res += renderHtmlTag('option', **kw) if value and not in_selection: kw = {'value': value, 'contents': 'invalid: '+ str(value), 'selected': 'selected'} res += renderHtmlTag('option', **kw) res += '' return res raise RuntimeError('unknown mode %s' % mode) InitializeClass(CPSSelectWidgetForRecord) widgetRegistry.register(CPSSelectWidgetForRecord) ###) class CPSStringWidgetForRecord(CPSStringWidget): ###( """String widget.""" meta_type = 'String Widget For Record' field_types = ('CPS String Field',) field_inits = ({'is_searchabletext': 1,},) _properties = CPSStringWidget._properties + ( {'id': 'record_id', 'type': 'string', 'mode': 'w', 'label': 'Record Id', 'is_required' : 1}, ) def render(self, mode, datastructure, **kw): """Render in mode from datastructure.""" value = datastructure[self.getWidgetId()] if mode == 'view': return escape(value) elif mode == 'edit': # XXX TODO should use an other name than kw ! # XXX change this everywhere html_widget_id = self.getHtmlWidgetId() kw = {'type': 'text', 'id' : html_widget_id, 'name': '%s.%s:records' % (self.record_id,html_widget_id), 'value': escape(value), 'size': self.display_width, } if self.size_max: kw['maxlength'] = self.size_max return renderHtmlTag('input', **kw) raise RuntimeError('unknown mode %s' % mode) InitializeClass(CPSStringWidgetForRecord) widgetRegistry.register(CPSStringWidgetForRecord) ###) class CertificateCourseIdWidget(CPSStringWidget): ###( """ CertificateCourseId Widget""" meta_type = "CertificateCourseId Widget" def validate(self, datastructure, **kw): """Validate datastructure and update datamodel.""" valid = CPSStringWidget.validate(self, datastructure, **kw) if not valid: return 0 else: widget_id = self.getWidgetId() value = datastructure[widget_id].upper() err = 0 c_ids = [c.id for c in self.portal_catalog({'meta_type': "Course"})] if hasattr(self.aq_parent,value): err = 'Course %s already exists' % (value) elif value not in c_ids: err = 'Course %s does not exist' % (value) if err: datastructure.setError(widget_id, err) else: datamodel = datastructure.getDataModel() datamodel[self.fields[0]] = value return not err InitializeClass(CertificateCourseIdWidget) widgetRegistry.register(CertificateCourseIdWidget) ###) class WAeUPIdWidget(CPSStringWidget): ###( """ WAeUPId Widget""" meta_type = "WAeUPId Widget" def validate(self, datastructure, **kw): """Validate datastructure and update datamodel.""" valid = CPSStringWidget.validate(self, datastructure, **kw) id_pat_str = r"\S" inv_id_pat = compile(r"^%s$" % id_pat_str) if not valid: return 0 else: widget_id = self.getWidgetId() value = datastructure[widget_id].upper() err = 0 if len(value.split()) > 1: err = 'Invalid Id (Id contains space(s)' elif hasattr(self.aq_parent,value): err = 'An object with the Id %s already exists in this context' % (value) if err: datastructure.setError(widget_id, err) else: datamodel = datastructure.getDataModel() datamodel[self.fields[0]] = value return not err InitializeClass(WAeUPIdWidget) widgetRegistry.register(WAeUPIdWidget) ###) class StudentIdWidget(CPSStringWidget): ###( """ StudentId Widget""" meta_type = "StudentId Widget" digits = 8 digits_str = "N"*digits letters = 2 letters_str = "L"*letters def validate(self, datastructure, **kw): """Validate datastructure and update datamodel.""" valid = CPSStringWidget.validate(self, datastructure, **kw) if not valid: return 0 else: widget_id = self.getWidgetId() value = datastructure[widget_id] err = 0 if not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()): err = 'invalid Student Id in the format: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str) if err: datastructure.setError(widget_id, err) else: datamodel = datastructure.getDataModel() datamodel[self.fields[0]] = value return not err InitializeClass(StudentIdWidget) widgetRegistry.register(StudentIdWidget) ##Class StudentIdWidgetType(CPSWidgetType): ## """Student Id String widget type.""" ## meta_type = "StudentId Widget Type" ## cls = StudentIdWidget ###) class ScratchcardPinWidget(CPSStringWidget): ###( """ ScratchcardPin Widget""" meta_type = "Scratchcard Pin Widget" valid_pins = ['12345678', '23456789', '34567890', '45678901', ] def validate(self, datastructure, **kw): """Validate datastructure and update datamodel.""" valid = CPSStringWidget.validate(self, datastructure, **kw) if not valid: return 0 else: widget_id = self.getWidgetId() value = datastructure[widget_id] err = 0 #import pdb; pdb.set_trace() if not value in self.valid_pins: err = 'invalid Pin' if err: datastructure.setError(widget_id, err) else: datamodel = datastructure.getDataModel() datamodel[self.fields[0]] = value return not err InitializeClass(ScratchcardPinWidget) widgetRegistry.register(ScratchcardPinWidget) ###) ###########