@@ -117,6 +117,9 @@ def validate_emails(value):
117117# -------------------------------------------------
118118# Form Classes
119119# -------------------------------------------------
120+ class AddCommentForm (forms .Form ):
121+ comment = forms .CharField (required = True , widget = forms .Textarea )
122+ private = forms .BooleanField (label = "Private comment" , required = False ,help_text = "If this box is checked the comment will not appear in the statement's public history view." )
120123
121124class RadioRenderer (RadioFieldRenderer ):
122125 def render (self ):
@@ -127,6 +130,7 @@ def render(self):
127130
128131
129132class SearchLiaisonForm (forms .Form ):
133+ '''Expects initial keyword argument queryset which then gets filtered based on form data'''
130134 text = forms .CharField (required = False )
131135 scope = forms .ChoiceField (choices = (("all" , "All text fields" ), ("title" , "Title field" )), required = False , initial = 'title' , widget = forms .RadioSelect (renderer = RadioRenderer ))
132136 source = forms .CharField (required = False )
@@ -135,11 +139,11 @@ class SearchLiaisonForm(forms.Form):
135139 end_date = DatepickerDateField (date_format = "yyyy-mm-dd" , picker_settings = {"autoclose" : "1" }, label = 'End date' , required = False )
136140
137141 def __init__ (self , * args , ** kwargs ):
138- self .state = kwargs .pop ('state ' )
142+ self .queryset = kwargs .pop ('queryset ' )
139143 super (SearchLiaisonForm , self ).__init__ (* args , ** kwargs )
140144
141145 def get_results (self ):
142- results = LiaisonStatement . objects . filter ( state = self .state )
146+ results = self .queryset
143147 if self .is_bound :
144148 query = self .cleaned_data .get ('text' )
145149 if query :
@@ -157,11 +161,19 @@ def get_results(self):
157161
158162 source = self .cleaned_data .get ('source' )
159163 if source :
160- results = results .filter (Q (from_groups__name__icontains = source ) | Q (from_groups__acronym__iexact = source ))
164+ source_list = source .split (',' )
165+ if len (source_list ) > 1 :
166+ results = results .filter (Q (from_groups__acronym__in = source_list ))
167+ else :
168+ results = results .filter (Q (from_groups__name__icontains = source ) | Q (from_groups__acronym__iexact = source ))
161169
162170 destination = self .cleaned_data .get ('destination' )
163171 if destination :
164- results = results .filter (Q (to_groups__name__icontains = destination ) | Q (to_groups__acronym__iexact = destination ))
172+ destination_list = destination .split (',' )
173+ if len (destination_list ) > 1 :
174+ results = results .filter (Q (to_groups__acronym__in = destination_list ))
175+ else :
176+ results = results .filter (Q (to_groups__name__icontains = destination ) | Q (to_groups__acronym__iexact = destination ))
165177
166178 start_date = self .cleaned_data .get ('start_date' )
167179 end_date = self .cleaned_data .get ('end_date' )
@@ -192,10 +204,13 @@ def prepare_value(self, value):
192204
193205
194206class LiaisonModelForm (BetterModelForm ):
195- '''Specify fields which require a custom widget or that are not part of the model'''
196- from_groups = forms .ModelMultipleChoiceField (queryset = Group .objects .all (),label = u'Groups' )
207+ '''Specify fields which require a custom widget or that are not part of the model.
208+ NOTE: from_groups and to_groups are marked as not required because select2 has
209+ a problem with validating
210+ '''
211+ from_groups = forms .ModelMultipleChoiceField (queryset = Group .objects .all (),label = u'Groups' ,required = False )
197212 from_contact = forms .EmailField ()
198- to_groups = forms .ModelMultipleChoiceField (queryset = Group .objects ,label = u'Groups' )
213+ to_groups = forms .ModelMultipleChoiceField (queryset = Group .objects ,label = u'Groups' , required = False )
199214 deadline = DatepickerDateField (date_format = "yyyy-mm-dd" , picker_settings = {"autoclose" : "1" }, label = 'Deadline' , required = True )
200215 related_to = SearchableLiaisonStatementsField (label = u'Related Liaison Statement' , required = False )
201216 submitted_date = DatepickerDateField (date_format = "yyyy-mm-dd" , picker_settings = {"autoclose" : "1" }, label = 'Submission date' , required = True , initial = datetime .date .today ())
@@ -221,6 +236,7 @@ class Meta:
221236 def __init__ (self , user , * args , ** kwargs ):
222237 super (LiaisonModelForm , self ).__init__ (* args , ** kwargs )
223238 self .user = user
239+ self .edit = False
224240 self .person = get_person_for_user (user )
225241 self .is_new = not self .instance .pk
226242
@@ -237,6 +253,18 @@ def __init__(self, user, *args, **kwargs):
237253 self .set_from_fields ()
238254 self .set_to_fields ()
239255
256+ def clean_from_groups (self ):
257+ from_groups = self .cleaned_data .get ('from_groups' )
258+ if not from_groups :
259+ raise forms .ValidationError ('You must specify a From Group' )
260+ return from_groups
261+
262+ def clean_to_groups (self ):
263+ to_groups = self .cleaned_data .get ('to_groups' )
264+ if not to_groups :
265+ raise forms .ValidationError ('You must specify a To Group' )
266+ return to_groups
267+
240268 def clean_from_contact (self ):
241269 contact = self .cleaned_data .get ('from_contact' )
242270 try :
@@ -294,6 +322,7 @@ def save(self, *args, **kwargs):
294322
295323 self .save_related_liaisons ()
296324 self .save_attachments ()
325+ self .save_tags ()
297326
298327 return self .instance
299328
@@ -350,6 +379,11 @@ def save_related_liaisons(self):
350379 if related .target not in new_related :
351380 related .delete ()
352381
382+ def save_tags (self ):
383+ '''Create tags as needed'''
384+ if self .instance .deadline and not self .instance .tags .filter (slug = 'taken' ):
385+ self .instance .tags .add ('required' )
386+
353387 def set_from_fields (self ):
354388 assert NotImplemented
355389
@@ -488,9 +522,9 @@ def set_from_fields(self):
488522 self .fields ['from_groups' ].choices = get_internal_choices (self .user )
489523 else :
490524 if has_role (self .user , "Secretariat" ):
491- queryset = Group .objects .filter (type = "sdo" , state = "active" ).order_by ('name' )
525+ queryset = Group .objects .filter (type = "sdo" ).order_by ('name' )
492526 else :
493- queryset = Group .objects .filter (type = "sdo" , state = "active" , role__person = self .person , role__name__in = ("liaiman" , "auth" )).distinct ().order_by ('name' )
527+ queryset = Group .objects .filter (type = "sdo" , role__person = self .person , role__name__in = ("liaiman" , "auth" )).distinct ().order_by ('name' )
494528 self .fields ['from_contact' ].widget .attrs ['readonly' ] = True
495529 self .fields ['from_groups' ].queryset = queryset
496530
@@ -501,10 +535,10 @@ def set_to_fields(self):
501535 if self .instance .is_outgoing ():
502536 # if the user is a Liaison Manager and nothing more, reduce to set to his SDOs
503537 if has_role (self .user , "Liaison Manager" ) and not self .person .role_set .filter (name__in = ('ad' ,'chair' ),group__state = 'active' ):
504- queryset = Group .objects .filter (type = "sdo" , state = "active" , role__person = self .person , role__name = "liaiman" ).distinct ().order_by ('name' )
538+ queryset = Group .objects .filter (type = "sdo" , role__person = self .person , role__name = "liaiman" ).distinct ().order_by ('name' )
505539 else :
506540 # get all outgoing entities
507- queryset = Group .objects .filter (type = "sdo" , state = "active" ).order_by ('name' )
541+ queryset = Group .objects .filter (type = "sdo" ).order_by ('name' )
508542 self .fields ['to_groups' ].queryset = queryset
509543 else :
510544 self .fields ['to_groups' ].choices = get_internal_choices (None )
0 commit comments