Changeset 12306 for main/waeup.ikoba/trunk/src/waeup/ikoba/utils
- Timestamp:
- 24 Dec 2014, 07:38:53 (10 years ago)
- Location:
- main/waeup.ikoba/trunk/src/waeup/ikoba/utils
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.ikoba/trunk/src/waeup/ikoba/utils/converters.py
r12261 r12306 30 30 from zope.schema.interfaces import IList 31 31 from waeup.ikoba.interfaces import ( 32 IObjectConverter, I ResultEntryField, IFieldConverter, SubjectSource,33 GradeSource,DELETION_MARKER, IGNORE_MARKER)32 IObjectConverter, IProductOptionEntryField, IFieldConverter, 33 DELETION_MARKER, IGNORE_MARKER) 34 34 from waeup.ikoba.schema.interfaces import IPhoneNumber 35 from waeup.ikoba.schoolgrades import ResultEntry35 from waeup.ikoba.schoolgrades import ProductOptionEntry 36 36 37 37 class ExtendedCheckBoxWidget(CheckBoxWidget): … … 175 175 return result 176 176 177 class ResultEntryConverter(grok.Adapter):178 grok.context(I ResultEntryField)177 class ProductOptionEntryConverter(grok.Adapter): 178 grok.context(IProductOptionEntryField) 179 179 grok.implements(IFieldConverter) 180 180 181 181 def request_data(self, name, value, schema_field, prefix='', 182 182 mode='create'): 183 """Turn CSV values into ResultEntry-compatible form data.184 185 Expects as `value` a _string_ like ``(u'my subject',186 u'my grade')`` and turns it into some dict like::183 """Turn CSV values into ProductOptionEntry-compatible form data. 184 185 Expects as `value` a _string_ like ``(u'mytitle', 186 u'myfee')`` and turns it into some dict like:: 187 187 188 188 { 189 'form.grade.subject': u'9234896395...', 190 'form.grade.grade': u'7e67e9e777..' 189 'form.option.title': u'9234896395...', 190 'form.option.fee': 7698769 191 'form.option.currency': u'7e67e9e777..' 191 192 } 192 193 193 194 where the values are tokens from appropriate sources. 194 195 195 Such dicts can be transformed into real ResultEntry objects by196 Such dicts can be transformed into real ProductOptionEntry objects by 196 197 input widgets used in converters. 197 198 """ 198 199 try: 199 entry = ResultEntry.from_string(value)200 subj, grade = entry.subject, entry.grade200 entry = ProductOptionEntry.from_string(value) 201 title, fee, currency = entry.title, entry.fee, entry.currency 201 202 except: 202 203 return {prefix: value} 203 # web forms send tokens instead of real values204 s_token = SubjectSource().factory.getToken(subj)205 g_token = GradeSource().factory.getToken(grade)206 204 result = { 207 "%ssubject" % (prefix): s_token, 208 "%sgrade" % (prefix): g_token, 205 "%stitle" % (prefix): title, 206 "%sfee" % (prefix): fee, 207 "%scurrency" % (prefix): currency, 209 208 } 210 209 return result -
main/waeup.ikoba/trunk/src/waeup/ikoba/utils/tests/test_converters.py
r11954 r12306 23 23 import tempfile 24 24 import unittest 25 from decimal import Decimal 25 26 from zope import schema 26 27 from zope.component import provideUtility … … 35 36 from waeup.ikoba.testing import FunctionalLayer, FunctionalTestCase 36 37 from waeup.ikoba.interfaces import ( 37 SimpleIkobaVocabulary, SubjectSource, GradeSource,IFieldConverter,38 SimpleIkobaVocabulary, IFieldConverter, 38 39 DELETION_MARKER, IGNORE_MARKER) 39 from waeup.ikoba.schoolgrades import ResultEntryField40 from waeup.ikoba.schoolgrades import ProductOptionEntryField 40 41 from waeup.ikoba.utils.converters import ( 41 42 IObjectConverter, IFieldConverter, DefaultFieldConverter, 42 ListFieldConverter, PhoneNumberFieldConverter, ResultEntryConverter,43 ListFieldConverter, PhoneNumberFieldConverter, ProductOptionEntryConverter, 43 44 DefaultObjectConverter) 44 45 from waeup.ikoba.utils.helpers import attrs_to_fields … … 91 92 vocabulary = car_nums, 92 93 ) 93 grades = schema.List(94 title = u' School Grades',95 value_type = ResultEntryField(),94 options = schema.List( 95 title = u'Options', 96 value_type = ProductOptionEntryField(), 96 97 required = True, 97 98 default = [], … … 136 137 obj2 = ListFieldConverter(None) 137 138 obj3 = PhoneNumberFieldConverter(None) 138 obj4 = ResultEntryConverter(None)139 obj4 = ProductOptionEntryConverter(None) 139 140 verify.verifyObject(IFieldConverter, obj1) 140 141 verify.verifyObject(IFieldConverter, obj2) … … 144 145 verify.verifyClass(IFieldConverter, ListFieldConverter) 145 146 verify.verifyClass(IFieldConverter, PhoneNumberFieldConverter) 146 verify.verifyClass(IFieldConverter, ResultEntryConverter)147 verify.verifyClass(IFieldConverter, ProductOptionEntryConverter) 147 148 return 148 149 … … 384 385 return 385 386 386 def test_list_of_ resultentries(self):387 def test_list_of_optionentries(self): 387 388 # We can handle lists of result entries 388 389 converter = IObjectConverter(IContact) 389 390 # get currently valid values 390 s_src, g_src = SubjectSource(), GradeSource() 391 s_val1, s_val2 = list(s_src.factory.getValues())[0:2] 392 g_val1, g_val2 = list(g_src.factory.getValues())[0:2] 393 req_string = u"[('%s', '%s'), ('%s', '%s')]" % ( 394 s_val1, g_val1, s_val2, g_val2) 395 err, inv_err, data = converter.fromStringDict( 396 {"grades": req_string, 397 }, 398 'contact') 399 result_grades = data['grades'] 400 self.assertTrue(isinstance(result_grades, list)) 401 self.assertEqual(len(result_grades), 2) 402 self.assertEqual(result_grades[0].subject, s_val1) 403 self.assertEqual(result_grades[0].grade, g_val1) 391 req_string = u"[(u'T1', u'55.6', u'usd'), (u'T2', u'66.6', u'eur')]" 392 err, inv_err, data = converter.fromStringDict( 393 {"options": req_string,}, 'contact') 394 result_options = data['options'] 395 self.assertTrue(isinstance(result_options, list)) 396 self.assertEqual(len(result_options), 2) 397 self.assertEqual(result_options[0].title, 'T1') 398 self.assertEqual(result_options[0].fee, Decimal('55.6')) 399 self.assertEqual(result_options[0].currency, 'usd') 400 self.assertEqual(result_options[1].title, 'T2') 401 self.assertEqual(result_options[1].fee, Decimal('66.6')) 402 self.assertEqual(result_options[1].currency, 'eur') 404 403 return 405 404 … … 420 419 converter = IObjectConverter(IContact) 421 420 err, inv_err, data = converter.fromStringDict( 422 {" grades": DELETION_MARKER,421 {"options": DELETION_MARKER, 423 422 "friends": DELETION_MARKER}, 424 423 'contact', mode='update') 425 424 # grades are about to be set to default, friends to None 426 self.assertEqual(data, {' grades': [], 'friends': None})427 return 425 self.assertEqual(data, {'options': [], 'friends': None}) 426 return -
main/waeup.ikoba/trunk/src/waeup/ikoba/utils/utils.py
r12297 r12306 107 107 'no': 'no contract', 108 108 } 109 110 EXAM_SUBJECTS_DICT = {111 'math': 'Mathematics',112 'computer_science': 'Computer Science',113 }114 115 #: Exam grades. The tuple is sorted as it should be displayed in116 #: select boxes.117 EXAM_GRADES = (118 ('A', 'Best'),119 ('B', 'Better'),120 ('C', 'Good'),121 )122 109 123 110 PAYMENT_CATEGORIES = {
Note: See TracChangeset for help on using the changeset viewer.