|
| 1 | +from django import forms |
| 2 | + |
| 3 | +from ietf.group.models import Group |
| 4 | +import os |
| 5 | + |
| 6 | +# ------------------------------------------------- |
| 7 | +# Globals |
| 8 | +# ------------------------------------------------- |
| 9 | + |
| 10 | +NUM_SESSION_CHOICES = (('','--Please select'),('1','1'),('2','2')) |
| 11 | +LENGTH_SESSION_CHOICES = (('','--Please select'),('1800','30 minutes'),('3600','1 hour'),('5400','1.5 hours'), ('7200','2 hours'),('9000','2.5 hours')) |
| 12 | +WG_CHOICES = list( Group.objects.filter(type__in=('wg','rg','ag'),state__in=('bof','proposed','active')).values_list('acronym','acronym').order_by('acronym')) |
| 13 | +WG_CHOICES.insert(0,('','--Select WG(s)')) |
| 14 | + |
| 15 | +# ------------------------------------------------- |
| 16 | +# Helper Functions |
| 17 | +# ------------------------------------------------- |
| 18 | +def check_conflict(groups): |
| 19 | + ''' |
| 20 | + Takes a string which is a list of group acronyms. Checks that they are all active groups |
| 21 | + ''' |
| 22 | + # convert to python list (allow space or comma separated lists) |
| 23 | + items = groups.replace(',',' ').split() |
| 24 | + active_groups = Group.objects.filter(type__in=('wg','ag','rg'), state__in=('bof','proposed','active')) |
| 25 | + for group in items: |
| 26 | + if not active_groups.filter(acronym=group): |
| 27 | + raise forms.ValidationError("Invalid or inactive group acronym: %s" % group) |
| 28 | + |
| 29 | +def join_conflicts(data): |
| 30 | + ''' |
| 31 | + Takes a dictionary (ie. data dict from a form) and concatenates all |
| 32 | + conflict fields into one list |
| 33 | + ''' |
| 34 | + conflicts = [] |
| 35 | + for groups in (data['conflict1'],data['conflict2'],data['conflict3']): |
| 36 | + # convert to python list (allow space or comma separated lists) |
| 37 | + items = groups.replace(',',' ').split() |
| 38 | + conflicts.extend(items) |
| 39 | + return conflicts |
| 40 | + |
| 41 | +# ------------------------------------------------- |
| 42 | +# Forms |
| 43 | +# ------------------------------------------------- |
| 44 | + |
| 45 | +class GroupSelectForm(forms.Form): |
| 46 | + group = forms.ChoiceField() |
| 47 | + |
| 48 | + def __init__(self,*args,**kwargs): |
| 49 | + choices = kwargs.pop('choices') |
| 50 | + super(GroupSelectForm, self).__init__(*args,**kwargs) |
| 51 | + self.fields['group'].widget.choices = choices |
| 52 | + |
| 53 | + |
| 54 | +class SessionForm(forms.Form): |
| 55 | + num_session = forms.ChoiceField(choices=NUM_SESSION_CHOICES) |
| 56 | + length_session1 = forms.ChoiceField(choices=LENGTH_SESSION_CHOICES) |
| 57 | + length_session2 = forms.ChoiceField(choices=LENGTH_SESSION_CHOICES,required=False) |
| 58 | + length_session3 = forms.ChoiceField(choices=LENGTH_SESSION_CHOICES,required=False) |
| 59 | + attendees = forms.IntegerField() |
| 60 | + conflict1 = forms.CharField(max_length=255,required=False) |
| 61 | + conflict2 = forms.CharField(max_length=255,required=False) |
| 62 | + conflict3 = forms.CharField(max_length=255,required=False) |
| 63 | + comments = forms.CharField(max_length=200,required=False) |
| 64 | + wg_selector1 = forms.ChoiceField(choices=WG_CHOICES,required=False) |
| 65 | + wg_selector2 = forms.ChoiceField(choices=WG_CHOICES,required=False) |
| 66 | + wg_selector3 = forms.ChoiceField(choices=WG_CHOICES,required=False) |
| 67 | + third_session = forms.BooleanField(required=False) |
| 68 | + |
| 69 | + def __init__(self, *args, **kwargs): |
| 70 | + super(SessionForm, self).__init__(*args, **kwargs) |
| 71 | + self.fields['num_session'].widget.attrs['onChange'] = "stat_ls(this.selectedIndex);" |
| 72 | + self.fields['length_session1'].widget.attrs['onClick'] = "if (check_num_session(1)) this.disabled=true;" |
| 73 | + self.fields['length_session2'].widget.attrs['onClick'] = "if (check_num_session(2)) this.disabled=true;" |
| 74 | + self.fields['length_session3'].widget.attrs['onClick'] = "if (check_third_session()) { this.disabled=true;}" |
| 75 | + self.fields['comments'].widget = forms.Textarea(attrs={'rows':'6','cols':'65'}) |
| 76 | + self.fields['wg_selector1'].widget.attrs['onChange'] = "document.form_post.conflict1.value=document.form_post.conflict1.value + ' ' + this.options[this.selectedIndex].value; return handleconflictfield(1);" |
| 77 | + self.fields['wg_selector2'].widget.attrs['onChange'] = "document.form_post.conflict2.value=document.form_post.conflict2.value + ' ' + this.options[this.selectedIndex].value; return handleconflictfield(2);" |
| 78 | + self.fields['wg_selector2'].widget.attrs['onClick'] = "return check_prior_conflict(2);" |
| 79 | + self.fields['wg_selector3'].widget.attrs['onChange'] = "document.form_post.conflict3.value=document.form_post.conflict3.value + ' ' + this.options[this.selectedIndex].value; return handleconflictfield(3);" |
| 80 | + self.fields['wg_selector3'].widget.attrs['onClick'] = "return check_prior_conflict(3);" |
| 81 | + self.fields['third_session'].widget.attrs['onClick'] = "if (document.form_post.num_session.selectedIndex < 2) { alert('Cannot use this field - Number of Session is not set to 2'); return false; } else { if (this.checked==true) { document.form_post.length_session3.disabled=false; } else { document.form_post.length_session3.value=0;document.form_post.length_session3.disabled=true; } }" |
| 82 | + |
| 83 | + # check third_session checkbox if instance and length_session3 |
| 84 | + # assert False, (self.instance, self.fields['length_session3'].initial) |
| 85 | + if self.initial and 'length_session3' in self.initial: |
| 86 | + if self.initial['length_session3'] != '0' and self.initial['length_session3'] != None: |
| 87 | + self.fields['third_session'].initial = True |
| 88 | + |
| 89 | + def clean_conflict1(self): |
| 90 | + conflict = self.cleaned_data['conflict1'] |
| 91 | + check_conflict(conflict) |
| 92 | + return conflict |
| 93 | + |
| 94 | + def clean_conflict2(self): |
| 95 | + conflict = self.cleaned_data['conflict2'] |
| 96 | + check_conflict(conflict) |
| 97 | + return conflict |
| 98 | + |
| 99 | + def clean_conflict3(self): |
| 100 | + conflict = self.cleaned_data['conflict3'] |
| 101 | + check_conflict(conflict) |
| 102 | + return conflict |
| 103 | + |
| 104 | + def clean(self): |
| 105 | + super(SessionForm, self).clean() |
| 106 | + data = self.cleaned_data |
| 107 | + if self.errors: |
| 108 | + return self.cleaned_data |
| 109 | + |
| 110 | + # error if conflits contain dupes |
| 111 | + all_conflicts = join_conflicts(data) |
| 112 | + temp = [] |
| 113 | + for c in all_conflicts: |
| 114 | + if c not in temp: |
| 115 | + temp.append(c) |
| 116 | + else: |
| 117 | + raise forms.ValidationError('%s appears in conflicts more than once' % c) |
| 118 | + |
| 119 | + # verify session_length and num_session correspond |
| 120 | + # if default (empty) option is selected, cleaned_data won't include num_session key |
| 121 | + if data.get('num_session','') == 2: |
| 122 | + if not data['length_session2']: |
| 123 | + raise forms.ValidationError('You must enter a length for session 2') |
| 124 | + |
| 125 | + if data.get('third_session',False): |
| 126 | + if not data.get('length_session3',None): |
| 127 | + raise forms.ValidationError('Length of third session not selected') |
| 128 | + |
| 129 | + return data |
| 130 | + |
| 131 | +class ToolStatusForm(forms.Form): |
| 132 | + message = forms.CharField(widget=forms.Textarea(attrs={'rows':'3','cols':'80'})) |
| 133 | + |
0 commit comments