-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added endpoint to add a user using a join code (#218)
* Fix #71 * added course join endpoint * internal server error is 500 * fixed code docs
- Loading branch information
1 parent
d9cd5f8
commit 12120eb
Showing
2 changed files
with
81 additions
and
0 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
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,78 @@ | ||
""" | ||
This file contains the endpoint to join a course using a join code | ||
""" | ||
|
||
|
||
from os import getenv | ||
from datetime import datetime | ||
from zoneinfo import ZoneInfo | ||
|
||
from flask import request | ||
from flask_restful import Resource | ||
|
||
from sqlalchemy.exc import SQLAlchemyError | ||
|
||
from project.models.course_share_code import CourseShareCode | ||
from project.models.course_relation import CourseStudent, CourseAdmin | ||
|
||
|
||
TIMEZONE = getenv("TIMEZONE", "GMT") | ||
API_URL = getenv("API_HOST") | ||
|
||
class CourseJoin(Resource): | ||
""" | ||
Class that will respond to the /courses/join link | ||
students or admins with a join code can join a course | ||
""" | ||
|
||
def post(self, uid=None): # pylint: disable=too-many-return-statements | ||
""" | ||
Post function for /courses/join | ||
students or admins with a join code can join a course | ||
""" | ||
|
||
response = { | ||
"url": f"{API_URL}/courses/join" | ||
} | ||
|
||
data = request.get_json() | ||
if not "join_code" in data: | ||
return {"message": "join_code is required"}, 400 | ||
|
||
join_code = data["join_code"] | ||
share_code = CourseShareCode.query.filter_by(join_code=join_code).first() | ||
|
||
if not share_code: | ||
response["message"] = "Invalid join code" | ||
return response, 400 | ||
|
||
if share_code.expiry_time and share_code.expiry_time < datetime.now(ZoneInfo(TIMEZONE)): | ||
response["message"] = "Join code has expired" | ||
return response, 400 | ||
|
||
|
||
course_id = share_code.course_id | ||
is_for_admins = share_code.for_admins | ||
|
||
course_relation = CourseStudent | ||
if is_for_admins: | ||
course_relation = CourseAdmin | ||
|
||
try: | ||
relation = course_relation.query.filter_by(course_id=course_id, uid=uid).first() | ||
if relation: | ||
response["message"] = "User already in course" | ||
return response, 400 | ||
except SQLAlchemyError: | ||
response["message"] = "Internal server error" | ||
return response, 500 | ||
|
||
course_relation = course_relation(course_id=course_id, uid=uid) | ||
|
||
try: | ||
course_relation.insert() | ||
response["message"] = "User added to course" | ||
return response, 201 | ||
except SQLAlchemyError: | ||
response["message"] = "Internal server error" | ||
return response, 500 |