Skip to content

Commit ab8cbbb

Browse files
committed
Save liaison and attachments. See ietf-tools#342
- Legacy-Id: 2374
1 parent a2cbb76 commit ab8cbbb

4 files changed

Lines changed: 63 additions & 11 deletions

File tree

ietf/liaisons/forms.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import datetime
2+
13
from django import forms
24
from django.conf import settings
35
from django.forms.util import ErrorList
46
from django.template.loader import render_to_string
57

68
from 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
911
from 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

1316
class 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

124163
class IncomingLiaisonForm(LiaisonForm):
125164

ietf/liaisons/views.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ def add_liaison(request):
1717
form = liaison_form_factory(request, data=request.POST.copy(),
1818
files = request.FILES)
1919
if form.is_valid():
20-
#form.save()
21-
pass
20+
form.save()
2221
else:
2322
form = liaison_form_factory(request)
2423

ietf/liaisons/widgets.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,16 @@ def render(self, name, value, attrs=None):
4040
html += u'<span style="display: none" class="attachDisabledLabel">%s</span>' % required_str
4141
html += u'<input type="button" class="addAttachmentWidget" value="%s" />' % self.label
4242
return mark_safe(html)
43+
44+
45+
class ShowAttachmentsWidget(Widget):
46+
47+
def render(self, name, value, attrs=None):
48+
html = u'<div id="id_%s">' % name
49+
html += u'<span style="display: none" class="showAttachmentsEmpty">No files attached</span>'
50+
if not value:
51+
html += u'<div class="attachedFiles">No files attached</div>'
52+
else:
53+
pass
54+
html += u'</div>'
55+
return mark_safe(html)

static/js/liaisons.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
}
2121

2222
config.showOn = $('#' + fieldset.find('.showAttachsOn').html());
23+
config.showOnDisplay = config.showOn.find('.attachedFiles');
24+
config.showOnEmpty = config.showOn.find('.showAttachmentsEmpty').html();
2325
config.enabledLabel = fieldset.find('.attachEnabledLabel').html();
24-
config.initialAttachments = config.showOn.html();
2526
};
2627

2728
var setState = function() {
@@ -44,7 +45,7 @@
4445
var cloneFields = function() {
4546
var html = '<div class="attachedFileInfo">';
4647
if (count) {
47-
html = config.showOn.html() + html;
48+
html = config.showOnDisplay.html() + html;
4849
}
4950
config.fields.each(function() {
5051
var field = $(this);
@@ -65,7 +66,7 @@
6566
});
6667
html += ' <a href="#" class="removeAttach">Remove</a>';
6768
html += '</div>';
68-
config.showOn.html(html);
69+
config.showOnDisplay.html(html);
6970
config.fields.val('');
7071
count += 1;
7172
};
@@ -83,8 +84,8 @@
8384
$('#' + $(this).html()).remove();
8485
});
8586
attach.remove();
86-
if (!config.showOn.html()) {
87-
config.showOn.html(config.initialAttachments);
87+
if (!config.showOnDisplay.html()) {
88+
config.showOnDisplay.html(config.showOnEmpty);
8889
count = 0;
8990
}
9091
return false;
@@ -93,7 +94,7 @@
9394
var initTriggers = function() {
9495
config.fields.change(setState);
9596
config.fields.keyup(setState);
96-
config.showOn.find('a.removeAttach').live('click', removeAttachment);
97+
config.showOnDisplay.find('a.removeAttach').live('click', removeAttachment);
9798
button.click(doAttach);
9899
};
99100

0 commit comments

Comments
 (0)