Added support for filerenaming on upload
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from models import MetadataType, DocumentType, Document, \
|
||||
DocumentTypeMetadataType, DocumentMetadata
|
||||
DocumentTypeMetadataType, DocumentMetadata, DocumentTypeFilename
|
||||
|
||||
|
||||
class MetadataTypeAdmin(admin.ModelAdmin):
|
||||
@@ -15,8 +15,15 @@ class DocumentTypeMetadataTypeInline(admin.StackedInline):
|
||||
allow_add = True
|
||||
|
||||
|
||||
class DocumentTypeFilenameInline(admin.StackedInline):
|
||||
model = DocumentTypeFilename
|
||||
extra = 1
|
||||
classes = ('collapse-open',)
|
||||
allow_add = True
|
||||
|
||||
|
||||
class DocumentTypeAdmin(admin.ModelAdmin):
|
||||
inlines = [DocumentTypeMetadataTypeInline]
|
||||
inlines = [DocumentTypeMetadataTypeInline, DocumentTypeFilenameInline]
|
||||
|
||||
|
||||
class DocumentMetadataInline(admin.StackedInline):
|
||||
|
||||
@@ -20,6 +20,12 @@ class DocumentForm(forms.ModelForm):
|
||||
if 'initial' in kwargs:
|
||||
if 'document_type' in kwargs['initial']:
|
||||
self.fields['document_type'].widget = forms.HiddenInput()
|
||||
if kwargs['initial']['document_type'].documenttypefilename_set.all().count() > 0:
|
||||
self.fields['new_filename'] = forms.ModelChoiceField(
|
||||
queryset=kwargs['initial']['document_type'].documenttypefilename_set.all(),
|
||||
required=False,
|
||||
label=_(u'Rename file'))
|
||||
|
||||
|
||||
class Meta:
|
||||
model = Document
|
||||
|
||||
@@ -134,4 +134,16 @@ class DocumentMetadata(models.Model):
|
||||
verbose_name_plural = _(u'document metadata')
|
||||
|
||||
|
||||
class DocumentTypeFilename(models.Model):
|
||||
document_type = models.ForeignKey(DocumentType, verbose_name=_(u'document type'))
|
||||
filename = models.CharField(max_length=64, verbose_name=_('filename'))
|
||||
|
||||
def __unicode__(self):
|
||||
return self.filename
|
||||
|
||||
class Meta:
|
||||
verbose_name = _(u'document type filename')
|
||||
verbose_name_plural = _(u'document types filenames')
|
||||
|
||||
|
||||
register(Document, _(u'document'), ['document_type__name', 'file_mimetype', 'file_filename', 'file_extension', 'documentmetadata__value'])
|
||||
|
||||
@@ -47,6 +47,11 @@ def upload_document_with_type(request, document_type_id, multiple=True):
|
||||
form = DocumentForm(request.POST, request.FILES, initial={'document_type':document_type})
|
||||
if form.is_valid():
|
||||
instance = form.save()
|
||||
if 'new_filename' in form.cleaned_data:
|
||||
if form.cleaned_data['new_filename']:
|
||||
instance.file_filename = form.cleaned_data['new_filename'].filename
|
||||
instance.save()
|
||||
|
||||
for key, value in request.GET.items():
|
||||
document_metadata = DocumentMetadata(
|
||||
document=instance,
|
||||
@@ -72,7 +77,6 @@ def upload_document_with_type(request, document_type_id, multiple=True):
|
||||
def document_view(request, document_id):
|
||||
document = get_object_or_404(Document, pk=document_id)
|
||||
form = DocumentForm_view(instance=document, extra_fields=[
|
||||
{'label':_(u'Document type'), 'field':'document_type'},
|
||||
{'label':_(u'Filename'), 'field':'file_filename'},
|
||||
{'label':_(u'File extension'), 'field':'file_extension'},
|
||||
{'label':_(u'File mimetype'), 'field':'file_mimetype'},
|
||||
|
||||
Reference in New Issue
Block a user