Skip to content

Commit

Permalink
refactor: IndexView based on class
Browse files Browse the repository at this point in the history
  • Loading branch information
YDX-2147483647 committed Aug 9, 2023
1 parent 7fc37a7 commit 8b48f98
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
7 changes: 2 additions & 5 deletions contest/quiz/urls.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
from django.urls import path

from . import views
from .constants import constants

app_name = "quiz"

extra_context = {"constants": constants}

urlpatterns = [
path("", views.index, name="index"),
path("info/", views.InfoView.as_view(extra_context=extra_context), name="info"),
path("", views.IndexView.as_view(), name="index"),
path("info/", views.InfoView.as_view(), name="info"),
path("contest/", views.contest, name="contest"),
path("contest/update/", views.contest_update, name="contest_update"),
path("contest/submit/", views.contest_submit, name="contest_submit"),
Expand Down
47 changes: 30 additions & 17 deletions contest/quiz/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from .models import Choice, DraftResponse, Question

if TYPE_CHECKING:
from typing import Any, Literal

from django.contrib.auth.models import AbstractBaseUser, AnonymousUser
from django.http import HttpRequest

Expand Down Expand Up @@ -68,33 +70,44 @@ def continue_or_finalize(draft: DraftResponse) -> bool:
return False


@require_GET
def index(request: HttpRequest) -> HttpResponse:
if request.user.is_authenticated and is_student(request.user):
student = request.user.student
def manage_status(
user: User | AnonymousUser,
) -> (
Literal["not taking"]
| Literal["deadline passed"]
| Literal["taking contest"]
| Literal[""]
):
"""检查状态及自动提交"""

if user.is_authenticated and is_student(user):
student = user.student

if not hasattr(student, "draft_response"):
status = "not taking"
return "not taking"
else:
finalized = continue_or_finalize(student.draft_response)
if finalized:
status = "deadline passed"
return "deadline passed"
else:
status = "taking contest"
return "taking contest"
else:
status = ""
return ""

return render(
request,
"index.html",
{
"constants": constants,
"status": status,
},
)

class IndexView(TemplateView):
template_name = "index.html"

def get_context_data(self, **kwargs) -> dict[str, Any]:
context = super().get_context_data(**kwargs)

context["status"] = manage_status(self.request.user)
context["constants"] = constants

return context


class InfoView(LoginRequiredMixin, TemplateView):
class InfoView(LoginRequiredMixin, IndexView):
template_name = "info.html"


Expand Down

0 comments on commit 8b48f98

Please sign in to comment.