Exemplo n.º 1
0
    def handle(self, req):

        verified_caretaker = pg.patients.Patient.verify_is_my_caretaker(
            self.cw.get_pgconn(),
            req.wz_req.args["patient_number"],
            req.user.person_uuid)

        if not verified_caretaker:

            return Response.json(dict(
                success=False,
                reply_timestamp=datetime.datetime.now(),
                message="Sorry, you're not a caretaker for patient {patient_number}!".format(**req.wz_req.args)))

        else:

            patient = pg.patients.Patient.by_patient_number(
                self.cw.get_pgconn(),
                req.wz_req.args["patient_number"])

            return Response.json(dict(
                success=True,
                message="Retrieved {0}.".format(patient.display_name),
                reply_timestamp=datetime.datetime.now(),
                patient=patient))
Exemplo n.º 2
0
    def handle(self, req):

        verified_caretaker = pg.patients.Patient.verify_is_my_caretaker(
            self.cw.get_pgconn(),
            req.json["patient_number"],
            req.user.person_uuid)

        if not verified_caretaker:

            return Response.json(dict(
                success=False,
                reply_timestamp=datetime.datetime.now(),
                message="Sorry, you're not a caretaker for patient {patient_number}!".format(**req.json)))

        else:

            pe = pg.patients.PatientEvent.insert(
                self.cw.get_pgconn(),
                req.json["patient_number"],
                req.json["event_timestamp"],
                req.user.person_uuid,
                req.json["extra_notes"],
                req.json["extra_data"])

            return Response.json(dict(
                success=True,
                reply_timestamp=datetime.datetime.now(),
                message="Stored patient event {0}!".format(pe.patient_event_number),
                patient_event=pe))
Exemplo n.º 3
0
    def handle(self, req):

        verified_caretaker = pg.patients.Patient.verify_is_my_caretaker(
            self.cw.get_pgconn(),
            req.json["patient_number"],
            req.user.person_uuid)

        if not verified_caretaker:

            return Response.json(dict(
                success=False,
                reply_timestamp=datetime.datetime.now(),
                message="Sorry, you're not a caretaker for patient {patient_number}!".format(**req.json)))

        else:

            deleted_patient = pg.patients.Patient.delete_patient(
                self.cw.get_pgconn(),
                req.json["patient_number"])

            return Response.json(dict(
                deleted_patient=deleted_patient,
                success=True,
                reply_timestamp=datetime.datetime.now(),
                message="Deleted patient {0}!".format(req.json["patient_number"])))
Exemplo n.º 4
0
    def handle(self, req):

        try:

            confirmed_person = pg.people.Person.confirm_email(
                self.cw.get_pgconn(),
                req.json["email_address"],
                req.json["confirmation_code"])

        except KeyError as ex:

            return Response.json(dict(
                success=False,

                message=ex.args[0],

                reply_timestamp=datetime.datetime.now()))

        else:

            return Response.json(dict(
                confirmed_person=confirmed_person,
                success=True,
                message="You ({0}) are now confirmed!".format(confirmed_person.display_name),
                reply_timestamp=datetime.datetime.now()))
Exemplo n.º 5
0
    def handle(self, req):

        session_from_qs = None

        if not req.session and "session_uuid" in req.wz_req.args:

            try:

                session_uuid = uuid.UUID(req.wz_req.args["session_uuid"])

            except ValueError as ex:

                return Response.json(dict(
                    reply_timestamp=datetime.datetime.now(),
                    success=False,
                    message="Invalid UUID: {0}!".format(
                        req.wz_req.args["session_uuid"])))

            else:

                session_from_qs = pg.sessions.Session.verify_session_uuid(
                    self.cw.get_pgconn(),
                    session_uuid)

        if session_from_qs or req.session:

            sesh = session_from_qs or req.session

            logged_in_person = pg.people.Person.by_person_uuid(
                self.cw.get_pgconn(),
                sesh.person_uuid)

            resp = Response.json(dict(
                reply_timestamp=datetime.datetime.now(),
                success=True,
                message="Verified session is good until {0}.".format(
                    sesh.expires),
                session=sesh,
                logged_in_person=logged_in_person))

            resp.set_session_cookie(
                sesh.session_uuid,
                self.cw.app_secret)

            return resp

        else:

            return Response.json(dict(
                reply_timestamp=datetime.datetime.now(),
                success=False,
                message="Could not verify session"))
Exemplo n.º 6
0
    def handle(self, req):

        email_address = req.json["email_address"].strip()
        confirmation_code = req.json["confirmation_code"].strip()

        session = None

        session = pg.sessions.Session.maybe_start_new_session_after_checking_email_and_confirmation_code(
            self.cw.get_pgconn(),
            email_address,
            confirmation_code,
            datetime.timedelta(days=3))

        if session:

            person = pg.people.Person.by_person_uuid(
                self.cw.get_pgconn(),
                session.person_uuid)

            log.info("{0} just logged in.".format(person.display_name))

            resp = Response.json(dict(
                reply_timestamp=datetime.datetime.now(),
                message="Session created and will expire on {0}".format(
                    session.expires),
                success=True,
                session=session,
                person=person))

            resp.set_session_cookie(
                session.session_uuid,
                self.cw.app_secret)

            log.debug(resp.headers)

            return resp

        else:

            log.info("Failed login attempt: {0} / {1}".format(
                email_address,
                confirmation_code))

            return Response.json(dict(
                message="Sorry, couldn't authenticate!",
                reply_timestamp=datetime.datetime.now(),
                success=False))
Exemplo n.º 7
0
    def get_session_from_cookie_or_json_or_QS(handler_method, self, req):

        """
        This thing sets the session attribute on the request object
        (req) from either the cookie, or from any JSON post data, or
        from a query string.

        I think the request object should figure the stuff about the
        different location of the session UUID, and then this decorator
        should only be in charge of just being a gatekeeper...

        You can test this like so::

            $ curl --data '{"session_uuid": "d5e19089-ce27-4ab9-b1d0-129a19c81a94"}' -H "Content-Type: application/json" http://circuit.xps/api/insert-club-fee

            $ curl http://circuit.xps/api/insert-club-fee?"session_uuid=d5e19089-ce27-4ab9-b1d0-129a19c81a94"

            $ curl 'http://circuit.xps/api/insert-club-fee' -H 'Cookie: session_uuid=d5e19089-ce27-4ab9-b1d0-129a19c81a94; session_hexdigest=162719e3a569b048b005d6d5140fe884'

        """

        found_session = False

        if req.user:
            found_session = True

        elif req.json and "session_uuid" in req.json:

            sesh = pg.sessions.Session.verify_session_uuid(
                self.cw.get_pgconn(),
                req.json["session_uuid"])

            if sesh:
                req.session = sesh
                found_session = True

        elif req.wz_req.args and "session_uuid" in req.wz_req.args:

            sesh = pg.sessions.Session.verify_session_uuid(
                self.cw.get_pgconn(),
                req.wz_req.args["session_uuid"])

            if sesh:
                req.session = sesh
                found_session = True

        if found_session:
            return handler_method(self, req)

        else:

            return Response.json(dict(
                reply_timestamp=datetime.datetime.now(),
                message="Sorry, you need to log in first!",
                needs_to_log_in=True,
                success=False))
Exemplo n.º 8
0
    def handle(self, req):

        if not pg.people.Person.email_is_valid(req.json["email_address"]):

            return Response.json(dict(
                success=False,
                reply_timestamp=datetime.datetime.now(),
                message="Sorry, {email_address} is not a valid email!".format(**req.json)))

        elif not pg.people.Person.email_address_is_new(
            self.cw.get_pgconn(),
            req.json["email_address"]):

            return Response.json(dict(
                success=False,
                reply_timestamp=datetime.datetime.now(),
                message="Sorry, Somebody else already registered "
                    "email {email_address}!".format(**req.json)))

        elif req.json["agreed_with_TOS"]:

            inserted_person = pg.people.Person.insert(
                self.cw.get_pgconn(),
                req.json["display_name"],
                req.json["email_address"],
                datetime.datetime.now())

            inserted_person.send_confirmation_code_via_email(
                self.cw.make_smtp_connection())

            return Response.json(dict(
                success=True,
                reply_timestamp=datetime.datetime.now(),
                inserted_person=inserted_person,
                message="Go check your email!"))

        else:

            return Response.json(dict(
                success=False,
                reply_timestamp=datetime.datetime.now(),
                inserted_person=inserted_person,
                message="Sorry, you must agree with the terms of service to use this application"))
Exemplo n.º 9
0
    def handle(self, req):

        try:
            pe = pg.patients.PatientEvent.by_patient_event_number(
                self.cw.get_pgconn(),
                req.json["patient_event_number"])

        except KeyError as ex:

            log.error(ex)

            return Response.json(dict(
                success=False,
                reply_timestamp=datetime.datetime.now(),
                message=ex.args[0]))

        else:

            verified_caretaker = pg.patients.Patient.verify_is_my_caretaker(
                self.cw.get_pgconn(),
                pe.patient_number,
                req.user.person_uuid)

        if not verified_caretaker:

            return Response.json(dict(
                success=False,
                reply_timestamp=datetime.datetime.now(),
                message="Sorry, you're not a caretaker for patient {0}!".format(pe.patient_number)))

        else:

            deleted_patient_event = pg.patients.PatientEvent.delete_patient_event(
                self.cw.get_pgconn(),
                req.json["patient_event_number"])

            return Response.json(dict(
                deleted_patient_event=deleted_patient_event,
                success=True,
                reply_timestamp=datetime.datetime.now(),
                message="Deleted patient event {0}!".format(req.json["patient_event_number"])))
Exemplo n.º 10
0
    def handle(self, req):

        verified_caretaker = pg.patients.Patient.verify_is_my_caretaker(
            self.cw.get_pgconn(),
            req.wz_req.args["patient_number"],
            req.user.person_uuid)

        if not verified_caretaker:

            return Response.json(dict(
                success=False,
                reply_timestamp=datetime.datetime.now(),
                message="Sorry, you're not a caretaker for patient {patient_number}!".format(**req.wz_req.args)))

        else:

            offset = int(req.wz_req.args.get("offset", 0))
            limit = int(req.wz_req.args.get("limit", 10))

            patient_events = list(pg.patients.PatientEvent.by_patient_number(
                self.cw.get_pgconn(),
                req.wz_req.args["patient_number"],
                offset,
                limit))

            total_event_count = pg.patients.PatientEvent.count_patient_events(
                self.cw.get_pgconn(),
                req.wz_req.args["patient_number"])

            return Response.json(dict(
                success=True,
                message="Retrieved {0} events of {1} total.".format(
                    len(patient_events),
                    total_event_count),
                reply_timestamp=datetime.datetime.now(),
                offset=offset,
                limit=limit,
                patient_events=patient_events,
                total_event_count=total_event_count))
Exemplo n.º 11
0
    def handle(self, req):

        try:

            session = pg.sessions.Session.by_session_uuid(
                self.cw.get_pgconn(),
                req.json["session_uuid"])

        except KeyError:

            return Response.json(dict(
                message="Sorry, could not find session {0}!".format(req.json["session_uuid"]),
                reply_timestamp=datetime.datetime.now(),
                success=False))

        else:

            updated_session = session.end_session(self.cw.get_pgconn())

            return Response.json(dict(
                message="Ended session {0}.".format(session.session_uuid),
                reply_timestamp=datetime.datetime.now(),
                success=True,
                session=updated_session))
Exemplo n.º 12
0
    def handle(self, req):

        person = pg.people.Person.by_email_address(
            self.cw.get_pgconn(),
            req.json["email_address"].strip())

        if not person.confirmation_code:
            person = person.reset_confirmation_code(self.cw.get_pgconn())

        person.send_confirmation_code_via_email(
            self.cw.make_smtp_connection())

        return Response.json(dict(
            message="Sent a confirmation code to {0}!".format(person.email_address),
            reply_timestamp=datetime.datetime.now(),
            success=True))
Exemplo n.º 13
0
    def handle(self, req):

        offset = int(req.wz_req.args.get("offset", 0))
        limit = int(req.wz_req.args.get("offset", 10))

        patients = list(pg.patients.Patient.patients_for_caretaker(
            self.cw.get_pgconn(),
            req.user.person_uuid,
            offset,
            limit))

        return Response.json(dict(
            success=True,
            message="Retrieved {0} patients".format(len(patients)),
            reply_timestamp=datetime.datetime.now(),
            offset=offset,
            limit=limit,
            patients=patients))
Exemplo n.º 14
0
    def handle(self, req):

        new_patient = pg.patients.Patient.insert(
            self.cw.get_pgconn(),
            req.json["display_name"],
            req.json["extra_notes"],
            req.json["extra_data"])

        link = pg.patients.Patient.link_to_caretaker(
            self.cw.get_pgconn(),
            new_patient.patient_number,
            req.user.person_uuid,
            None,
            None)

        return Response.json(dict(
            success=True,
            reply_timestamp=datetime.datetime.now(),
            message="Stored patient {0}!".format(new_patient.display_name),
            new_patient=new_patient,
            link=link))
Exemplo n.º 15
0
    def handle(self, req):

        return Response.json(dict(
            message="Welcome to the StuhlGang API",
            success=True,
            reply_timestamp=datetime.datetime.now()))