Changeset 13483


Ignore:
Timestamp:
19 Nov 2015, 10:24:46 (9 years ago)
Author:
Henrik Bettermann
Message:

Add some basic and customizable components for the calculation of hostels statistics.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/hostels
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/hostels/container.py

    r13319 r13483  
    2323from datetime import datetime
    2424from zope.catalog.interfaces import ICatalog
    25 from zope.component import queryUtility
    26 from waeup.kofa.hostels.interfaces import IHostelsContainer, IHostel
     25from zope.component import queryUtility, getUtility, ComponentLookupError
     26from waeup.kofa.hostels.interfaces import (
     27    IHostelsContainer, IHostel, IHostelsUtils)
    2728from waeup.kofa.utils.logger import Logger
    2829from waeup.kofa.utils.helpers import attrs_to_fields
     
    7980        return False
    8081
     82    @property
     83    def statistics(self):
     84        try:
     85          statistics = getUtility(
     86              IHostelsUtils).getBedStatistics()
     87        except ComponentLookupError:  # happens in unit tests
     88            return
     89        return statistics
     90
    8191    logger_name = 'waeup.kofa.${sitename}.hostels'
    8292    logger_filename = 'hostels.log'
  • main/waeup.kofa/trunk/src/waeup/kofa/hostels/interfaces.py

    r13475 r13483  
    2020from zope.component import getUtility
    2121from zope.catalog.interfaces import ICatalog
    22 from zope.interface import invariant, Invalid, Attribute
     22from zope.interface import invariant, Invalid, Attribute, Interface
    2323from zope import schema
    2424from waeup.kofa.interfaces import (
     
    4343
    4444    expired = Attribute('True if current datetime is in application period.')
     45    statistics = Attribute('Bed category statistics')
    4546
    4647    startdate = schema.Datetime(
     
    295296        """Add an INFO message to hostels.log.
    296297        """
     298
     299class IHostelsUtils(Interface):
     300    """A collection of methods which are subject to customization.
     301    """
     302
     303    def getBedStatistics():
     304        """Return bed statistics.
     305        """
  • main/waeup.kofa/trunk/src/waeup/kofa/hostels/tests.py

    r13445 r13483  
    3131from zope.security.interfaces import Unauthorized
    3232from zope.catalog.interfaces import ICatalog
    33 from zope.component import queryUtility
     33from zope.component import queryUtility, getUtility
    3434from waeup.kofa.app import University
    3535from waeup.kofa.hostels.interfaces import (
    36     IHostelsContainer, IHostel, IBed)
     36    IHostelsContainer, IHostel, IBed, IHostelsUtils)
    3737from waeup.kofa.hostels.vocabularies import NOT_OCCUPIED
    3838from waeup.kofa.hostels.container import HostelsContainer
     
    162162        bed.bed_id = u'hall_block_room_bed'
    163163        bed.bed_number = 1
    164         bed.bed_type = u'a_b_c'
     164        bed.bed_type = u'regular_male_fr'
    165165        self.app['hostels'][hostel.hostel_id][bed.bed_id] = bed
    166166
     
    230230        return
    231231
     232    def test_bed_statistics(self):
     233        utils = getUtility(IHostelsUtils)
     234        self.app['hostels']['hall-x'][
     235            'hall_block_room_bed'].owner = NOT_OCCUPIED
     236        notify(grok.ObjectModifiedEvent(
     237            self.app['hostels']['hall-x']['hall_block_room_bed']))
     238        stats = utils.getBedStatistics()
     239        self.assertEqual(stats,
     240            {'regular_male_fr': (1, 1),
     241             'regular_female_fi': (0, 0),
     242             'regular_male_re': (0, 0),
     243             'regular_female_fr': (0, 0),
     244             'regular_female_all': (0, 0),
     245             'regular_female_re': (0, 0),
     246             'regular_male_fi': (0, 0),
     247             'regular_male_all': (0, 0)}
     248             )
     249        self.app[
     250            'hostels']['hall-x']['hall_block_room_bed'].owner = self.student_id
     251        notify(grok.ObjectModifiedEvent(
     252            self.app['hostels']['hall-x']['hall_block_room_bed']))
     253        stats = utils.getBedStatistics()
     254        self.assertEqual(stats,
     255            {'regular_male_fr': (0, 1),
     256             'regular_female_fi': (0, 0),
     257             'regular_male_re': (0, 0),
     258             'regular_female_fr': (0, 0),
     259             'regular_female_all': (0, 0),
     260             'regular_female_re': (0, 0),
     261             'regular_male_fi': (0, 0),
     262             'regular_male_all': (0, 0)}
     263             )
     264
    232265class BedCatalogTests(HostelsFullSetup):
    233266
     
    242275        # We can find a certain bed
    243276        cat = queryUtility(ICatalog, name='beds_catalog')
    244         results = cat.searchResults(bed_type=(u'a_b_c', u'a_b_c'))
     277        results = cat.searchResults(
     278            bed_type=(u'regular_male_fr', u'regular_male_fr'))
    245279        results = [x for x in results] # Turn results generator into list
    246280        assert len(results) == 1
     
    531565            result,
    532566            'bed_id,bed_number,bed_type,owner,hall,block,room,bed,'
    533             'special_handling,sex,bt\r\nhall_block_room_bed,1,a_b_c,,'
    534             'hall,block,room,bed,a,b,c\r\n'
     567            'special_handling,sex,bt\r\nhall_block_room_bed,1,regular_male_fr,,'
     568            'hall,block,room,bed,regular,male,fr\r\n'
    535569            )
    536570        return
Note: See TracChangeset for help on using the changeset viewer.