def save_galley_image(galley, request, uploaded_file, label="Galley Image", fixed=False): if fixed: filename = request.POST.get('file_name') uploaded_file_mime = files.check_in_memory_mime(uploaded_file) expected_mime = files.guess_mime(filename) if not uploaded_file_mime == expected_mime: messages.add_message( request, messages.WARNING, 'The file you uploaded does not match the mime of the ' 'file expected.') new_file = files.save_file_to_article(uploaded_file, galley.article, request.user) new_file.is_galley = False new_file.label = label if fixed: new_file.original_filename = request.POST.get('file_name') new_file.save() galley.images.add(new_file) return new_file
def news(request): """ Allows an editor user to add or delete news items. :param request: HttpRequest object :return: HttpResponse object """ new_items = models.NewsItem.objects.filter(content_type=request.model_content_type, object_id=request.site_type.pk).order_by('-posted') form = forms.NewsItemForm() new_file = None if 'delete' in request.POST: news_item_pk = request.POST.get('delete') item = get_object_or_404(models.NewsItem, pk=news_item_pk, content_type=request.model_content_type, object_id=request.site_type.pk) item.delete() return redirect(reverse('core_manager_news')) if request.POST: form = forms.NewsItemForm(request.POST) if request.FILES: uploaded_file = request.FILES.get('image_file') if not files.guess_mime(uploaded_file.name) in files.IMAGE_MIMETYPES: form.add_error('image_file', 'File must be an image.') else: if request.model_content_type.name == 'journal': new_file = files.save_file_to_journal(request, uploaded_file, 'News Item', 'News Item', public=True) core_logic.resize_and_crop(new_file.journal_path(request.journal), [750, 324], 'middle') elif request.model_content_type.name == 'press': new_file = files.save_file_to_press(request, uploaded_file, 'News Item', 'News Item', public=True) core_logic.resize_and_crop(new_file.press_path(), [750, 324], 'middle') if form.is_valid(): new_item = form.save(commit=False) new_item.content_type = request.model_content_type new_item.object_id = request.site_type.pk new_item.posted_by = request.user new_item.posted = timezone.now() new_item.large_image_file = new_file new_item.save() return redirect(reverse('core_manager_news')) template = 'core/manager/news/index.html' context = { 'news_items': new_items, 'action': 'new', 'form': form, } return render(request, template, context)
def save_galley(article, request, uploaded_file, is_galley, label=None, save_to_disk=True): mime = files.guess_mime(uploaded_file.name) if mime == "application/zip": raise ZippedGalleyError("Zip galleys are not supported") new_file = files.save_file_to_article( uploaded_file, article, request.user, save=save_to_disk, ) new_file.is_galley = is_galley new_file.label = label new_file.save() article.save() type_ = None if new_file.mime_type in files.HTML_MIMETYPES: type_ = 'html' if not label: label = 'HTML' with open(new_file.self_article_path(), 'r+', encoding="utf-8") as f: html_contents = f.read() f.seek(0) cleaned_html = remove_css_from_html(html_contents) f.write(cleaned_html) f.truncate() elif new_file.mime_type in files.XML_MIMETYPES: type_ = 'xml' if not label: label = 'XML' elif new_file.mime_type in files.PDF_MIMETYPES: type_ = 'pdf' if not label: label = 'PDF' if not label: label = 'Other' if not type_: type_ = 'other' new_galley = core_models.Galley.objects.create( article=article, file=new_file, label=label, type=type_, sequence=article.get_next_galley_sequence(), ) return new_galley
def check_file(uploaded_file, request, form): if not uploaded_file: form.add_error(None, 'You must select a file.') return False submission_formats = setting_handler.get_setting('general', 'limit_manuscript_types', request.journal).value if submission_formats: mime = files.guess_mime(str(uploaded_file.name)) if mime in files.EDITABLE_FORMAT: return True else: form.add_error(None, 'You must upload a file that is either a Doc, Docx, RTF or ODT.') return False else: return True
def handle(self, *args, **options): """ Updates all render galleys from a folder. :param args: None :param options: None. :return: None """ journal_code = options.get('journal_code') folder_path = options.get('folder_path') try: journal = journal_models.Journal.objects.get(code=journal_code) except journal_models.Journal.DoesNotExist: print('No journal with that code was found.') sys.exit() journal_folder = os.path.join(folder_path, journal.code) if not os.path.isdir(journal_folder): print('No directory found.') sys.exit() article_folders = os.listdir(journal_folder) for folder in article_folders: try: article = submission_models.Article.objects.get(pk=folder) print('Article {0} found.'.format(article.pk)) try: file = os.listdir(os.path.join(journal_folder, folder))[0] print('File {0} found.'.format(file)) except IndexError: file = None if file: if article.render_galley: print( 'Article already has a render galley, updating it.' ) article.render_galley.file.unlink_file() shutil.copyfile( os.path.join(journal_folder, folder, file), article.render_galley.file.self_article_path()) print('Existing render galley updated.') else: print( 'Article does not have a render galley, creating one.' ) file_object = core_models.File.objects.create( article_id=article.pk, original_filename=file, uuid_filename=uuid.uuid4(), label='Render Galley', privacy='public', mime_type=files.guess_mime(file), ) galley_object = core_models.Galley.objects.create( article=article, file=file_object, is_remote=False, label='Render Galley', type=os.path.splitext(file)[1]) article.render_galley = galley_object article.save() print('New file and galley created.') else: print('No file was found in folder {0}'.format(folder)) except submission_models.Article.DoesNotExist: print('No article found with ID {0}'.format(folder)) cache.clear() print('Cache cleared.')