Exemple #1
0
 def get(self):
     # get the auth token
     auth_header = request.headers.get('Authorization')
     if auth_header:
         try:
             auth_token = auth_header.split(" ")[1]
         except IndexError:
             responseObject = {
                 'status': 'fail',
                 'message': 'Bearer token malformed.'
             }
             return make_response(json.dumps(responseObject,
                                             default=str)), 401
     else:
         auth_token = ''
     if auth_token:
         resp = User.decode_auth_token(auth_token)
         if not isinstance(resp, str):
             user = User.query.filter_by(id=resp).first()
             user = user._asdict()
             responseObject = {'status': 'success', 'data': user}
             return make_response(json.dumps(responseObject,
                                             default=str)), 200
         responseObject = {'status': 'fail', 'message': resp}
         return make_response(json.dumps(responseObject, default=str)), 401
     else:
         responseObject = {
             'status': 'fail',
             'message': 'Provide a valid auth token.'
         }
         return make_response(json.dumps(responseObject, default=str)), 401
Exemple #2
0
    def get(self):
        # get the auth token
        auth_header = request.headers.get('Authorization')
        if auth_header:
            try:
                auth_token = auth_header.split(" ")[1]
            except IndexError:
                responseObject = {
                    'status': 'fail',
                    'message': 'Bearer token malformed.'
                }
                return make_response(json.dumps(responseObject,
                                                default=str)), 401
        else:
            auth_token = ''
        if auth_token:
            resp = User.decode_auth_token(auth_token)
            if not isinstance(resp, str):
                hosp_arr = []
                hospitals = Hospital.query.all()
                for i in range(len(hospitals)):
                    hosp_arr.append(hospitals[i]._return_data())

                responseObject = {'status': 'success', 'data': hosp_arr}
                return make_response(json.dumps(responseObject,
                                                default=str)), 200
            responseObject = {'status': 'fail', 'message': resp}
            return make_response(json.dumps(responseObject, default=str)), 401
        else:
            responseObject = {
                'status': 'fail',
                'message': 'Provide a valid auth token.'
            }
            return make_response(json.dumps(responseObject, default=str)), 401
Exemple #3
0
    def put(self):
        """ put an item here """

        # check if user already exists
        auth_header = request.headers.get('Authorization')
        # get the post data
        post_data = request.json
        if auth_header:
            try:
                auth_token = auth_header.split(" ")[1]
            except IndexError:

                responseObject = {
                    'status': 'fail',
                    'message': 'Bearer token malformed.'
                }
                return make_response(json.dumps(responseObject,
                                                default=str)), 401
        else:
            auth_token = ''
        if auth_token:
            resp = User.decode_auth_token(auth_token)
            if not isinstance(resp, str):
                transfusion = Transfusion.query.filter_by(
                    id=post_data['id']).first()

                transfusion.hospital_id = post_data["hospital_id"]
                transfusion.subscriber_id = post_data["subscriber_id"]
                transfusion.hosp_unit = post_data["hosp_unit"]
                transfusion.medical_condition = post_data["medical_conditions"]
                transfusion.hem_level = post_data["hem_level"]
                transfusion.bp_requested = post_data["bp_requested"]
                transfusion.bp_received = post_data["bp_received"]
                transfusion.ubpt = post_data["ubpt"]
                transfusion.id_ut = post_data["id_ut"]
                transfusion.onset_time = post_data["onset_time"]
                transfusion.termination_time = post_data["termination_time"]
                transfusion.effect_of_transfusion = post_data[
                    "effect_of_transfusion"]
                transfusion.date_requested = post_data["date_requested"]
                transfusion.date_delivered = post_data["date_delivered"]
                transfusion.patient_end_status = post_data[
                    "patient_end_status"]
                transfusion.diagnosis = post_data["diagnosis"]

                db.session.add(transfusion)
                db.session.commit()
                data = transfusion._return_data()
                responseObject = {'status': 'success', 'data': data}
                return make_response(json.dumps(responseObject,
                                                default=str)), 200
            responseObject = {'status': 'fail', 'message': resp}
            return make_response(json.dumps(responseObject, default=str)), 401
        else:
            responseObject = {
                'status': 'fail',
                'message': 'Provide a valid auth token.'
            }
            return make_response(json.dumps(responseObject, default=str)), 401
Exemple #4
0
    def test_decode_auth_token(self):
        user = User(email='*****@*****.**', password='******')
        db.session.add(user)
        db.session.commit()
        auth_token = user.encode_auth_token(user.id)
        self.assertTrue(isinstance(auth_token, bytes))

        self.assertTrue(
            User.decode_auth_token(auth_token.decode("utf-8")) == 1)
Exemple #5
0
    def post(self):
        """ post an item here """

        # check if user already exists
        auth_header = request.headers.get('Authorization')
        # get the post data
        post_data = request.json
        if auth_header:
            try:
                auth_token = auth_header.split(" ")[1]
            except IndexError:

                responseObject = {
                    'status': 'fail',
                    'message': 'Bearer token malformed.'
                }
                return make_response(json.dumps(responseObject, default=str)), 401
        else:
            auth_token = ''
        if auth_token:
            resp = User.decode_auth_token(auth_token)
            if not isinstance(resp, str):

                donation = Donation()
                donation.hospital_id = post_data["hospital_id"]
                donation.donor_id = post_data["donor_id"]
                donation.volume_of_blood = post_data["volume_of_blood"]
                donation.onset_time = post_data["onset_time"]
                donation.termination_time = post_data["termination_time"]
                donation.torfru = post_data["torfru"]
                donation.donor.update_dolbd(donation.created_at)
                
                db.session.add(donation)
                db.session.commit()
                data = donation._asdict()
                responseObject = {
                    'status': 'success',
                    'data': data
                }
                return make_response(json.dumps(responseObject, default=str)), 200

            responseObject = {
                'status': 'fail',
                'message': resp
            }
            return make_response(json.dumps(responseObject, default=str)), 401
        else:
            responseObject = {
                'status': 'fail',
                'message': 'Provide a valid auth token.'
            }
            return make_response(json.dumps(responseObject, default=str)), 401
Exemple #6
0
    def post(self):
        """ post an item here """

        # check if user already exists
        auth_header = request.headers.get('Authorization')
        # get the post data
        post_data = request.json
        if auth_header:
            try:
                auth_token = auth_header.split(" ")[1]
            except IndexError:

                responseObject = {
                    'status': 'fail',
                    'message': 'Bearer token malformed.'
                }
                return make_response(json.dumps(responseObject,
                                                default=str)), 401
        else:
            auth_token = ''
        if auth_token:
            resp = User.decode_auth_token(auth_token)
            if not isinstance(resp, str):
                deferral = Deferral()

                if post_data["permanent_deferal"]:
                    deferral.ndefbd = datetime.datetime.now(
                    ) + datetime.timedelta(weeks=52000)
                else:
                    deferral.ndefbd = post_data["ndefbd"]
                deferral.donor_id = int(post_data["donor_id"]["value"])
                db.session.add(deferral)
                db.session.commit()
                deferral.update_donor_ndefbd()

                data = deferral._asdict()
                data['donor'] = deferral.donor._asdict()
                responseObject = {'status': 'success', 'data': data}
                return make_response(json.dumps(responseObject,
                                                default=str)), 200
            responseObject = {'status': 'fail', 'message': resp}
            return make_response(json.dumps(responseObject, default=str)), 401
        else:
            responseObject = {
                'status': 'fail',
                'message': 'Provide a valid auth token.'
            }
            return make_response(json.dumps(responseObject, default=str)), 401
Exemple #7
0
    def get(self):
        """ get an by id """
        auth_header = request.headers.get('Authorization')
        # get the post data
        post_data = request.get_json()
        if auth_header:
            try:
                auth_token = auth_header.split(" ")[1]
            except IndexError:

                responseObject = {
                    'status': 'fail',
                    'message': 'Bearer token malformed.'
                }
                return make_response(json.dumps(responseObject,
                                                default=str)), 401
        else:
            auth_token = ''
        if auth_token:
            resp = User.decode_auth_token(auth_token)
            if not isinstance(resp, str):
                deferral = Deferral().query.filter(
                    Deferral.c.id == post_data['id']).first()
                data = deferral._asdict()
                data['donor'] = deferral.donor._asdict()
                if deferral:
                    responseObject = {
                        'status': 'success',
                        'message': 'deferral found',
                        'data': data
                    }
                    return make_response(
                        json.dumps(responseObject, default=str)), 200
                else:
                    responseObject = {
                        'status': 'fail',
                        'message': 'deferral not found'
                    }
                    return make_response(
                        json.dumps(responseObject, default=str)), 402
            responseObject = {'status': 'fail', 'message': resp}
            return make_response(json.dumps(responseObject, default=str)), 401
        else:
            responseObject = {
                'status': 'fail',
                'message': 'Provide a valid auth token.'
            }
            return make_response(json.dumps(responseObject, default=str)), 401
Exemple #8
0
    def post(self):
        """ post an item here """

        # check if user already exists
        auth_header = request.headers.get('Authorization')
        # get the post data
        post_data = request.json
        if auth_header:
            try:
                auth_token = auth_header.split(" ")[1]
            except IndexError:

                responseObject = {
                    'status': 'fail',
                    'message': 'Bearer token malformed.'
                }
                return make_response(json.dumps(responseObject,
                                                default=str)), 401
        else:
            auth_token = ''
        if auth_token:
            resp = User.decode_auth_token(auth_token)
            if not isinstance(resp, str):
                hospital = Hospital()
                hospital.hospital_name = post_data["name"]
                hospital.city = post_data["city"]
                hospital.region = post_data["region"]
                hospital.address = post_data["address"]
                hospital.unit_blood_pile = post_data["unit_blood_pile"]
                hospital.phone1 = post_data["phone1"]
                hospital.phone2 = post_data["phone2"]

                db.session.add(hospital)
                db.session.commit()
                data = hospital._return_data()
                responseObject = {'status': 'success', 'data': data}
                return make_response(json.dumps(responseObject,
                                                default=str)), 200
            responseObject = {'status': 'fail', 'message': resp}
            return make_response(json.dumps(responseObject, default=str)), 401
        else:
            responseObject = {
                'status': 'fail',
                'message': 'Provide a valid auth token.'
            }
            return make_response(json.dumps(responseObject, default=str)), 401
Exemple #9
0
    def put(self):
        """ put an item here """

        # check if user already exists
        auth_header = request.headers.get('Authorization')
        # get the post data
        post_data = request.json
        if auth_header:
            try:
                auth_token = auth_header.split(" ")[1]
            except IndexError:

                responseObject = {
                    'status': 'fail',
                    'message': 'Bearer token malformed.'
                }
                return make_response(json.dumps(responseObject,
                                                default=str)), 401
        else:
            auth_token = ''
        if auth_token:
            resp = User.decode_auth_token(auth_token)
            if not isinstance(resp, str):
                deferral = Deferral.query.filter_by(id=post_data['id']).first()

                deferral.donor_id = post_data["donor_id"]
                deferral.reason = post_data["reason"]
                deferral.ndefbd = post_data["ndefbd"]

                db.session.add(deferral)
                db.session.commit()
                deferral.update_donor_ndefbd()

                data = deferral._asdict()
                data['donor'] = deferral.donor._asdict()
                responseObject = {'status': 'success', 'data': data}
                return make_response(json.dumps(responseObject,
                                                default=str)), 200
            responseObject = {'status': 'fail', 'message': resp}
            return make_response(json.dumps(responseObject, default=str)), 401
        else:
            responseObject = {
                'status': 'fail',
                'message': 'Provide a valid auth token.'
            }
            return make_response(json.dumps(responseObject, default=str)), 401
Exemple #10
0
    def put(self):
        """ put an item here """

        # check if user already exists
        auth_header = request.headers.get('Authorization')
        # get the post data
        post_data = request.json
        print("JSON DATA", post_data)
        if auth_header:
            try:
                auth_token = auth_header.split(" ")[1]
            except IndexError:

                responseObject = {
                    'status': 'fail',
                    'message': 'Bearer token malformed.'
                }
                return make_response(json.dumps(responseObject,
                                                default=str)), 401
        else:
            auth_token = ''
        if auth_token:
            resp = User.decode_auth_token(auth_token)
            if not isinstance(resp, str):
                admin = User.query.filter_by(id=post_data['id']).first()

                admin.email = post_data["email"]
                admin.name = post_data["name"]
                admin.password = admin.encrypt_password(post_data["password"])
                admin.admin = post_data["admin"]

                db.session.add(admin)
                db.session.commit()
                data = admin._asdict()
                responseObject = {'status': 'success', 'data': data}
                return make_response(json.dumps(responseObject,
                                                default=str)), 200
            responseObject = {'status': 'fail', 'message': resp}
            return make_response(json.dumps(responseObject, default=str)), 401
        else:
            responseObject = {
                'status': 'fail',
                'message': 'Provide a valid auth token.'
            }
            return make_response(json.dumps(responseObject, default=str)), 401
Exemple #11
0
    def delete(self):
        """ delete an item here """
        # check if user already exists
        auth_header = request.headers.get('Authorization')
        # get the post data
        post_data = request.json
        if auth_header:
            try:
                auth_token = auth_header.split(" ")[1]
            except IndexError:

                responseObject = {
                    'status': 'fail',
                    'message': 'Bearer token malformed.'
                }
                return make_response(json.dumps(responseObject,
                                                default=str)), 401
        else:
            auth_token = ''
        if auth_token:
            resp = User.decode_auth_token(auth_token)
            if not isinstance(resp, str):
                hospital = Hospital.query.filter_by(id=post_data['id']).first()

                db.session.delete(hospital)
                db.session.commit()

                responseObject = {
                    'status': 'success',
                }
                return make_response(json.dumps(responseObject,
                                                default=str)), 200
            responseObject = {'status': 'fail', 'message': resp}
            return make_response(json.dumps(responseObject, default=str)), 401
        else:
            responseObject = {
                'status': 'fail',
                'message': 'Provide a valid auth token.'
            }
            return make_response(json.dumps(responseObject, default=str)), 401
Exemple #12
0
 def post(self):
     # get auth token
     auth_header = request.headers.get('Authorization')
     if auth_header:
         auth_token = auth_header.split(" ")[1]
     else:
         auth_token = ''
     if auth_token:
         resp = User.decode_auth_token(auth_token)
         if not isinstance(resp, str):
             # mark the token as blacklisted
             blacklist_token = BlacklistToken(token=auth_token)
             try:
                 # insert the token
                 db.session.add(blacklist_token)
                 db.session.commit()
                 responseObject = {
                     'status': 'success',
                     'message': 'Successfully logged out.'
                 }
                 return make_response(
                     json.dumps(responseObject, default=str)), 200
             except Exception as e:
                 responseObject = {'status': 'fail', 'message': e}
                 return make_response(
                     json.dumps(responseObject, default=str)), 200
         else:
             responseObject = {'status': 'fail', 'message': resp}
             return make_response(json.dumps(responseObject,
                                             default=str)), 401
     else:
         responseObject = {
             'status': 'fail',
             'message': 'Provide a valid auth token.'
         }
         return make_response(json.dumps(responseObject, default=str)), 403
Exemple #13
0
    def post(self):
        """ post an item here """

        # check if user already exists
        auth_header = request.headers.get('Authorization')
        # get the post data
        post_data = request.json
        if auth_header:
            try:
                auth_token = auth_header.split(" ")[1]
            except IndexError:

                responseObject = {
                    'status': 'fail',
                    'message': 'Bearer token malformed.'
                }
                return make_response(json.dumps(responseObject,
                                                default=str)), 401
        else:
            auth_token = ''
        if auth_token:
            resp = User.decode_auth_token(auth_token)
            if not isinstance(resp, str):
                mc = ', '.join(
                    [obj['value'] for obj in post_data["medical_conditions"]])
                subscriber = Subscriber()

                subscriber.sn = post_data["sn"]
                subscriber.email = post_data["email"]
                subscriber.first_name = post_data["first_name"]
                subscriber.hospital_id = post_data["hospital_id"]
                subscriber.middle_name = post_data["middle_name"]
                subscriber.last_name = post_data["last_name"]
                subscriber.home_address = post_data["home_address"]
                subscriber.region = post_data["region"]
                subscriber.city = post_data["city"]
                subscriber.phone1 = post_data["phone1"]
                subscriber.phone2 = post_data["phone2"]
                subscriber.cni = post_data["cni"]
                subscriber.cni_doi = post_data["cni_doi"]
                subscriber.cni_poi = post_data["cni_poi"]
                subscriber.dob = post_data["dob"]
                subscriber.pob = post_data["pob"]
                subscriber.gender = post_data["gender"]
                subscriber.blood_group = post_data["blood_group"]
                subscriber.medical_conditions = mc
                subscriber.current_medications = post_data[
                    "current_medications"]
                subscriber.allergies = post_data["allergies"]
                subscriber.rhesus_factor = post_data["rhesus_factor"]

                db.session.add(subscriber)
                db.session.commit()
                data = subscriber._return_data()
                responseObject = {'status': 'success', 'data': data}
                return make_response(json.dumps(responseObject,
                                                default=str)), 200
            responseObject = {'status': 'fail', 'message': resp}
            return make_response(json.dumps(responseObject, default=str)), 401
        else:
            responseObject = {
                'status': 'fail',
                'message': 'Provide a valid auth token.'
            }
            return make_response(json.dumps(responseObject, default=str)), 401