def document_delete(request, document_id=None, document_id_list=None): check_permissions(request.user, [PERMISSION_DOCUMENT_DELETE]) post_action_redirect = None if document_id: documents = [get_object_or_404(Document, pk=document_id)] post_action_redirect = reverse('document_list') 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', '/')) previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/'))) next = request.POST.get('next', request.GET.get('next', post_action_redirect if post_action_redirect else request.META.get('HTTP_REFERER', '/'))) if request.method == 'POST': for document in documents: try: warnings = delete_indexes(document) if request.user.is_staff or request.user.is_superuser: for warning in warnings: messages.warning(request, warning) document.delete() #create_history(HISTORY_DOCUMENT_DELETED, data={'user': request.user, 'document': document}) messages.success(request, _(u'Document: %s deleted successfully.') % document) except Exception, e: messages.error(request, _(u'Document: %(document)s delete error: %(error)s') % { 'document': document, 'error': e }) return HttpResponseRedirect(next)
def document_edit(request, document_id): check_permissions(request.user, [PERMISSION_DOCUMENT_PROPERTIES_EDIT]) document = get_object_or_404(Document, pk=document_id) if request.method == 'POST': old_document = copy.copy(document) form = DocumentForm_edit(request.POST, instance=document) if form.is_valid(): warnings = delete_indexes(document) if request.user.is_staff or request.user.is_superuser: for warning in warnings: messages.warning(request, warning) document.file_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.save() create_history(HISTORY_DOCUMENT_EDITED, document, {'user': request.user, 'diff': return_diff(old_document, document, ['file_filename', 'description'])}) RecentDocument.objects.add_document_for_user(request.user, document) messages.success(request, _(u'Document "%s" edited successfully.') % document) warnings = update_indexes(document) if request.user.is_staff or request.user.is_superuser: for warning in warnings: messages.warning(request, warning) return HttpResponseRedirect(document.get_absolute_url()) else: form = DocumentForm_edit(instance=document, initial={ 'new_filename': document.file_filename}) return render_to_response('generic_form.html', { 'form': form, 'object': document, }, context_instance=RequestContext(request))
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)
def metadata_remove(request, document_id=None, document_id_list=None): check_permissions(request.user, [PERMISSION_METADATA_DOCUMENT_REMOVE]) 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 u'' initial = [] for key, value in metadata.items(): initial.append({ 'metadata_type': key, 'value': u', '.join(value) }) formset = MetadataRemoveFormSet(initial=initial) if request.method == 'POST': formset = MetadataRemoveFormSet(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) for form in formset.forms: if form.cleaned_data['update']: metadata_type = get_object_or_404(MetadataType, pk=form.cleaned_data['id']) try: document_metadata = DocumentMetadata.objects.get(document=document, metadata_type=metadata_type) document_metadata.delete() messages.success(request, _(u'Successfully remove metadata type: %(metadata_type)s from document: %(document)s.') % { 'metadata_type': metadata_type, 'document': document}) except: messages.error(request, _(u'Error removing metadata type: %(metadata_type)s from document: %(document)s.') % { 'metadata_type': metadata_type, 'document': 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) context = { 'form_display_mode_table': True, 'form': formset, 'next': next, } if len(documents) == 1: context['object'] = documents[0] context['title'] = _(u'Remove metadata types from document: %s') % ', '.join([unicode(d) for d in documents]) elif len(documents) > 1: context['title'] = _(u'Remove metadata types from documents: %s') % ', '.join([unicode(d) for d in documents]) return render_to_response('generic_form.html', context, context_instance=RequestContext(request))
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)
def metadata_remove(request, document_id=None, document_id_list=None): check_permissions(request.user, [PERMISSION_METADATA_DOCUMENT_REMOVE]) 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 u'' initial = [] for key, value in metadata.items(): initial.append({'metadata_type': key, 'value': u', '.join(value)}) formset = MetadataRemoveFormSet(initial=initial) if request.method == 'POST': formset = MetadataRemoveFormSet(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) for form in formset.forms: if form.cleaned_data['update']: metadata_type = get_object_or_404( MetadataType, pk=form.cleaned_data['id']) try: document_metadata = DocumentMetadata.objects.get( document=document, metadata_type=metadata_type) document_metadata.delete() messages.success( request, _(u'Successfully remove metadata type: %(metadata_type)s from document: %(document)s.' ) % { 'metadata_type': metadata_type, 'document': document }) except: messages.error( request, _(u'Error removing metadata type: %(metadata_type)s from document: %(document)s.' ) % { 'metadata_type': metadata_type, 'document': 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) context = { 'form_display_mode_table': True, 'form': formset, 'next': next, } if len(documents) == 1: context['object'] = documents[0] context['title'] = _(u'Remove metadata types from document: %s' ) % ', '.join([unicode(d) for d in documents]) elif len(documents) > 1: context['title'] = _(u'Remove metadata types from documents: %s' ) % ', '.join([unicode(d) for d in documents]) return render_to_response('generic_form.html', context, context_instance=RequestContext(request))