Document upload working from local and from staging folders

This commit is contained in:
Roberto Rosario
2011-07-06 01:51:29 -04:00
parent 2816c1f86f
commit ee3abd16ec
4 changed files with 72 additions and 56 deletions

View File

@@ -64,19 +64,6 @@ class BaseModel(models.Model):
abstract = True
#class MetadataValue(models.Model):
# source = models.ForeignKey(BaseModel, verbose_name=_(u'document source'))
# metadata_type = models.ForeignKey(MetadataType, verbose_name=_(u'metadata type'))
# value = models.CharField(max_length=256, blank=True, verbose_name=_(u'value'))
#
# def __unicode__(self):
# return self.source
#
# class Meta:
# verbose_name = _(u'source metadata')
# verbose_name_plural = _(u'sources metadata')
class InteractiveBaseModel(BaseModel):
icon = models.CharField(blank=True, null=True, max_length=24, choices=SOURCE_ICON_CHOICES, verbose_name=_(u'icon'))
@@ -111,6 +98,19 @@ class StagingFolder(InteractiveBaseModel):
class Meta:
verbose_name = _(u'staging folder')
verbose_name_plural = _(u'staging folder')
#class StagingFolderMetadataValue(models.Model):
# source = models.ForeignKey(BaseModel, verbose_name=_(u'document source'))
# metadata_type = models.ForeignKey(MetadataType, verbose_name=_(u'metadata type'))
# value = models.CharField(max_length=256, blank=True, verbose_name=_(u'value'))
#
# def __unicode__(self):
# return self.source
#
# class Meta:
# verbose_name = _(u'source metadata')
# verbose_name_plural = _(u'sources metadata')
class WebForm(InteractiveBaseModel):

View File

@@ -11,20 +11,8 @@ from django.utils.translation import ugettext_lazy as _
from converter import TRANFORMATION_CHOICES
from converter.api import convert, cache_cleanup
#from documents.conf.settings import STAGING_DIRECTORY
STAGING_DIRECTORY = u'/tmp'
DEFAULT_STAGING_DIRECTORY = u'/tmp'
#from documents.conf.settings import DEFAULT_TRANSFORMATIONS
#from documents.conf.settings import STAGING_FILES_PREVIEW_SIZE
STAGING_FILES_PREVIEW_SIZE = u'640'
#from documents.conf.settings import USER_STAGING_DIRECTORY_ROOT
#from documents.conf.settings import USER_STAGING_DIRECTORY_EXPRESSION
#from documents.literals import UPLOAD_SOURCE_STAGING, \
# UPLOAD_SOURCE_USER_STAGING
UPLOAD_SOURCE_LOCAL = u'local'
UPLOAD_SOURCE_STAGING = u'staging'
UPLOAD_SOURCE_USER_STAGING = u'user_staging'
HASH_FUNCTION = lambda x: hashlib.sha256(x).hexdigest()
#TODO: Do benchmarks
@@ -68,7 +56,7 @@ class StagingFile(object):
Simple class to encapsulate the files in a directory and hide the
specifics to the view
"""
path = STAGING_DIRECTORY
path = DEFAULT_STAGING_DIRECTORY
@classmethod
def set_path(cls, path):
@@ -126,9 +114,9 @@ class StagingFile(object):
except Exception, exc:
raise Exception(ugettext(u'Unable to upload staging file: %s') % exc)
def delete(self):
def delete(self, preview_size):
#tranformation_string, errors = get_transformation_string(DEFAULT_TRANSFORMATIONS)
cache_cleanup(self.filepath, size=STAGING_FILES_PREVIEW_SIZE)#, extra_options=tranformation_string)
cache_cleanup(self.filepath, size=preview_size)#, extra_options=tranformation_string)
try:
os.unlink(self.filepath)
except OSError, exc:

View File

@@ -1,20 +1,6 @@
from django.conf.urls.defaults import patterns, url
#from converter.api import QUALITY_HIGH, QUALITY_PRINT
#from documents.conf.settings import PREVIEW_SIZE
#from documents.conf.settings import PRINT_SIZE
#from documents.conf.settings import THUMBNAIL_SIZE
#from documents.conf.settings import DISPLAY_SIZE
#from documents.conf.settings import MULTIPAGE_PREVIEW_SIZE
#from documents.literals import UPLOAD_SOURCE_LOCAL, \
# UPLOAD_SOURCE_STAGING, UPLOAD_SOURCE_USER_STAGING
urlpatterns = patterns('sources.views',
#url(r'^upload/local/$', 'upload_document_with_type', {'source': UPLOAD_SOURCE_LOCAL}, 'upload_document_from_local'),
#url(r'^upload/staging/$', 'upload_document_with_type', {'source': UPLOAD_SOURCE_STAGING}, 'upload_document_from_staging'),
#url(r'^upload/staging/user/$', 'upload_document_with_type', {'source': UPLOAD_SOURCE_USER_STAGING}, 'upload_document_from_user_staging'),
url(r'^staging_file/type/(?P<source_type>\w+)/(?P<source_id>\d+)/(?P<staging_file_id>\w+)/preview/$', 'staging_file_preview', (), 'staging_file_preview'),
url(r'^staging_file/type/(?P<source_type>\w+)/(?P<source_id>\d+)/(?P<staging_file_id>\w+)/delete/$', 'staging_file_delete', (), 'staging_file_delete'),

View File

@@ -8,19 +8,23 @@ from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from permissions.api import check_permissions
from documents.literals import PERMISSION_DOCUMENT_CREATE
from documents.models import DocumentType
from metadata.api import save_metadata_list, \
decode_metadata_from_url, metadata_repr_as_list
from metadata.forms import MetadataFormSet, MetadataSelectionForm
import sendfile
from converter.exceptions import UnkownConvertError, UnknownFormat
from documents.literals import PICTURE_ERROR_SMALL, PICTURE_ERROR_MEDIUM, \
PICTURE_UNKNOWN_SMALL, PICTURE_UNKNOWN_MEDIUM
from documents.literals import PERMISSION_DOCUMENT_CREATE
from documents.literals import HISTORY_DOCUMENT_CREATED
from documents.models import RecentDocument, Document, DocumentType
from document_indexing.api import update_indexes
from history.api import create_history
from metadata.api import save_metadata_list, \
decode_metadata_from_url, metadata_repr_as_list
from metadata.forms import MetadataFormSet, MetadataSelectionForm
from permissions.api import check_permissions
import sendfile
#TEMP
from documents.forms import DocumentForm
#TEMP
from sources.models import WebForm, StagingFolder
from sources.models import SOURCE_CHOICE_WEB_FORM, SOURCE_CHOICE_STAGING
@@ -91,6 +95,23 @@ def upload_interactive(request, source_type=None, source_id=None):
if source_type == SOURCE_CHOICE_WEB_FORM:
web_form = get_object_or_404(WebForm, pk=source_id)
context['source'] = web_form
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES, document_type=document_type)
if form.is_valid():
try:
expand = form.cleaned_data['expand']
if (not expand) or (expand and not _handle_zip_file(request, request.FILES['file'], document_type)):
instance = form.save()
instance.save()
if document_type:
instance.document_type = document_type
_handle_save_document(request, instance, form)
messages.success(request, _(u'Document uploaded successfully.'))
except Exception, e:
messages.error(request, e)
return HttpResponseRedirect(request.get_full_path())
form = DocumentForm(document_type=document_type)
subtemplates_list.append({
@@ -104,6 +125,31 @@ def upload_interactive(request, source_type=None, source_id=None):
staging_folder = get_object_or_404(StagingFolder, pk=source_id)
context['source'] = staging_folder
StagingFile = create_staging_file_class(request, staging_folder.folder_path)
if request.method == 'POST':
form = StagingDocumentForm(request.POST, request.FILES,
cls=StagingFile, document_type=document_type)
if form.is_valid():
try:
staging_file = StagingFile.get(form.cleaned_data['staging_file_id'])
expand = form.cleaned_data['expand']
if (not expand) or (expand and not _handle_zip_file(request, staging_file.upload(), document_type)):
document = Document(file=staging_file.upload())
if document_type:
document.document_type = document_type
document.save()
_handle_save_document(request, document, form)
messages.success(request, _(u'Staging file: %s, uploaded successfully.') % staging_file.filename)
if staging_folder.delete_after_upload:
staging_file.delete()
messages.success(request, _(u'Staging file: %s, deleted successfully.') % staging_file.filename)
except Exception, e:
messages.error(request, e)
#return HttpResponseRedirect(request.META['HTTP_REFERER'])
return HttpResponseRedirect(request.get_full_path())
form = StagingDocumentForm(cls=StagingFile,
document_type=document_type)
try:
@@ -131,10 +177,6 @@ def upload_interactive(request, source_type=None, source_id=None):
]
context.update({
'document_type_id': document_type_id,
'subtemplates_list': subtemplates_list,
@@ -352,7 +394,7 @@ def staging_file_delete(request, source_type, source_id, staging_file_id):
if request.method == 'POST':
try:
staging_file.delete()
staging_file.delete(staging_folder.get_preview_size())
messages.success(request, _(u'Staging file delete successfully.'))
except Exception, e:
messages.error(request, e)