NDOJ/judge/views/bookmark.py

76 lines
2.1 KiB
Python
Raw Normal View History

2022-11-17 19:17:45 +00:00
from django.contrib.auth.decorators import login_required
from django.db import IntegrityError
from django.db.models import F
from django.http import (
Http404,
HttpResponse,
HttpResponseBadRequest,
HttpResponseForbidden,
)
from django.utils.translation import gettext as _
from django.views.generic.base import TemplateResponseMixin
from django.views.generic.detail import SingleObjectMixin
from django.views.generic import View, ListView
2024-04-13 22:02:54 +00:00
from judge.models.bookmark import BookMark, MakeBookMark, dirty_bookmark
2022-11-17 19:17:45 +00:00
__all__ = [
"dobookmark_page",
"undobookmark_page",
"BookMarkDetailView",
]
@login_required
def bookmark_page(request, delta):
if request.method != "POST":
return HttpResponseForbidden()
if "id" not in request.POST:
return HttpResponseBadRequest()
try:
bookmark_id = int(request.POST["id"])
2024-04-13 22:02:54 +00:00
bookmark = BookMark.objects.get(id=bookmark_id)
2022-11-17 19:17:45 +00:00
except ValueError:
return HttpResponseBadRequest()
2024-04-13 22:02:54 +00:00
except BookMark.DoesNotExist:
raise Http404()
2022-11-17 19:17:45 +00:00
if delta == 0:
2022-11-17 22:11:47 +00:00
bookmarklist = MakeBookMark.objects.filter(
2024-04-13 22:02:54 +00:00
bookmark=bookmark, user=request.profile
2022-11-17 22:11:47 +00:00
)
2022-11-17 19:17:45 +00:00
if not bookmarklist.exists():
newbookmark = MakeBookMark(
2024-04-13 22:02:54 +00:00
bookmark=bookmark,
2022-11-17 19:17:45 +00:00
user=request.profile,
)
newbookmark.save()
else:
2022-11-17 22:11:47 +00:00
bookmarklist = MakeBookMark.objects.filter(
2024-04-13 22:02:54 +00:00
bookmark=bookmark, user=request.profile
2022-11-17 22:11:47 +00:00
)
2022-11-17 19:17:45 +00:00
if bookmarklist.exists():
bookmarklist.delete()
2024-04-13 22:02:54 +00:00
dirty_bookmark(bookmark, request.profile)
2022-11-17 19:17:45 +00:00
return HttpResponse("success", content_type="text/plain")
def dobookmark_page(request):
return bookmark_page(request, 0)
def undobookmark_page(request):
return bookmark_page(request, 1)
class BookMarkDetailView(TemplateResponseMixin, SingleObjectMixin, View):
def get_context_data(self, **kwargs):
context = super(BookMarkDetailView, self).get_context_data(**kwargs)
context["bookmark"] = self.object.get_or_create_bookmark()
2022-11-17 19:17:45 +00:00
return context