dnmgmt/rrequests/forms.py

56 lines
2.4 KiB
Python
Raw Normal View History

2017-03-14 20:18:57 +01:00
from django import forms
from django.db.models import Q
from .models import Request
from whoisdb.models import Maintainer
class RequestForm(forms.Form):
RESOURCES = map(lambda _x: (_x, _x), [
"AS Number (16bit)", "IPv4 /27", "IPv4 > /27", "IPv6", "other"
])
applicant = forms.ModelChoiceField(Maintainer.objects.none(), label="Applicant (you)", help_text="Maintainer you want to request resources for")
provider = forms.ModelChoiceField(Maintainer.objects.none(), label="Provider", help_text="LIR/RIR you want to request resources from")
subject = forms.CharField(label="Subject")
resources = forms.CheckboxSelectMultiple(choices=RESOURCES)
message = forms.CharField(widget=forms.Textarea, help_text="Describe shortly what resources you need and what you need them for")
2017-03-14 20:18:57 +01:00
def __init__(self, user, *args, **kwargs):
super(RequestForm, self).__init__(*args, **kwargs)
self._user = user
self.fields['applicant'].queryset = self._user.maintainer_set.all()
self.fields['provider'].queryset = Maintainer.objects.filter(Q(rir=True) | Q(lir=True))
def clean(self):
cleaned_data = super(RequestForm, self).clean()
2017-03-20 00:16:33 +01:00
if not self.errors:
2017-03-14 20:18:57 +01:00
mnts = self._user.maintainer_set.all()
if cleaned_data['applicant'] in mnts and cleaned_data['provider'] in mnts:
raise forms.ValidationError("You could request resources from yourself, but this would actually not make that much sense.")
class ResponseForm(forms.Form):
new_status = forms.ChoiceField(choices=[("KEEP", "----", )] + list(Request.STATES), initial="KEEP", help_text="Only set this if you want to change the status of this request")
message = forms.CharField(widget=forms.Textarea)
def __init__(self, request, user, *args, **kwargs):
super(ResponseForm, self).__init__(*args, **kwargs)
self._request = request
self._user = user
def clean(self):
cleaned_data = super(ResponseForm, self).clean()
if not self.errors:
if cleaned_data['new_status'] == self._request.status:
raise forms.ValidationError("Status changed to same as ticket")
if self._request.status in (Request.STATE_RESOLVED, Request.STATE_REJECTED) and \
cleaned_data['new_status'] not in (Request.STATE_OPEN,):
raise forms.ValidationError("Please put this ticket in an open state before adding messages")
class ProviderResponseForm(ResponseForm):
createdResources = forms.CharField(label="Created resources", help_text="If you have created resources for this request, please enter their handles here", required=False)