You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cqtu/contest/forms.py

102 lines
3.2 KiB

from django import forms
from django.contrib.auth.forms import UserCreationForm
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from django.urls import reverse
from .models import User, Reference, QSO
from .validators import CallUsernameValidator, CallLogValidator
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = User
fields = ("username",)
username = forms.CharField(max_length=50, validators=[CallUsernameValidator()])
class UpdateRefForm(forms.Form):
existingRef = forms.ModelChoiceField(label="Existing Reference", queryset=Reference.objects.all(), help_text="If reference already exists, select it here.", required=False)
newRefName = forms.CharField(max_length=50, label="New Reference", help_text="Enter name of new ref, if we should create a new", required=False)
def clean_newRefName(self):
data = self.cleaned_data["newRefName"].strip()
if Reference.objects.filter(name=data).count() > 0:
raise forms.ValidationError("Reference already exists")
return data
def clean(self):
cleaned_data = super(UpdateRefForm, self).clean()
existingRef = cleaned_data.get("existingRef")
newRefName = cleaned_data.get("newRefName")
if existingRef and newRefName:
raise forms.ValidationError("Select an existing ref or create a new one, not both!")
if not existingRef and not newRefName:
raise forms.ValidationError("Select either an existing ref or create a new one!")
class QSOForm(forms.ModelForm):
class Meta:
model = QSO
fields = ["ownNo", "band", "call", "reportTX", "reportRX", "otherNo", "refStr", "remarks"]
def __init__(self, user, *args, **kwargs):
super(QSOForm, self).__init__(*args, **kwargs)
self.user = user
self.helper = FormHelper()
self.helper.form_id = "qso-log-form"
self.helper.form_class = "form-inline"
#self.helper.form_class = "form-horizontal"
self.helper.form_style = 'inline'
self.helper.field_template = "bootstrap3/layout/inline_field.html"
self.helper.action = reverse("contest:log")
self.helper.add_input(Submit('submit', 'Log'))
def clean_call(self):
data = self.cleaned_data["call"].upper().strip()
if Reference.objects.filter(name=data).count() > 0:
raise forms.ValidationError("Reference already exists")
try:
CallLogValidator()(data)
except forms.ValidationError:
raise forms.ValidationError("Enter a valid callsign (1-2 chars, a number, 1-4 chars, maybe a /[A-Z])")
if data == self.user.username:
raise forms.ValidationError("You cannot log QSOs with yourself")
return data
def clean_ownNo(self):
data = self.cleaned_data["ownNo"]
if data < 1 or data > 100000:
raise forms.ValidationError("Number has to be in range of [1, 1000000]")
try:
QSO.objects.get(owner=self.user, ownNo=data)
raise forms.ValidationError("You already logged a QSO with the number %s" % data)
except:
return data
def clean_otherNo(self):
data = self.cleaned_data["otherNo"]
if data < 1 or data > 100000:
raise forms.ValidationError("Number has to be in range of [1, 1000000]")
return data
def clean_refStr(self):
return self.cleaned_data["refStr"].upper()
class QSOFormWithTime(QSOForm):
class Meta:
model = QSO
fields = ["time", "ownNo", "band", "call", "reportTX", "reportRX", "otherNo", "refStr", "remarks"]