1+ import datetime
2+
13from django import forms
24from django .conf import settings
35from django .forms .util import ErrorList
46from django .template .loader import render_to_string
57
68from ietf .liaisons .accounts import (can_add_outgoing_liaison , can_add_incoming_liaison ,
79 get_person_for_user )
8- from ietf .liaisons .models import LiaisonDetail
10+ from ietf .liaisons .models import LiaisonDetail , Uploads
911from ietf .liaisons .utils import IETFHierarchyManager
10- from ietf .liaisons .widgets import FromWidget , ReadOnlyWidget , ButtonWidget
12+ from ietf .liaisons .widgets import (FromWidget , ReadOnlyWidget , ButtonWidget ,
13+ ShowAttachmentsWidget )
1114
1215
1316class LiaisonForm (forms .ModelForm ):
@@ -20,7 +23,7 @@ class LiaisonForm(forms.ModelForm):
2023 purpose_text = forms .CharField (widget = forms .Textarea , label = 'Other purpose' )
2124 deadline_date = forms .DateField (label = 'Deadline' )
2225 title = forms .CharField (label = u'Title' )
23- attachments = forms .CharField (label = 'Attachments' , widget = ReadOnlyWidget , initial = 'No files attached' , required = False )
26+ attachments = forms .CharField (label = 'Attachments' , widget = ShowAttachmentsWidget , required = False )
2427 attach_title = forms .CharField (label = 'Title' , required = False )
2528 attach_file = forms .FileField (label = 'File' , required = False )
2629 attach_button = forms .CharField (label = '' ,
@@ -120,6 +123,42 @@ def clean(self):
120123 self ._errors ['attachments' ] = ErrorList ([u'You must provide a body or attachment files' ])
121124 return self .cleaned_data
122125
126+ def get_organization (self ):
127+ organization_key = self .cleaned_data .get ('organization' )
128+ return self .hm .get_entity_by_key (organization_key )
129+
130+ def save (self , * args , ** kwargs ):
131+ now = datetime .datetime .now ()
132+ liaison = super (LiaisonForm , self ).save (* args , ** kwargs )
133+ liaison .submitted_date = now
134+ liaison .last_modified_date = now
135+ organization = self .get_organization ()
136+ liaison .to_body = organization .name
137+ liaison .to_poc = ', ' .join ([i .email ()[1 ] for i in organization .get_poc ()])
138+ liaison .submitter_name , liaison .submitter_email = self .person .email ()
139+ liaison .cc1 = ', ' .join (['%s <%s>' % i .email () for i in organization .get_cc ()])
140+ liaison .save ()
141+ self .save_attachments (liaison )
142+
143+ def save_attachments (self , instance ):
144+ for key in self .files .keys ():
145+ title_key = key .replace ('file' , 'title' )
146+ if not key .startswith ('attach_file_' ) or not title_key in self .data .keys ():
147+ continue
148+ attached_file = self .files .get (key )
149+ extension = attached_file .name .rsplit ('.' , 1 )
150+ basename = extension [0 ]
151+ if len (extension ) > 1 :
152+ extension = '.' + extension [1 ]
153+ else :
154+ extension = ''
155+ attach = Uploads .objects .create (
156+ file_title = self .data .get (title_key ),
157+ person = self .person ,
158+ detail = instance ,
159+ file_extension = extension
160+ )
161+
123162
124163class IncomingLiaisonForm (LiaisonForm ):
125164
0 commit comments