Save liaison and attachments. See #342

- Legacy-Id: 2374
This commit is contained in:
Emilio A. Sánchez López 2010-07-22 12:00:50 +00:00
parent a2cbb76acd
commit ab8cbbb835
4 changed files with 63 additions and 11 deletions

View file

@ -1,3 +1,5 @@
import datetime
from django import forms
from django.conf import settings
from django.forms.util import ErrorList
@ -5,9 +7,10 @@ from django.template.loader import render_to_string
from ietf.liaisons.accounts import (can_add_outgoing_liaison, can_add_incoming_liaison,
get_person_for_user)
from ietf.liaisons.models import LiaisonDetail
from ietf.liaisons.models import LiaisonDetail, Uploads
from ietf.liaisons.utils import IETFHierarchyManager
from ietf.liaisons.widgets import FromWidget, ReadOnlyWidget, ButtonWidget
from ietf.liaisons.widgets import (FromWidget, ReadOnlyWidget, ButtonWidget,
ShowAttachmentsWidget)
class LiaisonForm(forms.ModelForm):
@ -20,7 +23,7 @@ class LiaisonForm(forms.ModelForm):
purpose_text = forms.CharField(widget=forms.Textarea, label='Other purpose')
deadline_date = forms.DateField(label='Deadline')
title = forms.CharField(label=u'Title')
attachments = forms.CharField(label='Attachments', widget=ReadOnlyWidget, initial='No files attached', required=False)
attachments = forms.CharField(label='Attachments', widget=ShowAttachmentsWidget, required=False)
attach_title = forms.CharField(label='Title', required=False)
attach_file = forms.FileField(label='File', required=False)
attach_button = forms.CharField(label='',
@ -120,6 +123,42 @@ class LiaisonForm(forms.ModelForm):
self._errors['attachments'] = ErrorList([u'You must provide a body or attachment files'])
return self.cleaned_data
def get_organization(self):
organization_key = self.cleaned_data.get('organization')
return self.hm.get_entity_by_key(organization_key)
def save(self, *args, **kwargs):
now = datetime.datetime.now()
liaison = super(LiaisonForm, self).save(*args, **kwargs)
liaison.submitted_date = now
liaison.last_modified_date = now
organization = self.get_organization()
liaison.to_body = organization.name
liaison.to_poc = ', '.join([i.email()[1] for i in organization.get_poc()])
liaison.submitter_name, liaison.submitter_email = self.person.email()
liaison.cc1 = ', '.join(['%s <%s>' % i.email() for i in organization.get_cc()])
liaison.save()
self.save_attachments(liaison)
def save_attachments(self, instance):
for key in self.files.keys():
title_key = key.replace('file', 'title')
if not key.startswith('attach_file_') or not title_key in self.data.keys():
continue
attached_file = self.files.get(key)
extension=attached_file.name.rsplit('.', 1)
basename = extension[0]
if len(extension) > 1:
extension = '.' + extension[1]
else:
extension = ''
attach = Uploads.objects.create(
file_title = self.data.get(title_key),
person = self.person,
detail = instance,
file_extension = extension
)
class IncomingLiaisonForm(LiaisonForm):

View file

@ -17,8 +17,7 @@ def add_liaison(request):
form = liaison_form_factory(request, data=request.POST.copy(),
files = request.FILES)
if form.is_valid():
#form.save()
pass
form.save()
else:
form = liaison_form_factory(request)

View file

@ -40,3 +40,16 @@ class ButtonWidget(Widget):
html += u'<span style="display: none" class="attachDisabledLabel">%s</span>' % required_str
html += u'<input type="button" class="addAttachmentWidget" value="%s" />' % self.label
return mark_safe(html)
class ShowAttachmentsWidget(Widget):
def render(self, name, value, attrs=None):
html = u'<div id="id_%s">' % name
html += u'<span style="display: none" class="showAttachmentsEmpty">No files attached</span>'
if not value:
html += u'<div class="attachedFiles">No files attached</div>'
else:
pass
html += u'</div>'
return mark_safe(html)

View file

@ -20,8 +20,9 @@
}
config.showOn = $('#' + fieldset.find('.showAttachsOn').html());
config.showOnDisplay = config.showOn.find('.attachedFiles');
config.showOnEmpty = config.showOn.find('.showAttachmentsEmpty').html();
config.enabledLabel = fieldset.find('.attachEnabledLabel').html();
config.initialAttachments = config.showOn.html();
};
var setState = function() {
@ -44,7 +45,7 @@
var cloneFields = function() {
var html = '<div class="attachedFileInfo">';
if (count) {
html = config.showOn.html() + html;
html = config.showOnDisplay.html() + html;
}
config.fields.each(function() {
var field = $(this);
@ -65,7 +66,7 @@
});
html += ' <a href="#" class="removeAttach">Remove</a>';
html += '</div>';
config.showOn.html(html);
config.showOnDisplay.html(html);
config.fields.val('');
count += 1;
};
@ -83,8 +84,8 @@
$('#' + $(this).html()).remove();
});
attach.remove();
if (!config.showOn.html()) {
config.showOn.html(config.initialAttachments);
if (!config.showOnDisplay.html()) {
config.showOnDisplay.html(config.showOnEmpty);
count = 0;
}
return false;
@ -93,7 +94,7 @@
var initTriggers = function() {
config.fields.change(setState);
config.fields.keyup(setState);
config.showOn.find('a.removeAttach').live('click', removeAttachment);
config.showOnDisplay.find('a.removeAttach').live('click', removeAttachment);
button.click(doAttach);
};