dnmgmt/rrequests/forms.py

60 lines
2.8 KiB
Python
Raw Permalink Normal View History

2018-01-17 00:23:00 +01:00
# This file is part of dnmgmt, a number resource management system
# Licensed under GNU General Public License v3 or later
# Written by Sebastian Lohff (seba@someserver.de)
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):
2019-05-30 22:10:55 +02:00
RESOURCES = map(lambda _x: (_x, _x), [
"AS Number (16bit)", "IPv4 /27", "IPv4 > /27", "IPv6", "other"
])
2017-03-14 20:18:57 +01:00
2019-05-30 22:10:55 +02:00
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")
2017-03-14 20:18:57 +01:00
2019-05-30 22:10:55 +02:00
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
2019-05-30 22:10:55 +02:00
def __init__(self, user, *args, **kwargs):
super(RequestForm, self).__init__(*args, **kwargs)
2017-03-14 20:18:57 +01:00
2019-05-30 22:10:55 +02:00
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))
2017-03-14 20:18:57 +01:00
2019-05-30 22:10:55 +02:00
def clean(self):
cleaned_data = super(RequestForm, self).clean()
2017-03-14 20:18:57 +01:00
2019-05-30 22:10:55 +02:00
if not self.errors:
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.")
2017-03-14 20:18:57 +01:00
class ResponseForm(forms.Form):
2019-05-30 22:10:55 +02:00
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)
2017-03-14 20:18:57 +01:00
2019-05-30 22:10:55 +02:00
def __init__(self, request, user, *args, **kwargs):
super(ResponseForm, self).__init__(*args, **kwargs)
self._request = request
self._user = user
2017-03-14 20:18:57 +01:00
2019-05-30 22:10:55 +02:00
def clean(self):
cleaned_data = super(ResponseForm, self).clean()
2017-03-14 20:18:57 +01:00
2019-05-30 22:10:55 +02:00
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")
2017-03-14 20:18:57 +01:00
class ProviderResponseForm(ResponseForm):
2019-05-30 22:10:55 +02:00
createdResources = forms.CharField(label="Created resources", help_text="If you have created resources for this request, please enter their handles here", required=False)