Skip to content

Commit

Permalink
MS Backend server for BIT development purpose
Browse files Browse the repository at this point in the history
replace db uri

add Flask-Cors to requirements.txt
  • Loading branch information
mtreacy002 committed Mar 3, 2021
1 parent 479f1c7 commit f66749b
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 79 deletions.
8 changes: 4 additions & 4 deletions app/api/dao/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,11 @@ def update_user_profile(user_id: int, data: Dict[str, str]):
else:
user.occupation = None

if "organization" in data:
if data["organization"]:
user.organization = data["organization"]
if "current_organization" in data:
if data["current_organization"]:
user.current_organization = data["current_organization"]
else:
user.organization = None
user.current_organization = None

if "slack_username" in data:
if data["slack_username"]:
Expand Down
2 changes: 1 addition & 1 deletion app/api/models/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def add_models_to_namespace(api_namespace):
"bio": fields.String(required=True, description="User bio"),
"location": fields.String(required=True, description="User location"),
"occupation": fields.String(required=True, description="User occupation"),
"organization": fields.String(required=True, description="User organization"),
"current_organization": fields.String(required=True, description="User current organization"),
"skills": fields.String(required=True, description="User skills"),
},
)
6 changes: 3 additions & 3 deletions app/api/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def add_models_to_namespace(api_namespace):
"bio": fields.String(required=True, description="User bio"),
"location": fields.String(required=True, description="User location"),
"occupation": fields.String(required=True, description="User occupation"),
"organization": fields.String(required=True, description="User organization"),
"current_organization": fields.String(required=True, description="User current_organization"),
"interests": fields.String(required=True, description="User interests"),
"skills": fields.String(required=True, description="User skills"),
"need_mentoring": fields.Boolean(
Expand Down Expand Up @@ -91,7 +91,7 @@ def add_models_to_namespace(api_namespace):
"bio": fields.String(required=False, description="User bio"),
"location": fields.String(required=False, description="User location"),
"occupation": fields.String(required=False, description="User occupation"),
"organization": fields.String(required=False, description="User organization"),
"current_organization": fields.String(required=False, description="User current_organization"),
"slack_username": fields.String(
required=False, description="User slack username"
),
Expand Down Expand Up @@ -179,7 +179,7 @@ def add_models_to_namespace(api_namespace):
"bio": fields.String(required=False, description="User bio"),
"location": fields.String(required=False, description="User location"),
"occupation": fields.String(required=False, description="User occupation"),
"organization": fields.String(required=False, description="User organization"),
"current_organization": fields.String(required=False, description="User current_organization"),
"slack_username": fields.String(
required=False, description="User slack username"
),
Expand Down
2 changes: 1 addition & 1 deletion app/api/resources/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get(cls):
A admin user with valid access token can view the list of all admins. The endpoint
doesn't take any other input. A JSON array having an object for each admin user is
returned. The array contains id, username, name, slack_username, bio,
location, occupation, organization, skills.
location, occupation, current_organization, skills.
The current admin user's details are not returned.
"""
user_id = get_jwt_identity()
Expand Down
10 changes: 5 additions & 5 deletions app/api/resources/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def get(cls):
A user with valid access token can view the list of users. The endpoint
doesn't take any other input. A JSON array having an object for each user is
returned. The array contains id, username, name, slack_username, bio,
location, occupation, organization, interests, skills, need_mentoring,
available_to_mentor, registration_date. The current user's details are not returned.
location, occupation, current_organization, interests, skills, need_mentoring,
available_to_mentor. The current user's details are not returned.
"""

page = request.args.get("page", default=UserDAO.DEFAULT_PAGE, type=int)
Expand Down Expand Up @@ -158,7 +158,7 @@ def put(cls):
A user with valid access token can use this endpoint to edit his/her own
user details. The endpoint takes any of the given parameters (name, username,
bio, location, occupation, organization, slack_username, social_media_links,
bio, location, occupation, current_organization, slack_username, social_media_links,
skills, interests, resume_url, photo_url, need_mentoring, available_to_mentor).
The response contains a success message.
"""
Expand Down Expand Up @@ -269,7 +269,7 @@ def get(cls):
A user with valid access token can view the list of verified users. The endpoint
doesn't take any other input. A JSON array having an object for each user is
returned. The array contains id, username, name, slack_username, bio,
location, occupation, organization, interests, skills, need_mentoring,
location, occupation, current_organization, interests, skills, need_mentoring,
available_to_mentor. The current user's details are not returned.
"""

Expand Down Expand Up @@ -337,7 +337,7 @@ def post(cls):

if result[1] is HTTPStatus.CREATED:
send_email_verification_message(data["name"], data["email"])

print(result[0])
return result


Expand Down
8 changes: 4 additions & 4 deletions app/api/validations/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ def validate_update_profile_request_data(data):
if not is_valid[0]:
return is_valid[1]

organization = data.get("organization", None)
if organization:
current_organization = data.get("current_organization", None)
if current_organization:
is_valid = validate_length(
len(get_stripped_string(organization)),
len(get_stripped_string(current_organization)),
0,
ORGANIZATION_MAX_LENGTH,
"organization",
"current_organization",
)
if not is_valid[0]:
return is_valid[1]
Expand Down
16 changes: 8 additions & 8 deletions app/database/models/mentorship_relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class MentorshipRelationModel(db.Model):
action_user_id: integer indicates id of action user.
mentor: relationship between UserModel and mentorship_relation.
mentee: relationship between UserModel and mentorship_relation.
creation_date: float that defines the date of creation of the mentorship.
accept_date: float that indicates the date of acceptance of mentorship.
start_date: float that indicates the starting date of mentorship.
end_date: float that indicates the ending date of mentorship.
creation_date: numeric that defines the date of creation of the mentorship.
accept_date: numeric that indicates the date of acceptance of mentorship.
start_date: numeric that indicates the starting date of mentorship.
end_date: numeric that indicates the ending date of mentorship.
state: enumeration that indicates state of mentorship.
notes: string that indicates any notes.
tasks_list_id: integer indicates the id of the tasks_list
Expand Down Expand Up @@ -47,10 +47,10 @@ class MentorshipRelationModel(db.Model):
primaryjoin="MentorshipRelationModel.mentee_id == UserModel.id",
)

creation_date = db.Column(db.Float, nullable=False)
accept_date = db.Column(db.Float)
start_date = db.Column(db.Float)
end_date = db.Column(db.Float)
creation_date = db.Column(db.Numeric("16,6", asdecimal=False), nullable=False)
accept_date = db.Column(db.Numeric("16,6", asdecimal=False))
start_date = db.Column(db.Numeric("16,6", asdecimal=False))
end_date = db.Column(db.Numeric("16,6", asdecimal=False))

state = db.Column(db.Enum(MentorshipRelationState), nullable=False)
notes = db.Column(db.String(400))
Expand Down
20 changes: 10 additions & 10 deletions app/database/models/task_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
class TaskCommentModel(db.Model):
"""Defines attributes for the task comment.
Attributes:
task_id: An integer for storing the task's id.
user_id: An integer for storing the user's id.
relation_id: An integer for storing the relation's id.
creation_date: A float indicating comment's creation date.
modification_date: A float indicating the modification date.
comment: A string indicating the comment.
"""
Attributes:
task_id: An integer for storing the task's id.
user_id: An integer for storing the user's id.
relation_id: An integer for storing the relation's id.
creation_date: A numeric indicating comment's creation date.
modification_date: A numeric indicating the modification date.
comment: A string indicating the comment.
"""

# Specifying database table used for TaskCommentModel
__tablename__ = "tasks_comments"
Expand All @@ -24,8 +24,8 @@ class TaskCommentModel(db.Model):
user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
task_id = db.Column(db.Integer, db.ForeignKey("tasks_list.id"))
relation_id = db.Column(db.Integer, db.ForeignKey("mentorship_relations.id"))
creation_date = db.Column(db.Float, nullable=False)
modification_date = db.Column(db.Float)
creation_date = db.Column(db.Numeric("16,6", asdecimal=False), nullable=False)
modification_date = db.Column(db.Numeric("16,6", asdecimal=False))
comment = db.Column(db.String(COMMENT_MAX_LENGTH), nullable=False)

def __init__(self, user_id, task_id, relation_id, comment):
Expand Down
4 changes: 2 additions & 2 deletions app/database/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class UserModel(db.Model):
bio = db.Column(db.String(500))
location = db.Column(db.String(80))
occupation = db.Column(db.String(80))
organization = db.Column(db.String(80))
current_organization = db.Column(db.String(80))
slack_username = db.Column(db.String(80))
social_media_links = db.Column(db.String(500))
skills = db.Column(db.String(500))
Expand Down Expand Up @@ -97,7 +97,7 @@ def json(self):
"bio": self.bio,
"location": self.location,
"occupation": self.occupation,
"organization": self.organization,
"current_organization": self.current_organization,
"slack_username": self.slack_username,
"social_media_links": self.social_media_links,
"skills": self.skills,
Expand Down
45 changes: 25 additions & 20 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class BaseConfig(object):
DB_PASSWORD = os.getenv("DB_PASSWORD")
DB_ENDPOINT = os.getenv("DB_ENDPOINT")
DB_NAME = os.getenv("DB_NAME")
DB_TEST_NAME = os.getenv("DB_TEST_NAME")

UNVERIFIED_USER_THRESHOLD = 2592000 # 30 days

Expand Down Expand Up @@ -76,23 +77,26 @@ class BaseConfig(object):
# mail accounts
MAIL_DEFAULT_SENDER = os.getenv("MAIL_DEFAULT_SENDER")


@staticmethod
def build_db_uri(
db_type_arg=DB_TYPE,
db_user_arg=DB_USERNAME,
db_password_arg=DB_PASSWORD,
db_endpoint_arg=DB_ENDPOINT,
db_name_arg=DB_NAME,
db_type_arg = os.getenv("DB_TYPE"),
db_user_arg = os.getenv("DB_USERNAME"),
db_password_arg = os.getenv("DB_PASSWORD"),
db_endpoint_arg = os.getenv("DB_ENDPOINT"),
db_name_arg = os.getenv("DB_NAME"),
):
"""Build remote database uri using specific environment variables."""

return "{db_type}://{db_user}:{db_password}@{db_endpoint}/{db_name}".format(
db_type=db_type_arg,
db_user=db_user_arg,
db_password=db_password_arg,
db_endpoint=db_endpoint_arg,
db_name=db_name_arg,
)
return f"{db_type_arg}://{db_user_arg}:{db_password_arg}@{db_endpoint_arg}/{db_name_arg}"

@staticmethod
def build_db_test_uri(
db_type_arg = os.getenv("DB_TYPE"),
db_user_arg = os.getenv("DB_USERNAME"),
db_password_arg = os.getenv("DB_PASSWORD"),
db_endpoint_arg = os.getenv("DB_ENDPOINT"),
db_name_arg = os.getenv("DB_TEST_NAME"),
):
return f"{db_type_arg}://{db_user_arg}:{db_password_arg}@{db_endpoint_arg}/{db_name_arg}"


class ProductionConfig(BaseConfig):
Expand Down Expand Up @@ -122,20 +126,21 @@ class LocalConfig(BaseConfig):

DEBUG = True

# Using a local sqlite database
SQLALCHEMY_DATABASE_URI = "sqlite:///local_data.db"


# Using a local postgre database
SQLALCHEMY_DATABASE_URI = "postgresql:///bit_schema"
# SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_uri()
class TestingConfig(BaseConfig):
"""Testing configuration."""

TESTING = True
MOCK_EMAIL = True

# Use in-memory SQLite database for testing
SQLALCHEMY_DATABASE_URI = "sqlite://"

SQLALCHEMY_DATABASE_URI = "postgresql:///bit_schema_test"
# SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_test_uri()


def get_env_config() -> str:
flask_config_name = os.getenv("FLASK_ENVIRONMENT_CONFIG", "dev")
if flask_config_name not in ["prod", "test", "dev", "local", "stag"]:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
APScheduler==3.6.3
coverage==5.3
Flask==1.0.2
Flask-Cors==3.0.9
Flask-JWT-Extended==3.25.0
Flask-Mail==0.9.1
Flask-Migrate==2.5.3
Expand Down
14 changes: 9 additions & 5 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from flask import Flask
from config import get_env_config
from flask_migrate import Migrate
from flask_cors import CORS

cors = CORS()

def create_app(config_filename: str) -> Flask:
app = Flask(__name__)
Expand All @@ -16,6 +18,8 @@ def create_app(config_filename: str) -> Flask:

migrate = Migrate(app, db)

cors.init_app(app, resources={r"*": {"origins": "http:localhost:5000"}})

from app.api.jwt_extension import jwt

jwt.init_app(app)
Expand All @@ -38,12 +42,12 @@ def create_app(config_filename: str) -> Flask:
application = create_app(get_env_config())


@application.before_first_request
def create_tables():
from app.database.sqlalchemy_extension import db
# @application.before_first_request
# def create_tables():
# from app.database.sqlalchemy_extension import db

db.create_all()
# db.create_all()


if __name__ == "__main__":
application.run(port=5000)
application.run(port=4000)
6 changes: 0 additions & 6 deletions tests/test_app_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def test_app_testing_config(self):
self.assertFalse(application.config["DEBUG"])
self.assertTrue(application.config["TESTING"])
self.assertFalse(application.config["SQLALCHEMY_TRACK_MODIFICATIONS"])
self.assertEqual("sqlite://", application.config["SQLALCHEMY_DATABASE_URI"])
self.assertIsNotNone(current_app)

# testing JWT configurations
Expand Down Expand Up @@ -89,7 +88,6 @@ def test_app_development_config(self):
self.assertTrue(application.config["DEBUG"])
self.assertFalse(application.config["TESTING"])
self.assertFalse(application.config["SQLALCHEMY_TRACK_MODIFICATIONS"])
# self.assertEqual('mysql_something', application.config['SQLALCHEMY_DATABASE_URI'])
self.assertIsNotNone(current_app)

# testing JWT configurations
Expand All @@ -113,9 +111,6 @@ def test_app_development_config(self):
self.assertTrue(application.config["DEBUG"])
self.assertFalse(application.config["TESTING"])
self.assertFalse(application.config["SQLALCHEMY_TRACK_MODIFICATIONS"])
self.assertEqual(
"sqlite:///local_data.db", application.config["SQLALCHEMY_DATABASE_URI"]
)
self.assertIsNotNone(current_app)

# testing JWT configurations
Expand Down Expand Up @@ -143,7 +138,6 @@ def test_app_production_config(self):
self.assertFalse(application.config["DEBUG"])
self.assertFalse(application.config["TESTING"])
self.assertFalse(application.config["SQLALCHEMY_TRACK_MODIFICATIONS"])
# self.assertEqual('mysql_something', application.config['SQLALCHEMY_DATABASE_URI'])
self.assertIsNotNone(current_app)

# testing JWT configurations
Expand Down
16 changes: 8 additions & 8 deletions tests/users/test_dao_update_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ class TestUpdateUserDao(BaseTestCase):
def test_dao_update_user(self):

self.assertIsNone(self.admin_user.occupation)
self.assertIsNone(self.admin_user.organization)
self.assertIsNone(self.admin_user.current_organization)

data = dict(occupation="good_developer", organization="good_org")
data = dict(occupation="good_developer", current_organization="good_org")
UserDAO.update_user_profile(self.admin_user.id, data)

self.assertEqual("good_developer", self.admin_user.occupation)
self.assertEqual("good_org", self.admin_user.organization)
self.assertEqual("good_org", self.admin_user.current_organization)

def test_update_fields_with_empty_data(self):

self.assertIsNone(self.admin_user.occupation)
self.assertIsNone(self.admin_user.organization)
self.assertIsNone(self.admin_user.current_organization)

data = dict(occupation="good_developer", organization="good_org")
data = dict(occupation="good_developer", current_organization="good_org")
UserDAO.update_user_profile(self.admin_user.id, data)

self.assertEqual("good_developer", self.admin_user.occupation)
self.assertEqual("good_org", self.admin_user.organization)
self.assertEqual("good_org", self.admin_user.current_organization)

data = dict(occupation="", organization="")
data = dict(occupation="", current_organization="")
UserDAO.update_user_profile(self.admin_user.id, data)

self.assertIsNone(self.admin_user.occupation)
self.assertIsNone(self.admin_user.organization)
self.assertIsNone(self.admin_user.current_organization)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit f66749b

Please sign in to comment.