Update documents app to support multiple versions

This commit is contained in:
Roberto Rosario
2011-12-03 19:35:03 -04:00
parent 9f7e5de5ac
commit 11725c943b
5 changed files with 373 additions and 240 deletions

View File

@@ -11,9 +11,11 @@ from common.conf.settings import DEFAULT_PAPER_SIZE
from common.conf.settings import DEFAULT_PAGE_ORIENTATION
from common.widgets import TextAreaDiv
from documents.models import Document, DocumentType, \
DocumentPage, DocumentPageTransformation, DocumentTypeFilename
from documents.models import (Document, DocumentType,
DocumentPage, DocumentPageTransformation, DocumentTypeFilename,
DocumentVersion)
from documents.widgets import document_html_widget
from documents.literals import (RELEASE_LEVEL_FINAL, RELEASE_LEVEL_CHOICES)
# Document page forms
class DocumentPageTransformationForm(forms.ModelForm):
@@ -147,6 +149,8 @@ class DocumentForm(forms.ModelForm):
instance = kwargs.pop('instance', None)
super(DocumentForm, self).__init__(*args, **kwargs)
if instance:
self.version_fields(instance)
if 'document_type' in self.fields:
# To allow merging with DocumentForm_edit
@@ -164,11 +168,51 @@ class DocumentForm(forms.ModelForm):
queryset=filenames_qs,
required=False,
label=_(u'Quick document rename'))
def version_fields(self, document):
self.fields['comment'] = forms.CharField(
label=_(u'Comment'),
required=False,
widget=forms.widgets.Textarea(attrs={'rows': 4}),
)
self.fields['version_update'] = forms.ChoiceField(
label=_(u'Version update'),
#widget=forms.widgets.RadioSelect(),
choices=DocumentVersion.get_version_update_choices(document.latest_version)
)
self.fields['release_level'] = forms.ChoiceField(
label=_(u'Release level'),
choices=RELEASE_LEVEL_CHOICES,
initial=RELEASE_LEVEL_FINAL,
#required=False,
)
self.fields['serial'] = forms.IntegerField(
label=_(u'Release level serial'),
initial=0,
widget=forms.widgets.TextInput(
attrs = {'style': 'width: auto;'}
),
#required=False
)
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'),
'release_level': self.cleaned_data.get('release_level'),
'serial': self.cleaned_data.get('serial'),
}
# Always return the full collection of cleaned data.
return cleaned_data
class DocumentForm_edit(DocumentForm):
"""
@@ -177,6 +221,13 @@ class DocumentForm_edit(DocumentForm):
class Meta:
model = Document
exclude = ('file', 'document_type', 'tags')
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')
class DocumentPropertiesForm(DetailForm):

View File

@@ -44,3 +44,21 @@ HISTORY_DOCUMENT_DELETED = {
'details': _(u'Document "%(document)s" deleted on %(datetime)s by %(fullname)s.'),
'expressions': {'fullname': 'user.get_full_name() if user.get_full_name() else user.username'}
}
RELEASE_LEVEL_FINAL = 1
RELEASE_LEVEL_ALPHA = 2
RELEASE_LEVEL_BETA = 3
RELEASE_LEVEL_RC = 4
RELEASE_LEVEL_HF = 5
RELEASE_LEVEL_CHOICES = (
(RELEASE_LEVEL_FINAL, _(u'final')),
(RELEASE_LEVEL_ALPHA, _(u'alpha')),
(RELEASE_LEVEL_BETA, _(u'beta')),
(RELEASE_LEVEL_RC, _(u'release candidate')),
(RELEASE_LEVEL_HF, _(u'hotfix')),
)
VERSION_UPDATE_MAJOR = u'major'
VERSION_UPDATE_MINOR = u'minor'
VERSION_UPDATE_MICRO = u'micro'

File diff suppressed because it is too large Load Diff

View File

@@ -35,7 +35,7 @@ urlpatterns = patterns('documents.views',
url(r'^(?P<document_id>\d+)/clear_transformations/$', 'document_clear_transformations', (), 'document_clear_transformations'),
url(r'^(?P<document_pk>\d+)/version/all/$', 'document_version_list', (), 'document_version_list'),
url(r'^document/version/(?P<document_version_pk>\d+)/download/$', 'document_version_download', (), 'document_version_download'),
url(r'^document/version/(?P<document_version_pk>\d+)/download/$', 'document_download', (), 'document_version_download'),
url(r'^multiple/clear_transformations/$', 'document_multiple_clear_transformations', (), 'document_multiple_clear_transformations'),
url(r'^duplicates/list/$', 'document_find_all_duplicates', (), 'document_find_all_duplicates'),

View File

@@ -45,13 +45,13 @@ from documents.literals import PERMISSION_DOCUMENT_CREATE, \
from documents.literals import HISTORY_DOCUMENT_CREATED, \
HISTORY_DOCUMENT_EDITED, HISTORY_DOCUMENT_DELETED
from documents.forms import DocumentTypeSelectForm, \
DocumentForm_edit, DocumentPropertiesForm, \
DocumentPreviewForm, \
DocumentPageForm, DocumentPageTransformationForm, \
DocumentContentForm, DocumentPageForm_edit, \
DocumentPageForm_text, PrintForm, DocumentTypeForm, \
DocumentTypeFilenameForm, DocumentTypeFilenameForm_create
from documents.forms import (DocumentTypeSelectForm,
DocumentForm_edit, DocumentPropertiesForm,
DocumentPreviewForm, DocumentPageForm,
DocumentPageTransformationForm, DocumentContentForm,
DocumentPageForm_edit, DocumentPageForm_text, PrintForm,
DocumentTypeForm, DocumentTypeFilenameForm,
DocumentTypeFilenameForm_create)
from documents.wizards import DocumentCreateWizard
from documents.models import (Document, DocumentType, DocumentPage,
DocumentPageTransformation, RecentDocument, DocumentTypeFilename,
@@ -242,12 +242,12 @@ def document_edit(request, document_id):
for warning in warnings:
messages.warning(request, warning)
document.file_filename = form.cleaned_data['new_filename']
document.filename = form.cleaned_data['new_filename']
document.description = form.cleaned_data['description']
if 'document_type_available_filenames' in form.cleaned_data:
if form.cleaned_data['document_type_available_filenames']:
document.file_filename = form.cleaned_data['document_type_available_filenames'].filename
document.filename = 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, ['file_filename', 'description'])})
@@ -291,27 +291,27 @@ def get_document_image(request, document_id, size=PREVIEW_SIZE, base64_version=F
if base64_version:
return HttpResponse(u'<html><body><img src="%s" /></body></html>' % document.get_image(size=size, page=page, zoom=zoom, rotation=rotation, as_base64=True))
else:
# TODO: hardcoded MIMETYPE
# TODO: fix hardcoded MIMETYPE
return sendfile.sendfile(request, document.get_image(size=size, page=page, zoom=zoom, rotation=rotation), mimetype=DEFAULT_FILE_FORMAT_MIMETYPE)
def document_version_download(request, document_version_pk):
document_version = get_object_or_404(DocumentVersion, pk=document_version_pk)
return document_download(request, document_version.document.pk)
def document_download(request, document_id):
def document_download(request, document_id=None, document_version_pk=None):
check_permissions(request.user, [PERMISSION_DOCUMENT_DOWNLOAD])
document = get_object_or_404(Document, pk=document_id)
if document_version_pk:
document_version = get_object_or_404(DocumentVersion, pk=document_version_pk)
else:
document_version = get_object_or_404(Document, pk=document_id).latest_version
try:
#Test permissions and trigger exception
document.open()
# Test permissions and trigger exception
fd = document_version.open()
fd.close()
return serve_file(
request,
document.file,
save_as=u'"%s"' % document.get_fullname(),
content_type=document.file_mimetype if document.file_mimetype else 'application/octet-stream'
document_version.file,
save_as=u'"%s"' % document_version.filename,
content_type=document_version.mimetype if document_version.mimetype else 'application/octet-stream'
)
except Exception, e:
messages.error(request, e)
@@ -1154,14 +1154,14 @@ def document_version_list(request, document_pk):
document = get_object_or_404(Document, pk=document_pk)
context = {
'object_list': document.versions.all(),
'object_list': document.versions.order_by('-timestamp'),
'title': _(u'versions for document: %s') % document,
'hide_object': True,
'object': document,
'extra_columns': [
{
'name': _(u'version'),
'attribute': 'get_version',
'attribute': 'get_formated_version',
},
{
'name': _(u'time and date'),