Ejemplo n.º 1
0
    def put(self, id):
        json_data = request.get_json()

        if not json_data:
            return {'message': 'No input data received for updating CSR'}, 400

        auth_csr = CSR.find_by_username(g.jwt_oidc_token_info['username'])
        edit_csr = CSR.query.filter_by(csr_id=id).first_or_404()

        if auth_csr.csr_id != edit_csr.csr_id:
            return {
                'message': 'You do not have permission to edit this CSR'
            }, 403

        #  Get Invited and Being Served states, to see if CSR has any open tickets.
        period_state_invited = PeriodState.get_state_by_name("Invited")
        period_state_being_served = PeriodState.get_state_by_name(
            "Being Served")

        #  See if CSR has any open tickets.
        citizen = Citizen.query \
            .join(Citizen.service_reqs) \
            .join(ServiceReq.periods) \
            .filter(Period.time_end.is_(None)) \
            .filter(Period.csr_id==id) \
            .filter(or_(Period.ps_id==period_state_invited.ps_id, Period.ps_id==period_state_being_served.ps_id)) \
            .all()

        if len(citizen) != 0:
            return {
                'message': 'CSR has an open ticket and cannot be edited.'
            }, 403

        try:
            edit_csr = self.csr_schema.load(json_data,
                                            instance=edit_csr,
                                            partial=True)
        except ValidationError as err:
            return {'message': err.messages}, 422

        db.session.add(edit_csr)
        db.session.commit()

        # Purge CSR cache, otherwise lazy loaded relationships
        # like Office will be out of date.
        CSR.update_user_cache(id)

        result = self.csr_schema.dump(edit_csr)
        socketio.emit('csr_update', \
                      { "csr_id": edit_csr.csr_id, \
                        "receptionist_ind" : edit_csr.receptionist_ind }, \
                      room=auth_csr.office.office_name)

        # Purge cache of old CSR record so the new one can be fetched by the next request for it.
        CSR.delete_user_cache(g.jwt_oidc_token_info['username'])

        return {
            'csr': result,
            'errors': self.csr_schema.validate(edit_csr)
        }, 200
Ejemplo n.º 2
0
def clear_csr_user_id(csr_id):
    CSR.update_user_cache(csr_id)