cqtu/contest/forms.py

169 lines
6.1 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 django.utils import timezone
2017-01-20 14:28:59 +01:00
from crispy_forms.helper import FormHelper
2017-01-25 01:53:22 +01:00
from crispy_forms.layout import Submit, Layout
2017-01-20 14:28:59 +01:00
from django.urls import reverse
from .models import User, Reference, QSO, ShadowCall, EntryCategory, Contest
from .validators import CallUsernameValidator, CallLogValidator
2017-01-20 14:28:59 +01:00
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = User
2019-01-26 12:45:15 +01:00
fields = ("username", "email", "dncall", "qrv2m", "qrv70cm", "extra2m70cm")
2017-01-20 14:28:59 +01:00
username = forms.CharField(max_length=50, validators=[CallUsernameValidator()])
2019-01-26 12:45:15 +01:00
email = forms.EmailField(required=True)
2017-01-20 14:28:59 +01:00
class UpdateRefForm(forms.Form):
existingRef = forms.ModelChoiceField(label="Existing Exchange", queryset=Reference.objects.all(), help_text="If exchange already exists, select it here.", required=False)
newRefName = forms.CharField(max_length=50, label="New Exchange", help_text="Enter name of new exchange, if we should create a new", required=False)
location = forms.CharField(max_length=128, label='Exact Location', help_text="E.g. MAR bei den Fahrstuehlen, TEL 15. OG", required=False)
opName = forms.CharField(max_length=128, label='Operators', help_text="Name of operator(s)", required=False)
regTime = forms.DateTimeField(label="Registration time", help_text="Time of Registration")
2017-01-20 14:28:59 +01:00
def clean_newRefName(self):
2017-01-25 03:23:21 +01:00
data = self.cleaned_data["newRefName"].strip().upper()
2017-01-20 14:28:59 +01:00
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 exchange or create a new one, not both!")
2017-01-20 14:28:59 +01:00
if not existingRef and not newRefName:
raise forms.ValidationError("Select either an existing exchange or create a new one!")
2017-01-20 14:28:59 +01:00
2017-01-26 20:36:55 +01:00
class UpdateCategoryForm(forms.Form):
entry = forms.ModelChoiceField(label="Entry category", queryset=EntryCategory.objects.all())
def __init__(self, *args, **kwargs):
super(UpdateCategoryForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = "form-inline "
self.helper.form_style = 'inline'
self.helper.field_template = "bootstrap3/layout/inline_field.html"
self.helper.action = reverse("profile")
self.helper.add_input(Submit('submit', 'Set category'))
self.helper.layout = Layout('entry')
def clean(self):
contest = Contest.objects.get(id=1)
if contest.deadline < timezone.now():
raise forms.ValidationError("The deadline for setting your contest category has passed")
2017-01-20 14:28:59 +01:00
class QSOForm(forms.ModelForm):
class Meta:
model = QSO
#fields = ["ownNo", "band", "call", "reportTX", "reportRX", "refStr", "otherNo", "remarks"]
fields = ["ownNo", "band", "call", "reportTX", "reportRX", "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"
2017-01-24 12:23:28 +01:00
#self.helper.form_class = "form-inline "
2017-01-20 23:36:55 +01:00
#self.helper.form_class = "form-horizontal"
2017-01-24 12:23:28 +01:00
#self.helper.form_style = 'inline'
#self.helper.field_template = "bootstrap3/layout/inline_field.html"
2017-01-20 14:28:59 +01:00
self.helper.action = reverse("contest:log")
self.helper.add_input(Submit('submit', 'Log'))
2017-01-24 12:23:28 +01:00
#self.helper.layout = Layout(
# #*(QSOForm.Meta.fields + [ButtonHolder(Submit('submit', 'Submit', css_class='button white'))]))
# *(QSOForm.Meta.fields + [FormActions(Submit('submit', 'Log!'))]))
2017-01-20 14:28:59 +01:00
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:
2017-02-02 20:20:41 +01:00
o = QSO.objects.get(owner=self.user.id, ownNo=data)
if not (self.instance and self.instance.id and self.instance.id == o.id):
raise forms.ValidationError("You already logged a QSO with the number %s" % data)
2017-01-26 23:47:18 +01:00
except QSO.DoesNotExist:
2017-02-02 20:20:41 +01:00
pass
return data
2017-01-20 14:28:59 +01:00
def clean_otherNo(self):
data = self.cleaned_data["otherNo"]
2017-01-26 21:24:23 +01:00
if not data:
# empty value
return None
2017-01-20 14:28:59 +01:00
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
def clean(self):
cleaned_data = super(QSOForm, self).clean()
band = cleaned_data.get("band")
if band.contest.deadline < timezone.now():
raise forms.ValidationError("The deadline for logging and editing QSOs has passed")
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"]
fields = ["time", "ownNo", "band", "call", "reportTX", "reportRX", "refStr", "remarks"]
2017-01-21 00:16:39 +01:00
2017-01-25 01:53:22 +01:00
class ShadowCallAddForm(forms.ModelForm):
class Meta:
model = ShadowCall
fields = ['username']
def __init__(self, *args, **kwargs):
super(ShadowCallAddForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = "form-inline "
self.helper.form_style = 'inline'
self.helper.field_template = "bootstrap3/layout/inline_field.html"
self.helper.action = reverse("contest:registerRefs")
self.helper.add_input(Submit('submit', 'Add shadow'))
self.helper.layout = Layout('username')
2017-01-25 01:53:22 +01:00
def clean_username(self):
data = self.cleaned_data["username"]
if User.objects.filter(username=data).count() > 0:
raise forms.ValidationError("A user with this call already exists, this is not a shadow!")
return data