Skip to content
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

ADD get course data #302

Merged
merged 6 commits into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion api/anubis/lms/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import string
import traceback
import urllib.parse
from typing import Any, Dict, List, Set, Tuple, Union
from typing import Any, Dict, List, Optional, Set, Tuple, Union

from flask import g, request
from werkzeug.local import LocalProxy
Expand Down Expand Up @@ -347,6 +347,52 @@ def get_courses(netid: str):
return [c.data for c in classes]


@cache.memoize(timeout=60, unless=is_debug)
def get_course_data(netid: str, course_id: str) -> Optional[Dict[str, Any]]:
"""
Get course data and tas for course

:param netid:
:param course_id:
:return:
"""

# Get User
user = User.query.filter(User.netid == netid).first()

# Get course ids
course_ids = get_student_course_ids(user)

# Course doesnt exist
if not user.is_superuser and course_id not in course_ids:
return None

# Query for course with id
course: Course = Course.query.filter(
Course.id == course_id
).first()

course_data = course.data

# Query for tas in course
tas = (
User.query.join(TAForCourse)
.filter(
TAForCourse.course_id == course_id,
)
.all()
)

# Return course and ta data
return {**course_data, "tas": [
{
"name": ta.name,
"netid": ta.id,
}
for ta in tas
]}


@cache.memoize(timeout=60, unless=is_debug)
def get_student_course_ids(user: User, default: str = None) -> List[str]:
"""
Expand Down
20 changes: 19 additions & 1 deletion api/anubis/views/public/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from flask import Blueprint

from anubis.lms.assignments import get_assignments
from anubis.lms.courses import get_courses, get_courses_with_visuals, valid_join_code
from anubis.lms.courses import get_courses, get_courses_with_visuals, valid_join_code, get_course_data
from anubis.models import Course, InCourse, db
from anubis.utils.auth.http import require_user
from anubis.utils.auth.user import current_user
Expand Down Expand Up @@ -38,6 +38,24 @@ def public_courses_list():
return success_response({"courses": courses})


@courses_.route("/get/<string:course_id>")
@require_user()
@json_response
def public_courses_get(course_id):
"""
Get course information by course id with tas
This required authentication.

:return:
"""
course_data = get_course_data(current_user.netid, course_id)

if not course_data:
return error_response('Course does not exist')

return success_response({"course": course_data})


@courses_.route("/join/<string:join_code>")
@require_user()
@json_response
Expand Down
5 changes: 5 additions & 0 deletions api/tests/test_courses_public.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def test_courses_public():
s.get("/public/courses/")
courses = s.get("/public/courses/list")["courses"]
join_code = courses[0]["join_code"]
id = courses[0]["id"]

sn = Session("student", new=True, add_to_os=False)
courses = sn.get("/public/courses/list")["courses"]
Expand All @@ -14,3 +15,7 @@ def test_courses_public():
sn.get(f"/public/courses/join/{join_code}")
courses = sn.get("/public/courses/list")["courses"]
assert len(courses) == 1
sn.get("/public/courses/get/12345", should_fail=True)
course = sn.get(f"/public/courses/get/{id}")["course"]
assert course