Ignore:
Timestamp:
24 Dec 2014, 07:38:53 (10 years ago)
Author:
Henrik Bettermann
Message:

Turn ResultEntry? (school grades) components into ProductOptionEntry? components.

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  
    3030from zope.schema.interfaces import IList
    3131from waeup.ikoba.interfaces import (
    32     IObjectConverter, IResultEntryField, IFieldConverter, SubjectSource,
    33     GradeSource, DELETION_MARKER, IGNORE_MARKER)
     32    IObjectConverter, IProductOptionEntryField, IFieldConverter,
     33    DELETION_MARKER, IGNORE_MARKER)
    3434from waeup.ikoba.schema.interfaces import IPhoneNumber
    35 from waeup.ikoba.schoolgrades import ResultEntry
     35from waeup.ikoba.schoolgrades import ProductOptionEntry
    3636
    3737class ExtendedCheckBoxWidget(CheckBoxWidget):
     
    175175        return result
    176176
    177 class ResultEntryConverter(grok.Adapter):
    178     grok.context(IResultEntryField)
     177class ProductOptionEntryConverter(grok.Adapter):
     178    grok.context(IProductOptionEntryField)
    179179    grok.implements(IFieldConverter)
    180180
    181181    def request_data(self, name, value, schema_field, prefix='',
    182182                     mode='create'):
    183         """Turn CSV values into ResultEntry-compatible form data.
    184 
    185         Expects as `value` a _string_ like ``(u'mysubject',
    186         u'mygrade')`` 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::
    187187
    188188          {
    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..'
    191192            }
    192193
    193194        where the values are tokens from appropriate sources.
    194195
    195         Such dicts can be transformed into real ResultEntry objects by
     196        Such dicts can be transformed into real ProductOptionEntry objects by
    196197        input widgets used in converters.
    197198        """
    198199        try:
    199             entry = ResultEntry.from_string(value)
    200             subj, grade = entry.subject, entry.grade
     200            entry = ProductOptionEntry.from_string(value)
     201            title, fee, currency = entry.title, entry.fee, entry.currency
    201202        except:
    202203            return {prefix: value}
    203         # web forms send tokens instead of real values
    204         s_token = SubjectSource().factory.getToken(subj)
    205         g_token = GradeSource().factory.getToken(grade)
    206204        result = {
    207             "%ssubject" % (prefix): s_token,
    208             "%sgrade" % (prefix): g_token,
     205            "%stitle" % (prefix): title,
     206            "%sfee" % (prefix): fee,
     207            "%scurrency" % (prefix): currency,
    209208            }
    210209        return result
  • main/waeup.ikoba/trunk/src/waeup/ikoba/utils/tests/test_converters.py

    r11954 r12306  
    2323import tempfile
    2424import unittest
     25from decimal import Decimal
    2526from zope import schema
    2627from zope.component import provideUtility
     
    3536from waeup.ikoba.testing import FunctionalLayer, FunctionalTestCase
    3637from waeup.ikoba.interfaces import (
    37     SimpleIkobaVocabulary, SubjectSource, GradeSource, IFieldConverter,
     38    SimpleIkobaVocabulary, IFieldConverter,
    3839    DELETION_MARKER, IGNORE_MARKER)
    39 from waeup.ikoba.schoolgrades import ResultEntryField
     40from waeup.ikoba.schoolgrades import ProductOptionEntryField
    4041from waeup.ikoba.utils.converters import (
    4142    IObjectConverter, IFieldConverter, DefaultFieldConverter,
    42     ListFieldConverter, PhoneNumberFieldConverter, ResultEntryConverter,
     43    ListFieldConverter, PhoneNumberFieldConverter, ProductOptionEntryConverter,
    4344    DefaultObjectConverter)
    4445from waeup.ikoba.utils.helpers import attrs_to_fields
     
    9192        vocabulary = car_nums,
    9293        )
    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(),
    9697        required = True,
    9798        default = [],
     
    136137        obj2 = ListFieldConverter(None)
    137138        obj3 = PhoneNumberFieldConverter(None)
    138         obj4 = ResultEntryConverter(None)
     139        obj4 = ProductOptionEntryConverter(None)
    139140        verify.verifyObject(IFieldConverter, obj1)
    140141        verify.verifyObject(IFieldConverter, obj2)
     
    144145        verify.verifyClass(IFieldConverter, ListFieldConverter)
    145146        verify.verifyClass(IFieldConverter, PhoneNumberFieldConverter)
    146         verify.verifyClass(IFieldConverter, ResultEntryConverter)
     147        verify.verifyClass(IFieldConverter, ProductOptionEntryConverter)
    147148        return
    148149
     
    384385        return
    385386
    386     def test_list_of_resultentries(self):
     387    def test_list_of_optionentries(self):
    387388        # We can handle lists of result entries
    388389        converter = IObjectConverter(IContact)
    389390        # 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')
    404403        return
    405404
     
    420419        converter = IObjectConverter(IContact)
    421420        err, inv_err, data = converter.fromStringDict(
    422             {"grades": DELETION_MARKER,
     421            {"options": DELETION_MARKER,
    423422             "friends": DELETION_MARKER},
    424423            'contact', mode='update')
    425424        # 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  
    107107        'no': 'no contract',
    108108        }
    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 in
    116     #: select boxes.
    117     EXAM_GRADES = (
    118         ('A', 'Best'),
    119         ('B', 'Better'),
    120         ('C', 'Good'),
    121         )
    122109
    123110    PAYMENT_CATEGORIES = {
Note: See TracChangeset for help on using the changeset viewer.