Source code for oioioi.participants.forms
import bleach
from django import forms
from django.forms import ValidationError
from django.utils.translation import gettext_lazy as _
from oioioi.contests.models import Round
from oioioi.participants.models import (
OpenRegistration,
Participant,
Region,
TermsAcceptedPhrase,
)
[docs]class ParticipantForm(forms.ModelForm):
[docs] def clean_user(self):
if Participant.objects.filter(
contest=self.request_contest, user=self.cleaned_data['user']
).exists() and (
self.instance is None or self.instance.user != self.cleaned_data['user']
):
raise ValidationError(
_("%s is already a participant of this contest.")
% self.cleaned_data['user'].username
)
return self.cleaned_data['user']
[docs]class RegionForm(forms.ModelForm):
[docs] def clean_short_name(self):
if Region.objects.filter(
contest=self.request_contest, short_name=self.cleaned_data['short_name']
).exists() and (
self.instance is None
or self.instance.short_name != self.cleaned_data['short_name']
):
raise ValidationError(_("Region with this name already exists."))
return self.cleaned_data['short_name']
[docs]class OpenRegistrationForm(forms.ModelForm):
[docs] def clean_terms_accepted(self):
if not self.cleaned_data['terms_accepted']:
raise ValidationError(_("Terms not accepted"))
return True
[docs]class ExtendRoundForm(forms.Form):
def __init__(self, request_contest, *args, **kwargs):
super(ExtendRoundForm, self).__init__(*args, **kwargs)
self.fields['round'] = forms.ModelChoiceField(
queryset=Round.objects.filter(contest=request_contest)
)
[docs]class TermsAcceptedPhraseForm(forms.ModelForm):
[docs] allowed_tags = [
'a',
'article',
'b',
'blockquote',
'br',
'center',
'code',
'em',
'font',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'i',
'p',
'strong',
'u',
]
[docs] allowed_attributes = {
'a': ['href', 'title', 'target'],
'font': ['color', 'size'],
'table': ['align'],
}
[docs] def tag_as_str(self, tag):
if tag in self.allowed_attributes:
return '{} ({})'.format(
tag, ', '.join(sorted(self.allowed_attributes[tag]))
)
else:
return tag
def __init__(self, *args, **kwargs):
super(TermsAcceptedPhraseForm, self).__init__(*args, **kwargs)
if 'text' in self.fields:
self.fields['text'].widget.attrs['class'] = 'monospace'
self.fields['text'].help_text = _(
"You can use the following tags and attributes: {}."
).format(
', '.join(self.tag_as_str(tag) for tag in sorted(self.allowed_tags))
)
[docs] def clean_content(self):
return bleach.clean(
self.cleaned_data['text'],
tags=self.allowed_tags,
attributes=self.allowed_attributes,
strip=True,
)