-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: Add post organization/<int:organization_id>/programs/<int:program_id>/send_request endpoint #185
Open
decon-harsh
wants to merge
4
commits into
anitab-org:develop
Choose a base branch
from
decon-harsh:Issue161
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feat: Add post organization/<int:organization_id>/programs/<int:program_id>/send_request endpoint #185
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,37 @@ | ||
import ast | ||
from http import HTTPStatus | ||
from flask import json | ||
from app.database.models.bit_schema.mentorship_relation_extension import MentorshipRelationExtensionModel | ||
from app import messages | ||
from app.api.request_api_utils import AUTH_COOKIE | ||
from app.utils.bitschema_utils import Timezone | ||
|
||
|
||
class MentorshipRelationExtensionDAO: | ||
|
||
"""Data Access Object for Users_Extension functionalities""" | ||
|
||
@staticmethod | ||
def createMentorshipRelationExtension(program_id, mentorship_relation_id , mentee_request_date): | ||
"""Creates the extending mentorship relation between organization's program and the user which is logged in. | ||
|
||
Arguments: | ||
organization_id: The ID organization | ||
program_id: The ID of program | ||
|
||
Returns: | ||
A dictionary containing "message" which indicates whether or not | ||
the relation was created successfully and "code" for the HTTP response code. | ||
""" | ||
|
||
try: | ||
mentorship_relation_extension_object = MentorshipRelationExtensionModel(program_id,mentorship_relation_id) | ||
mentorship_relation_extension_object.mentee_request_date = mentee_request_date | ||
mentorship_relation_extension_object.save_to_db() | ||
return messages.MENTORSHIP_RELATION_WAS_SENT_SUCCESSFULLY, HTTPStatus.CREATED | ||
except: | ||
return messages.INTERNAL_SERVER_ERROR, HTTPStatus.BAD_REQUEST | ||
|
||
|
||
|
||
|
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,26 @@ | ||
from flask_restx import fields, Model | ||
|
||
from app.utils.enum_utils import MentorshipRelationState | ||
|
||
|
||
def add_models_to_namespace(api_namespace): | ||
api_namespace.models[send_mentorship_extension_request_body.name] = send_mentorship_extension_request_body | ||
|
||
|
||
send_mentorship_extension_request_body = Model( | ||
"Send mentorship relation request to organziation model", | ||
{ | ||
"mentee_id": fields.Integer( | ||
required=True, description="Mentorship relation mentee ID" | ||
), | ||
"mentee_request_date": fields.Float( | ||
required=True, description="Mentorship relation mentee_request_date in UNIX timestamp format" | ||
), | ||
"end_date": fields.Float( | ||
required=True, | ||
description="Mentorship relation end date in UNIX timestamp format", | ||
), | ||
"notes": fields.String(required=True, description="Mentorship relation notes"), | ||
|
||
}, | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import ast | ||
import json | ||
from http import HTTPStatus, cookies | ||
from datetime import datetime, timedelta | ||
from flask import request | ||
from flask_restx import Resource, marshal, Namespace | ||
from app import messages | ||
from app.api.request_api_utils import ( | ||
post_request, | ||
post_request_with_token, | ||
get_request, | ||
put_request, | ||
http_response_checker, | ||
AUTH_COOKIE, | ||
validate_token) | ||
# Common Resources | ||
from app.api.resources.common import auth_header_parser | ||
# Validations | ||
from app.api.validations.user import * | ||
from app.api.validations.task_comment import ( | ||
validate_task_comment_request_data, | ||
COMMENT_MAX_LENGTH, | ||
) | ||
from app.api.validations.mentorship_relation import org_mentee_req_body_is_valid_data | ||
from app.utils.validation_utils import get_length_validation_error_message,expected_fields_validator | ||
from app.utils.ms_constants import DEFAULT_PAGE, DEFAULT_USERS_PER_PAGE | ||
# Namespace Models | ||
from app.api.models.mentorship_relation import * | ||
# Databases Models | ||
from app.database.models.bit_schema.user_extension import UserExtensionModel | ||
from app.database.models.ms_schema.mentorship_relation import MentorshipRelationModel | ||
from app.database.models.bit_schema.organization import OrganizationModel | ||
from app.database.models.bit_schema.program import ProgramModel | ||
# DAOs | ||
from app.api.dao.user_extension import UserExtensionDAO | ||
from app.api.dao.personal_background import PersonalBackgroundDAO | ||
from app.api.dao.mentorship_relation_extension import MentorshipRelationExtensionDAO | ||
from app.api.dao.organization import OrganizationDAO | ||
from app.api.dao.program import ProgramDAO | ||
|
||
mentorship_relation_ns = Namespace( | ||
"Mentorship Relation", | ||
description="Operations related to " "mentorship relations " "between users", | ||
) | ||
add_models_to_namespace(mentorship_relation_ns) | ||
|
||
mentorshipRelationExtensionDAO = MentorshipRelationExtensionDAO() | ||
userExtensionDAO = UserExtensionDAO() | ||
OrganizationDAO = OrganizationDAO() | ||
ProgramDAO = ProgramDAO() | ||
|
||
@mentorship_relation_ns.route("organizations/<int:organization_id>/programs/<int:program_id>/send_request") | ||
class ApplyForProgram(Resource): | ||
@classmethod | ||
@mentorship_relation_ns.doc("send_request") | ||
@mentorship_relation_ns.expect(auth_header_parser, send_mentorship_extension_request_body) | ||
@mentorship_relation_ns.response( | ||
HTTPStatus.CREATED, f"{messages.MENTORSHIP_RELATION_WAS_SENT_SUCCESSFULLY}" | ||
) | ||
@mentorship_relation_ns.response( | ||
HTTPStatus.BAD_REQUEST, | ||
f"{messages.NO_DATA_WAS_SENT}\n" | ||
f"{messages.MATCH_EITHER_MENTOR_OR_MENTEE}\n" | ||
f"{messages.MENTOR_ID_SAME_AS_MENTEE_ID}\n" | ||
f"{messages.END_TIME_BEFORE_PRESENT}\n" | ||
f"{messages.MENTOR_TIME_GREATER_THAN_MAX_TIME}\n" | ||
f"{messages.MENTOR_TIME_LESS_THAN_MIN_TIME}\n" | ||
f"{messages.MENTOR_NOT_AVAILABLE_TO_MENTOR}\n" | ||
f"{messages.MENTEE_NOT_AVAIL_TO_BE_MENTORED}\n" | ||
f"{messages.MENTOR_ALREADY_IN_A_RELATION}\n" | ||
f"{messages.MENTEE_ALREADY_IN_A_RELATION}\n" | ||
f"{messages.MENTOR_ID_FIELD_IS_MISSING}\n" | ||
f"{messages.MENTEE_ID_FIELD_IS_MISSING}\n" | ||
f"{messages.NOTES_FIELD_IS_MISSING}\n" | ||
f"{messages.ORGANIZATION_DOES_NOT_EXIST}\n" | ||
f"{messages.PROGRAM_DOES_NOT_EXIST}" | ||
) | ||
@mentorship_relation_ns.response( | ||
HTTPStatus.UNAUTHORIZED, | ||
f"{messages.TOKEN_HAS_EXPIRED}\n" | ||
f"{messages.TOKEN_IS_INVALID}\n" | ||
f"{messages.AUTHORISATION_TOKEN_IS_MISSING}" | ||
) | ||
@mentorship_relation_ns.response( | ||
HTTPStatus.NOT_FOUND, | ||
f"{messages.MENTOR_DOES_NOT_EXIST}\n" | ||
f"{messages.MENTEE_DOES_NOT_EXIST}" | ||
) | ||
def post(cls,organization_id,program_id): | ||
""" | ||
Creates a new mentorship relation request. | ||
|
||
Also, sends an email notification to the recipient about new relation request. | ||
|
||
Input: | ||
1. Header: valid access token | ||
2. Body: A dict containing | ||
- mentor_request_date,end_date: UNIX timestamp | ||
- notes: description of relation request | ||
|
||
Returns: | ||
Success or failure message. A mentorship request is send to the other | ||
person whose ID is mentioned. The relation appears at /pending endpoint. | ||
""" | ||
|
||
token = request.headers.environ["HTTP_AUTHORIZATION"] | ||
is_wrong_token = validate_token(token) | ||
|
||
if not is_wrong_token: | ||
try: | ||
user_json = (AUTH_COOKIE["user"].value) | ||
user = ast.literal_eval(user_json) | ||
data = request.json | ||
|
||
if not data: | ||
return messages.NO_DATA_WAS_SENT, HTTPStatus.BAD_REQUEST | ||
|
||
is_field_valid = expected_fields_validator(data, send_mentorship_extension_request_body) | ||
if not is_field_valid.get("is_field_valid"): | ||
return is_field_valid.get("message"), HTTPStatus.BAD_REQUEST | ||
|
||
is_valid = org_mentee_req_body_is_valid_data(data) | ||
if is_valid != {}: | ||
return is_valid, HTTPStatus.BAD_REQUEST | ||
|
||
# Checking whether organization exists | ||
organization = OrganizationModel.query.filter_by(id=organization_id).first() | ||
if not organization: | ||
return messages.ORGANIZATION_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND | ||
|
||
# Checking whether program exists | ||
program = ProgramModel.find_by_id(program_id) | ||
if not program or (program.organization_id != organization_id): | ||
return messages.PROGRAM_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND | ||
|
||
mentor_id = organization.rep_id | ||
mentee_id = data['mentee_id'] | ||
|
||
mentorship_relation_data={} | ||
mentorship_relation_data['mentee_id'] = mentee_id | ||
mentorship_relation_data['mentor_id'] = int(mentor_id) | ||
mentorship_relation_data['end_date'] = data['end_date'] | ||
mentorship_relation_data['notes'] = data['notes'] | ||
|
||
response = http_response_checker(post_request_with_token("/mentorship_relation/send_request",token, mentorship_relation_data)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that you used the internal call here, that's good. |
||
if response.status_code == 201: | ||
mentorshipRelationId = MentorshipRelationModel.query.filter_by(mentor_id=mentor_id).filter_by(mentee_id=mentee_id).first().id | ||
return MentorshipRelationExtensionDAO.createMentorshipRelationExtension(program_id, mentorshipRelationId ,data['mentee_request_date']) | ||
else: | ||
return response.message, HTTPStatus.BAD_REQUEST | ||
except ValueError as e: | ||
return e, HTTPStatus.BAD_REQUEST | ||
|
||
return is_wrong_token |
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,14 @@ | ||
from app import messages | ||
|
||
def org_mentee_req_body_is_valid_data(data): | ||
|
||
# Verify if request body has required fields | ||
if "mentee_id" not in data: | ||
return messages.MENTEE_ID_FIELD_IS_MISSING | ||
if "end_date" not in data: | ||
return messages.END_DATE_FIELD_IS_MISSING | ||
if "notes" not in data: | ||
return messages.NOTES_FIELD_IS_MISSING | ||
if "mentee_request_date" not in data: | ||
return messages.MENTEE_REQUEST_DATE_FIELD_IS_MISSING | ||
return {} |
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
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we should use the same model as Mentorship relation request. We could create our own model for BIT by using the request body with action_user_id with value being organization representative_id.