Skip to content

Commit

Permalink
add test form
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Kaufeld committed Jun 18, 2021
1 parent ecc6882 commit a8fad24
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
26 changes: 19 additions & 7 deletions website/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ class LoginForm(forms.Form):
password = forms.CharField(widget=forms.PasswordInput)


class TestForm(forms.Form):
string = forms.CharField()
integer = forms.IntegerField()
fileinput = forms.FileField()
textfield = forms.CharField(widget=forms.Textarea)
datefield = forms.DateField()
passwordfield = forms.CharField(widget=forms.PasswordInput)


class PostAddForm(forms.ModelForm):
class Meta:
model = Post
Expand All @@ -31,21 +40,24 @@ class AddUserForm(forms.Form):
is_superuser = forms.BooleanField(label="Is this user a superuser?", required=False)

# the clean functions will be run automatically on save
def clean_username(self):
def clean_username(self) -> str:
"""Validate username."""
username = self.cleaned_data["username"].lower()
r = BlossomUser.objects.filter(username=username)
if r.count():
resp = BlossomUser.objects.filter(username=username)
if resp.count():
raise ValidationError("Username already exists")
return username

def clean_email(self):
def clean_email(self) -> str:
"""Validate email."""
email = self.cleaned_data["email"].lower()
r = BlossomUser.objects.filter(email=email)
if r.count():
resp = BlossomUser.objects.filter(email=email)
if resp.count():
raise ValidationError("Email already exists")
return email

def save(self, commit=True):
def save(self, commit: bool = True) -> BlossomUser:
"""Save the instance."""
user = BlossomUser.objects.create_user(
self.cleaned_data["username"],
self.cleaned_data["email"],
Expand Down
1 change: 1 addition & 0 deletions website/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
path(
"admin/", grafeas_staff_required(views.AdminView.as_view()), name="admin_view"
),
path("testform", views.testform, name="homepage"),
]

urlpatterns += auth_urls
10 changes: 9 additions & 1 deletion website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
from django.views.generic import DetailView, TemplateView, UpdateView

from authentication.mixins import GrafeasStaffRequired
from website.forms import AddUserForm, PostAddForm
from website.forms import AddUserForm, PostAddForm, TestForm
from website.helpers import get_additional_context
from website.models import Post


def testform(request: HttpRequest) -> HttpResponse:
"""Simple view for testing form behavior."""
form = TestForm()
return render(
request, "website/generic_form.html", get_additional_context({"form": form})
)


def index(request: HttpRequest) -> HttpResponse:
"""Build and render the homepage for the website."""
context = {
Expand Down

0 comments on commit a8fad24

Please sign in to comment.