cqtu/contest/forms.py

173 lines
6.7 KiB
Python
Raw Permalink 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
fields = ("username", "email", "dncall", "qrv2m", "qrv70cm", "extra2m70cm")
2017-01-20 14:28:59 +01:00
username = forms.CharField(max_length=50, validators=[CallUsernameValidator()])
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):
data = self.cleaned_data["newRefName"].strip().upper()
2017-01-20 14:28:59 +01:00
return data
2017-01-20 14:28:59 +01:00
def clean(self):
cleaned_data = super(UpdateRefForm, self).clean()
2017-01-20 14:28:59 +01:00
existingRef = cleaned_data.get("existingRef")
newRefName = cleaned_data.get("newRefName")
2017-01-20 14:28:59 +01:00
if newRefName:
try:
ref = Reference.objects.get(name=newRefName)
self.cleaned_data['newRefName'] = None
self.cleaned_data['existingRef'] = ref
except Reference.DoesNotExist:
pass
if existingRef and newRefName:
raise forms.ValidationError("Select an existing exchange or create a new one, not both!")
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)
2017-01-26 20:36:55 +01:00
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')
2017-01-26 20:36:55 +01:00
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
2017-01-20 14:28:59 +01:00
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'))
#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()
2017-01-20 14:28:59 +01:00
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])")
2017-01-20 14:28:59 +01:00
if data == self.user.username:
raise forms.ValidationError("You cannot log QSOs with yourself")
2017-01-20 14:28:59 +01:00
return data
2017-01-20 14:28:59 +01:00
def clean_ownNo(self):
data = self.cleaned_data["ownNo"]
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]")
2017-01-20 14:28:59 +01:00
try:
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)
except QSO.DoesNotExist:
pass
2017-02-02 20:20:41 +01:00
return data
2017-01-20 14:28:59 +01:00
def clean_otherNo(self):
data = self.cleaned_data["otherNo"]
2017-01-20 14:28:59 +01:00
if not data:
# empty value
return None
2017-01-26 21:24:23 +01:00
if data < 1 or data > 100000:
raise forms.ValidationError("Number has to be in range of [1, 1000000]")
2017-01-20 14:28:59 +01:00
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']
2017-01-25 01:53:22 +01:00
def __init__(self, *args, **kwargs):
super(ShadowCallAddForm, self).__init__(*args, **kwargs)
2017-01-25 01:53:22 +01:00
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!")
2017-01-25 01:53:22 +01:00
return data