Example #1
0
    def post(cls):
        data = cls.patient_parser.parse_args()

        patient = PatientModel.find_by_username(data["username"])

        if patient and check_password_hash(patient.password, data["password"]):
            access_token = create_access_token(
                identity=patient.id,
                fresh=True,
                user_claims={"type": "patient"},
                expires_delta=timedelta(1),
            )
            refresh_token = create_refresh_token(
                identity=patient.id, user_claims={"type": "patient"})
            return {
                "access_token": access_token,
                "refresh_token": refresh_token
            }, 200
        return {"message": "Invaild credentials"}, 401
Example #2
0
    def post(self):
        data = PatientRegister.patient_parser.parse_args()

        if (data["username"].isspace() or data["password"].isspace()
                or data["address"].isspace() or data["mobile"].isspace()
                or data["email"].isspace() or data["first_name"].isspace()
                or data["last_name"].isspace()):
            return {"message": "One of the inputs is empty"}, 400

        if len(data["username"]) < 5:
            return {"message": "Username is too short"}, 400

        if PatientModel.find_by_username(data["username"]):
            return {"message": "A user with that username already exists"}, 400

        if PatientModel.find_by_email(data["email"]):
            return {"message": "A user with that email already exists"}, 400

        y, m, d = [int(x) for x in data["birthdate"].split("-")]
        data["birthdate"] = datetime(y, m, d).date()
        if ((datetime.now().date() - data["birthdate"]).days // 365) < 1:
            return {"message": "Invalid age"}, 400

        data["gender"] = int(data["gender"])
        if data["gender"] != 0 and data["gender"] != 1:
            return {
                "message":
                "Invalid request: gender is only '0' if male or '1' if female"
            }

        data["created_at"] = datetime.now().date()

        patient = PatientModel(**data)
        patient.save_to_db()

        return {"message": "User created successfully."}, 201