<s> from datetime import datetime <EOL> from couchdbkit import MultipleResultsFound <EOL> from couchforms . models import XFormInstance <EOL> from django . db import models <EOL> from django . db . models import Q <EOL> from django . utils . translation import ugettext_noop <EOL> XFORMS_SESSION_SMS = "<STR_LIT>" <EOL> XFORMS_SESSION_IVR = "<STR_LIT>" <EOL> XFORMS_SESSION_TYPES = [ XFORMS_SESSION_SMS , XFORMS_SESSION_IVR ] <EOL> class SQLXFormsSession ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> couch_id = models . CharField ( db_index = True , max_length = <NUM_LIT:50> ) <EOL> connection_id = models . CharField ( null = True , blank = True , db_index = True , max_length = <NUM_LIT:50> ) <EOL> session_id = models . CharField ( null = True , blank = True , db_index = True , max_length = <NUM_LIT:50> ) <EOL> form_xmlns = models . CharField ( null = True , blank = True , max_length = <NUM_LIT:100> ) <EOL> start_time = models . DateTimeField ( ) <EOL> modified_time = models . DateTimeField ( ) <EOL> end_time = models . DateTimeField ( null = True ) <EOL> completed = models . BooleanField ( default = False ) <EOL> domain = models . CharField ( null = True , blank = True , db_index = True , max_length = <NUM_LIT:100> ) <EOL> user_id = models . CharField ( null = True , blank = True , max_length = <NUM_LIT:50> ) <EOL> app_id = models . CharField ( null = True , blank = True , max_length = <NUM_LIT:50> ) <EOL> submission_id = models . CharField ( null = True , blank = True , max_length = <NUM_LIT:50> ) <EOL> survey_incentive = models . CharField ( null = True , blank = True , max_length = <NUM_LIT:100> ) <EOL> session_type = models . CharField ( max_length = <NUM_LIT:10> , choices = zip ( XFORMS_SESSION_TYPES , XFORMS_SESSION_TYPES ) , <EOL> default = XFORMS_SESSION_SMS ) <EOL> workflow = models . CharField ( null = True , blank = True , max_length = <NUM_LIT:20> ) <EOL> reminder_id = models . CharField ( null = True , blank = True , max_length = <NUM_LIT:50> ) <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT>' % { "<STR_LIT>" : self . form_xmlns , <EOL> "<STR_LIT>" : self . domain , <EOL> "<STR_LIT>" : self . modified_time } <EOL> @ property <EOL> def _id ( self ) : <EOL> return self . couch_id <EOL> def end ( self , completed ) : <EOL> """<STR_LIT>""" <EOL> self . completed = completed <EOL> self . modified_time = self . end_time = datetime . utcnow ( ) <EOL> @ property <EOL> def related_subevent ( self ) : <EOL> subevents = self . messagingsubevent_set . all ( ) <EOL> return subevents [ <NUM_LIT:0> ] if subevents else None <EOL> @ property <EOL> def status ( self ) : <EOL> xform_instance = None <EOL> if self . submission_id : <EOL> xform_instance = XFormInstance . get ( self . submission_id ) <EOL> if xform_instance : <EOL> if xform_instance . partial_submission : <EOL> return ugettext_noop ( '<STR_LIT>' ) <EOL> else : <EOL> return ugettext_noop ( '<STR_LIT>' ) <EOL> else : <EOL> if self . is_open and self . session_type == XFORMS_SESSION_SMS : <EOL> return ugettext_noop ( '<STR_LIT>' ) <EOL> else : <EOL> return ugettext_noop ( '<STR_LIT>' ) <EOL> @ property <EOL> def is_open ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . end_time is None <EOL> @ classmethod <EOL> def get_all_open_sms_sessions ( cls , domain , contact_id ) : <EOL> return cls . objects . filter ( <EOL> Q ( session_type__isnull = True ) | Q ( session_type = XFORMS_SESSION_SMS ) , <EOL> domain = domain , <EOL> connection_id = contact_id , <EOL> end_time__isnull = True , <EOL> ) <EOL> @ classmethod <EOL> def close_all_open_sms_sessions ( cls , domain , contact_id ) : <EOL> sessions = cls . get_all_open_sms_sessions ( domain , contact_id ) <EOL> for session in sessions : <EOL> session . end ( False ) <EOL> session . save ( ) <EOL> @ classmethod <EOL> def by_session_id ( cls , id ) : <EOL> try : <EOL> return cls . objects . get ( session_id = id ) <EOL> except SQLXFormsSession . DoesNotExist : <EOL> return None <EOL> @ classmethod <EOL> def get_open_sms_session ( cls , domain , contact_id ) : <EOL> """<STR_LIT>""" <EOL> objs = cls . get_all_open_sms_sessions ( domain , contact_id ) . all ( ) <EOL> if len ( objs ) > <NUM_LIT:1> : <EOL> raise MultipleResultsFound ( '<STR_LIT>' . format ( <EOL> len ( objs ) , domain , contact_id <EOL> ) ) <EOL> elif len ( objs ) == <NUM_LIT:0> : <EOL> return None <EOL> return objs [ <NUM_LIT:0> ] <EOL> def get_session_by_session_id ( id ) : <EOL> return SQLXFormsSession . by_session_id ( id ) <EOL> from . import signals </s>
