Ignore:
Timestamp:
28 Feb 2012, 19:34:51 (13 years ago)
Author:
Henrik Bettermann
Message:

Internationalize hostels package.

Remove exception catcher in switchReservations and corresponding test.

When testing the hostel configuration manually I found out that the beds_reserved list attribute of halls
only changed during the lifetime of a running instance. After restarting the portal all changes were gone.
Also "hostel._p_changed = True" didn't help. The only solution was to reassign the attribute to ensure persistance.

Unfortunately, the current tests don't catch such a malfunction. This has to be improved.

Location:
main/waeup.sirp/trunk/src/waeup/sirp/hostels
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/hostels/browser.py

    r7694 r7718  
    2020import grok
    2121import sys
     22from zope.i18n import translate
     23from zope.component import getUtility
    2224from waeup.sirp.browser import (
    2325    SIRPEditFormPage, SIRPAddFormPage, SIRPDisplayFormPage,
     
    3032    ManageActionButton, PrimaryNavTab)
    3133from waeup.sirp.browser.layout import jsaction, action
    32 from waeup.sirp.interfaces import ISIRPObject
     34from waeup.sirp.interfaces import ISIRPObject, ISIRPUtils
     35from waeup.sirp.interfaces import MessageFactory as _
    3336from waeup.sirp.hostels.vocabularies import NOT_OCCUPIED
    3437from waeup.sirp.hostels.hostel import Hostel
    3538from waeup.sirp.hostels.interfaces import (
    3639    IHostelsContainer, IHostel, IBed, IBedAllocateStudent)
    37 
    38 from waeup.sirp.interfaces import MessageFactory as _
    3940
    4041def write_log_message(view, message):
     
    5152    fields_string = ' + '.join(changed_fields)
    5253    view.context._p_changed = True
    53     view.flash('Form has been saved.')
     54    view.flash(_('Form has been saved.'))
    5455    if fields_string:
    5556        write_log_message(view, 'saved: % s' % fields_string)
     
    7576    """
    7677    grok.context(IHostelsContainer)
    77     title = u'Hostels'
     78    title = _(u'Hostels')
    7879
    7980class HostelBreadcrumb(Breadcrumb):
     
    9293    def title(self):
    9394        co = self.context.getBedCoordinates()
    94         return 'Block %s, Room %s, Bed %s' % (co[1], co[2], co[3])
     95        return _('Block ${a}, Room ${b}, Bed ${c}',
     96            mapping = {'a':co[1], 'b':co[2], 'c':co[3]})
    9597
    9698class HostelsContainerPage(SIRPDisplayFormPage):
     
    101103    grok.require('waeup.viewHostels')
    102104    grok.template('containerpage')
    103     label = 'Accommodation Section'
     105    label = _('Accommodation Section')
    104106    pnav = 5
    105107
     
    109111    grok.view(HostelsContainerPage)
    110112    grok.require('waeup.manageHostels')
    111     text = 'Manage accommodation section'
     113    text = _('Manage accommodation section')
    112114
    113115class HostelsContainerManagePage(SIRPDisplayFormPage):
     
    119121    grok.template('containermanagepage')
    120122    pnav = 5
    121     label = 'Manage accommodation section'
     123    label = _('Manage accommodation section')
    122124
    123125    def update(self):
     
    127129    # It's quite dangerous to remove entire hostels with its content (beds).
    128130    # Thus, this remove method should be combined with an archiving function.
    129     @jsaction('Remove selected')
     131    @jsaction(_('Remove selected'))
    130132    def delHostels(self, **data):
    131133        form = self.request.form
     
    141143        return
    142144
    143     @action('Add hostel', validator=NullValidator)
     145    @action(_('Add hostel'), validator=NullValidator)
    144146    def addSubunit(self, **data):
    145147        self.redirect(self.url(self.context, 'addhostel'))
     
    153155    grok.name('addhostel')
    154156    #grok.template('hosteladdpage')
    155     form_fields = grok.AutoFields(IHostel)
    156     label = 'Add hostel'
    157     pnav = 5
    158 
    159     @action('Create hostel')
     157    form_fields = grok.AutoFields(IHostel).omit('beds_reserved')
     158    label = _('Add hostel')
     159    pnav = 5
     160
     161    @action(_('Create hostel'))
    160162    def addHostel(self, **data):
    161163        hostel = Hostel()
     
    166168            self.context.addHostel(hostel)
    167169        except KeyError:
    168             self.flash('The hostel already exists.')
     170            self.flash(_('The hostel already exists.'))
    169171            return
    170         self.flash('Hostel created.')
     172        self.flash(_('Hostel created.'))
    171173        write_log_message(self, 'added: % s' % data['hostel_name'])
    172174        self.redirect(self.url(self.context[hostel.hostel_id], 'index'))
     
    191193    grok.view(HostelDisplayFormPage)
    192194    grok.require('waeup.manageHostels')
    193     text = 'Manage'
     195    text = _('Manage')
    194196    target = 'manage'
    195197
     
    201203    grok.require('waeup.manageHostels')
    202204    form_fields = grok.AutoFields(IHostel).omit('hostel_id')
     205    form_fields['beds_reserved'].for_display = True
    203206    grok.template('hostelmanagepage')
    204     label = 'Manage hostel'
    205     pnav = 5
    206     taboneactions = ['Save']
    207     tabtwoactions = ['Update all beds',
    208         'Switch reservation of selected beds',
    209         'Release selected beds']
     207    label = _('Manage hostel')
     208    pnav = 5
     209    taboneactions = [_('Save')]
     210    tabtwoactions = [_('Update all beds'),
     211        _('Switch reservation of selected beds'),
     212        _('Release selected beds')]
    210213    not_occupied = NOT_OCCUPIED
    211214
     
    226229        return
    227230
    228     @action('Save')
     231    @action(_('Save'))
    229232    def save(self, **data):
    230233        msave(self, **data)
    231234        return
    232235
    233     @action('Update all beds')
     236    @action(_('Update all beds'))
    234237    def updateBeds(self, **data):
    235238        removed, added, modified, modified_beds = self.context.updateBeds()
    236239        message = '%d empty beds removed, %d beds added, %d occupied beds modified (%s)' % (
    237240            removed, added, modified, modified_beds)
    238         self.flash(message)
     241        flash_message = _(
     242            '${a} empty beds removed, ${b} beds added, '
     243            + '${c} occupied beds modified (${d})',
     244            mapping = {'a':removed, 'b':added, 'c':modified, 'd':modified_beds})
     245        self.flash(flash_message)
    239246        write_log_message(self, message)
    240247        self.redirect(self.url(self.context, '@@manage')+'?tab2')
    241248        return
    242249
    243     @action('Switch reservation of selected beds')
     250    @action(_('Switch reservation of selected beds'))
    244251    def switchReservations(self, **data):
    245252        form = self.request.form
     
    247254            child_id = form['val_id']
    248255        else:
    249             self.flash('No item selected.')
     256            self.flash(_('No item selected.'))
    250257            self.redirect(self.url(self.context, '@@manage')+'?tab2')
    251258            return
    252259        if not isinstance(child_id, list):
    253260            child_id = [child_id]
    254         switched = []
     261        switched = [] # for log file
     262        switched_translated = [] # for flash message
     263        portal_language = getUtility(ISIRPUtils).PORTAL_LANGUAGE
     264        preferred_language = self.request.cookies.get(
     265            'sirp.language', portal_language)
    255266        for bed_id in child_id:
    256             try:
    257                 message = self.context[bed_id].switchReservation()
    258                 switched.append('%s (%s)' % (bed_id,message))
    259             except:
    260                 self.flash('Could not switch %s: %s: %s' % (
    261                         id, sys.exc_info()[0], sys.exc_info()[1]))
    262                 self.redirect(self.url(self.context, '@@manage')+'?tab2')
    263                 return
     267            message = self.context[bed_id].switchReservation()
     268            switched.append('%s (%s)' % (bed_id,message))
     269            m_translated = translate(message, 'waeup.sirp',
     270                target_language=preferred_language)
     271            switched_translated.append('%s (%s)' % (bed_id,m_translated))
    264272        if len(switched):
    265273            message = ', '.join(switched)
    266             self.flash('Successfully switched beds: %s' % message)
     274            m_translated = ', '.join(switched_translated)
     275            self.flash(_('Successfully switched beds: ${a}',
     276                mapping = {'a':m_translated}))
    267277            write_log_message(self, 'switched: %s' % message)
    268278            self.redirect(self.url(self.context, '@@manage')+'?tab2')
    269279        return
    270280
    271     @action('Release selected beds')
     281    @action(_('Release selected beds'))
    272282    def releaseBeds(self, **data):
    273283        form = self.request.form
     
    275285            child_id = form['val_id']
    276286        else:
    277             self.flash('No item selected.')
     287            self.flash(_('No item selected.'))
    278288            self.redirect(self.url(self.context, '@@manage')+'?tab2')
    279289            return
     
    287297        if len(released):
    288298            message = ', '.join(released)
    289             self.flash('Successfully released beds: %s' % message)
     299            self.flash(_('Successfully released beds: ${a}',
     300                mapping = {'a':message}))
    290301            write_log_message(self, 'released: %s' % message)
    291302            self.redirect(self.url(self.context, '@@manage')+'?tab2')
    292303        else:
    293             self.flash('No allocated bed selected.')
     304            self.flash(_('No allocated bed selected.'))
    294305            self.redirect(self.url(self.context, '@@manage')+'?tab2')
    295306        return
     
    303314    form_fields = grok.AutoFields(IBedAllocateStudent).omit(
    304315        'bed_id').omit('bed_number').omit('bed_type')
    305     label = 'Allocate student'
    306     pnav = 5
    307 
    308     @action('Save')
     316    label = _('Allocate student')
     317    pnav = 5
     318
     319    @action(_('Save'))
    309320    def save(self, **data):
    310321        msave(self, **data)
  • main/waeup.sirp/trunk/src/waeup/sirp/hostels/browser_templates/containermanagepage.pt

    r7464 r7718  
    1 <form action="." tal:attributes="action request/URL" method="POST" enctype="multipart/form-data">
     1<form action="." tal:attributes="action request/URL"
     2    i18n:domain="waeup.sirp"
     3    method="POST" enctype="multipart/form-data">
    24  <table>
    35    <thead>
     
    57        <th>&nbsp;
    68        </th>
    7         <th>Id
     9        <th i18n:translate="">Id
    810        </th>
    9         <th>Name
     11        <th i18n:translate="">Name
    1012        </th>
    1113      </tr>
  • main/waeup.sirp/trunk/src/waeup/sirp/hostels/browser_templates/containerpage.pt

    r7464 r7718  
    1 <div tal:condition="python: not len(context.keys())">
     1<div i18n:domain="waeup.sirp"
     2   i18n:translate="" tal:condition="python: not len(context.keys())">
    23There no subobjects registered yet.
    34</div>
    45
    5 <table>
     6<table i18n:domain="waeup.sirp">
    67  <thead>
    78    <tr>
    8       <th>Id</th>
    9       <th>Name</th>
     9      <th i18n:translate="">Id</th>
     10      <th i18n:translate="">Name</th>
    1011    </tr>
    1112  </thead>
     
    1314    <tr tal:repeat="value context/values">
    1415      <td> <a tal:attributes="href value/__name__">
    15           <span tal:content="value/hostel_id">Id</span></a></td>
    16       <td tal:content="value/hostel_name">Name</td>
     16          <span tal:content="value/hostel_id">ID</span></a></td>
     17      <td tal:content="value/hostel_name">NAME</td>
    1718    </tr>
    1819  </tbody>
  • main/waeup.sirp/trunk/src/waeup/sirp/hostels/browser_templates/hostelmanagepage.pt

    r7669 r7718  
    11<form action="." tal:attributes="action request/URL" method="POST"
    2       enctype="multipart/form-data">
     2      i18n:domain="waeup.sirp" enctype="multipart/form-data">
    33
    44  <ul class="tabs" data-tabs="tabs">
    5     <li tal:attributes="class view/tab1"><a href="#tab-1"><span>Hostel Data</span></a></li>
    6     <li tal:attributes="class view/tab2"><a href="#tab-2"><span>Beds</span></a></li>
     5    <li tal:attributes="class view/tab1"><a href="#tab-1">
     6      <span i18n:translate="">Hostel Data</span></a>
     7    </li>
     8    <li tal:attributes="class view/tab2"><a href="#tab-2">
     9      <span i18n:translate="">Beds</span></a>
     10    </li>
    711  </ul>
    812
     
    1519          <td class="fieldname">
    1620            <span class="required" tal:condition="widget/required">*</span>
    17             <span tal:content="widget/label">label</span>:
     21            <span tal:content="widget/label">LABEL</span>:
    1822          </td>
    1923          <td>
     
    2226            </span>
    2327            <tal:error tal:condition="widget/error">
    24               <span tal:replace="structure widget/error">error</span>
     28              <span tal:replace="structure widget/error">ERROR</span>
    2529            </tal:error>
    2630            <tal:hint tal:condition="widget/hint">
    27               <span tal:content="structure widget/hint">hint</span>
     31              <span tal:content="structure widget/hint">HINT</span>
    2832            </tal:hint>
    2933          </td>
     
    4751        <tr>
    4852          <th>&nbsp;</th>
    49           <th>Id</th>
    50           <th>Type</th>
    51           <th>Number</th>
    52           <th>Owner</th>
     53          <th i18n:translate="">Id</th>
     54          <th i18n:translate="">Type</th>
     55          <th i18n:translate="">Number</th>
     56          <th i18n:translate="">Owner</th>
    5357        </tr>
    5458      </thead>
     
    5963                  tal:attributes="value value/__name__" />
    6064          </td>
    61           <td tal:content="value/bed_id">Id</td>
    62           <td tal:content="value/bed_type">Type</td>
    63           <td tal:content="value/bed_number">Number</td>
     65          <td tal:content="value/bed_id">ID</td>
     66          <td tal:content="value/bed_type">TYPE</td>
     67          <td tal:content="value/bed_number">NUMBER</td>
    6468          <td>
    6569            <a tal:condition="python: value.owner != view.not_occupied"
    6670               tal:attributes="href python: '%s/%s/accommodation' %
    6771                    (view.students_url,value.owner)">
    68               <span tal:content="value/owner">Owner</span>
     72              <span tal:content="value/owner">OWNER</span>
    6973            </a>
    7074            <a tal:condition="python: value.owner == view.not_occupied"
    71                tal:attributes="href python: view.url(value)">
     75               tal:attributes="href python: view.url(value)"
     76               i18n:translate="">
    7277            [allocate student]
    7378            </a>
  • main/waeup.sirp/trunk/src/waeup/sirp/hostels/hostel.py

    r7257 r7718  
    2626from waeup.sirp.hostels.interfaces import IHostel, IBed, IBedAllocateStudent
    2727from waeup.sirp.students.interfaces import IBedTicket
     28from waeup.sirp.interfaces import MessageFactory as _
    2829
    2930class Hostel(grok.Container):
     
    159160                bt = 're'
    160161            bt = u'%s_%s_%s' % (sh, sex, bt)
    161             hostel.beds_reserved.remove(bed_string)
    162             message = u'unreserved'
    163         else:
    164             bt = u'%s_%s_reserved' % (sh, sex)
    165             hostel.beds_reserved.append(bed_string)
     162
    166163            # Comment of Martijn:
    167164            # If you have a non-Persistent subobject (like a list) and you change it,
     
    172169            # (PersistentList, PersistentMapping), and more advanced building blocks
    173170            # for indexes (BTrees), that don't have this issue.
    174             hostel._p_changed = True
    175             message = u'reserved'
     171            #hostel._p_changed = True
     172
     173            # Henrik: Unfortunately, this doesn't work in our case.
     174            # After restarting the portal,
     175            # added or removed list items are gone. The only way to ensure
     176            # persistance is to reassign the list attribute.
     177            br = hostel.beds_reserved
     178            br.remove(bed_string)
     179            hostel.beds_reserved = br
     180            message = _(u'unreserved')
     181        else:
     182            bt = u'%s_%s_reserved' % (sh, sex)
     183            # See comment above
     184            br = hostel.beds_reserved
     185            br.append(bed_string)
     186            hostel.beds_reserved = br
     187            message = _(u'reserved')
    176188        self.bed_type = bt
    177189        return message
  • main/waeup.sirp/trunk/src/waeup/sirp/hostels/interfaces.py

    r7321 r7718  
    1919from zope import schema
    2020from waeup.sirp.interfaces import ISIRPObject
     21from waeup.sirp.interfaces import MessageFactory as _
    2122from waeup.sirp.hostels.vocabularies import (
    2223    bed_letters, blocks, special_handling, StudentSource)
     
    4142
    4243    hostel_id = schema.TextLine(
    43         title = u'Hostel Id',
     44        title = _(u'Hostel Id'),
    4445        readonly = True,
    4546        )
    4647
    4748    sort_id = schema.Int(
    48         title = u'Sort Id',
     49        title = _(u'Sort Id'),
    4950        required = True,
    5051        default = 10,
     
    5253
    5354    hostel_name = schema.TextLine(
    54         title = u'Hall Name',
     55        title = _(u'Hostel Name'),
    5556        required = True,
    5657        default = u'Hall 1',
     
    5859
    5960    floors_per_block = schema.Int(
    60         title = u'Floors per Block',
     61        title = _(u'Floors per Block'),
    6162        required = True,
    6263        default = 1,
     
    6465
    6566    rooms_per_floor = schema.Int(
    66         title = u'Rooms per Floor',
     67        title = _(u'Rooms per Floor'),
    6768        required = True,
    6869        default = 2,
     
    7071
    7172    beds_reserved = schema.List(
    72         title = u'Reserved Beds',
     73        title = _(u'Reserved Beds'),
    7374        value_type = schema.TextLine(
    7475            default = u'',
     
    8182
    8283    blocks_for_female = schema.List(
    83         title = u'Blocks for Female Students',
     84        title = _(u'Blocks for Female Students'),
    8485        value_type = schema.Choice(
    8586            vocabulary = blocks
     
    8889
    8990    blocks_for_male = schema.List(
    90         title = u'Blocks for Male Students',
     91        title = _(u'Blocks for Male Students'),
    9192        value_type = schema.Choice(
    9293            vocabulary = blocks
     
    9596
    9697    beds_for_fresh = schema.List(
    97         title = u'Beds for Fresh Students',
     98        title = _(u'Beds for Fresh Students'),
    9899        value_type = schema.Choice(
    99100            vocabulary = bed_letters
     
    102103
    103104    beds_for_returning = schema.List(
    104         title = u'Beds for Returning Students',
     105        title = _(u'Beds for Returning Students'),
    105106        value_type = schema.Choice(
    106107            vocabulary = bed_letters
     
    109110
    110111    beds_for_final = schema.List(
    111         title = u'Beds for Final Year Students',
     112        title = _(u'Beds for Final Year Students'),
    112113        value_type = schema.Choice(
    113114            vocabulary = bed_letters
     
    116117
    117118    beds_for_all = schema.List(
    118         title = u'Beds without category',
     119        title = _(u'Beds without category'),
    119120        value_type = schema.Choice(
    120121            vocabulary = bed_letters
     
    123124
    124125    special_handling = schema.Choice(
    125         title = u'Special Handling',
     126        title = _(u'Special Handling'),
    126127        vocabulary = special_handling,
    127128        required = True,
     
    134135        bma = hostel.blocks_for_male
    135136        if set(bfe).intersection(set(bma)):
    136             raise Invalid('Female and male blocks overlap.')
     137            raise Invalid(_('Female and male blocks overlap.'))
    137138
    138139    @invariant
     
    143144                hostel.beds_for_all)
    144145        if len(beds) != len(set(beds)):
    145             raise Invalid('Bed categories overlap.')
     146            raise Invalid(_('Bed categories overlap.'))
    146147
    147148class IBed(ISIRPObject):
     
    166167
    167168    bed_id = schema.TextLine(
    168         title = u'Bed Id',
     169        title = _(u'Bed Id'),
    169170        required = True,
    170171        default = u'',
     
    172173
    173174    bed_type = schema.TextLine(
    174         title = u'Bed Type',
     175        title = _(u'Bed Type'),
    175176        required = True,
    176177        default = u'',
     
    178179
    179180    bed_number = schema.Int(
    180         title = u'Bed Number',
     181        title = _(u'Bed Number'),
    181182        required = True,
    182183        )
    183184
    184185    owner = schema.TextLine(
    185         title = u'Owner (Student)',
     186        title = _(u'Owner (Student)'),
    186187        required = True,
    187188        default = u'',
     
    196197
    197198    owner = schema.Choice(
    198         title = u'Owner (Student)',
     199        title = _(u'Owner (Student)'),
    199200        source = StudentSource(),
    200201        default = None,
  • main/waeup.sirp/trunk/src/waeup/sirp/hostels/tests.py

    r7484 r7718  
    246246        assert self.app['hostels']['hall-1'][
    247247            'hall-1_A_101_D'].bed_type == 'regular_female_reserved'
    248         expected = 'name="form.beds_reserved.0." size="20" type="text" value="A_101_A"  />'
    249         self.assertTrue(expected in self.browser.contents)
    250         # Provoke exception
    251         self.app['hostels']['hall-1']['hall-1_A_101_D'].bed_type = u'nonsense'
    252         ctrl = self.browser.getControl(name='val_id')
    253         ctrl.getControl(value='hall-1_A_101_D').selected = True
    254         self.browser.getControl("Switch reservation", index=0).click()
    255         self.assertMatches(
    256             '...need more than 1 value to unpack...', self.browser.contents)
    257         self.app['hostels']['hall-1'][
    258             'hall-1_A_101_D'].bed_type = u'regular_female_reserved'
     248        self.assertTrue('<li>A_101_A</li>' in self.browser.contents)
    259249        # Change hostel configuration
    260250        hall.beds_for_all = ['D']
  • main/waeup.sirp/trunk/src/waeup/sirp/hostels/vocabularies.py

    r7321 r7718  
    2323from zc.sourcefactory.contextual import BasicContextualSourceFactory
    2424from waeup.sirp.interfaces import SimpleSIRPVocabulary
     25from waeup.sirp.interfaces import MessageFactory as _
    2526
    2627NOT_OCCUPIED = u'not occupied'
     
    5455
    5556bed_letters = SimpleSIRPVocabulary(
    56     ('Bed A','A'),
    57     ('Bed B','B'),
    58     ('Bed C','C'),
    59     ('Bed D','D'),
    60     ('Bed E','E'),
    61     ('Bed F','F'),
    62     ('Bed G','G'),
    63     ('Bed H','H'),
    64     ('Bed I','I'),
     57    (_('Bed A'),'A'),
     58    (_('Bed B'),'B'),
     59    (_('Bed C'),'C'),
     60    (_('Bed D'),'D'),
     61    (_('Bed E'),'E'),
     62    (_('Bed F'),'F'),
     63    (_('Bed G'),'G'),
     64    (_('Bed H'),'H'),
     65    (_('Bed I'),'I'),
    6566    )
    6667
    6768blocks = SimpleSIRPVocabulary(
    68     ('Block A','A'),
    69     ('Block B','B'),
    70     ('Block C','C'),
    71     ('Block D','D'),
    72     ('Block E','E'),
    73     ('Block F','F'),
    74     ('Block G','G'),
    75     ('Block H','H'),
    76     ('Block I','I'),
    77     ('Block K','K'),
    78     ('Block L','L'),
    79     ('Block M','M'),
    80     ('Block N','N'),
    81     ('Block O','O'),
    82     ('Block P','P'),
     69    (_('Block A'),'A'),
     70    (_('Block B'),'B'),
     71    (_('Block C'),'C'),
     72    (_('Block D'),'D'),
     73    (_('Block E'),'E'),
     74    (_('Block F'),'F'),
     75    (_('Block G'),'G'),
     76    (_('Block H'),'H'),
     77    (_('Block I'),'I'),
     78    (_('Block K'),'K'),
     79    (_('Block L'),'L'),
     80    (_('Block M'),'M'),
     81    (_('Block N'),'N'),
     82    (_('Block O'),'O'),
     83    (_('Block P'),'P'),
    8384    )
    8485
    8586special_handling = SimpleSIRPVocabulary(
    86     ('Regular Hostel','regular'),
    87     ('Blocked Hostel','blocked'),
    88     ('Postgraduate Hostel','pg'),
     87    (_('Regular Hostel'),'regular'),
     88    (_('Blocked Hostel'),'blocked'),
     89    (_('Postgraduate Hostel'),'pg'),
    8990    )
Note: See TracChangeset for help on using the changeset viewer.