Skip to content

Commit

Permalink
Added endpoint to add a user using a join code (#218)
Browse files Browse the repository at this point in the history
* Fix #71

* added course join endpoint

* internal server error is 500

* fixed code docs
  • Loading branch information
AronBuzogany authored Apr 18, 2024
1 parent d9cd5f8 commit 12120eb
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions backend/project/endpoints/courses/courses_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from project.endpoints.courses.course_details import CourseByCourseId
from project.endpoints.courses.course_admin_relation import CourseForAdmins
from project.endpoints.courses.course_student_relation import CourseToAddStudents
from project.endpoints.courses.join import CourseJoin

courses_bp = Blueprint("courses", __name__)
courses_api = Api(courses_bp)
Expand All @@ -30,3 +31,5 @@

courses_bp.add_url_rule("/courses/<int:course_id>/students",
view_func=CourseToAddStudents.as_view('course_students'))

courses_bp.add_url_rule("/courses/join", view_func=CourseJoin.as_view('course_join'))
78 changes: 78 additions & 0 deletions backend/project/endpoints/courses/join.py
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

0 comments on commit 12120eb

Please sign in to comment.