Initial commit of the sources upload, new version and new document refactor
This commit is contained in:
@@ -94,15 +94,9 @@ class DocumentPreviewForm(forms.Form):
|
||||
|
||||
|
||||
class DocumentForm(forms.ModelForm):
|
||||
"""
|
||||
Baseform for document creation, and editing, made generic enough to
|
||||
be used by document creation from staging files
|
||||
"""
|
||||
class Meta:
|
||||
model = Document
|
||||
# TODO: Don't use exclude here, use fields, this app shouldn't know
|
||||
# anything about tags
|
||||
exclude = ('tags', 'document_type')
|
||||
fields = ('label', 'description', 'language')
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
document_type = kwargs.pop('document_type', None)
|
||||
@@ -110,22 +104,6 @@ class DocumentForm(forms.ModelForm):
|
||||
|
||||
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:
|
||||
filenames_qs = document_type.documenttypefilename_set.filter(enabled=True)
|
||||
if filenames_qs.count() > 0:
|
||||
@@ -134,55 +112,26 @@ class DocumentForm(forms.ModelForm):
|
||||
required=False,
|
||||
label=_(u'Quick document rename'))
|
||||
|
||||
if instance:
|
||||
if instance.latest_version:
|
||||
self.version_fields(instance)
|
||||
|
||||
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
|
||||
|
||||
|
||||
class DocumentForm_edit(DocumentForm):
|
||||
# TODO: merge DocumentForm and DocumentForm_edit
|
||||
class DocumentForm_edit(forms.ModelForm):
|
||||
"""
|
||||
Form sub classes from DocumentForm used only when editing a document
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
model = Document
|
||||
exclude = ('file', 'document_type', 'tags')
|
||||
fields = ('label', 'description', 'language')
|
||||
|
||||
def __init__(self, *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):
|
||||
@@ -270,3 +219,36 @@ class DocumentDownloadForm(forms.Form):
|
||||
if len(self.document_versions) > 1:
|
||||
self.fields['compressed'].initial = 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
|
||||
'''
|
||||
|
||||
@@ -114,7 +114,6 @@ def document_view(request, document_id, advanced=False):
|
||||
]
|
||||
if document.latest_version:
|
||||
document_fields.extend([
|
||||
{'label': _(u'Filename'), 'field': 'filename'},
|
||||
{'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 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)
|
||||
|
||||
if request.method == 'POST':
|
||||
old_document = copy.copy(document)
|
||||
form = DocumentForm_edit(request.POST, instance=document)
|
||||
if form.is_valid():
|
||||
document.filename = form.cleaned_data['new_filename']
|
||||
document.label = form.cleaned_data['label']
|
||||
document.description = form.cleaned_data['description']
|
||||
document.language = form.cleaned_data['language']
|
||||
|
||||
if 'document_type_available_filenames' in form.cleaned_data:
|
||||
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()
|
||||
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)
|
||||
|
||||
messages.success(request, _(u'Document "%s" edited successfully.') % document)
|
||||
|
||||
return HttpResponseRedirect(document.get_absolute_url())
|
||||
else:
|
||||
if document.latest_version:
|
||||
form = DocumentForm_edit(instance=document, initial={
|
||||
'new_filename': document.filename, 'description': document.description})
|
||||
else:
|
||||
form = DocumentForm_edit(instance=document, initial={
|
||||
'description': document.description})
|
||||
form = DocumentForm_edit(instance=document)
|
||||
|
||||
return render_to_response('main/generic_form.html', {
|
||||
'form': form,
|
||||
@@ -335,7 +328,7 @@ def document_download(request, document_id=None, document_id_list=None, document
|
||||
compressed_file = CompressedFile()
|
||||
for document_version in document_versions:
|
||||
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()
|
||||
|
||||
compressed_file.close()
|
||||
@@ -361,7 +354,7 @@ def document_download(request, document_id=None, document_id_list=None, document
|
||||
return serve_file(
|
||||
request,
|
||||
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'
|
||||
)
|
||||
except Exception as exception:
|
||||
@@ -1111,10 +1104,6 @@ def document_version_list(request, document_pk):
|
||||
'name': _(u'Encoding'),
|
||||
'attribute': 'encoding',
|
||||
},
|
||||
{
|
||||
'name': _(u'Filename'),
|
||||
'attribute': 'filename',
|
||||
},
|
||||
{
|
||||
'name': _(u'Comment'),
|
||||
'attribute': 'comment',
|
||||
|
||||
@@ -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>
|
||||
@@ -14,15 +14,31 @@ from .models import (IMAPEmail, POP3Email, SourceTransformation,
|
||||
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
|
||||
StagingFile class passed as 'cls' argument
|
||||
"""
|
||||
def __init__(self, *args, **kwargs):
|
||||
show_expand = kwargs.pop('show_expand', False)
|
||||
self.source = kwargs.pop('source')
|
||||
super(StagingDocumentForm, self).__init__(*args, **kwargs)
|
||||
super(StagingUploadForm, self).__init__(*args, **kwargs)
|
||||
|
||||
try:
|
||||
self.fields['staging_file_id'].choices = [
|
||||
@@ -32,12 +48,6 @@ class StagingDocumentForm(DocumentForm):
|
||||
logger.error('exception: %s' % exception)
|
||||
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
|
||||
staging_list_index = self.fields.keyOrder.index('staging_file_id')
|
||||
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'))
|
||||
|
||||
class Meta(DocumentForm.Meta):
|
||||
exclude = ('description', 'file', 'document_type', 'tags')
|
||||
|
||||
|
||||
class WebFormForm(DocumentForm):
|
||||
class WebFormUploadForm(UploadBaseForm):
|
||||
file = forms.FileField(label=_(u'File'))
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
show_expand = kwargs.pop('show_expand', False)
|
||||
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')
|
||||
)
|
||||
super(WebFormUploadForm, self).__init__(*args, **kwargs)
|
||||
|
||||
# Move the file filed to the top
|
||||
self.fields.keyOrder.remove('file')
|
||||
self.fields.keyOrder.insert(0, 'file')
|
||||
|
||||
def clean_file(self):
|
||||
data = self.cleaned_data['file']
|
||||
return data
|
||||
|
||||
|
||||
class WebFormSetupForm(forms.ModelForm):
|
||||
class Meta:
|
||||
|
||||
@@ -59,7 +59,7 @@ class Source(models.Model):
|
||||
def get_transformation_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
|
||||
|
||||
if expand:
|
||||
@@ -69,7 +69,7 @@ class Source(models.Model):
|
||||
for fp in cf.children():
|
||||
if command_line:
|
||||
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()
|
||||
count += 1
|
||||
|
||||
@@ -78,57 +78,29 @@ class Source(models.Model):
|
||||
logging.debug('Exception: NotACompressedFile')
|
||||
if command_line:
|
||||
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:
|
||||
is_compressed = True
|
||||
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()
|
||||
return {'is_compressed': is_compressed}
|
||||
|
||||
@transaction.atomic
|
||||
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_document = not document
|
||||
def upload_single_document(self, file_object, label, document_type, metadata_dict_list=None, user=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)
|
||||
|
||||
if new_document:
|
||||
document = Document()
|
||||
if document_type:
|
||||
document.document_type = document_type
|
||||
transformations, errors = self.get_transformation_list()
|
||||
new_version.apply_default_transformations(transformations)
|
||||
|
||||
if description:
|
||||
document.description = description
|
||||
|
||||
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()
|
||||
if metadata_dict_list:
|
||||
save_metadata_list(metadata_dict_list, document, create=True)
|
||||
|
||||
def upload_new_version(self, file_object, document, user, new_version_data=None, comment=None):
|
||||
if not 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
|
||||
|
||||
if metadata_dict_list and new_document:
|
||||
# Only do for new documents
|
||||
save_metadata_list(metadata_dict_list, document, create=True)
|
||||
new_version = document.new_version(file=file_object, user=user, **new_version_data)
|
||||
|
||||
class Meta:
|
||||
ordering = ('title',)
|
||||
@@ -137,6 +109,8 @@ class Source(models.Model):
|
||||
|
||||
|
||||
class InteractiveSource(Source):
|
||||
objects = InheritanceManager()
|
||||
|
||||
class Meta:
|
||||
verbose_name = _(u'Interactive source')
|
||||
verbose_name_plural = _(u'Interactive sources')
|
||||
|
||||
@@ -21,18 +21,9 @@ def task_check_interval_source(source_id):
|
||||
|
||||
|
||||
@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)
|
||||
|
||||
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:
|
||||
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:
|
||||
#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:
|
||||
# messages.error(request, _(u'New version uploads are not allowed for this document.'))
|
||||
|
||||
|
||||
@@ -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/$', '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/$', '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_new_version', (), 'upload_version'),
|
||||
|
||||
# Setup views
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from .forms import (POP3EmailSetupForm, IMAPEmailSetupForm,
|
||||
StagingFolderSetupForm, WatchFolderSetupForm,
|
||||
WebFormSetupForm)
|
||||
StagingFolderSetupForm, StagingUploadForm,
|
||||
WatchFolderSetupForm, WebFormSetupForm, WebFormUploadForm)
|
||||
from .literals import (SOURCE_CHOICE_EMAIL_IMAP, SOURCE_CHOICE_EMAIL_POP3,
|
||||
SOURCE_CHOICE_STAGING, SOURCE_CHOICE_WATCH,
|
||||
SOURCE_CHOICE_WEB_FORM)
|
||||
@@ -32,3 +32,10 @@ def get_form_class(source_type):
|
||||
return POP3EmailSetupForm
|
||||
elif source_type == SOURCE_CHOICE_EMAIL_IMAP:
|
||||
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
|
||||
|
||||
@@ -23,8 +23,8 @@ from documents.permissions import (PERMISSION_DOCUMENT_CREATE,
|
||||
from metadata.api import decode_metadata_from_url, metadata_repr_as_list
|
||||
from permissions.models import Permission
|
||||
|
||||
from .forms import (StagingDocumentForm, SourceTransformationForm,
|
||||
SourceTransformationForm_create, WebFormForm)
|
||||
from .forms import (NewDocumentForm, SourceTransformationForm,
|
||||
SourceTransformationForm_create)
|
||||
from .literals import (SOURCE_CHOICE_STAGING, SOURCE_CHOICE_WEB_FORM,
|
||||
SOURCE_UNCOMPRESS_CHOICE_ASK, SOURCE_UNCOMPRESS_CHOICE_Y)
|
||||
from .models import (InteractiveSource, Source, StagingFolderSource,
|
||||
@@ -34,7 +34,7 @@ from .permissions import (PERMISSION_SOURCES_SETUP_CREATE,
|
||||
PERMISSION_SOURCES_SETUP_EDIT,
|
||||
PERMISSION_SOURCES_SETUP_VIEW)
|
||||
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):
|
||||
@@ -46,7 +46,6 @@ def document_create_siblings(request, document_id):
|
||||
query_dict['metadata%s_id' % pk] = metadata.metadata_type_id
|
||||
query_dict['metadata%s_value' % pk] = metadata.value
|
||||
|
||||
if document.document_type_id:
|
||||
query_dict['document_type_id'] = document.document_type_id
|
||||
|
||||
url = reverse('sources:upload_interactive')
|
||||
@@ -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 = []
|
||||
|
||||
if document_pk:
|
||||
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)
|
||||
else:
|
||||
Permission.objects.check_permissions(request.user, [PERMISSION_DOCUMENT_CREATE])
|
||||
document = None
|
||||
results = get_active_tab_links()
|
||||
|
||||
context = {}
|
||||
|
||||
if InteractiveSource.objects.count() == 0:
|
||||
if InteractiveSource.objects.filter(enabled=True).count() == 0:
|
||||
subtemplates_list.append(
|
||||
{
|
||||
'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)
|
||||
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
|
||||
document_type = get_object_or_404(DocumentType, pk=request.GET['document_type_id'])
|
||||
|
||||
if source_id:
|
||||
source = get_object_or_404(Source.objects.select_subclasses(), pk=source_id)
|
||||
if isinstance(source, WebFormSource):
|
||||
form_class = WebFormForm
|
||||
source = get_object_or_404(Source.objects.filter(enabled=True).select_subclasses(), pk=source_id)
|
||||
else:
|
||||
form_class = StagingDocumentForm
|
||||
source = InteractiveSource.objects.filter(enabled=True).select_subclasses().first()
|
||||
|
||||
if source:
|
||||
upload_form_class = get_upload_form_class(source.source_type)
|
||||
|
||||
context['source'] = source
|
||||
|
||||
if request.method == 'POST':
|
||||
form = form_class(
|
||||
upload_form = upload_form_class(
|
||||
request.POST, request.FILES,
|
||||
document_type=document_type,
|
||||
show_expand=(source.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK) and not document,
|
||||
show_expand=(source.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK),
|
||||
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:
|
||||
if document:
|
||||
expand = False
|
||||
else:
|
||||
if source.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK:
|
||||
expand = form.cleaned_data.get('expand')
|
||||
expand = upload_form.cleaned_data.get('expand')
|
||||
else:
|
||||
if source.uncompress == SOURCE_UNCOMPRESS_CHOICE_Y:
|
||||
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):
|
||||
file_object = request.FILES['file']
|
||||
staging_file = None
|
||||
file_object = request.FILES['source-file']
|
||||
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()
|
||||
|
||||
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)
|
||||
for chunk in file_object.chunks():
|
||||
temporary_file.write(chunk)
|
||||
@@ -192,63 +178,56 @@ def upload_interactive(request, source_id=None, document_pk=None):
|
||||
temporary_file.close()
|
||||
file_object.close()
|
||||
|
||||
# TODO: move this to model: "post_upload(request)"
|
||||
if isinstance(source, StagingFolderSource):
|
||||
if source.delete_after_upload:
|
||||
try:
|
||||
staging_file.delete()
|
||||
except Exception as exception:
|
||||
messages.error(request, _(u'Error deleting staging file; %s') % exception)
|
||||
|
||||
if not request.user.is_anonymous():
|
||||
user_id = request.user.pk
|
||||
else:
|
||||
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(
|
||||
source_id=source.pk,
|
||||
file_path=temporary_file.name,
|
||||
filename=new_filename or file_object.name,
|
||||
use_file_name=form.cleaned_data.get('use_file_name', False),
|
||||
document_type_id=document_type_id,
|
||||
label=label,
|
||||
document_type_id=document_type.pk,
|
||||
expand=expand,
|
||||
metadata_dict_list=decode_metadata_from_url(request.GET),
|
||||
user_id=user_id,
|
||||
document_id=document_id,
|
||||
new_version_data=form.cleaned_data.get('new_version_data'),
|
||||
description=form.cleaned_data.get('description'),
|
||||
language=form.cleaned_data.get('language')
|
||||
description=document_form.cleaned_data.get('description'),
|
||||
language=document_form.cleaned_data.get('language')
|
||||
), queue='uploads')
|
||||
|
||||
# TODO: Notify user
|
||||
if document:
|
||||
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:
|
||||
if settings.DEBUG:
|
||||
raise
|
||||
messages.error(request, _(u'Unhandled exception: %s') % exception)
|
||||
else:
|
||||
form = form_class(
|
||||
show_expand=(source.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK) and not document,
|
||||
document_type=document_type,
|
||||
upload_form = upload_form_class(
|
||||
show_expand=(source.uncompress == SOURCE_UNCOMPRESS_CHOICE_ASK),
|
||||
source=source,
|
||||
instance=document
|
||||
prefix='source',
|
||||
)
|
||||
document_form = NewDocumentForm(
|
||||
document_type=document_type,
|
||||
prefix='document')
|
||||
|
||||
if document:
|
||||
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):
|
||||
try:
|
||||
staging_filelist = list(source.get_files())
|
||||
@@ -258,9 +237,9 @@ def upload_interactive(request, source_id=None, document_pk=None):
|
||||
finally:
|
||||
subtemplates_list = [
|
||||
{
|
||||
'name': 'main/generic_form_subtemplate.html',
|
||||
'name': 'main/generic_multiform_subtemplate.html',
|
||||
'context': {
|
||||
'form': form,
|
||||
'forms': [upload_form, document_form],
|
||||
'title': title,
|
||||
}
|
||||
},
|
||||
@@ -273,12 +252,18 @@ def upload_interactive(request, source_id=None, document_pk=None):
|
||||
}
|
||||
},
|
||||
]
|
||||
|
||||
if document:
|
||||
context['object'] = document
|
||||
else:
|
||||
subtemplates_list.append({
|
||||
'name': 'main/generic_multiform_subtemplate.html',
|
||||
'context': {
|
||||
'forms': [upload_form, document_form],
|
||||
'title': title,
|
||||
'is_multipart': True
|
||||
},
|
||||
})
|
||||
|
||||
context.update({
|
||||
'document_type_id': document_type_id,
|
||||
'document_type_id': document_type.pk,
|
||||
'subtemplates_list': subtemplates_list,
|
||||
'temporary_navigation_links': {
|
||||
'form_header': {
|
||||
@@ -292,7 +277,6 @@ def upload_interactive(request, source_id=None, document_pk=None):
|
||||
},
|
||||
})
|
||||
|
||||
if not document:
|
||||
context.update(
|
||||
{
|
||||
'sidebar_subtemplates_list': [
|
||||
@@ -320,19 +304,6 @@ def upload_interactive(request, source_id=None, document_pk=None):
|
||||
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):
|
||||
Permission.objects.check_permissions(request.user, [PERMISSION_DOCUMENT_CREATE, PERMISSION_DOCUMENT_NEW_VERSION])
|
||||
staging_folder = get_object_or_404(StagingFolderSource, pk=staging_folder_pk)
|
||||
|
||||
Reference in New Issue
Block a user