コード例 #1
0
def create():
    name = request.json.get('name')
    password = request.json.get('password')
    email = request.json.get('email')
    ic_number = request.json.get('ic_number')
    gender = request.json.get('gender')
    role = request.json.get('role')
    disease = request.json.get('disease')

    online_user = get_jwt_identity()
    user = User.get_or_none(User.id == online_user['id'])

    if "admin" not in user.role:
        return jsonify({
            "message": "401 Unauthorized (Only admin is allowed)",
            "status": "Fail"
        })

    if (("1" in role) or ("2" in role)) and (("3" in role) or ("4" in role)):
        response = {
            "message": "Patient/Guardian cannot be Doctor/Admin at the same time.",
            "status": "fail"
        }
    else:
        if ("1" in role) or (("1" in role) and ("2" in role)):  # if this is patient or patient/guardian
            guardian = request.json.get('guardian')
            if guardian: #If there is guardian entered
                new_guardian = User.get_or_none(User.ic_number == request.json.get('guardian'))
                guardian_id = new_guardian
            else:
                guardian_id = None
            if disease: #If there is disease entered
                pass
            else: #If there is no disease entered for a patient, return error
                return jsonify({
                    "message": "Did not enter disease for patient.",
                    "status": "fail"
                })
            new_user = User(name=name, password=password, email=email, ic_number=ic_number, gender=gender, guardian=guardian_id)
            if new_user.save():
                #handle disease for both patient and patient/guardian
                for i in range(len(disease)):
                    new_user_disease = UserDisease(disease=int(disease[i-1]), user=new_user)
                    if new_user_disease.save():
                        pass
                    else:
                        response = new_user.error() #method from models.user
                if "1" in role and "2" in role:
                    #handle role for patient/guardian
                    user_role = UserRole(role=role[0], user=new_user)
                    user_role2 = UserRole(role=role[1], user=new_user)
                    if user_role.save() and user_role2.save():
                        #response after saved user, disease, role
                        response = {
                            "message": f"Successfully created a user.",
                            "status": "success",
                            "user": {
                                "id": new_user.id,
                                "name": new_user.name,
                                "email": new_user.email,
                                "ic_number": new_user.ic_number,
                                "gender": new_user.gender,
                                "role": new_user.role, #hybrid property
                                "disease": new_user.disease #hybrid property
                            }
                        }
                        if new_user.guardian: #append guardian if there is one, else append as none
                            response['user']['guardian'] = new_user.guardian.name                        
                        else:
                            response['user']['guardian'] = None
                    else:
                        response = new_user.error() #method from models.user
                else:
                    #handle role for patient
                    user_role = UserRole(role=role[0], user=new_user)
                    if user_role.save():
                        response = {
                            "message": f"Successfully created a user.",
                            "status": "success",
                            "user": {
                                "id": new_user.id,
                                "name": new_user.name,
                                "email": new_user.email,
                                "ic_number": new_user.ic_number,
                                "gender": new_user.gender,
                                "role": new_user.role, #hybrid property
                                "disease": new_user.disease #hybrid property
                            }
                        }
                        if new_user.guardian: #append guardian if there is one, else append as none
                            response['user']['guardian'] = new_user.guardian.name                        
                        else:
                            response['user']['guardian'] = None
                    else:
                        response = new_user.error() #method from models.user
            else:
                response = new_user.error() #method from models.user
        else:  # doctor or admin
            if ("3" in role) and ("4" in role):
                response = {
                    "message": "A user cannot be a Doctor and Admin at the same time",
                    "status": "fail"
                }
            else:
                new_user = User(name=name, password=password, email=email,ic_number=ic_number, gender=gender, guardian=None)
                if new_user.save():
                    user_role = UserRole(role=role[0], user=new_user)
                    if user_role.save():
                        response = {
                            "message": f"Successfully created a user.",
                            "status": "success",
                            "user": {
                                "id": new_user.id,
                                "name": new_user.name,
                                "email": new_user.email,
                                "ic_number": new_user.ic_number,
                                "gender": new_user.gender,
                                "role": new_user.role, #hybrid property
                            }
                        }
                    else:
                        response = new_user.error() #method from models.user
                else:
                   response = new_user.error() #method from models.user
    return jsonify(response)
コード例 #2
0
def edit_by_admin():
    ic_number = request.json.get('ic_number')
    user = User.get_or_none(User.ic_number == ic_number) #user to be edited
    online_user = get_jwt_identity() 
    current_user = User.get_or_none(User.id == online_user['id']) #user that is editing
    
    if user: #check existence of user
        if ("admin" in current_user.role): #check editing user is admin (if not, 401)
            #set attributes that is 'sure have'
            user.name = request.json.get('name')
            user.password = request.json.get('password')
            user.email = request.json.get('email')
            user.gender = request.json.get('gender')

            #handle guardian
            if request.json.get('guardian'): #if guardian is inputted
                new_guardian = User.get_or_none(User.ic_number == request.json.get('guardian'))
                user.guardian = new_guardian.id #update/remain according to input
            else: #if no guardian is inputted, delete the guardian
                user.guardian = None 
            
            if user.save(): # save the fields in user table first
                #handle role
                role = request.json.get('role')
                if role: #if role is inputted
                    if (("1" in role) or ("2" in role)) and (("3" in role) or ("4" in role)):
                        return jsonify({
                            "message": "Patient/Guardian cannot be Doctor/Admin at the same time.",
                            "status": "fail"
                        })
                    elif ("3" in role) and ("4" in role):
                        return jsonify({
                            "message": "A user cannot be a Doctor and Admin at the same time",
                            "status": "fail"
                        })
                    elif (("1" in user.role_id) or ("2" in user.role_id)) and (("3" in role) or ("4" in role)):
                        return jsonify({
                            "message": "A user cannot switch role from Patient/Guardian to Doctor/Admin",
                            "status": "fail"
                        })
                    elif (("3" in user.role_id) or ("4" in user.role_id)) and (("1" in role) or ("2" in role)):
                        return jsonify({
                            "message": "A user cannot switch role from Doctor/Admin to Patient/Guardian",
                            "status": "fail"
                        })
                    else:
                        if sorted(user.role_id) == sorted(role): #if same, no need change
                            pass
                        else:
                            # Delete obselete role
                            role_to_delete = []
                            for i in range(len(user.role_id)):
                                if user.role_id[i] not in role:
                                    role_to_delete.append(user.role_id[i])
                            for i in range(len(role_to_delete)):
                                del_role = UserRole.get_or_none(UserRole.role == role_to_delete[i], UserRole.user == user)
                                del_role.delete_instance()
                            # Add new role
                            for i in range(0,len(role)):
                                if role[i] not in user.role_id:
                                    new_role = UserRole(user=user, role=role[i])
                                    if new_role.save():
                                        pass
                                    else:
                                        return jsonify({
                                            "message": "Problem occured when saving new role.",
                                            "status": "fail"
                                        })
                else: #if no role is inputted, remain
                    pass #Note: If can prefill role in frontend, should change to warning message "user cannot have no role"
                if "1" in user.role_id: #after saving new role(s), this checks the updated version of the user's role(s)
                    #handle disease
                    disease = request.json.get('disease')
                    if disease:
                        if sorted(user.disease_id) == sorted(disease): #if same, no need change
                            pass
                        else:
                            # Delete obselete disease
                            disease_to_delete = []
                            for i in range(len(user.disease_id)):
                                if user.disease_id[i] not in disease:
                                    disease_to_delete.append(user.disease_id[i])
                            for i in range(len(disease_to_delete)):
                                del_disease = UserDisease.get_or_none(UserDisease.disease == disease_to_delete[i], UserDisease.user == user)
                                del_disease.delete_instance()
                            # Add new disease
                            for i in range(len(disease)):
                                if disease[i] not in user.disease_id:
                                    new_disease = UserDisease(user=user, disease=disease[i])
                                    if new_disease.save():
                                        pass
                                    else:
                                        return jsonify({
                                            "message": "Problem occured when saving new disease.",
                                            "status": "fail"
                                        })
                    else: #if no disease is inputted, remain
                        pass #Note: If can prefill disease in frontend, should change to warning message "patient cannot have no disease"
                response = {
                    "message": "Successfully edited user.",
                    "status": "success"
                }
            else:
                response = user.error()
        else:
            response = {
                "message": "401 Unauthorized",
                "status": "fail"
            }
    else:
        response = {
            "message": "User not found, please try again",
            "status": "fail"
        }
    return jsonify(response)