This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from unb-mds/feature/contest-forms
[feature request] Initial problems form development
- Loading branch information
Showing
2 changed files
with
32 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |