Allow documents with no versions to exist, previously these were considered an anomaly and were unhandled
This commit is contained in:
@@ -32,19 +32,25 @@ class DocumentVersionSignatureManager(models.Manager):
|
||||
document_signature.save()
|
||||
|
||||
def has_detached_signature(self, document):
|
||||
document_signature = self.get_document_signature(document)
|
||||
|
||||
if document_signature.signature_file:
|
||||
return True
|
||||
else:
|
||||
try:
|
||||
document_signature = self.get_document_signature(document)
|
||||
except ValueError:
|
||||
return False
|
||||
else:
|
||||
if document_signature.signature_file:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def has_embedded_signature(self, document):
|
||||
logger.debug('document: %s' % document)
|
||||
|
||||
document_signature = self.get_document_signature(document)
|
||||
|
||||
return document_signature.has_embedded_signature
|
||||
try:
|
||||
document_signature = self.get_document_signature(document)
|
||||
except ValueError:
|
||||
return False
|
||||
else:
|
||||
return document_signature.has_embedded_signature
|
||||
|
||||
def detached_signature(self, document):
|
||||
document_signature = self.get_document_signature(document)
|
||||
|
||||
@@ -15,7 +15,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from acls.models import AccessEntry
|
||||
from filetransfers.api import serve_file
|
||||
from django_gpg.api import SIGNATURE_STATES
|
||||
from django_gpg.api import SIGNATURE_STATE_NONE, SIGNATURE_STATES
|
||||
from documents.models import Document, RecentDocument
|
||||
from permissions.models import Permission
|
||||
|
||||
@@ -37,9 +37,13 @@ def document_verify(request, document_pk):
|
||||
|
||||
RecentDocument.objects.add_document_for_user(request.user, document)
|
||||
|
||||
signature = DocumentVersionSignature.objects.verify_signature(document)
|
||||
|
||||
signature_state = SIGNATURE_STATES.get(getattr(signature, 'status', None))
|
||||
try:
|
||||
signature = DocumentVersionSignature.objects.verify_signature(document)
|
||||
except AttributeError:
|
||||
signature_state = SIGNATURE_STATES.get(SIGNATURE_STATE_NONE)
|
||||
signature = None
|
||||
else:
|
||||
signature_state = SIGNATURE_STATES.get(getattr(signature, 'status', None))
|
||||
|
||||
widget = (u'<img style="vertical-align: middle;" src="%simages/icons/%s" />' % (settings.STATIC_URL, signature_state['icon']))
|
||||
paragraphs = [
|
||||
@@ -49,10 +53,13 @@ def document_verify(request, document_pk):
|
||||
},
|
||||
]
|
||||
|
||||
if DocumentVersionSignature.objects.has_embedded_signature(document):
|
||||
signature_type = _(u'embedded')
|
||||
else:
|
||||
signature_type = _(u'detached')
|
||||
try:
|
||||
if DocumentVersionSignature.objects.has_embedded_signature(document):
|
||||
signature_type = _(u'embedded')
|
||||
else:
|
||||
signature_type = _(u'detached')
|
||||
except ValueError:
|
||||
signature_type = _(u'None')
|
||||
|
||||
if signature:
|
||||
paragraphs.extend(
|
||||
|
||||
@@ -100,7 +100,7 @@ register_maintenance_links([document_find_all_duplicates, document_update_page_c
|
||||
register_model_list_columns(Document, [
|
||||
{
|
||||
'name': _(u'thumbnail'), 'attribute':
|
||||
encapsulate(lambda x: document_thumbnail(x, gallery_name='document_list', title=x.filename, size=THUMBNAIL_SIZE))
|
||||
encapsulate(lambda x: document_thumbnail(x, gallery_name='document_list', title=getattr(x, 'filename', None), size=THUMBNAIL_SIZE))
|
||||
},
|
||||
])
|
||||
|
||||
|
||||
@@ -86,7 +86,10 @@ class DocumentPreviewForm(forms.Form):
|
||||
document = kwargs.pop('document', None)
|
||||
super(DocumentPreviewForm, self).__init__(*args, **kwargs)
|
||||
self.fields['preview'].initial = document
|
||||
self.fields['preview'].label = _(u'Document pages (%s)') % document.pages.count()
|
||||
try:
|
||||
self.fields['preview'].label = _(u'Document pages (%d)') % document.pages.count()
|
||||
except AttributeError:
|
||||
self.fields['preview'].label = _(u'Document pages (%d)') % 0
|
||||
|
||||
preview = forms.CharField(widget=DocumentPagesCarouselWidget())
|
||||
|
||||
@@ -131,7 +134,8 @@ class DocumentForm(forms.ModelForm):
|
||||
label=_(u'Quick document rename'))
|
||||
|
||||
if instance:
|
||||
self.version_fields(instance)
|
||||
if instance.latest_version:
|
||||
self.version_fields(instance)
|
||||
|
||||
def version_fields(self, document):
|
||||
self.fields['version_update'] = forms.ChoiceField(
|
||||
@@ -186,10 +190,14 @@ class DocumentForm_edit(DocumentForm):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(DocumentForm_edit, self).__init__(*args, **kwargs)
|
||||
self.fields.pop('serial')
|
||||
self.fields.pop('release_level')
|
||||
self.fields.pop('version_update')
|
||||
self.fields.pop('comment')
|
||||
if kwargs['instance'].latest_version:
|
||||
self.fields.pop('serial')
|
||||
self.fields.pop('release_level')
|
||||
self.fields.pop('version_update')
|
||||
self.fields.pop('comment')
|
||||
else:
|
||||
self.fields.pop('new_filename')
|
||||
|
||||
self.fields.pop('use_file_name')
|
||||
|
||||
|
||||
@@ -212,7 +220,12 @@ class DocumentContentForm(forms.Form):
|
||||
super(DocumentContentForm, self).__init__(*args, **kwargs)
|
||||
content = []
|
||||
self.fields['contents'].initial = u''
|
||||
for page in self.document.pages.all():
|
||||
try:
|
||||
document_pages = self.document.pages.all()
|
||||
except AttributeError:
|
||||
document_pages = []
|
||||
|
||||
for page in document_pages:
|
||||
if page.content:
|
||||
content.append(conditional_escape(force_unicode(page.content)))
|
||||
content.append(u'\n\n\n<hr/><div style="text-align: center;">- %s %s -</div><hr/>\n\n\n' % (ugettext(u'Page'), page.page_number))
|
||||
|
||||
@@ -96,19 +96,24 @@ def document_view(request, document_id, advanced=False):
|
||||
subtemplates_list = []
|
||||
|
||||
if advanced:
|
||||
document_properties_form = DocumentPropertiesForm(instance=document, extra_fields=[
|
||||
{'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 '-'},
|
||||
{'label': _(u'Exists in storage'), 'field': 'exists'},
|
||||
{'label': _(u'File path in storage'), 'field': 'file'},
|
||||
document_fields = [
|
||||
{'label': _(u'Date added'), 'field': lambda x: x.date_added.date()},
|
||||
{'label': _(u'Time added'), 'field': lambda x: unicode(x.date_added.time()).split('.')[0]},
|
||||
{'label': _(u'Checksum'), 'field': 'checksum'},
|
||||
{'label': _(u'UUID'), 'field': 'uuid'},
|
||||
{'label': _(u'Pages'), 'field': 'page_count'},
|
||||
])
|
||||
]
|
||||
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 '-'},
|
||||
{'label': _(u'Exists in storage'), 'field': 'exists'},
|
||||
{'label': _(u'File path in storage'), 'field': 'file'},
|
||||
{'label': _(u'Checksum'), 'field': 'checksum'},
|
||||
{'label': _(u'Pages'), 'field': 'page_count'},
|
||||
])
|
||||
|
||||
document_properties_form = DocumentPropertiesForm(instance=document, extra_fields=document_fields)
|
||||
|
||||
subtemplates_list.append(
|
||||
{
|
||||
@@ -235,8 +240,12 @@ def document_edit(request, document_id):
|
||||
|
||||
return HttpResponseRedirect(document.get_absolute_url())
|
||||
else:
|
||||
form = DocumentForm_edit(instance=document, initial={
|
||||
'new_filename': document.filename, 'description': document.description})
|
||||
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})
|
||||
|
||||
return render_to_response('generic_form.html', {
|
||||
'form': form,
|
||||
|
||||
@@ -38,8 +38,12 @@ class DocumentPagesCarouselWidget(forms.widgets.Widget):
|
||||
output = []
|
||||
output.append(u'<div class="carousel-container" style="white-space:nowrap; overflow: auto;">')
|
||||
|
||||
for page in value.pages.all():
|
||||
try:
|
||||
document_pages = value.pages.all()
|
||||
except AttributeError:
|
||||
document_pages = []
|
||||
|
||||
for page in document_pages:
|
||||
output.append(u'<div style="display: inline-block; margin: 5px 10px 10px 10px;">')
|
||||
output.append(u'<div class="tc">%(page_string)s %(page)s</div>' % {'page_string': ugettext(u'Page'), 'page': page.page_number})
|
||||
output.append(
|
||||
@@ -77,7 +81,10 @@ def document_html_widget(document, click_view=None, page=DEFAULT_PAGE_NUMBER, zo
|
||||
alt_text = _(u'document page image')
|
||||
|
||||
if not version:
|
||||
version = document.latest_version.pk
|
||||
try:
|
||||
version = document.latest_version.pk
|
||||
except AttributeError:
|
||||
version = None
|
||||
|
||||
query_dict = {
|
||||
'page': page,
|
||||
|
||||
Reference in New Issue
Block a user