def ajax_query_publication(request, publication_uid): publication = get_object_or_404(Publication, uid=publication_uid) permission_backend = get_permission_backend(request) if not permission_backend.can_view_publication( request.user, publication.organization, {"publication": publication} ): raise Http404 return response_json_success( { "uid": str(publication.uid), "title": publication.title, "description": publication.description, "tag_names": ",".join([tag.tag_name for tag in publication.tags.all()]), "uploaded": format_abbr_datetime(publication.uploaded), "uploaded_by": publication.uploaded_by.get_profile().get_fullname(), "file_ext": publication.file_ext, "file_size_text": humanize_file_size(publication.uploaded_file.file.size), "shelves": ",".join([str(shelf.id) for shelf in publication.shelves.all()]), "thumbnail_url": publication.get_large_thumbnail(), "download_url": reverse("download_publication", args=[publication.uid]), "readonly": "true" if not permission_backend.can_edit_publication( request.user, publication.organization, {"publication": publication} ) else "false", } )
def replace_publication(request, organization_slug): organization = get_object_or_404(Organization, slug=organization_slug) publication_id = request.POST.get("publication_id") print publication_id if publication_id: publication = get_object_or_404(Publication, uid=publication_id) else: transaction.rollback() raise Http404 if not get_permission_backend(request).can_edit_publication( request.user, publication.organization, {"publication": publication} ): transaction.rollback() raise Http404 try: file = request.FILES[u"files[]"] if file.size > settings.MAX_PUBLICATION_FILE_SIZE: transaction.rollback() return response_json_error("file-size-exceed") uploading_file = UploadedFile(file) publication = domain_functions.replace_publication(request, uploading_file, publication) if not publication: transaction.rollback() return response_json_error() transaction.commit() try: generate_thumbnails.delay(publication.uid) except: import sys import traceback logger.critical(traceback.format_exc(sys.exc_info()[2])) return response_json_success( { "uid": str(publication.uid), "file_ext": publication.file_ext, "file_size": humanize_file_size(uploading_file.file.size), "uploaded": format_abbr_datetime(publication.uploaded), "replaced": format_abbr_datetime(publication.replaced), "thumbnail_url": publication.get_large_thumbnail(), "download_url": reverse("download_publication", args=[publication.uid]), } ) except: transaction.rollback() return response_json_error()
def upload_publication(request, organization_slug): organization = get_object_or_404(Organization, slug=organization_slug) shelf_id = request.POST.get("shelf") if shelf_id: shelf = get_object_or_404(OrganizationShelf, pk=shelf_id) else: transaction.rollback() raise Http404 if shelf.organization.id != organization.id or not get_permission_backend(request).can_upload_shelf( request.user, organization, {"shelf": shelf} ): transaction.rollback() raise Http404 try: file = request.FILES[u"files[]"] if file.size > settings.MAX_PUBLICATION_FILE_SIZE: transaction.rollback() return response_json_error("file-size-exceed") uploading_file = UploadedFile(file) publication = domain_functions.upload_publication(request, uploading_file, organization, shelf) if not publication: transaction.rollback() return response_json_error() transaction.commit() # Need to commit before create task try: generate_thumbnails.delay(publication.uid) except: import sys import traceback logger.critical(traceback.format_exc(sys.exc_info()[2])) return response_json_success( { "uid": str(publication.uid), "title": publication.title, "file_ext": publication.file_ext, "file_size_text": humanize_file_size(uploading_file.file.size), "shelf": shelf.id if shelf else "", "uploaded": format_abbr_datetime(publication.uploaded), "thumbnail_url": publication.get_large_thumbnail(), "download_url": reverse("download_publication", args=[publication.uid]), } ) except: transaction.rollback() return response_json_error()
def humanize_file_size(size_in_byte): return utilities.humanize_file_size(size_in_byte)