Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #65 from unb-mds/feature/contest-forms
Browse files Browse the repository at this point in the history
[feature request] Initial problems form development
  • Loading branch information
bitterteriyaki authored Nov 8, 2023
2 parents dca5400 + c332100 commit 6f7396f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions apps/problems/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.forms import CharField, Form


class ProblemForm(Form):
code = CharField(label="Source Code")
29 changes: 27 additions & 2 deletions apps/problems/views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any, Dict

from django.http import HttpRequest, HttpResponse
from django.views import generic
from django.views.generic.edit import FormMixin

from apps.problems.forms import ProblemForm
from apps.problems.models import Problem

if TYPE_CHECKING:
DetailViewBase = generic.DetailView[Problem]
FormMixinBase = FormMixin[ProblemForm]
else:
DetailViewBase = generic.DetailView
FormMixinBase = FormMixin


class DetailView(DetailViewBase):
class DetailView(FormMixinBase, DetailViewBase):
model = Problem
template_name = "problems/detail.html"
form_class = ProblemForm

def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
context = super().get_context_data(**kwargs)
context["form"] = ProblemForm()

return context

def post(self, request: HttpRequest) -> HttpResponse:
self.object = self.get_object()
form = self.get_form()

return (
self.form_valid(form)
if form.is_valid()
else self.form_invalid(form)
)

def form_valid(self, form: ProblemForm) -> HttpResponse:
return super().form_valid(form)

0 comments on commit 6f7396f

Please sign in to comment.