[47] | 1 | #-*- mode: python; mode: fold -*- |
---|
[295] | 2 | |
---|
| 3 | from cgi import escape |
---|
| 4 | |
---|
[22] | 5 | from Globals import InitializeClass |
---|
[199] | 6 | ##from Products.CPSSchemas.Widget import CPSWidgetType |
---|
[295] | 7 | from Products.CMFCore.utils import getToolByName |
---|
| 8 | from Products.CPSSchemas.BasicWidgets import CPSWidget, CPSStringWidget, CPSEmailWidget |
---|
| 9 | from Products.CPSSchemas.BasicWidgets import renderHtmlTag,CPSSelectWidget, CPSStringWidget |
---|
[22] | 10 | from Products.CPSSchemas.ExtendedWidgets import CPSDateTimeWidget |
---|
[199] | 11 | from Products.CPSSchemas.Widget import widgetRegistry |
---|
| 12 | ##from Products.CPSSchemas.WidgetTypesTool import WidgetTypeRegistry |
---|
[22] | 13 | from DateTime.DateTime import DateTime |
---|
| 14 | from AccessControl import getSecurityManager |
---|
| 15 | |
---|
| 16 | from re import compile |
---|
| 17 | |
---|
| 18 | from zLOG import LOG, DEBUG |
---|
| 19 | |
---|
[295] | 20 | class CPSSelectWidgetForRecord(CPSSelectWidget): ###( |
---|
| 21 | """Select widget. with record names""" |
---|
| 22 | meta_type = 'Select Widget for Records' |
---|
| 23 | |
---|
| 24 | field_types = ('CPS String Field',) |
---|
| 25 | field_inits = ({'is_searchabletext': 1,},) |
---|
| 26 | |
---|
| 27 | _properties = CPSSelectWidget._properties + ( |
---|
| 28 | {'id': 'record_id', 'type': 'string', 'mode': 'w', |
---|
| 29 | 'label': 'Record Id', 'is_required' : 1}, |
---|
| 30 | ) |
---|
| 31 | |
---|
| 32 | def render(self, mode, datastructure, **kw): |
---|
| 33 | """Render in mode from datastructure.""" |
---|
| 34 | value = datastructure[self.getWidgetId()] |
---|
| 35 | vocabulary = self._getVocabulary(datastructure) |
---|
| 36 | portal = getToolByName(self, 'portal_url').getPortalObject() |
---|
| 37 | cpsmcat = portal.translation_service |
---|
| 38 | if mode == 'view': |
---|
| 39 | if self.translated: |
---|
| 40 | return escape(cpsmcat(vocabulary.getMsgid(value, value)).encode('ISO-8859-15', 'ignore')) |
---|
| 41 | else: |
---|
| 42 | return escape(vocabulary.get(value, value)) |
---|
| 43 | elif mode == 'edit': |
---|
| 44 | html_widget_id = self.getHtmlWidgetId() |
---|
| 45 | res = renderHtmlTag('select', |
---|
| 46 | name='%s.%s:records' % (self.record_id,html_widget_id), |
---|
| 47 | id=html_widget_id) |
---|
| 48 | in_selection = 0 |
---|
| 49 | for k, v in vocabulary.items(): |
---|
| 50 | if self.translated: |
---|
| 51 | kw = {'value': k, |
---|
| 52 | 'contents': cpsmcat(vocabulary.getMsgid(k, k)).encode('ISO-8859-15', 'ignore') |
---|
| 53 | } |
---|
| 54 | else: |
---|
| 55 | kw = {'value': k, 'contents': v} |
---|
| 56 | if value == k: |
---|
| 57 | kw['selected'] = 'selected' |
---|
| 58 | in_selection = 1 |
---|
| 59 | res += renderHtmlTag('option', **kw) |
---|
| 60 | if value and not in_selection: |
---|
| 61 | kw = {'value': value, 'contents': 'invalid: '+ str(value), |
---|
| 62 | 'selected': 'selected'} |
---|
| 63 | res += renderHtmlTag('option', **kw) |
---|
| 64 | res += '</select>' |
---|
| 65 | return res |
---|
| 66 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 67 | |
---|
| 68 | InitializeClass(CPSSelectWidgetForRecord) |
---|
| 69 | |
---|
| 70 | widgetRegistry.register(CPSSelectWidgetForRecord) |
---|
| 71 | |
---|
| 72 | ###) |
---|
| 73 | |
---|
| 74 | class CPSStringWidgetForRecord(CPSStringWidget): |
---|
| 75 | """String widget.""" |
---|
| 76 | meta_type = 'String Widget For Record' |
---|
| 77 | |
---|
| 78 | field_types = ('CPS String Field',) |
---|
| 79 | field_inits = ({'is_searchabletext': 1,},) |
---|
| 80 | _properties = CPSStringWidget._properties + ( |
---|
| 81 | {'id': 'record_id', 'type': 'string', 'mode': 'w', |
---|
| 82 | 'label': 'Record Id', 'is_required' : 1}, |
---|
| 83 | ) |
---|
| 84 | |
---|
| 85 | def render(self, mode, datastructure, **kw): |
---|
| 86 | """Render in mode from datastructure.""" |
---|
| 87 | value = datastructure[self.getWidgetId()] |
---|
| 88 | if mode == 'view': |
---|
| 89 | return escape(value) |
---|
| 90 | elif mode == 'edit': |
---|
| 91 | # XXX TODO should use an other name than kw ! |
---|
| 92 | # XXX change this everywhere |
---|
| 93 | html_widget_id = self.getHtmlWidgetId() |
---|
| 94 | kw = {'type': 'text', |
---|
| 95 | 'id' : html_widget_id, |
---|
| 96 | 'name': '%s.%s:records' % (self.record_id,html_widget_id), |
---|
| 97 | 'value': escape(value), |
---|
| 98 | 'size': self.display_width, |
---|
| 99 | } |
---|
| 100 | if self.size_max: |
---|
| 101 | kw['maxlength'] = self.size_max |
---|
| 102 | return renderHtmlTag('input', **kw) |
---|
| 103 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 104 | |
---|
| 105 | InitializeClass(CPSStringWidgetForRecord) |
---|
| 106 | |
---|
| 107 | widgetRegistry.register(CPSStringWidgetForRecord) |
---|
| 108 | |
---|
[47] | 109 | class StudentIdWidget(CPSStringWidget): ###( |
---|
[22] | 110 | """ StudentId Widget""" |
---|
| 111 | meta_type = "StudentId Widget" |
---|
[57] | 112 | digits = 8 |
---|
| 113 | digits_str = "N"*digits |
---|
| 114 | letters = 2 |
---|
| 115 | letters_str = "L"*letters |
---|
[22] | 116 | |
---|
| 117 | def validate(self, datastructure, **kw): |
---|
| 118 | """Validate datastructure and update datamodel.""" |
---|
| 119 | |
---|
| 120 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 121 | if not valid: |
---|
| 122 | return 0 |
---|
| 123 | else: |
---|
| 124 | widget_id = self.getWidgetId() |
---|
| 125 | value = datastructure[widget_id] |
---|
| 126 | err = 0 |
---|
[57] | 127 | if not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()): |
---|
| 128 | err = 'invalid Student Id in the format: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str) |
---|
[22] | 129 | if err: |
---|
| 130 | datastructure.setError(widget_id, err) |
---|
| 131 | else: |
---|
| 132 | datamodel = datastructure.getDataModel() |
---|
| 133 | datamodel[self.fields[0]] = value |
---|
| 134 | |
---|
| 135 | return not err |
---|
| 136 | |
---|
| 137 | InitializeClass(StudentIdWidget) |
---|
| 138 | |
---|
[199] | 139 | widgetRegistry.register(StudentIdWidget) |
---|
[22] | 140 | |
---|
[199] | 141 | ##Class StudentIdWidgetType(CPSWidgetType): |
---|
| 142 | ## """Student Id String widget type.""" |
---|
| 143 | ## meta_type = "StudentId Widget Type" |
---|
| 144 | ## cls = StudentIdWidget |
---|
| 145 | |
---|
[47] | 146 | ###) |
---|
| 147 | |
---|
| 148 | class ScratchcardPinWidget(CPSStringWidget): ###( |
---|
[22] | 149 | """ ScratchcardPin Widget""" |
---|
[199] | 150 | meta_type = "Scratchcard Pin Widget" |
---|
[204] | 151 | valid_pins = ['12345678', |
---|
| 152 | '23456789', |
---|
| 153 | '34567890', |
---|
| 154 | '45678901', |
---|
[22] | 155 | ] |
---|
| 156 | |
---|
| 157 | def validate(self, datastructure, **kw): |
---|
| 158 | """Validate datastructure and update datamodel.""" |
---|
| 159 | |
---|
| 160 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 161 | if not valid: |
---|
| 162 | return 0 |
---|
| 163 | else: |
---|
| 164 | widget_id = self.getWidgetId() |
---|
| 165 | value = datastructure[widget_id] |
---|
| 166 | err = 0 |
---|
[204] | 167 | #import pdb; pdb.set_trace() |
---|
[57] | 168 | if not value in self.valid_pins: |
---|
[22] | 169 | err = 'invalid Pin' |
---|
| 170 | if err: |
---|
| 171 | datastructure.setError(widget_id, err) |
---|
| 172 | else: |
---|
| 173 | datamodel = datastructure.getDataModel() |
---|
| 174 | datamodel[self.fields[0]] = value |
---|
| 175 | return not err |
---|
| 176 | |
---|
| 177 | InitializeClass(ScratchcardPinWidget) |
---|
[199] | 178 | widgetRegistry.register(ScratchcardPinWidget) |
---|
[22] | 179 | |
---|
[47] | 180 | |
---|
| 181 | ###) |
---|
| 182 | |
---|
[22] | 183 | ########### |
---|
| 184 | |
---|