1- import datetime
1+ import datetime , os
22from email .utils import parseaddr
33
44from django import forms
1414from ietf .liaisons .widgets import (FromWidget , ReadOnlyWidget , ButtonWidget ,
1515 ShowAttachmentsWidget , RelatedLiaisonWidget )
1616from ietf .liaisons .models import LiaisonStatement , LiaisonStatementPurposeName
17+ from ietf .liaisons .proxy import LiaisonDetailProxy
1718from redesign .group .models import Group
1819from redesign .person .models import Person
20+ from redesign .doc .models import Document
1921
2022
2123class LiaisonForm (forms .Form ):
24+ person = forms .ModelChoiceField (Person .objects .all ())
2225 from_field = forms .ChoiceField (widget = FromWidget , label = u'From' )
2326 replyto = forms .CharField (label = u'Reply to' )
2427 organization = forms .ChoiceField ()
@@ -64,15 +67,16 @@ def __init__(self, user, *args, **kwargs):
6467 self .fake_person = None
6568 self .person = get_person_for_user (user )
6669 if kwargs .get ('data' , None ):
67- kwargs ['data' ].update ({'person' : self .person .pk })
6870 if is_secretariat (self .user ) and 'from_fake_user' in kwargs ['data' ].keys ():
6971 self .fake_person = Person .objects .get (pk = kwargs ['data' ]['from_fake_user' ])
7072 kwargs ['data' ].update ({'person' : self .fake_person .pk })
73+ else :
74+ kwargs ['data' ].update ({'person' : self .person .pk })
7175
7276 self .instance = kwargs .pop ("instance" , None )
7377
7478 super (LiaisonForm , self ).__init__ (* args , ** kwargs )
75-
79+
7680 # now copy in values from instance, like a ModelForm
7781 if self .instance :
7882 for name , field in self .fields .iteritems ():
@@ -120,8 +124,7 @@ def set_from_field(self):
120124 assert NotImplemented
121125
122126 def set_replyto_field (self ):
123- email = self .person .email_address ()
124- self .fields ['replyto' ].initial = email
127+ self .fields ['replyto' ].initial = self .person .email ()[1 ]
125128
126129 def set_organization_field (self ):
127130 assert NotImplemented
@@ -193,7 +196,7 @@ def get_to_entity(self):
193196 return self .hm .get_entity_by_key (organization_key )
194197
195198 def get_poc (self , organization ):
196- return ', ' .join ([ i . email_address () for i in organization .get_poc ()] )
199+ return ', ' .join (u"%s <%s>" % i . email () for i in organization .get_poc ())
197200
198201 def clean_cc1 (self ):
199202 value = self .cleaned_data .get ('cc1' , '' )
@@ -223,7 +226,7 @@ def get_cc(self, from_entity, to_entity):
223226 def save (self , * args , ** kwargs ):
224227 l = self .instance
225228 if not l :
226- l = LiaisonStatement ()
229+ l = LiaisonDetailProxy ()
227230
228231 l .title = self .cleaned_data ["title" ]
229232 l .purpose = LiaisonStatementPurposeName .objects .get (order = self .cleaned_data ["purpose" ])
@@ -238,35 +241,31 @@ def save(self, *args, **kwargs):
238241
239242 l .modified = now
240243 l .submitted = datetime .datetime .combine (self .cleaned_data ["submitted_date" ], now .time ())
241- if self .cleaned_data .get ("approval" ): # FIXME
242- if not l .approved :
243- l .approved = now
244+ if not l .approved :
245+ l .approved = now
244246
245247 self .save_extra_fields (l )
248+
246249 l .save () # we have to save here to make sure we get an id for the attachments
247250 self .save_attachments (l )
248251
249252 return l
250253
251254 def save_extra_fields (self , liaison ):
252255 from_entity = self .get_from_entity ()
253- print from_entity , type (from_entity )
254- print self .cleaned_data .get ("from_field" )
255- liason .from_name = from_entity .name
256- print from_entity .obj # FIXME? c.get("from_field")
257- liason .from_group = from_entity .obj
258- liason .from_contact = self .person # FIXME?
256+ liaison .from_name = from_entity .name
257+ liaison .from_group = from_entity .obj
258+ liaison .from_contact = self .cleaned_data ["person" ].email_address ()
259259
260260 organization = self .get_to_entity ()
261- liason .to_name = organization .name
262- print organization .obj # FIXME? self.cleaned_data.get('organization')
263- liason .to_group = organization .obj
264- liason .to_contact = self .get_poc (organization )
265-
261+ liaison .to_name = organization .name
262+ liaison .to_group = organization .obj
263+ liaison .to_contact = self .get_poc (organization )
264+
266265 liaison .cc = self .get_cc (from_entity , organization )
267266
268267 def save_attachments (self , instance ):
269- return # FIXME
268+ written = instance . attachments . all (). count ()
270269 for key in self .files .keys ():
271270 title_key = key .replace ('file' , 'title' )
272271 if not key .startswith ('attach_file_' ) or not title_key in self .data .keys ():
@@ -277,13 +276,16 @@ def save_attachments(self, instance):
277276 extension = '.' + extension [1 ]
278277 else :
279278 extension = ''
280- attach = Uploads .objects .create (
281- file_title = self .data .get (title_key ),
282- person = self .person ,
283- detail = instance ,
284- file_extension = extension ,
279+ written += 1
280+ name = instance .name () + ("-attachment-%s" % written )
281+ attach = Document .objects .create (
282+ title = self .data .get (title_key ),
283+ type_id = "liaison" ,
284+ name = name ,
285+ external_url = name + extension , # strictly speaking not necessary, but just for the time being ...
285286 )
286- attach_file = open ('%sfile%s%s' % (settings .LIAISON_ATTACH_PATH , attach .pk , attach .file_extension ), 'w' )
287+ instance .attachments .add (attach )
288+ attach_file = open (os .path .join (settings .LIAISON_ATTACH_PATH , attach .name + extension ), 'w' )
287289 attach_file .write (attached_file .read ())
288290 attach_file .close ()
289291
@@ -380,21 +382,13 @@ def get_poc(self, organization):
380382 return self .cleaned_data ['to_poc' ]
381383
382384 def save_extra_fields (self , liaison ):
383- raise NotImplemented
384385 super (OutgoingLiaisonForm , self ).save_extra_fields (liaison )
385386 from_entity = self .get_from_entity ()
386387 needs_approval = from_entity .needs_approval (self .person )
387388 if not needs_approval or self .cleaned_data .get ('approved' , False ):
388- approved = True
389- approval_date = datetime .datetime .now ()
389+ liaison .approved = datetime .datetime .now ()
390390 else :
391- approved = False
392- approval_date = None
393- approval = OutgoingLiaisonApproval .objects .create (
394- approved = approved ,
395- approval_date = approval_date )
396- liaison .approval = approval
397- liaison .save ()
391+ liaison .approved = None
398392
399393 def clean_to_poc (self ):
400394 value = self .cleaned_data .get ('to_poc' , None )
0 commit comments