Initial commit of the sources upload, new version and new document refactor

This commit is contained in:
Roberto Rosario
2014-10-23 17:10:02 -04:00
parent e6c2628b2e
commit 4acf88c9a6
9 changed files with 289 additions and 298 deletions

View File

@@ -94,15 +94,9 @@ class DocumentPreviewForm(forms.Form):
class DocumentForm(forms.ModelForm): class DocumentForm(forms.ModelForm):
"""
Baseform for document creation, and editing, made generic enough to
be used by document creation from staging files
"""
class Meta: class Meta:
model = Document model = Document
# TODO: Don't use exclude here, use fields, this app shouldn't know fields = ('label', 'description', 'language')
# anything about tags
exclude = ('tags', 'document_type')
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
document_type = kwargs.pop('document_type', None) document_type = kwargs.pop('document_type', None)
@@ -110,22 +104,6 @@ class DocumentForm(forms.ModelForm):
super(DocumentForm, self).__init__(*args, **kwargs) super(DocumentForm, self).__init__(*args, **kwargs)
if 'document_type' in self.fields:
# To allow merging with DocumentForm_edit
self.fields['document_type'].widget = forms.HiddenInput()
if instance:
self.fields['use_file_name'] = forms.BooleanField(
label=_(u'Use the new version filename as the document filename'),
initial=False,
required=False,
)
# Instance's document_type overrides the passed document_type
if instance:
if hasattr(instance, 'document_type'):
document_type = instance.document_type
if document_type: if document_type:
filenames_qs = document_type.documenttypefilename_set.filter(enabled=True) filenames_qs = document_type.documenttypefilename_set.filter(enabled=True)
if filenames_qs.count() > 0: if filenames_qs.count() > 0:
@@ -134,55 +112,26 @@ class DocumentForm(forms.ModelForm):
required=False, required=False,
label=_(u'Quick document rename')) label=_(u'Quick document rename'))
if instance:
if instance.latest_version:
self.version_fields(instance)
def version_fields(self, document): # TODO: merge DocumentForm and DocumentForm_edit
self.fields['version_update'] = forms.ChoiceField( class DocumentForm_edit(forms.ModelForm):
label=_(u'Version update'),
choices=DocumentVersion.get_version_update_choices(document.latest_version)
)
self.fields['comment'] = forms.CharField(
label=_(u'Comment'),
required=False,
widget=forms.widgets.Textarea(attrs={'rows': 4}),
)
new_filename = forms.CharField(
label=_('New document filename'), required=False
)
def clean(self):
cleaned_data = self.cleaned_data
cleaned_data['new_version_data'] = {
'comment': self.cleaned_data.get('comment'),
'version_update': self.cleaned_data.get('version_update'),
}
# Always return the full collection of cleaned data.
return cleaned_data
class DocumentForm_edit(DocumentForm):
""" """
Form sub classes from DocumentForm used only when editing a document Form sub classes from DocumentForm used only when editing a document
""" """
class Meta: class Meta:
model = Document model = Document
exclude = ('file', 'document_type', 'tags') fields = ('label', 'description', 'language')
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(DocumentForm_edit, self).__init__(*args, **kwargs) super(DocumentForm_edit, self).__init__(*args, **kwargs)
if kwargs['instance'].latest_version:
self.fields.pop('version_update')
self.fields.pop('comment')
self.fields['language'].initial = kwargs['instance'].language
else:
self.fields.pop('new_filename')
self.fields.pop('use_file_name') filenames_qs = self.instance.document_type.documenttypefilename_set.filter(enabled=True)
if filenames_qs.count() > 0:
self.fields['document_type_available_filenames'] = forms.ModelChoiceField(
queryset=filenames_qs,
required=False,
label=_(u'Quick document rename'))
class DocumentPropertiesForm(DetailForm): class DocumentPropertiesForm(DetailForm):
@@ -270,3 +219,36 @@ class DocumentDownloadForm(forms.Form):
if len(self.document_versions) > 1: if len(self.document_versions) > 1:
self.fields['compressed'].initial = True self.fields['compressed'].initial = True
self.fields['compressed'].widget.attrs.update({'disabled': True}) self.fields['compressed'].widget.attrs.update({'disabled': True})
class NewVersionForm(forms.ModelForm):
class Meta:
model = DocumentVersion
'''
def version_fields(self, document):
self.fields['version_update'] = forms.ChoiceField(
label=_(u'Version update'),
choices=DocumentVersion.get_version_update_choices(document.latest_version)
)
self.fields['comment'] = forms.CharField(
label=_(u'Comment'),
required=False,
widget=forms.widgets.Textarea(attrs={'rows': 4}),
)
new_filename = forms.CharField(
label=_('New document filename'), required=False
)
def clean(self):
cleaned_data = self.cleaned_data
cleaned_data['new_version_data'] = {
'comment': self.cleaned_data.get('comment'),
'version_update': self.cleaned_data.get('version_update'),
}
# Always return the full collection of cleaned data.
return cleaned_data
'''

View File

@@ -114,7 +114,6 @@ def document_view(request, document_id, advanced=False):
] ]
if document.latest_version: if document.latest_version:
document_fields.extend([ document_fields.extend([
{'label': _(u'Filename'), 'field': 'filename'},
{'label': _(u'File mimetype'), 'field': lambda x: x.file_mimetype or _(u'None')}, {'label': _(u'File mimetype'), 'field': lambda x: x.file_mimetype or _(u'None')},
{'label': _(u'File mime encoding'), 'field': lambda x: x.file_mime_encoding or _(u'None')}, {'label': _(u'File mime encoding'), 'field': lambda x: x.file_mime_encoding or _(u'None')},
{'label': _(u'File size'), 'field': lambda x: pretty_size(x.size) if x.size else '-'}, {'label': _(u'File size'), 'field': lambda x: pretty_size(x.size) if x.size else '-'},
@@ -233,31 +232,25 @@ def document_edit(request, document_id):
AccessEntry.objects.check_access(PERMISSION_DOCUMENT_PROPERTIES_EDIT, request.user, document) AccessEntry.objects.check_access(PERMISSION_DOCUMENT_PROPERTIES_EDIT, request.user, document)
if request.method == 'POST': if request.method == 'POST':
old_document = copy.copy(document)
form = DocumentForm_edit(request.POST, instance=document) form = DocumentForm_edit(request.POST, instance=document)
if form.is_valid(): if form.is_valid():
document.filename = form.cleaned_data['new_filename'] document.label = form.cleaned_data['label']
document.description = form.cleaned_data['description'] document.description = form.cleaned_data['description']
document.language = form.cleaned_data['language'] document.language = form.cleaned_data['language']
if 'document_type_available_filenames' in form.cleaned_data: if 'document_type_available_filenames' in form.cleaned_data:
if form.cleaned_data['document_type_available_filenames']: if form.cleaned_data['document_type_available_filenames']:
document.filename = form.cleaned_data['document_type_available_filenames'].filename document.label = form.cleaned_data['document_type_available_filenames'].filename
document.save() document.save()
create_history(HISTORY_DOCUMENT_EDITED, document, {'user': request.user, 'diff': return_diff(old_document, document, ['filename', 'description'])}) create_history(HISTORY_DOCUMENT_EDITED, document, {'user': request.user})
document.add_as_recent_document_for_user(request.user) document.add_as_recent_document_for_user(request.user)
messages.success(request, _(u'Document "%s" edited successfully.') % document) messages.success(request, _(u'Document "%s" edited successfully.') % document)
return HttpResponseRedirect(document.get_absolute_url()) return HttpResponseRedirect(document.get_absolute_url())
else: else:
if document.latest_version: form = DocumentForm_edit(instance=document)
form = DocumentForm_edit(instance=document, initial={
'new_filename': document.filename, 'description': document.description})
else:
form = DocumentForm_edit(instance=document, initial={
'description': document.description})
return render_to_response('main/generic_form.html', { return render_to_response('main/generic_form.html', {
'form': form, 'form': form,
@@ -335,7 +328,7 @@ def document_download(request, document_id=None, document_id_list=None, document
compressed_file = CompressedFile() compressed_file = CompressedFile()
for document_version in document_versions: for document_version in document_versions:
descriptor = document_version.open() descriptor = document_version.open()
compressed_file.add_file(descriptor, arcname=document_version.filename) compressed_file.add_file(descriptor, arcname=document_version.document.label)
descriptor.close() descriptor.close()
compressed_file.close() compressed_file.close()
@@ -361,7 +354,7 @@ def document_download(request, document_id=None, document_id_list=None, document
return serve_file( return serve_file(
request, request,
document_versions[0].file, document_versions[0].file,
save_as=u'"%s"' % document_versions[0].filename, save_as=u'"%s"' % document_versions[0].document.label,
content_type=document_versions[0].mimetype if document_versions[0].mimetype else 'application/octet-stream' content_type=document_versions[0].mimetype if document_versions[0].mimetype else 'application/octet-stream'
) )
except Exception as exception: except Exception as exception:
@@ -1111,10 +1104,6 @@ def document_version_list(request, document_pk):
'name': _(u'Encoding'), 'name': _(u'Encoding'),
'attribute': 'encoding', 'attribute': 'encoding',
}, },
{
'name': _(u'Filename'),
'attribute': 'filename',
},
{ {
'name': _(u'Comment'), 'name': _(u'Comment'),
'attribute': 'comment', 'attribute': 'comment',

View File

@@ -0,0 +1,82 @@
{% load i18n %}
<div class="content">
<h2 class="title">
{% include 'main/calculate_form_title.html' %}
</h2>
<div class="inner">
{% if is_multipart %}
<form enctype="multipart/form-data" method="{{ submit_method|default:'post' }}" action="{{ form_action }}" class="form">
{% else %}
<form method="{{ submit_method|default:'post' }}" action="{{ form_action }}" class="form">
{% endif %}
{% for form in forms %}
{% if submit_method != 'GET' and submit_method != 'get' %}
{% csrf_token %}
{% endif %}
{% if next %}
<input name="next" type="hidden" value="{{ next }}" />
{% endif %}
{% if previous %}
<input name="previous" type="hidden" value="{{ previous }}" />
{% endif %}
{% for hidden_field in hidden_fields %}
{{ hidden_field.as_hidden }}
{% endfor %}
{% if form.management_form %}
{% with form as formset %}
{{ formset.management_form }}
{% if form_display_mode_table %}
<table class="table">
<tbody>
<tr>
{% for field in formset.forms.0.visible_fields %}
<th>
{{ field.label_tag }}{% if field.field.required and not read_only %} ({% trans 'required' %}){% endif %}
</th>
{#{% if field.help_text %}<span class="description">{{ field.help_text }}</span>{% endif %}#}
{% endfor %}
</tr>
{% endif %}
{% for form in formset.forms %}
{% include 'main/generic_form_instance.html' %}
{% endfor %}
{% if form_display_mode_table %}
</tbody>
</table>
{% endif %}
{% endwith %}
{% else %}
{% include 'main/generic_form_instance.html' %}
{% endif %}
{% endfor %}
{% if not read_only %}
<div class="group navform wat-cf">
<button class="button" type="submit" name="submit">
{% if submit_icon_famfam %}
<span class="famfam active famfam-{{ submit_icon_famfam|default:'tick' }}"></span>
{% else %}
<img src="{{ STATIC_URL }}web_theme_media/images/icons/tick.png" alt="{% if submit_label %}{{ submit_label }}{% else %}{% if object %}{% trans 'Save' %}{% else %}{% trans 'Submit' %}{% endif %}{% endif %}" />
{% endif %}
{% if submit_label %}{{ submit_label }}{% else %}{% if object %}{% trans 'Save' %}{% else %}{% trans 'Submit' %}{% endif %}{% endif %}
</button>
{% if previous %}
<a href="#header" onclick='{% if previous %}window.location.replace("{{ previous }}");{% else %}history.go(-1);{% endif %}' class="button">
<img src="{{ STATIC_URL }}web_theme_media/images/icons/cross.png" alt="{% if cancel_label %}{{ cancel_label }}{% else %}{% trans 'Cancel' %}{% endif %}"/> {% if cancel_label %}{{ cancel_label }}{% else %}{% trans 'Cancel' %}{% endif %}
</a>
{% endif %}
</div>
{% endif %}
</form>
</div>
</div>

View File

@@ -14,15 +14,31 @@ from .models import (IMAPEmail, POP3Email, SourceTransformation,
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class StagingDocumentForm(DocumentForm): class NewDocumentForm(DocumentForm):
class Meta(DocumentForm.Meta):
exclude = ('label',)
class UploadBaseForm(forms.Form):
def __init__(self, *args, **kwargs):
show_expand = kwargs.pop('show_expand', False)
self.source = kwargs.pop('source')
super(UploadBaseForm, self).__init__(*args, **kwargs)
if show_expand:
self.fields['expand'] = forms.BooleanField(
label=_(u'Expand compressed files'), required=False,
help_text=ugettext(u'Upload a compressed file\'s contained files as individual documents')
)
class StagingUploadForm(UploadBaseForm):
""" """
Form that show all the files in the staging folder specified by the Form that show all the files in the staging folder specified by the
StagingFile class passed as 'cls' argument StagingFile class passed as 'cls' argument
""" """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
show_expand = kwargs.pop('show_expand', False) super(StagingUploadForm, self).__init__(*args, **kwargs)
self.source = kwargs.pop('source')
super(StagingDocumentForm, self).__init__(*args, **kwargs)
try: try:
self.fields['staging_file_id'].choices = [ self.fields['staging_file_id'].choices = [
@@ -32,12 +48,6 @@ class StagingDocumentForm(DocumentForm):
logger.error('exception: %s' % exception) logger.error('exception: %s' % exception)
pass pass
if show_expand:
self.fields['expand'] = forms.BooleanField(
label=_(u'Expand compressed files'), required=False,
help_text=ugettext(u'Upload a compressed file\'s contained files as individual documents')
)
# Put staging_list field first in the field order list # Put staging_list field first in the field order list
staging_list_index = self.fields.keyOrder.index('staging_file_id') staging_list_index = self.fields.keyOrder.index('staging_file_id')
staging_list = self.fields.keyOrder.pop(staging_list_index) staging_list = self.fields.keyOrder.pop(staging_list_index)
@@ -45,32 +55,17 @@ class StagingDocumentForm(DocumentForm):
staging_file_id = forms.ChoiceField(label=_(u'Staging file')) staging_file_id = forms.ChoiceField(label=_(u'Staging file'))
class Meta(DocumentForm.Meta):
exclude = ('description', 'file', 'document_type', 'tags')
class WebFormUploadForm(UploadBaseForm):
class WebFormForm(DocumentForm):
file = forms.FileField(label=_(u'File')) file = forms.FileField(label=_(u'File'))
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
show_expand = kwargs.pop('show_expand', False) super(WebFormUploadForm, self).__init__(*args, **kwargs)
self.source = kwargs.pop('source')
super(WebFormForm, self).__init__(*args, **kwargs)
if show_expand:
self.fields['expand'] = forms.BooleanField(
label=_(u'Expand compressed files'), required=False,
help_text=ugettext(u'Upload a compressed file\'s contained files as individual documents')
)
# Move the file filed to the top # Move the file filed to the top
self.fields.keyOrder.remove('file') self.fields.keyOrder.remove('file')
self.fields.keyOrder.insert(0, 'file') self.fields.keyOrder.insert(0, 'file')
def clean_file(self):
data = self.cleaned_data['file']
return data
class WebFormSetupForm(forms.ModelForm): class WebFormSetupForm(forms.ModelForm):
class Meta: class Meta:

View File

@@ -59,7 +59,7 @@ class Source(models.Model):
def get_transformation_list(self): def get_transformation_list(self):
return SourceTransformation.transformations.get_for_object_as_list(self) return SourceTransformation.transformations.get_for_object_as_list(self)
def upload_file(self, file_object, filename=None, use_file_name=False, document_type=None, expand=False, metadata_dict_list=None, user=None, document=None, new_version_data=None, command_line=False, description=None, language=None): def upload_document(self, file_object, document_type, label, expand=False, metadata_dict_list=None, user=None, document=None, command_line=False, description=None, language=None):
is_compressed = None is_compressed = None
if expand: if expand:
@@ -69,7 +69,7 @@ class Source(models.Model):
for fp in cf.children(): for fp in cf.children():
if command_line: if command_line:
print 'Uploading file #%d: %s' % (count, fp) print 'Uploading file #%d: %s' % (count, fp)
self.upload_single_file(file_object=fp, filename=None, document_type=document_type, metadata_dict_list=metadata_dict_list, user=user, description=description, language=language) self.upload_single_document(file_object=fp, label=unicode(fp), document_type=document_type, metadata_dict_list=metadata_dict_list, user=user, description=description, language=language)
fp.close() fp.close()
count += 1 count += 1
@@ -78,57 +78,29 @@ class Source(models.Model):
logging.debug('Exception: NotACompressedFile') logging.debug('Exception: NotACompressedFile')
if command_line: if command_line:
raise raise
self.upload_single_file(file_object=file_object, filename=filename, document_type=document_type, metadata_dict_list=metadata_dict_list, user=user, description=description, language=language) self.upload_single_document(file_object=file_object, label=label, document_type=document_type, metadata_dict_list=metadata_dict_list, user=user, description=description, language=language)
else: else:
is_compressed = True is_compressed = True
else: else:
self.upload_single_file(file_object, filename, use_file_name, document_type, metadata_dict_list, user, document, new_version_data, description=description, language=language) self.upload_single_document(file_object=file_object, label=label, document_type=document_type, metadata_dict_list=metadata_dict_list, user=user, description=description, language=language)
file_object.close() file_object.close()
return {'is_compressed': is_compressed}
@transaction.atomic def upload_single_document(self, file_object, label, document_type, metadata_dict_list=None, user=None, description=None, language=None):
def upload_single_file(self, file_object, filename=None, use_file_name=False, document_type=None, metadata_dict_list=None, user=None, document=None, new_version_data=None, description=None, language=None): new_version = Document.objects.new_document(file_object=file_object, document_type=document_type, label=label, description=description, language=language, user=user)
new_document = not document
if new_document: transformations, errors = self.get_transformation_list()
document = Document() new_version.apply_default_transformations(transformations)
if document_type:
document.document_type = document_type
if description: if metadata_dict_list:
document.description = description save_metadata_list(metadata_dict_list, document, create=True)
if language:
document.language = language
document.save(user=user)
else:
if use_file_name:
filename = None
else:
filename = filename if filename else document.latest_version.filename
if description:
document.description = description
document.save()
def upload_new_version(self, file_object, document, user, new_version_data=None, comment=None):
if not new_version_data: if not new_version_data:
new_version_data = {} new_version_data = {}
new_version = document.new_version(file=file_object, user=user, **new_version_data)
if filename:
document.rename(filename)
transformations, errors = self.get_transformation_list()
new_version.apply_default_transformations(transformations)
# TODO: new HISTORY for version updates # TODO: new HISTORY for version updates
new_version = document.new_version(file=file_object, user=user, **new_version_data)
if metadata_dict_list and new_document:
# Only do for new documents
save_metadata_list(metadata_dict_list, document, create=True)
class Meta: class Meta:
ordering = ('title',) ordering = ('title',)
@@ -137,6 +109,8 @@ class Source(models.Model):
class InteractiveSource(Source): class InteractiveSource(Source):
objects = InheritanceManager()
class Meta: class Meta:
verbose_name = _(u'Interactive source') verbose_name = _(u'Interactive source')
verbose_name_plural = _(u'Interactive sources') verbose_name_plural = _(u'Interactive sources')

View File

@@ -21,18 +21,9 @@ def task_check_interval_source(source_id):
@app.task(ignore_result=True) @app.task(ignore_result=True)
def task_upload_document(source_id, file_path, filename=None, use_file_name=False, document_type_id=None, expand=False, metadata_dict_list=None, user_id=None, document_id=None, new_version_data=None, command_line=False, description=None, language=None): def task_upload_document(source_id, file_path, label, document_type_id=None, expand=False, metadata_dict_list=None, user_id=None, command_line=False, description=None, language=None):
source = Source.objects.get_subclass(pk=source_id) source = Source.objects.get_subclass(pk=source_id)
document_type = DocumentType.objects.get(pk=document_type_id)
if document_type_id:
document_type = DocumentType.objects.get(pk=document_type_id)
else:
document_type = None
if document_id:
document = Document.objects.get(pk=document_id)
else:
document = None
if user_id: if user_id:
user = User.objects.get(pk=user_id) user = User.objects.get(pk=user_id)
@@ -41,7 +32,7 @@ def task_upload_document(source_id, file_path, filename=None, use_file_name=Fals
with File(file=open(file_path, mode='rb')) as file_object: with File(file=open(file_path, mode='rb')) as file_object:
#try: #try:
result = source.upload_file(file_object, filename=filename, use_file_name=use_file_name, document_type=document_type, expand=expand, metadata_dict_list=metadata_dict_list, user=user, document=document, new_version_data=new_version_data, command_line=command_line, description=description, language=language) result = source.upload_document(file_object, label=label, document_type=document_type, expand=expand, metadata_dict_list=metadata_dict_list, user=user, command_line=command_line, description=description, language=language)
#except NewDocumentVersionNotAllowed: #except NewDocumentVersionNotAllowed:
# messages.error(request, _(u'New version uploads are not allowed for this document.')) # messages.error(request, _(u'New version uploads are not allowed for this document.'))

View File

@@ -13,8 +13,8 @@ urlpatterns = patterns('sources.views',
url(r'^upload/document/new/interactive/(?P<source_id>\d+)/$', 'upload_interactive', (), 'upload_interactive'), url(r'^upload/document/new/interactive/(?P<source_id>\d+)/$', 'upload_interactive', (), 'upload_interactive'),
url(r'^upload/document/new/interactive/$', 'upload_interactive', (), 'upload_interactive'), url(r'^upload/document/new/interactive/$', 'upload_interactive', (), 'upload_interactive'),
url(r'^upload/document/(?P<document_pk>\d+)/version/interactive/(?P<source_id>\d+)/$', 'upload_interactive', (), 'upload_version'), url(r'^upload/document/(?P<document_pk>\d+)/version/interactive/(?P<source_id>\d+)/$', 'upload_new_version', (), 'upload_version'),
url(r'^upload/document/(?P<document_pk>\d+)/version/interactive/$', 'upload_interactive', (), 'upload_version'), url(r'^upload/document/(?P<document_pk>\d+)/version/interactive/$', 'upload_new_version', (), 'upload_version'),
# Setup views # Setup views

View File

@@ -1,6 +1,6 @@
from .forms import (POP3EmailSetupForm, IMAPEmailSetupForm, from .forms import (POP3EmailSetupForm, IMAPEmailSetupForm,
StagingFolderSetupForm, WatchFolderSetupForm, StagingFolderSetupForm, StagingUploadForm,
WebFormSetupForm) WatchFolderSetupForm, WebFormSetupForm, WebFormUploadForm)
from .literals import (SOURCE_CHOICE_EMAIL_IMAP, SOURCE_CHOICE_EMAIL_POP3, from .literals import (SOURCE_CHOICE_EMAIL_IMAP, SOURCE_CHOICE_EMAIL_POP3,
SOURCE_CHOICE_STAGING, SOURCE_CHOICE_WATCH, SOURCE_CHOICE_STAGING, SOURCE_CHOICE_WATCH,
SOURCE_CHOICE_WEB_FORM) SOURCE_CHOICE_WEB_FORM)
@@ -32,3 +32,10 @@ def get_form_class(source_type):
return POP3EmailSetupForm return POP3EmailSetupForm
elif source_type == SOURCE_CHOICE_EMAIL_IMAP: elif source_type == SOURCE_CHOICE_EMAIL_IMAP:
return IMAPEmailSetupForm return IMAPEmailSetupForm
def get_upload_form_class(source_type):
if source_type == SOURCE_CHOICE_WEB_FORM:
return WebFormUploadForm
elif source_type == SOURCE_CHOICE_STAGING:
return StagingUploadForm

View File

@@ -23,8 +23,8 @@ from documents.permissions import (PERMISSION_DOCUMENT_CREATE,
from metadata.api import decode_metadata_from_url, metadata_repr_as_list from metadata.api import decode_metadata_from_url, metadata_repr_as_list
from permissions.models import Permission from permissions.models import Permission
from .forms import (StagingDocumentForm, SourceTransformationForm, from .forms import (NewDocumentForm, SourceTransformationForm,
SourceTransformationForm_create, WebFormForm) SourceTransformationForm_create)
from .literals import (SOURCE_CHOICE_STAGING, SOURCE_CHOICE_WEB_FORM, from .literals import (SOURCE_CHOICE_STAGING, SOURCE_CHOICE_WEB_FORM,
SOURCE_UNCOMPRESS_CHOICE_ASK, SOURCE_UNCOMPRESS_CHOICE_Y) SOURCE_UNCOMPRESS_CHOICE_ASK, SOURCE_UNCOMPRESS_CHOICE_Y)
from .models import (InteractiveSource, Source, StagingFolderSource, from .models import (InteractiveSource, Source, StagingFolderSource,
@@ -34,7 +34,7 @@ from .permissions import (PERMISSION_SOURCES_SETUP_CREATE,
PERMISSION_SOURCES_SETUP_EDIT, PERMISSION_SOURCES_SETUP_EDIT,
PERMISSION_SOURCES_SETUP_VIEW) PERMISSION_SOURCES_SETUP_VIEW)
from .tasks import task_upload_document from .tasks import task_upload_document
from .utils import get_class, get_form_class from .utils import get_class, get_form_class, get_upload_form_class
def document_create_siblings(request, document_id): def document_create_siblings(request, document_id):
@@ -46,8 +46,7 @@ def document_create_siblings(request, document_id):
query_dict['metadata%s_id' % pk] = metadata.metadata_type_id query_dict['metadata%s_id' % pk] = metadata.metadata_type_id
query_dict['metadata%s_value' % pk] = metadata.value query_dict['metadata%s_value' % pk] = metadata.value
if document.document_type_id: query_dict['document_type_id'] = document.document_type_id
query_dict['document_type_id'] = document.document_type_id
url = reverse('sources:upload_interactive') url = reverse('sources:upload_interactive')
return HttpResponseRedirect('%s?%s' % (url, urlencode(query_dict))) return HttpResponseRedirect('%s?%s' % (url, urlencode(query_dict)))
@@ -92,25 +91,33 @@ def get_active_tab_links(document=None):
} }
def upload_interactive(request, source_id=None, document_pk=None): def upload_new_version(request, document_id):
document = get_object_or_404(Document, pk=document_pk)
# try:
# Permission.objects.check_permissions(request.user, [PERMISSION_DOCUMENT_NEW_VERSION])
# except PermissionDenied:
# AccessEntry.objects.check_access(PERMISSION_DOCUMENT_NEW_VERSION, request.user, document)
# results = get_active_tab_links(document)
# messages.success(request, _(u'New document version queued for uploaded and will be available shortly.'))
# return HttpResponseRedirect(reverse('documents:document_version_list', args=[document.pk]))
# title = _(u'Upload a new version from source: %s') % source.title
# TODO: move to version upload
#if document:
#context['object'] = document
pass
def upload_interactive(request, source_id=None):
subtemplates_list = [] subtemplates_list = []
if document_pk: Permission.objects.check_permissions(request.user, [PERMISSION_DOCUMENT_CREATE])
document = get_object_or_404(Document, pk=document_pk) results = get_active_tab_links()
try:
Permission.objects.check_permissions(request.user, [PERMISSION_DOCUMENT_NEW_VERSION])
except PermissionDenied:
AccessEntry.objects.check_access(PERMISSION_DOCUMENT_NEW_VERSION, request.user, document)
results = get_active_tab_links(document)
else:
Permission.objects.check_permissions(request.user, [PERMISSION_DOCUMENT_CREATE])
document = None
results = get_active_tab_links()
context = {} context = {}
if InteractiveSource.objects.count() == 0: if InteractiveSource.objects.filter(enabled=True).count() == 0:
subtemplates_list.append( subtemplates_list.append(
{ {
'name': 'main/generic_subtemplate.html', 'name': 'main/generic_subtemplate.html',
@@ -122,69 +129,48 @@ def upload_interactive(request, source_id=None, document_pk=None):
} }
}) })
document_type_id = request.GET.get('document_type_id', None) document_type = get_object_or_404(DocumentType, pk=request.GET['document_type_id'])
if document_type_id:
document_type = get_object_or_404(DocumentType, pk=document_type_id)
else:
document_type = None
# TODO: Use InteractiveSource subclasses query
if source_id is None:
if results[SOURCE_CHOICE_WEB_FORM].count():
source_id = results[SOURCE_CHOICE_WEB_FORM][0].pk
elif results[SOURCE_CHOICE_STAGING].count():
source_id = results[SOURCE_CHOICE_STAGING][0].pk
if source_id: if source_id:
source = get_object_or_404(Source.objects.select_subclasses(), pk=source_id) source = get_object_or_404(Source.objects.filter(enabled=True).select_subclasses(), pk=source_id)
if isinstance(source, WebFormSource): else:
form_class = WebFormForm source = InteractiveSource.objects.filter(enabled=True).select_subclasses().first()
else:
form_class = StagingDocumentForm if source:
upload_form_class = get_upload_form_class(source.source_type)
context['source'] = source context['source'] = source
if request.method == 'POST': if request.method == 'POST':
form = form_class( upload_form = upload_form_class(
request.POST, request.FILES, request.POST, request.FILES,
document_type=document_type, show_expand=(source.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK),
show_expand=(source.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK) and not document,
source=source, source=source,
instance=document prefix='source'
) )
document_form = NewDocumentForm(
data=request.POST,
document_type=document_type,
prefix='document')
if form.is_valid(): if upload_form.is_valid() and document_form.is_valid():
try: try:
if document: if source.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK:
expand = False expand = upload_form.cleaned_data.get('expand')
else: else:
if source.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK: if source.uncompress == SOURCE_UNCOMPRESS_CHOICE_Y:
expand = form.cleaned_data.get('expand') expand = True
else: else:
if source.uncompress == SOURCE_UNCOMPRESS_CHOICE_Y: expand = False
expand = True
else:
expand = False
new_filename = get_form_filename(form)
# TODO: move this to model: "get_file_object(request, upload_form)"
if isinstance(source, WebFormSource): if isinstance(source, WebFormSource):
file_object = request.FILES['file']
staging_file = None staging_file = None
file_object = request.FILES['source-file']
else: else:
staging_file = source.get_file(encoded_filename=form.cleaned_data['staging_file_id']) staging_file = source.get_file(encoded_filename=upload_form.cleaned_data['staging_file_id'])
file_object = staging_file.as_file() file_object = staging_file.as_file()
if document_type:
document_type_id = document_type.pk
else:
document_type_id = None
if document:
document_id = document.pk
else:
document_id = None
temporary_file = tempfile.NamedTemporaryFile(delete=False) temporary_file = tempfile.NamedTemporaryFile(delete=False)
for chunk in file_object.chunks(): for chunk in file_object.chunks():
temporary_file.write(chunk) temporary_file.write(chunk)
@@ -192,62 +178,55 @@ def upload_interactive(request, source_id=None, document_pk=None):
temporary_file.close() temporary_file.close()
file_object.close() file_object.close()
# TODO: move this to model: "post_upload(request)"
if isinstance(source, StagingFolderSource): if isinstance(source, StagingFolderSource):
if source.delete_after_upload: if source.delete_after_upload:
staging_file.delete() try:
staging_file.delete()
except Exception as exception:
messages.error(request, _(u'Error deleting staging file; %s') % exception)
if not request.user.is_anonymous(): if not request.user.is_anonymous():
user_id = request.user.pk user_id = request.user.pk
else: else:
user_id = None user_id = None
# Determine how to name the new document
label = file_object.name
if 'document_type_available_filenames' in document_form.cleaned_data:
if document_form.cleaned_data['document_type_available_filenames']:
label = document_form.cleaned_data['document_type_available_filenames'].filename
task_upload_document.apply_async(kwargs=dict( task_upload_document.apply_async(kwargs=dict(
source_id=source.pk, source_id=source.pk,
file_path=temporary_file.name, file_path=temporary_file.name,
filename=new_filename or file_object.name, label=label,
use_file_name=form.cleaned_data.get('use_file_name', False), document_type_id=document_type.pk,
document_type_id=document_type_id,
expand=expand, expand=expand,
metadata_dict_list=decode_metadata_from_url(request.GET), metadata_dict_list=decode_metadata_from_url(request.GET),
user_id=user_id, user_id=user_id,
document_id=document_id, description=document_form.cleaned_data.get('description'),
new_version_data=form.cleaned_data.get('new_version_data'), language=document_form.cleaned_data.get('language')
description=form.cleaned_data.get('description'),
language=form.cleaned_data.get('language')
), queue='uploads') ), queue='uploads')
# TODO: Notify user messages.success(request, _(u'New document queued for uploaded and will be available shortly.'))
if document: return HttpResponseRedirect(request.get_full_path())
messages.success(request, _(u'New document queued for uploaded and will be available shortly.'))
return HttpResponseRedirect(reverse('documents:document_version_list', args=[document.pk]))
else:
messages.success(request, _(u'New document version queued for uploaded and will be available shortly.'))
return HttpResponseRedirect(request.get_full_path())
except Exception as exception: except Exception as exception:
if settings.DEBUG: if settings.DEBUG:
raise raise
messages.error(request, _(u'Unhandled exception: %s') % exception) messages.error(request, _(u'Unhandled exception: %s') % exception)
else: else:
form = form_class( upload_form = upload_form_class(
show_expand=(source.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK) and not document, show_expand=(source.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK),
document_type=document_type,
source=source, source=source,
instance=document prefix='source',
) )
document_form = NewDocumentForm(
document_type=document_type,
prefix='document')
if document: title = _(u'Upload a local document from source: %s') % source.title
title = _(u'Upload a new version from source: %s') % source.title
else:
title = _(u'Upload a local document from source: %s') % source.title
subtemplates_list.append({
'name': 'main/generic_form_subtemplate.html',
'context': {
'form': form,
'title': title,
},
})
if isinstance(source, StagingFolderSource): if isinstance(source, StagingFolderSource):
try: try:
@@ -258,9 +237,9 @@ def upload_interactive(request, source_id=None, document_pk=None):
finally: finally:
subtemplates_list = [ subtemplates_list = [
{ {
'name': 'main/generic_form_subtemplate.html', 'name': 'main/generic_multiform_subtemplate.html',
'context': { 'context': {
'form': form, 'forms': [upload_form, document_form],
'title': title, 'title': title,
} }
}, },
@@ -273,12 +252,18 @@ def upload_interactive(request, source_id=None, document_pk=None):
} }
}, },
] ]
else:
if document: subtemplates_list.append({
context['object'] = document 'name': 'main/generic_multiform_subtemplate.html',
'context': {
'forms': [upload_form, document_form],
'title': title,
'is_multipart': True
},
})
context.update({ context.update({
'document_type_id': document_type_id, 'document_type_id': document_type.pk,
'subtemplates_list': subtemplates_list, 'subtemplates_list': subtemplates_list,
'temporary_navigation_links': { 'temporary_navigation_links': {
'form_header': { 'form_header': {
@@ -292,47 +277,33 @@ def upload_interactive(request, source_id=None, document_pk=None):
}, },
}) })
if not document: context.update(
context.update( {
{ 'sidebar_subtemplates_list': [
'sidebar_subtemplates_list': [ {
{ 'name': 'main/generic_subtemplate.html',
'name': 'main/generic_subtemplate.html', 'context': {
'context': { 'title': _(u'Current document type'),
'title': _(u'Current document type'), 'paragraphs': [document_type if document_type else _(u'None')],
'paragraphs': [document_type if document_type else _(u'None')], 'side_bar': True,
'side_bar': True,
}
},
{
'name': 'main/generic_subtemplate.html',
'context': {
'title': _(u'Current metadata'),
'paragraphs': metadata_repr_as_list(decode_metadata_from_url(request.GET)),
'side_bar': True,
}
} }
], },
} {
) 'name': 'main/generic_subtemplate.html',
'context': {
'title': _(u'Current metadata'),
'paragraphs': metadata_repr_as_list(decode_metadata_from_url(request.GET)),
'side_bar': True,
}
}
],
}
)
return render_to_response('main/generic_form.html', context, return render_to_response('main/generic_form.html', context,
context_instance=RequestContext(request)) context_instance=RequestContext(request))
def get_form_filename(form):
filename = None
if form:
if form.cleaned_data['new_filename']:
return form.cleaned_data['new_filename']
if form and 'document_type_available_filenames' in form.cleaned_data:
if form.cleaned_data['document_type_available_filenames']:
return form.cleaned_data['document_type_available_filenames'].filename
return filename
def staging_file_delete(request, staging_folder_pk, encoded_filename): def staging_file_delete(request, staging_folder_pk, encoded_filename):
Permission.objects.check_permissions(request.user, [PERMISSION_DOCUMENT_CREATE, PERMISSION_DOCUMENT_NEW_VERSION]) Permission.objects.check_permissions(request.user, [PERMISSION_DOCUMENT_CREATE, PERMISSION_DOCUMENT_NEW_VERSION])
staging_folder = get_object_or_404(StagingFolderSource, pk=staging_folder_pk) staging_folder = get_object_or_404(StagingFolderSource, pk=staging_folder_pk)