class ServiceReq(Base): sr_id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) citizen_id = db.Column(db.Integer, db.ForeignKey('citizen.citizen_id'), nullable=False) quantity = db.Column(db.Integer, default=1, nullable=False) channel_id = db.Column(db.Integer, db.ForeignKey('channel.channel_id'), nullable=False) service_id = db.Column(db.Integer, db.ForeignKey('service.service_id'), nullable=False) sr_state_id = db.Column(db.Integer, db.ForeignKey('srstate.sr_state_id'), nullable=False) sr_number = db.Column(db.Integer, default=1, nullable=False) channel = db.relationship('Channel') periods = db.relationship('Period', backref=db.backref("request_periods", lazy=False), lazy='joined', order_by='Period.period_id') sr_state = db.relationship('SRState', lazy='joined') citizen = db.relationship('Citizen') service = db.relationship('Service', lazy='joined') def __init__(self, **kwargs): super(ServiceReq, self).__init__(**kwargs) def get_active_period(self): sorted_periods = sorted(self.periods, key=lambda p: p.period_id) return sorted_periods[-1] def invite(self, csr, invite_type, sr_count=1): active_period = self.get_active_period() if active_period.ps.ps_name in ["Invited", "Being Served", "On hold"]: raise TypeError( "You cannot invite a citizen that has already been invited") # If a generic invite type, event is either invitecitizen or returninvite. if invite_type == "generic": # If only one SR, one period, an invitecitizen call, from First Time in Line state. if sr_count == 1 and len(self.periods) == 2: snowplow_event = "invitecitizen" # Otherwise from the Back in Line state. else: snowplow_event = "returninvite" # A specific invite type. Event is invitefromlist, returnfromlist or invitefromhold else: # If only one SR, one period, an invitefromlist call, from First Time in Line state. if sr_count == 1 and len(self.periods) == 2: snowplow_event = "invitefromlist" # Either from back in line or hold state. else: if active_period.ps.ps_name == "Waiting": snowplow_event = "returnfromlist" else: snowplow_event = "invitefromhold" active_period.time_end = datetime.utcnow() # db.session.add(active_period) period_state_invite = PeriodState.get_state_by_name("Invited") new_period = Period(sr_id=self.sr_id, csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_invite.ps_id, time_start=datetime.utcnow()) self.periods.append(new_period) SnowPlow.snowplow_event(self.citizen_id, csr, snowplow_event, current_sr_number=self.sr_number) def add_to_queue(self, csr, snowplow_event): active_period = self.get_active_period() active_period.time_end = datetime.utcnow() #db.session.add(active_period) period_state_waiting = PeriodState.get_state_by_name("Waiting") new_period = Period(sr_id=self.sr_id, csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_waiting.ps_id, time_start=datetime.utcnow()) self.periods.append(new_period) SnowPlow.snowplow_event(self.citizen_id, csr, snowplow_event, current_sr_number=self.sr_number) def remove_from_queue(self): service_req_ids = [int(x.period_id) for x in self.periods] Period.delete_periods(service_req_ids) def begin_service(self, csr, snowplow_event): active_period = self.get_active_period() if active_period.ps.ps_name in ["Being Served"]: raise TypeError( "You cannot begin serving a citizen that is already being served" ) active_period.time_end = datetime.utcnow() # db.session.add(active_period) period_state_being_served = PeriodState.get_state_by_name( "Being Served") new_period = Period(sr_id=self.sr_id, csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_being_served.ps_id, time_start=datetime.utcnow()) self.periods.append(new_period) # Calculate number of active periods, for Snowplow call. period_count = len(self.periods) SnowPlow.snowplow_event(self.citizen_id, csr, snowplow_event, period_count=period_count, current_sr_number=self.sr_number) def place_on_hold(self, csr): active_period = self.get_active_period() active_period.time_end = datetime.utcnow() # db.session.add(active_period) period_state_on_hold = PeriodState.get_state_by_name("On hold") new_period = Period(sr_id=self.sr_id, csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_on_hold.ps_id, time_start=datetime.utcnow()) self.periods.append(new_period) SnowPlow.snowplow_event(self.citizen_id, csr, "hold", current_sr_number=self.sr_number) def finish_service(self, csr, clear_comments=True): active_period = self.get_active_period() active_period.time_end = datetime.utcnow() if clear_comments: self.citizen.citizen_comments = None
class ServiceReq(Base): sr_id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False) citizen_id = db.Column(db.Integer, db.ForeignKey('citizen.citizen_id'), nullable=False) quantity = db.Column(db.Integer, default=1, nullable=False) channel_id = db.Column(db.Integer, db.ForeignKey('channel.channel_id'), nullable=False) service_id = db.Column(db.Integer, db.ForeignKey('service.service_id'), nullable=False) sr_state_id = db.Column(db.Integer, db.ForeignKey('srstate.sr_state_id'), nullable=False) channel = db.relationship('Channel') periods = db.relationship('Period', backref=db.backref("request_periods", lazy=False), lazy='joined', order_by='Period.period_id') sr_state = db.relationship('SRState', lazy='joined') citizen = db.relationship('Citizen') service = db.relationship('Service', lazy='joined') def __init__(self, **kwargs): super(ServiceReq, self).__init__(**kwargs) def get_active_period(self): sorted_periods = sorted(self.periods, key=lambda p: p.period_id) return sorted_periods[-1] def invite(self, csr, snowplow_event="use_period"): active_period = self.get_active_period() if active_period.ps.ps_name in ["Invited", "Being Served", "On Hold"]: raise TypeError( "You cannot invite a citizen that has already been invited") # Calculate what Snowplow event to call. if (snowplow_event == "use_period"): if (active_period.ps.ps_name == "Waiting"): snowplow_event = "invitefromlist" else: snowplow_event = "invitefromhold" active_period.time_end = datetime.now() # db.session.add(active_period) period_state_invite = PeriodState.query.filter_by( ps_name="Invited").first() new_period = Period(sr_id=self.sr_id, csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_invite.ps_id, time_start=datetime.now()) self.periods.append(new_period) SnowPlow.snowplow_event(self.citizen_id, csr, snowplow_event) def add_to_queue(self, csr, snowplow_event): active_period = self.get_active_period() active_period.time_end = datetime.now() #db.session.add(active_period) period_state_waiting = PeriodState.query.filter_by( ps_name="Waiting").first() new_period = Period(sr_id=self.sr_id, csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_waiting.ps_id, time_start=datetime.now()) self.periods.append(new_period) SnowPlow.snowplow_event(self.citizen_id, csr, snowplow_event) def begin_service(self, csr, snowplow_event): active_period = self.get_active_period() if active_period.ps.ps_name in ["Being Served"]: raise TypeError( "You cannot begin serving a citizen that is already being served" ) active_period.time_end = datetime.now() # db.session.add(active_period) period_state_being_served = PeriodState.query.filter_by( ps_name="Being Served").first() new_period = Period(sr_id=self.sr_id, csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_being_served.ps_id, time_start=datetime.now()) self.periods.append(new_period) # Calculate number of active periods, for Snowplow call. period_count = len(self.periods) SnowPlow.snowplow_event(self.citizen_id, csr, snowplow_event, period_count=period_count) def place_on_hold(self, csr): active_period = self.get_active_period() active_period.time_end = datetime.now() # db.session.add(active_period) period_state_on_hold = PeriodState.query.filter_by( ps_name="On hold").first() new_period = Period(sr_id=self.sr_id, csr_id=csr.csr_id, reception_csr_ind=csr.receptionist_ind, ps_id=period_state_on_hold.ps_id, time_start=datetime.now()) self.periods.append(new_period) SnowPlow.snowplow_event(self.citizen_id, csr, "hold") def finish_service(self, csr, clear_comments=True): active_period = self.get_active_period() active_period.time_end = datetime.now() if clear_comments: self.citizen.citizen_comments = None