-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: POST and GET /user/personal_background
- Loading branch information
1 parent
cfcfa93
commit 3099657
Showing
7 changed files
with
322 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
from http import HTTPStatus | ||
from flask import json | ||
from app.database.models.bit_schema.personal_background import PersonalBackgroundModel | ||
from app import messages | ||
from app.api.request_api_utils import AUTH_COOKIE | ||
|
||
|
||
class PersonalBackgroundDAO: | ||
|
||
"""Data Access Object for Personal_Background functionalities""" | ||
|
||
@staticmethod | ||
def create_user_personal_background(data): | ||
"""Creates a personal_background instance for a new registered user. | ||
Arguments: | ||
data: A list containing user's id, and user's background details (gender, | ||
age, ethnicity, sexual_orientation, religion, physical_ability, mental_ability, | ||
socio_economic, highest_education, years_of_experience, others) as well as | ||
whether or not user agrees to make their personal background information | ||
public to other members of BridgeInTech. | ||
Returns: | ||
A dictionary containing "message" which indicates whether or not the user_exension | ||
was created successfully and "code" for the HTTP response code. | ||
""" | ||
|
||
user_id = AUTH_COOKIE["user_id"].value, | ||
|
||
existing_personal_background = PersonalBackgroundModel.find_by_user_id(user_id) | ||
if existing_personal_background: | ||
return messages.PERSONAL_BACKGROUND_OF_USER_ALREADY_EXIST, HTTPStatus.CONFLICT | ||
|
||
try: | ||
personal_background = PersonalBackgroundModel( | ||
user_id=user_id, | ||
gender=data["gender"], | ||
age=data["age"], | ||
ethnicity=data["ethnicity"], | ||
sexual_orientation=data["sexual_orientation"], | ||
religion=data["religion"], | ||
physical_ability=data["physical_ability"], | ||
mental_ability=data["mental_ability"], | ||
socio_economic=data["socio_economic"], | ||
highest_education=data["highest_education"], | ||
years_of_experience=data["years_of_experience"], | ||
) | ||
personal_background.others = { | ||
"gender_other": data["gender_other"], | ||
"age_other": data["age_other"], | ||
"ethnicity_other": data["ethnicity_other"], | ||
"sexual_orientation_other": data["sexual_orientation_other"], | ||
"religion_other": data["religion_other"], | ||
"physical_ability_other": data["physical_ability_other"], | ||
"mental_ability_other": data["mental_ability_other"], | ||
"socio_economic_other": data["socio_economic_other"], | ||
"highest_education_other": data["highest_education_other"], | ||
"years_of_experience_other": data["years_of_experience_other"] | ||
} | ||
personal_background.is_public = data["is_public"] | ||
except KeyError as e: | ||
return e, HTTPStatus.BAD_REQUEST | ||
|
||
personal_background.save_to_db() | ||
|
||
return messages.PERSONAL_BACKGROUND_SUCCESSFULLY_CREATED, HTTPStatus.CREATED | ||
|
||
|
||
@staticmethod | ||
def get_user_personal_background_info(user_id): | ||
"""Retrieves a user's perrsonal background information using a specified ID. | ||
Arguments: | ||
user_id: The ID of the user to be searched. | ||
Returns: | ||
The PersonalBackgroundModel class of the user whose ID was searched, containing their additional information. | ||
""" | ||
|
||
result = PersonalBackgroundModel.find_by_user_id(user_id) | ||
if result: | ||
return { | ||
"user_id": result.user_id, | ||
"gender": result.gender.name, | ||
"age": result.age.name, | ||
"ethnicity": result.ethnicity.name, | ||
"sexual_orientation": result.sexual_orientation.name, | ||
"religion": result.religion.name, | ||
"physical_ability": result.physical_ability.name, | ||
"mental_ability": result.mental_ability.name, | ||
"socio_economic": result.socio_economic.name, | ||
"highest_education": result.highest_education.name, | ||
"years_of_experience": result.years_of_experience.name, | ||
"gender_other": result.others["gender_other"], | ||
"age_other": result.others["age_other"], | ||
"ethnicity_other": result.others["ethnicity_other"], | ||
"sexual_orientation_other": result.others["sexual_orientation_other"], | ||
"religion_other": result.others["religion_other"], | ||
"physical_ability_other": result.others["physical_ability_other"], | ||
"mental_ability_other": result.others["mental_ability_other"], | ||
"socio_economic_other": result.others["socio_economic_other"], | ||
"highest_education_other": result.others["highest_education_other"], | ||
"years_of_experience_other": result.others["years_of_experience_other"], | ||
"is_public": result.is_public | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.