Skip to content

Commit

Permalink
[Student] Add bookmark course API
Browse files Browse the repository at this point in the history
- add bookmark field to Registration model
- edit CourseAPI for bookmark course list
  • Loading branch information
mixxeo authored and cranemont committed Jan 12, 2022
1 parent 454f29c commit 98aeec3
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 6 deletions.
38 changes: 38 additions & 0 deletions backend/account/migrations/0018_auto_20211226_1458.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 3.2.5 on 2021-12-26 05:58

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('account', '0017_auto_20210703_1612'),
]

operations = [
migrations.AlterField(
model_name='user',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='user',
name='session_keys',
field=models.JSONField(default=list),
),
migrations.AlterField(
model_name='userprofile',
name='acm_problems_status',
field=models.JSONField(default=dict),
),
migrations.AlterField(
model_name='userprofile',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='userprofile',
name='oi_problems_status',
field=models.JSONField(default=dict),
),
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.4 on 2021-10-30 06:35
# Generated by Django 3.2.5 on 2021-12-26 05:58

from django.db import migrations, models

Expand Down
18 changes: 18 additions & 0 deletions backend/assignment/migrations/0002_alter_assignment_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.5 on 2021-12-26 05:58

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('assignment', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='assignment',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
]
2 changes: 1 addition & 1 deletion backend/conf/migrations/0005_alter_judgeserver_id.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 3.2.4 on 2021-10-30 06:35
# Generated by Django 3.2.5 on 2021-12-26 05:58

from django.db import migrations, models

Expand Down
48 changes: 48 additions & 0 deletions backend/contest/migrations/0011_auto_20211226_1458.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 3.2.5 on 2021-12-26 05:58

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('contest', '0010_auto_20190326_0201'),
]

operations = [
migrations.AlterField(
model_name='acmcontestrank',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='acmcontestrank',
name='submission_info',
field=models.JSONField(default=dict),
),
migrations.AlterField(
model_name='contest',
name='allowed_ip_ranges',
field=models.JSONField(default=list),
),
migrations.AlterField(
model_name='contest',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='contestannouncement',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='oicontestrank',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='oicontestrank',
name='submission_info',
field=models.JSONField(default=dict),
),
]
23 changes: 23 additions & 0 deletions backend/course/migrations/0003_auto_20211226_1458.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.5 on 2021-12-26 05:58

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('course', '0002_auto_20210808_1709'),
]

operations = [
migrations.AlterField(
model_name='course',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='registration',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
]
18 changes: 18 additions & 0 deletions backend/course/migrations/0004_registration_bookmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.5 on 2021-12-26 07:28

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('course', '0003_auto_20211226_1458'),
]

operations = [
migrations.AddField(
model_name='registration',
name='bookmark',
field=models.BooleanField(default=True),
),
]
1 change: 1 addition & 0 deletions backend/course/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Meta:
class Registration(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
bookmark = models.BooleanField(default=True)

class Meta:
db_table = "registration"
6 changes: 5 additions & 1 deletion backend/course/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ class CourseStudentSerializer(serializers.ModelSerializer):

class Meta:
model = Registration
fields = ("course",)
fields = ("course", "bookmark",)


class BookmarkCourseSerializer(serializers.Serializer):
course_id = serializers.IntegerField()
bookmark = serializers.BooleanField()

class UserListSerializer(serializers.ModelSerializer):
user = UserAdminSerializer()

Expand Down
3 changes: 2 additions & 1 deletion backend/course/urls/student.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.urls import path

from ..views.student import CourseAPI
from ..views.student import BookmarkCourseAPI, CourseAPI

urlpatterns = [
path("course/", CourseAPI.as_view(), name="course_api"),
path("bookmark_course/", BookmarkCourseAPI.as_view(), name="bookmark_course_api"),
]
38 changes: 36 additions & 2 deletions backend/course/views/student.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from utils.api import APIView
from utils.api import APIView, validate_serializer
from utils.decorators import login_required
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
from ..models import Course, Registration
from ..serializers import CourseStudentSerializer
from ..serializers import BookmarkCourseSerializer, CourseStudentSerializer


class CourseAPI(APIView):
Expand All @@ -16,6 +16,12 @@ class CourseAPI(APIView):
requied=True,
type=openapi.TYPE_INTEGER,
),
openapi.Parameter(
name="bookmark",
in_=openapi.IN_QUERY,
description="True for get bookmark list",
type=openapi.TYPE_BOOLEAN
),
openapi.Parameter(
name="limit",
in_=openapi.IN_QUERY,
Expand Down Expand Up @@ -51,4 +57,32 @@ def get(self, request):

courses = Registration.objects.filter(user_id=user_id)

if request.GET.get("bookmark") == "true":
courses = courses.filter(bookmark=True)

return self.success(self.paginate_data(request, courses, CourseStudentSerializer))


class BookmarkCourseAPI(APIView):
@swagger_auto_schema(
request_body=BookmarkCourseSerializer,
operation_description="Bookmark a course"
)
@validate_serializer(BookmarkCourseSerializer)
@login_required
def put(self, request):
data = request.data
course_id = data["course_id"]

try:
Course.objects.get(id=course_id)
registration = Registration.objects.get(user_id=request.user.id, course_id=course_id)
except Course.DoesNotExist:
return self.error("Course does not exist")
except Registration.DoesNotExist:
return self.error("Invalid access, not registered course")

registration.bookmark = data["bookmark"]

registration.save()
return self.success(BookmarkCourseSerializer(registration).data)
23 changes: 23 additions & 0 deletions backend/options/migrations/0004_auto_20211226_1458.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.5 on 2021-12-26 05:58

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('options', '0003_migrate_languages_options'),
]

operations = [
migrations.AlterField(
model_name='sysoptions',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='sysoptions',
name='value',
field=models.JSONField(),
),
]
54 changes: 54 additions & 0 deletions backend/problem/migrations/0018_auto_20211226_1458.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Generated by Django 3.2.5 on 2021-12-26 05:58

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


class Migration(migrations.Migration):

dependencies = [
('problem', '0017_auto_20210814_1415'),
]

operations = [
migrations.AlterField(
model_name='problem',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='problem',
name='io_mode',
field=models.JSONField(default=problem.models._default_io_mode),
),
migrations.AlterField(
model_name='problem',
name='languages',
field=models.JSONField(),
),
migrations.AlterField(
model_name='problem',
name='samples',
field=models.JSONField(),
),
migrations.AlterField(
model_name='problem',
name='statistic_info',
field=models.JSONField(default=dict),
),
migrations.AlterField(
model_name='problem',
name='template',
field=models.JSONField(),
),
migrations.AlterField(
model_name='problem',
name='test_case_score',
field=models.JSONField(),
),
migrations.AlterField(
model_name='problemtag',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
]
23 changes: 23 additions & 0 deletions backend/submission/migrations/0016_auto_20211226_1458.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.5 on 2021-12-26 05:58

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('submission', '0015_remove_submission_score'),
]

operations = [
migrations.AlterField(
model_name='submission',
name='info',
field=models.JSONField(default=dict),
),
migrations.AlterField(
model_name='submission',
name='statistic_info',
field=models.JSONField(default=dict),
),
]

0 comments on commit 98aeec3

Please sign in to comment.