Ignore:
Timestamp:
14 Oct 2015, 09:34:30 (9 years ago)
Author:
Henrik Bettermann
Message:

Add some methods to release expired bed allocations. View
components have to be added in custom packages.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/hostels
Files:
5 edited

Legend:

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

    r7811 r13316  
    3030    grok.context(IBed)
    3131
    32     #bed_id = index.Field(attribute='bed_id')
    33     #bed_number = index.Field(attribute='bed_number')
    3432    bed_type = index.Field(attribute='bed_type')
    3533    owner = index.Field(attribute='owner')
  • main/waeup.kofa/trunk/src/waeup/kofa/hostels/container.py

    r13166 r13316  
    2222import pytz
    2323from datetime import datetime
     24from zope.catalog.interfaces import ICatalog
     25from zope.component import queryUtility
    2426from waeup.kofa.hostels.interfaces import IHostelsContainer, IHostel
    2527from waeup.kofa.utils.logger import Logger
     
    5254        return
    5355
     56    def releaseExpiredAllocations(self, n=7):
     57        """Release bed if bed allocation has expired. Allocation expires
     58        after `n` days if maintenance fee has not been paid.
     59        """
     60        cat = queryUtility(ICatalog, name='beds_catalog')
     61        results = cat.searchResults(owner=(None, None))
     62        counter = 0
     63        for bed in results:
     64            success = bed.releaseBedIfMaintenanceNotPaid(n=n)
     65            if success:
     66                counter += 1
     67        return counter
     68
    5469    @property
    5570    def expired(self):
  • main/waeup.kofa/trunk/src/waeup/kofa/hostels/hostel.py

    r13170 r13316  
    253253        if self.owner == NOT_OCCUPIED:
    254254            return
    255         else:
    256             old_owner = self.owner
     255        old_owner = self.owner
     256        self.owner = NOT_OCCUPIED
     257        notify(grok.ObjectModifiedEvent(self))
     258        accommodation_session = grok.getSite()[
     259            'hostels'].accommodation_session
     260        try:
     261            bedticket = grok.getSite()['students'][old_owner][
     262                          'accommodation'][str(accommodation_session)]
     263        except KeyError:
     264            return '%s without bed ticket' % old_owner
     265        bedticket.bed = None
     266        tz = getUtility(IKofaUtils).tzinfo
     267        timestamp = now(tz).strftime("%Y-%m-%d %H:%M:%S %Z")
     268        bedticket.bed_coordinates = u'-- booking cancelled on %s --' % (
     269            timestamp,)
     270        return old_owner
     271
     272    def releaseBedIfMaintenanceNotPaid(self, n=7):
     273        if self.owner == NOT_OCCUPIED:
     274            return
     275        accommodation_session = grok.getSite()[
     276            'hostels'].accommodation_session
     277        try:
     278            bedticket = grok.getSite()['students'][self.owner][
     279                          'accommodation'][str(accommodation_session)]
     280        except KeyError:
     281            return
     282        if bedticket.maint_payment_made:
     283            return
     284        jetzt = datetime.utcnow()
     285        days_ago = getattr(jetzt - bedticket.booking_date, 'days')
     286        if days_ago > n:
    257287            self.owner = NOT_OCCUPIED
    258288            notify(grok.ObjectModifiedEvent(self))
    259             accommodation_session = grok.getSite()[
    260                 'hostels'].accommodation_session
    261             try:
    262                 bedticket = grok.getSite()['students'][old_owner][
    263                               'accommodation'][str(accommodation_session)]
    264             except KeyError:
    265                 return '%s without bed ticket' % old_owner
    266289            bedticket.bed = None
    267290            tz = getUtility(IKofaUtils).tzinfo
    268291            timestamp = now(tz).strftime("%Y-%m-%d %H:%M:%S %Z")
    269             bedticket.bed_coordinates = u'-- booking cancelled on %s --' % (
     292            bedticket.bed_coordinates = u'-- booking expired (%s) --' % (
    270293                timestamp,)
    271             return old_owner
     294            return True
     295        return
    272296
    273297    def writeLogMessage(self, view, message):
  • main/waeup.kofa/trunk/src/waeup/kofa/hostels/interfaces.py

    r13280 r13316  
    7171        """
    7272
     73    def releaseExpiredAllocations(n):
     74        """Release bed if bed allocation has expired. Allocation expires
     75        after `n` days if maintenance fee has not been paid.
     76        """
     77
    7378    def writeLogMessage(view, message):
    7479        """Add an INFO message to hostels.log.
     
    230235    def switchReservation():
    231236        """Reserves bed or relases reserved bed respectively.
     237        """
     238
     239    def releaseBedIfMaintenanceNotPaid():
     240        """Release bed if maintenance fee has not been paid on time.
    232241        """
    233242
  • main/waeup.kofa/trunk/src/waeup/kofa/hostels/tests.py

    r13315 r13316  
    3535from waeup.kofa.hostels.interfaces import (
    3636    IHostelsContainer, IHostel, IBed)
     37from waeup.kofa.hostels.vocabularies import NOT_OCCUPIED
    3738from waeup.kofa.hostels.container import HostelsContainer
    3839from waeup.kofa.hostels.hostel import Hostel, Bed
     
    173174        clearSite()
    174175        shutil.rmtree(self.dc_root)
     176
     177class HostelsContainerTests(HostelsFullSetup):
     178
     179    layer = FunctionalLayer
     180
     181    def test_release_expired_allocations(self):
     182        cat = queryUtility(ICatalog, name='beds_catalog')
     183        bedticket = BedTicket()
     184        bedticket.booking_session = 2004
     185        bedticket.bed_coordinates = u'anything'
     186        self.student['accommodation'].addBedTicket(bedticket)
     187        self.app[
     188            'hostels']['hall-x']['hall_block_room_bed'].owner = self.student_id
     189        notify(grok.ObjectModifiedEvent(
     190            self.app['hostels']['hall-x']['hall_block_room_bed']))
     191        results = cat.searchResults(owner=(self.student_id, self.student_id))
     192        self.assertEqual(len(results), 1)
     193        released = self.app['hostels'].releaseExpiredAllocations(7)
     194        self.assertEqual(released, 0)
     195        delta = timedelta(days=10)
     196        bedticket.booking_date = datetime.utcnow() - delta
     197        released = self.app['hostels'].releaseExpiredAllocations(7)
     198        self.assertEqual(released, 1)
     199        results = cat.searchResults(owner=(self.student_id, self.student_id))
     200        self.assertEqual(len(results), 0)
     201        self.assertMatches(bedticket.display_coordinates,
     202            '-- booking expired (2015-10-14 08:35:38 UTC) --')
     203        self.assertEqual(
     204            self.app['hostels']['hall-x']['hall_block_room_bed'].owner,
     205            NOT_OCCUPIED)
     206        return
    175207
    176208class BedCatalogTests(HostelsFullSetup):
     
    330362        self.assertMatches(bedticket.bed_coordinates,
    331363          u' -- booking cancelled on <YYYY-MM-DD hh:mm:ss> UTC --')
    332         # The catalog was updated.
     364        # The catalog has been updated.
    333365        results = cat.searchResults(owner=(self.student_id, self.student_id))
    334366        assert len(results) == 0
Note: See TracChangeset for help on using the changeset viewer.