-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add BIT personal background to MS schema
Add new migration script
- Loading branch information
1 parent
f25a0b8
commit 231b3be
Showing
4 changed files
with
377 additions
and
1 deletion.
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,135 @@ | ||
from sqlalchemy import null | ||
from app.database.sqlalchemy_extension import db | ||
from app.database.db_types.JsonCustomType import JsonCustomType | ||
from app.utils.bitschema_utils import Gender, Age, Ethnicity, SexualOrientation, Religion, PhysicalAbility, MentalAbility, SocioEconomic, HighestEducation, YearsOfExperience | ||
|
||
|
||
class PersonalBackgroundModel(db.Model): | ||
"""Defines attributes for user's personal background. | ||
Attributes: | ||
user_id: An integer for storing the user's id. | ||
gender: A string for storing the user's gender. | ||
age: A string for storing the user's age. | ||
ethnicity: A string for storing the user's wthnicity. | ||
sexual_orientation: A string for storing the user's sexual orientation. | ||
religion: A string for storing the user's religion. | ||
physical_ability: A string for storing the user's physical ability. | ||
mental_ability: A string for storing the user's mental ability. | ||
socio_economic: A string for storing the user's socio economic level. | ||
highest_education: A string for storing the user's highest education level. | ||
years_of_experience: A string for storing the user's length of expeprience in the It related area. | ||
others: A JSON data type for storing users descriptions of 'other' fields. | ||
is_public: A boolean indicating if user has agreed to display their personal background information publicly to other members. | ||
""" | ||
|
||
# Specifying database table used for PersonalBackgroundModel | ||
__tablename__ = "personal_backgrounds" | ||
__table_args__ = {"extend_existing": True} | ||
|
||
id = db.Column(db.Integer, primary_key=True) | ||
|
||
# User's personal background data | ||
user_id = db.Column( | ||
db.Integer, | ||
db.ForeignKey("users.id", ondelete="CASCADE"), | ||
nullable=False, | ||
unique=True, | ||
) | ||
gender = db.Column(db.Enum(Gender)) | ||
age = db.Column(db.Enum(Age)) | ||
ethnicity = db.Column(db.Enum(Ethnicity)) | ||
sexual_orientation = db.Column(db.Enum(SexualOrientation)) | ||
religion = db.Column(db.Enum(Religion)) | ||
physical_ability = db.Column(db.Enum(PhysicalAbility)) | ||
mental_ability = db.Column(db.Enum(MentalAbility)) | ||
socio_economic = db.Column(db.Enum(SocioEconomic)) | ||
highest_education = db.Column(db.Enum(HighestEducation)) | ||
years_of_experience = db.Column(db.Enum(YearsOfExperience)) | ||
others = db.Column(JsonCustomType) | ||
is_public = db.Column(db.Boolean) | ||
|
||
def __init__( | ||
self, | ||
user_id, | ||
gender, | ||
age, | ||
ethnicity, | ||
sexual_orientation, | ||
religion, | ||
physical_ability, | ||
mental_ability, | ||
socio_economic, | ||
highest_education, | ||
years_of_experience, | ||
): | ||
"""Initialises PersonalBackgroundModel class.""" | ||
## required fields | ||
self.user_id = user_id | ||
self.gender = gender | ||
self.age = age | ||
self.ethnicity = ethnicity | ||
self.sexual_orientation = sexual_orientation | ||
self.religion = religion | ||
self.physical_ability = physical_ability | ||
self.mental_ability = mental_ability | ||
self.socio_economic = socio_economic | ||
self.highest_education = highest_education | ||
self.years_of_experience = years_of_experience | ||
|
||
# default values | ||
self.others = None | ||
self.is_public = False | ||
|
||
def json(self): | ||
"""Returns PersonalBackgroundModel object in json format.""" | ||
return { | ||
"id": self.id, | ||
"user_id": self.user_id, | ||
"age": self.age, | ||
"ethnicity": self.ethnicity, | ||
"sexual_orientation": self.sexual_orientation, | ||
"religion": self.religion, | ||
"physical_ability": self.physical_ability, | ||
"mental_ability": self.mental_ability, | ||
"socio_economic": self.socio_economic, | ||
"highest_education": self.highest_education, | ||
"years_of_experience": self.years_of_experience, | ||
"others": self.others, | ||
"is_public": self.is_public, | ||
} | ||
|
||
def __repr__(self): | ||
"""Returns user's background.""" | ||
|
||
return ( | ||
f"User's id is {self.user_id}.\n" | ||
f"User's age is: {self.age}\n" | ||
f"User's ethnicity is: {self.ethnicity}\n" | ||
f"User's sexual orientation is: {self.sexual_orientation}\n" | ||
f"User's religion is: {self.religion}\n" | ||
f"User's physical ability is: {self.physical_ability}\n" | ||
f"User's mental ability is: {self.mental_ability}\n" | ||
f"User's socio economic category is: {self.socio_economic}\n" | ||
f"User's highest level of education is: {self.highest_education}\n" | ||
f"User's length of experience is: {self.years_of_experience}\n" | ||
) | ||
|
||
@classmethod | ||
def find_by_user_id(cls, user_id) -> "PersonalBackgroundModel": | ||
|
||
"""Returns the user's background that has the passed user id. | ||
Args: | ||
_id: The id of a user. | ||
""" | ||
return cls.query.filter_by(user_id=user_id).first() | ||
|
||
def save_to_db(self) -> None: | ||
"""Adds user's personal background to the database.""" | ||
db.session.add(self) | ||
db.session.commit() | ||
|
||
def delete_from_db(self) -> None: | ||
"""Deletes user's personal background from the database.""" | ||
db.session.delete(self) | ||
db.session.commit() |
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
47 changes: 47 additions & 0 deletions
47
migrations/versions/0cfd533818e7_add_bit_personal_background.py
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,47 @@ | ||
"""Add BIT personal background | ||
Revision ID: 0cfd533818e7 | ||
Revises: 1272e4b54a32 | ||
Create Date: 2021-05-03 11:36:51.484640 | ||
""" | ||
from alembic import op | ||
from app.database.db_types.JsonCustomType import JsonCustomType | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '0cfd533818e7' | ||
down_revision = '1272e4b54a32' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('personal_backgrounds', | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('user_id', sa.Integer(), nullable=False), | ||
sa.Column('gender', sa.Enum('FEMALE', 'MALE', 'OTHER', 'DECLINED', 'NOT_APPLICABLE', name='gender'), nullable=True), | ||
sa.Column('age', sa.Enum('UNDER_18', 'AGE_18_TO_20', 'AGE_21_TO_24', 'AGE_25_TO_34', 'AGE_35_TO_44', 'AGE_45_TO_54', 'AGE_55_TO_64', 'ABOVE_65_YO', 'DECLINED', 'NOT_APPLICABLE', name='age'), nullable=True), | ||
sa.Column('ethnicity', sa.Enum('AFRICAN_AMERICAN', 'CAUCASIAN', 'HISPANIC', 'NATIVE_AMERICAN', 'MIDDLE_EASTERN', 'ASIAN', 'OTHER', 'DECLINED', 'NOT_APPLICABLE', name='ethnicity'), nullable=True), | ||
sa.Column('sexual_orientation', sa.Enum('HETEROSEXUAL', 'LGBTIA', 'OTHER', 'DECLINED', 'NOT_APPLICABLE', name='sexualorientation'), nullable=True), | ||
sa.Column('religion', sa.Enum('CHRISTIANITY', 'JUDAISM', 'ISLAM', 'HINDUISM', 'BUDDHISM', 'OTHER', 'DECLINED', 'NOT_APPLICABLE', name='religion'), nullable=True), | ||
sa.Column('physical_ability', sa.Enum('WITH_DISABILITY', 'WITHOUT_DISABILITY', 'OTHER', 'DECLINED', 'NOT_APPLICABLE', name='physicalability'), nullable=True), | ||
sa.Column('mental_ability', sa.Enum('WITH_DISORDER', 'WITHOUT_DISORDER', 'OTHER', 'DECLINED', 'NOT_APPLICABLE', name='mentalability'), nullable=True), | ||
sa.Column('socio_economic', sa.Enum('UPPER', 'UPPER_MIDDLE', 'LOWER_MIDDLE', 'WORKING', 'BELOW_POVERTY', 'OTHER', 'DECLINED', 'NOT_APPLICABLE', name='socioeconomic'), nullable=True), | ||
sa.Column('highest_education', sa.Enum('BELOW_HIGH_SCHOOL', 'HIGH_SCHOOL', 'ASSOCIATE', 'BACHELOR', 'MASTER', 'PHD', 'OTHER', 'DECLINED', 'NOT_APPLICABLE', name='highesteducation'), nullable=True), | ||
sa.Column('years_of_experience', sa.Enum('UNDER_ONE', 'UP_TO_3', 'UP_TO_5', 'UP_TO_10', 'OVER_10', 'DECLINED', 'NOT_APPLICABLE', name='yearsofexperience'), nullable=True), | ||
sa.Column('others', JsonCustomType, nullable=True), | ||
sa.Column('is_public', sa.Boolean(), nullable=True), | ||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('user_id') | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('personal_backgrounds') | ||
# ### end Alembic commands ### |
Oops, something went wrong.