<s> from __future__ import print_function , unicode_literals <EOL> import datetime <EOL> import traceback <EOL> from sqlalchemy . exc import IntegrityError <EOL> from sqlalchemy . orm . exc import ConcurrentModificationError <EOL> from voodoo . typechecker import typecheck <EOL> from weblab . core . coordinator . sql . model import PostReservationRetrievedData <EOL> import weblab . core . coordinator . status as WSS <EOL> class PostReservationDataManager ( object ) : <EOL> def __init__ ( self , session_maker , time_provider ) : <EOL> self . _session_maker = session_maker <EOL> self . time_provider = time_provider <EOL> @ typecheck ( basestring , datetime . datetime , datetime . datetime , basestring ) <EOL> def create ( self , reservation_id , date , expiration_date , initial_data ) : <EOL> session = self . _session_maker ( ) <EOL> try : <EOL> registry = PostReservationRetrievedData ( reservation_id = reservation_id , finished = False , date = date , expiration_date = expiration_date , initial_data = initial_data , end_data = None ) <EOL> session . add ( registry ) <EOL> session . commit ( ) <EOL> finally : <EOL> session . close ( ) <EOL> def delete ( self , reservation_id ) : <EOL> session = self . _session_maker ( ) <EOL> try : <EOL> reservation = session . query ( PostReservationRetrievedData ) . filter ( PostReservationRetrievedData . reservation_id == reservation_id ) . first ( ) <EOL> if reservation is None : <EOL> return <EOL> session . delete ( reservation ) <EOL> session . commit ( ) <EOL> finally : <EOL> session . close ( ) <EOL> def finish ( self , reservation_id , end_data ) : <EOL> session = self . _session_maker ( ) <EOL> try : <EOL> reservation = session . query ( PostReservationRetrievedData ) . filter ( PostReservationRetrievedData . reservation_id == reservation_id ) . first ( ) <EOL> if reservation is None : <EOL> return <EOL> reservation . finished = True <EOL> reservation . end_data = end_data <EOL> session . add ( reservation ) <EOL> session . commit ( ) <EOL> finally : <EOL> session . close ( ) <EOL> def find ( self , reservation_id ) : <EOL> session = self . _session_maker ( ) <EOL> try : <EOL> reservation = session . query ( PostReservationRetrievedData ) . filter ( PostReservationRetrievedData . reservation_id == reservation_id ) . first ( ) <EOL> if reservation is None : <EOL> return None <EOL> return WSS . PostReservationStatus ( reservation_id , reservation . finished , reservation . initial_data , reservation . end_data ) <EOL> finally : <EOL> session . close ( ) <EOL> def clean_expired ( self ) : <EOL> session = self . _session_maker ( ) <EOL> try : <EOL> found = False <EOL> for expired_data in session . query ( PostReservationRetrievedData ) . filter ( PostReservationRetrievedData . expiration_date < self . time_provider . get_datetime ( ) ) . all ( ) : <EOL> session . delete ( expired_data ) <EOL> found = True <EOL> if found : <EOL> try : <EOL> session . commit ( ) <EOL> except ( ConcurrentModificationError , IntegrityError ) : <EOL> traceback . print_exc ( ) <EOL> finally : <EOL> session . close ( ) <EOL> def _clean ( self ) : <EOL> session = self . _session_maker ( ) <EOL> try : <EOL> for registry in session . query ( PostReservationRetrievedData ) . all ( ) : <EOL> session . delete ( registry ) <EOL> session . commit ( ) <EOL> finally : <EOL> session . close ( ) </s>
