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 name to timetable #994

Draft
wants to merge 6 commits into
base: chore/back-container-permission
Choose a base branch
from
Draft
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
27 changes: 27 additions & 0 deletions apps/timetable/migrations/0007_add_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 2.2.28 on 2024-02-08 07:34

from django.db import migrations, models

from apps.timetable.models import Timetable


class Migration(migrations.Migration):

dependencies = [
('timetable', '0006_auto_20220123_1745'),
]

def generate_default_name(apps, schema_editor):
for timetable in Timetable.objects.all():
timetable.name = f'table {timetable.arrange_order + 1}'
timetable.save()

operations = [
migrations.AddField(
model_name='timetable',
name='name',
field=models.CharField(max_length=20),
preserve_default=False,
),
migrations.RunPython(generate_default_name)
]
18 changes: 18 additions & 0 deletions apps/timetable/migrations/0008_add_is_pinned.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.28 on 2024-02-08 08:43

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('timetable', '0007_add_name'),
]

operations = [
migrations.AddField(
model_name='timetable',
name='is_pinned',
field=models.BooleanField(default=False),
),
]
4 changes: 4 additions & 0 deletions apps/timetable/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ class Timetable(models.Model):
year = models.IntegerField(null=True, db_index=True) # 몇넌도의 타임테이블인지
semester = models.SmallIntegerField(null=True, db_index=True) # 어떤학기의 타임테이블인지
arrange_order = models.SmallIntegerField(db_index=True)
name = models.CharField(max_length=20)
is_pinned = models.BooleanField(default=False)

def to_json(self, nested=False):
result = {
"id": self.id,
"lectures": [lecture.to_json(nested=False)
for lecture in self.lectures.filter(deleted=False)],
"arrange_order": self.arrange_order,
"name": self.name,
"is_pinned": self.is_pinned
}
return result

Expand Down
44 changes: 44 additions & 0 deletions apps/timetable/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

from django.db.models import F
from django.db import transaction
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound, JsonResponse
from django.utils.decorators import method_decorator
from django.views import View
Expand Down Expand Up @@ -199,6 +200,49 @@ def post(self, request, user_id, timetable_id):
return JsonResponse(timetable.to_json())


@method_decorator(login_required_ajax, name="dispatch")
class UserInstanceTimetableInstanceChangeNameView(View):
def patch(self, request, user_id, timetable_id):
BODY_STRUCTURE = [
("name", ParseType.STR, True, []),
]

userprofile = request.user.userprofile
if userprofile.id != int(user_id):
return HttpResponse(status=401)

try:
timetable = userprofile.timetables.get(id=timetable_id)
except Timetable.DoesNotExist:
return HttpResponseNotFound()

name, = parse_body(request.body, BODY_STRUCTURE)

timetable.name = name
timetable.save()
return JsonResponse(timetable.to_json())

@method_decorator(login_required_ajax, name="dispatch")
class UserInstanceTimetableInstancePinView(View):
def post(self, request, user_id, timetable_id):
userprofile = request.user.userprofile
if userprofile.id != int(user_id):
return HttpResponse(status=401)

try:
timetable = userprofile.timetables.get(id=timetable_id)
except Timetable.DoesNotExist:
return HttpResponseNotFound()

with transaction.atomic():
Timetable.objects.filter(user_id=user_id, semester=timetable.semester).update(is_pinned=False)
timetable.is_pinned = True
timetable.save()

return JsonResponse(timetable.to_json())



@method_decorator(login_required_ajax, name="dispatch")
class UserInstanceWishlistView(View):
def get(self, request, user_id):
Expand Down
5 changes: 5 additions & 0 deletions otlplus/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
timetable_views.UserInstanceTimetableInstanceRemoveLectureView.as_view()),
url(r"^api/users/(?P<user_id>\d+)/timetables/(?P<timetable_id>\d+)/reorder",
timetable_views.UserInstanceTimetableInstanceReorderView.as_view()),
url(r"^api/users/(?P<user_id>\d+)/timetables/(?P<timetable_id>\d+)/name",
timetable_views.UserInstanceTimetableInstanceChangeNameView.as_view()),
url(r"^api/users/(?P<user_id>\d+)/timetables/(?P<timetable_id>\d+)/pin",
timetable_views.UserInstanceTimetableInstancePinView.as_view()),


url(r"^api/users/(?P<user_id>\d+)/planners$",
planner_views.UserInstancePlannerListView.as_view()),
Expand Down