ku-polls/polls/forms.py
sosokker 7db720598a Use Lazy Import (get_model) To solve migration problem
It look like migrate occur at import time and that make error, i'm not sure what happen under the hood but I think this way fix it.
2023-09-18 02:12:33 +07:00

90 lines
3.8 KiB
Python

import logging
from typing import Any
from django import forms
from django.apps import apps
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from .models import Question
class SignUpForm(UserCreationForm):
tailwind_class = """w-full border-2 border-gray-300 bg-gray-100 rounded-lg
focus:ring focus:border-blue-300 focus:shadow-none"""
logger = logging.getLogger('signup_form')
username = forms.CharField(widget=forms.TextInput(attrs={'class': tailwind_class}), error_messages={
'unique': 'This username is already in use.',
'invalid': 'Invalid username format.',
'max_length': 'Username should not exceed 150 characters.',
}
)
password1 = forms.CharField(widget=forms.PasswordInput(attrs={'class': tailwind_class}),
error_messages={'min_length': 'Password must contain at least 8 characters.', }
)
password2 = forms.CharField(widget=forms.PasswordInput(attrs={'class': tailwind_class}),)
# commit -> default =True -> If commit is True -> want to save the user object to the database.
def save(self, commit: bool = True) -> Any:
user = super().save(commit=False)
if commit:
user.save()
self.logger.info(f"User registered with username: {user.username}")
return user
class Meta:
model = User
fields = ('username', 'password1', 'password2')
error_messages = {
'password_mismatch': "The two password fields didn't match.",
}
class PollSearchForm(forms.Form):
q = forms.CharField()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class PollCreateForm(forms.ModelForm):
box_style = """w-full py-2 px-2 border-2 border-gray-300 bg-gray-100 rounded-lg
focus:ring focus:border-blue-300 focus:shadow-none"""
large_box_style = """w-full border-2 border-gray-300 bg-gray-100 rounded-lg
focus:ring focus:border-blue-300 focus:shadow-none"""
question_text = forms.CharField(min_length=10, max_length=100, required=True,
widget=forms.TextInput(attrs={'class': box_style,
'placeholder': "What is your question?"}))
pub_date = forms.DateTimeField(widget=forms.DateInput(attrs={'type': 'date'}))
end_date = forms.DateTimeField(widget=forms.DateInput(attrs={'type': 'date'}))
short_description = forms.CharField(max_length=200,
widget=forms.TextInput(
attrs={'class': box_style,
'placeholder': "Short description (Maximum 200 characters)"}))
long_description = forms.CharField(max_length=2000,
widget=forms.Textarea(
attrs={'class': large_box_style,
'placeholder': "Long description (Maximum 2000 characters)"}))
user_choice = forms.CharField(
widget=forms.TextInput(attrs={'placeholder': 'Enter a choice'}),
required=True
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Tag = apps.get_model('polls', 'Tag')
tags = forms.MultipleChoiceField(
choices=[(tag.id, tag.tag_text) for tag in Tag.objects.all()],
widget=forms.CheckboxSelectMultiple,
)
class Meta:
model = Question
fields = ['question_text', 'pub_date', 'end_date', 'short_description', 'long_description', 'tags']