Esempio n. 1
0
 def step_post_upload_process(cls, document, querystring=None):
     metadata_dict_list = decode_metadata_from_querystring(querystring=querystring)
     if metadata_dict_list:
         save_metadata_list(
             metadata_list=metadata_dict_list, document=document,
             create=True
         )
Esempio n. 2
0
 def step_post_upload_process(cls, document, querystring=None):
     metadata_dict_list = decode_metadata_from_querystring(
         querystring=querystring)
     if metadata_dict_list:
         save_metadata_list(metadata_list=metadata_dict_list,
                            document=document,
                            create=True)
Esempio n. 3
0
    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):
        if not document:
            document = Document()
            if document_type:
                document.document_type = document_type
            document.save()            

            if metadata_dict_list:
                save_metadata_list(metadata_dict_list, document, create=True)
            warnings = update_indexes(document)

            if user:
                document.add_as_recent_document_for_user(user)
                create_history(HISTORY_DOCUMENT_CREATED, document, {'user': user})
            else:
                create_history(HISTORY_DOCUMENT_CREATED, document)
        else:
            if use_file_name:
                filename = None
            else:
                filename = filename if filename else document.latest_version.filename

        if not new_version_data:
            new_version_data = {}
            
        new_version = document.new_version(file=file_object, **new_version_data)
        if filename:
            new_version.filename = filename
            new_version.save()

        transformations, errors = self.get_transformation_list()

        new_version.apply_default_transformations(transformations)
Esempio n. 4
0
    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):
        new_document = not document

        if not document:
            document = Document()
            if document_type:
                document.document_type = document_type
            document.save()

            apply_default_acls(document, user)

            if user:
                document.add_as_recent_document_for_user(user)
                create_history(HISTORY_DOCUMENT_CREATED, document,
                               {'user': user})
            else:
                create_history(HISTORY_DOCUMENT_CREATED, document)
        else:
            if use_file_name:
                filename = None
            else:
                filename = filename if filename else document.latest_version.filename

        if not new_version_data:
            new_version_data = {}

        try:
            new_version = document.new_version(file=file_object,
                                               user=user,
                                               **new_version_data)
        except Exception:
            # Don't leave the database in a broken state
            # document.delete()
            transaction.rollback()
            raise

        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)
            warnings = update_indexes(document)

        return document
Esempio n. 5
0
    def upload_document(self, file_object, document_type, description=None, label=None, language=None, metadata_dict_list=None, metadata_dictionary=None, tag_ids=None, user=None):
        """
        Upload an individual document
        """
        try:
            with transaction.atomic():
                document = Document(
                    description=description or '', document_type=document_type,
                    label=label or file_object.name,
                    language=language or setting_language.value
                )
                document.save(_user=user)
        except Exception as exception:
            logger.critical(
                'Unexpected exception while trying to create new document '
                '"%s" from source "%s"; %s',
                label or file_object.name, self, exception
            )
            raise
        else:
            try:
                document_version = document.new_version(
                    file_object=file_object, _user=user,
                )

                if user:
                    document.add_as_recent_document_for_user(user)

                Transformation.objects.copy(
                    source=self, targets=document_version.pages.all()
                )

                if metadata_dict_list:
                    save_metadata_list(
                        metadata_dict_list, document, create=True
                    )

                if metadata_dictionary:
                    set_bulk_metadata(
                        document=document,
                        metadata_dictionary=metadata_dictionary
                    )

                if tag_ids:
                    for tag in Tag.objects.filter(pk__in=tag_ids):
                        tag.documents.add(document)
            except Exception as exception:
                logger.critical(
                    'Unexpected exception while trying to create version for '
                    'new document "%s" from source "%s"; %s',
                    label or file_object.name, self, exception
                )
                document.delete(to_trash=False)
                raise
Esempio n. 6
0
    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):
        new_document = not document

        if not document:
            document = Document()
            if document_type:
                document.document_type = document_type

            if description:
                document.description = description

            document.save()

            apply_default_acls(document, user)

            if user:
                document.add_as_recent_document_for_user(user)
                create_history(HISTORY_DOCUMENT_CREATED, document, {'user': user})
            else:
                create_history(HISTORY_DOCUMENT_CREATED, document)
        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 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)
            warnings = update_indexes(document)
Esempio n. 7
0
    def upload_document(self, file_object, label, description=None, document_type=None, expand=False, language=None, metadata_dict_list=None, user=None):
        new_versions = Document.objects.new_document(
            description=description,
            document_type=document_type or self.document_type,
            expand=expand,
            file_object=file_object,
            label=label,
            language=language,
            user=user
        )

        transformations, errors = self.get_transformation_list()
        for new_version in new_versions:
            new_version.apply_default_transformations(transformations)

            if metadata_dict_list:
                save_metadata_list(metadata_dict_list, new_version.document, create=True)
Esempio n. 8
0
    def upload_document(self,
                        file_object,
                        document_type,
                        description=None,
                        label=None,
                        language=None,
                        metadata_dict_list=None,
                        metadata_dictionary=None,
                        user=None):
        try:
            with transaction.atomic():
                document = Document.objects.create(description=description
                                                   or '',
                                                   document_type=document_type,
                                                   label=label
                                                   or unicode(file_object),
                                                   language=language
                                                   or setting_language.value)
                document.save(_user=user)

                document_version = document.new_version(
                    file_object=file_object, _user=user)

                Transformation.objects.copy(
                    source=self, targets=document_version.pages.all())

                if metadata_dict_list:
                    save_metadata_list(metadata_dict_list,
                                       document,
                                       create=True)

                if metadata_dictionary:
                    set_bulk_metadata(document=document,
                                      metadata_dictionary=metadata_dictionary)

        except Exception as exception:
            logger.critical(
                'Unexpected exception while trying to create new document '
                '"%s" from source "%s"; %s', label or unicode(file_object),
                self, exception)
            raise
Esempio n. 9
0
    def upload_single_file(self, file_object, filename=None, document_type=None, metadata_dict_list=None, user=None):
        transformations, errors = self.get_transformation_list()
        document = Document(file=file_object)
        if document_type:
            document.document_type = document_type
        document.save()
        if filename:
            document.file_filename = filename
            document.save()    

        document.apply_default_transformations(transformations)

        if metadata_dict_list:
            save_metadata_list(metadata_dict_list, document, create=True)
        warnings = update_indexes(document)

        if user:
            document.add_as_recent_document_for_user(user)
            create_history(HISTORY_DOCUMENT_CREATED, document, {'user': user})
        else:
            create_history(HISTORY_DOCUMENT_CREATED, document)
Esempio n. 10
0
    def upload_document(self, file_object, document_type, description=None, label=None, language=None, metadata_dict_list=None, metadata_dictionary=None, user=None):
        try:
            with transaction.atomic():
                document = Document.objects.create(
                    description=description or '', document_type=document_type,
                    label=label or unicode(file_object),
                    language=language or setting_language.value
                )
                document.save(_user=user)

                document_version = document.new_version(
                    file_object=file_object, _user=user
                )

                Transformation.objects.copy(
                    source=self, targets=document_version.pages.all()
                )

                if metadata_dict_list:
                    save_metadata_list(
                        metadata_dict_list, document, create=True
                    )

                if metadata_dictionary:
                    set_bulk_metadata(
                        document=document,
                        metadata_dictionary=metadata_dictionary
                    )

        except Exception as exception:
            logger.critical(
                'Unexpected exception while trying to create new document '
                '"%s" from source "%s"; %s',
                label or unicode(file_object), self, exception
            )
            raise
Esempio n. 11
0
def metadata_edit(request, document_id=None, document_id_list=None):
    check_permissions(request.user, [PERMISSION_METADATA_DOCUMENT_EDIT])

    if document_id:
        documents = [get_object_or_404(Document, pk=document_id)]
        if documents[0].documentmetadata_set.count() == 0:
            messages.warning(request, _(u'The selected document doesn\'t have any metadata.'))
            return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
    elif document_id_list:
        documents = [get_object_or_404(Document, pk=document_id) for document_id in document_id_list.split(',')]
    else:
        messages.error(request, _(u'Must provide at least one document.'))
        return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))

    post_action_redirect = reverse('document_list_recent')

    next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', post_action_redirect)))

    metadata = {}
    for document in documents:
        RecentDocument.objects.add_document_for_user(request.user, document)

        for item in document.documentmetadata_set.all():
            value = item.value
            if item.metadata_type in metadata:
                if value not in metadata[item.metadata_type]:
                    metadata[item.metadata_type].append(value)
            else:
                metadata[item.metadata_type] = [value] if value else []

    initial = []
    for key, value in metadata.items():
        initial.append({
            'metadata_type': key,
            'value': u', '.join(value)
        })

    formset = MetadataFormSet(initial=initial)
    if request.method == 'POST':
        formset = MetadataFormSet(request.POST)
        if formset.is_valid():
            for document in documents:

                warnings = delete_indexes(document)
                if request.user.is_staff or request.user.is_superuser:
                    for warning in warnings:
                        messages.warning(request, _(u'Error deleting document indexes; %s') % warning)

                errors = []
                for form in formset.forms:
                    if form.cleaned_data['update']:
                        try:
                            save_metadata_list([form.cleaned_data], document)
                        except Exception, e:
                            errors.append(e)

                if errors:
                    for error in errors:
                        messages.error(request, _(u'Error editing metadata for document %(document)s; %(error)s.') % {
                        'document': document, 'error': error})
                else:
                    messages.success(request, _(u'Metadata for document %s edited successfully.') % document)

                warnings = update_indexes(document)
                if warnings and (request.user.is_staff or request.user.is_superuser):
                    for warning in warnings:
                        messages.warning(request, _(u'Error updating document indexes; %s') % warning)
                else:
                    messages.success(request, _(u'Document indexes updated successfully.'))

            return HttpResponseRedirect(next)
Esempio n. 12
0
def metadata_edit(request, document_id=None, document_id_list=None):
    check_permissions(request.user, [PERMISSION_METADATA_DOCUMENT_EDIT])

    if document_id:
        documents = [get_object_or_404(Document, pk=document_id)]
        if documents[0].documentmetadata_set.count() == 0:
            messages.warning(
                request,
                _(u'The selected document doesn\'t have any metadata.'))
            return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
    elif document_id_list:
        documents = [
            get_object_or_404(Document, pk=document_id)
            for document_id in document_id_list.split(',')
        ]
    else:
        messages.error(request, _(u'Must provide at least one document.'))
        return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))

    post_action_redirect = reverse('document_list_recent')

    next = request.POST.get(
        'next',
        request.GET.get('next',
                        request.META.get('HTTP_REFERER',
                                         post_action_redirect)))

    metadata = {}
    for document in documents:
        RecentDocument.objects.add_document_for_user(request.user, document)

        for item in document.documentmetadata_set.all():
            value = item.value
            if item.metadata_type in metadata:
                if value not in metadata[item.metadata_type]:
                    metadata[item.metadata_type].append(value)
            else:
                metadata[item.metadata_type] = [value] if value else []

    initial = []
    for key, value in metadata.items():
        initial.append({'metadata_type': key, 'value': u', '.join(value)})

    formset = MetadataFormSet(initial=initial)
    if request.method == 'POST':
        formset = MetadataFormSet(request.POST)
        if formset.is_valid():
            for document in documents:

                warnings = delete_indexes(document)
                if request.user.is_staff or request.user.is_superuser:
                    for warning in warnings:
                        messages.warning(
                            request,
                            _(u'Error deleting document indexes; %s') %
                            warning)

                errors = []
                for form in formset.forms:
                    if form.cleaned_data['update']:
                        try:
                            save_metadata_list([form.cleaned_data], document)
                        except Exception, e:
                            errors.append(e)

                if errors:
                    for error in errors:
                        messages.error(
                            request,
                            _(u'Error editing metadata for document %(document)s; %(error)s.'
                              ) % {
                                  'document': document,
                                  'error': error
                              })
                else:
                    messages.success(
                        request,
                        _(u'Metadata for document %s edited successfully.') %
                        document)

                warnings = update_indexes(document)
                if warnings and (request.user.is_staff
                                 or request.user.is_superuser):
                    for warning in warnings:
                        messages.warning(
                            request,
                            _(u'Error updating document indexes; %s') %
                            warning)
                else:
                    messages.success(
                        request, _(u'Document indexes updated successfully.'))

            return HttpResponseRedirect(next)