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

Implement course problem page for prof #303

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions backend/course/views/professor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class CourseAPI(APIView):
type=openapi.TYPE_STRING,
default=0,
),
openapi.Parameter(
name="bookmark",
in_=openapi.IN_QUERY,
description="bookmark state of course",
type=openapi.TYPE_BOOLEAN,
),
],
operation_description="Get course list or specific course generated by requesting admin",
responses={200: CourseRegistrationSerializer},
Expand All @@ -49,10 +55,10 @@ def get(self, request):
return self.error("Course does not exist")

if request.GET.get("bookmark") == "true":
courses = Registration.objects.filter(course__created_by=request.user, bookmark=True)
courses = Registration.objects.filter(course__created_by=request.user, bookmark=True, user=request.user)
return self.success(self.paginate_data(request, courses, CourseRegistrationSerializer))

courses = Registration.objects.filter(course__created_by=request.user)
courses = Registration.objects.filter(course__created_by=request.user, user=request.user)
return self.success(self.paginate_data(request, courses, CourseRegistrationSerializer))

@swagger_auto_schema(
Expand Down
20 changes: 20 additions & 0 deletions backend/problem/migrations/0019_problem_course.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.2.11 on 2022-02-10 09:21

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('course', '0004_registration_bookmark'),
('problem', '0018_auto_20211226_1458'),
]

operations = [
migrations.AddField(
model_name='problem',
name='course',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='problems', to='course.course'),
),
]
3 changes: 3 additions & 0 deletions backend/problem/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from utils.constants import Choices

from assignment.models import Assignment
from course.models import Course


class ProblemTag(models.Model):
Expand Down Expand Up @@ -44,6 +45,8 @@ def _default_io_mode():
class Problem(models.Model):
# display ID
_id = models.TextField(db_index=True)
# course ID
course = models.ForeignKey(Course, null=True, on_delete=models.CASCADE, related_name="problems")
# assignment ID
assignment = models.ForeignKey(Assignment, null=True, on_delete=models.CASCADE, related_name="problems")
contest = models.ForeignKey(Contest, null=True, on_delete=models.CASCADE)
Expand Down
34 changes: 33 additions & 1 deletion backend/problem/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,21 @@ class EditContestProblemSerializer(CreateOrEditProblemSerializer):

class CreateAssignmentProblemSerializer(CreateOrEditProblemSerializer):
assignment_id = serializers.IntegerField()
course_id = serializers.IntegerField()


class EditAssignmentProblemSerializer(CreateOrEditProblemSerializer):
id = serializers.IntegerField()


class CreateCourseProblemSerializer(CreateOrEditProblemSerializer):
course_id = serializers.IntegerField()


class EditCourseProblemSerializer(CreateOrEditProblemSerializer):
id = serializers.IntegerField()


class TagSerializer(serializers.ModelSerializer):
class Meta:
model = ProblemTag
Expand All @@ -117,11 +126,16 @@ class BaseProblemSerializer(serializers.ModelSerializer):
tags = serializers.SlugRelatedField(many=True, slug_field="name", read_only=True)
created_by = UsernameSerializer()
contest_name = serializers.SerializerMethodField()
assignment_name = serializers.SerializerMethodField()

def get_contest_name(self, obj):
if obj.contest:
return obj.contest.title

def get_assignment_name(self, obj):
if obj.assignment:
return obj.assignment.title

def get_public_template(self, obj):
ret = {}
for lang, code in obj.template.items():
Expand All @@ -135,7 +149,7 @@ class Meta:
fields = "__all__"


class ProblemProfessorSerializer(BaseProblemSerializer):
class ProblemAssignmentProfessorSerializer(BaseProblemSerializer):
total_submission_assignment = serializers.SerializerMethodField()

def get_total_submission_assignment(self, obj):
Expand All @@ -146,6 +160,17 @@ class Meta:
fields = "__all__"


class ProblemCourseProfessorSerializer(BaseProblemSerializer):
total_submission_course = serializers.SerializerMethodField()

def get_total_submission_course(self, obj):
return Submission.objects.filter(problem=obj, course=obj.course).values("user_id").distinct().count()

class Meta:
model = Problem
fields = "__all__"


class ProblemSerializer(BaseProblemSerializer):

class Meta:
Expand Down Expand Up @@ -176,11 +201,18 @@ class AddContestProblemSerializer(serializers.Serializer):


class AddAssignmentProblemSerializer(serializers.Serializer):
course_id = serializers.IntegerField()
assignment_id = serializers.IntegerField()
problem_id = serializers.IntegerField()
display_id = serializers.CharField()


class AddCourseProblemSerializer(serializers.Serializer):
course_id = serializers.IntegerField()
problem_id = serializers.IntegerField()
display_id = serializers.CharField()


class ExportProblemRequestSerialzier(serializers.Serializer):
problem_id = serializers.ListField(child=serializers.IntegerField(), allow_empty=False)

Expand Down
7 changes: 6 additions & 1 deletion backend/problem/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ def setUp(self):
admin = self.create_admin()
course = Course.objects.create(created_by=admin, **DEFAULT_COURSE_DATA)
Registration.objects.create(user_id=student.id, course_id=course.id)
self.course_id = course.id
self.assignment_id = Assignment.objects.create(created_by=admin, course=course, **DEFAULT_ASSIGNMENT_DATA).id
self.url = self.reverse("assignment_problem_professor_api")

Expand All @@ -286,6 +287,7 @@ def test_get_assignment_problem(self):
def test_create_assignment_problem(self):
data = copy.deepcopy(DEFAULT_PROBLEM_DATA)
data["assignment_id"] = self.assignment_id
data["course_id"] = self.course_id
res = self.client.post(self.url, data=data)
self.assertSuccess(res)
return res.data["data"]
Expand All @@ -295,6 +297,7 @@ def test_edit_assignment_problem(self):
data = copy.deepcopy(DEFAULT_PROBLEM_DATA)
data["id"] = problem_id
data["title"] = "edit test"
data["course"] = self.course_id
data["assignment"] = self.assignment_id
res = self.client.put(self.url, data=data)
self.assertSuccess(res)
Expand Down Expand Up @@ -357,12 +360,14 @@ def setUp(self):
admin = self.create_admin()
course = Course.objects.create(created_by=admin, **DEFAULT_COURSE_DATA)
self.assignment_id = Assignment.objects.create(created_by=admin, course=course, **DEFAULT_ASSIGNMENT_DATA).id
self.course_id = course.id
self.problem = self.add_problem(DEFAULT_PROBLEM_DATA, admin)
self.url = self.reverse("add_assignment_problem_from_public_api")
self.data = {
"display_id": "1000",
"assignment_id": self.assignment_id,
"problem_id": self.problem.id
"problem_id": self.problem.id,
"course_id": self.course_id
}

def test_add_assignment_problem(self):
Expand Down
4 changes: 3 additions & 1 deletion backend/problem/urls/professor.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.urls import path

from ..views.professor import AssignmentProblemAPI, AddAssignmentProblemAPI
from ..views.professor import AssignmentProblemAPI, AddAssignmentProblemAPI, CourseProblemAPI, AddCourseProblemAPI

urlpatterns = [
path("course/assignment/problem/", AssignmentProblemAPI.as_view(), name="assignment_problem_professor_api"),
path("course/assignment/add_problem_from_public/", AddAssignmentProblemAPI.as_view(), name="add_assignment_problem_from_public_api"),
path("course/problem/", CourseProblemAPI.as_view(), name="course_problem_professor_api"),
path("course/add_problem_from_public/", AddCourseProblemAPI.as_view(), name="add_course_problem_from_public_api"),
]
2 changes: 2 additions & 0 deletions backend/problem/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ def get(self, request):
ensure_created_by(problem.contest, request.user)
elif problem.assignment:
ensure_created_by(problem.assignment, request.user)
elif problem.course:
ensure_created_by(problem.course, request.user)
else:
ensure_created_by(problem, request.user)

Expand Down
4 changes: 2 additions & 2 deletions backend/problem/views/oj.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def get(self, request):
if problem_id:
try:
problem = Problem.objects.select_related("created_by") \
.get(_id=problem_id, contest_id__isnull=True, assignment_id__isnull=True, visible=True)
.get(_id=problem_id, contest_id__isnull=True, assignment_id__isnull=True, course_id__isnull=True, visible=True)
problem_data = ProblemSerializer(problem).data
self._add_problem_status(request, problem_data)
return self.success(problem_data)
Expand All @@ -89,7 +89,7 @@ def get(self, request):
if not limit:
return self.error("Limit is needed")

problems = Problem.objects.select_related("created_by").filter(contest_id__isnull=True, assignment_id__isnull=True, visible=True)
problems = Problem.objects.select_related("created_by").filter(contest_id__isnull=True, assignment_id__isnull=True, course_id__isnull=True, visible=True)
# filter by label
tag_text = request.GET.get("tag")
if tag_text:
Expand Down
Loading