cqtu/contest/forms.py

102 lines
3.2 KiB
Python
Raw Normal View History

2017-01-20 14:28:59 +01:00
from django import forms
from django.contrib.auth.forms import UserCreationForm
from crispy_forms.helper import FormHelper
2017-01-20 23:36:55 +01:00
from crispy_forms.layout import Submit
2017-01-20 14:28:59 +01:00
from django.urls import reverse
from .models import User, Reference, QSO
from .validators import CallUsernameValidator, CallLogValidator
2017-01-20 14:28:59 +01:00
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
2017-01-20 23:36:55 +01:00
fields = ["ownNo", "band", "call", "reportTX", "reportRX", "otherNo", "refStr", "remarks"]
2017-01-20 14:28:59 +01:00
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"
2017-01-20 23:36:55 +01:00
#self.helper.form_class = "form-horizontal"
2017-01-20 14:28:59 +01:00
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)
2017-01-20 14:28:59 +01:00
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
2017-01-20 23:36:55 +01:00
def clean_refStr(self):
return self.cleaned_data["refStr"].upper()
2017-01-21 00:16:39 +01:00
class QSOFormWithTime(QSOForm):
class Meta:
model = QSO
fields = ["time", "ownNo", "band", "call", "reportTX", "reportRX", "otherNo", "refStr", "remarks"]