<commit_before>__copyright__ = "Copyright 2017 Birkbeck, University of London"
__author__ = "<NAME> & <NAME>"
__license__ = "AGPL v3"
__maintainer__ = "Birkbeck Centre for Technology and Publishing"

from os import listdir, makedirs
from os.path import isfile, join
import requests
from dateutil import parser as dateparser
from bs4 import BeautifulSoup
import csv

from django.contrib import messages
from django.conf import settings
from django.shortcuts import redirect, get_object_or_404
from django.urls import reverse
from django.template.loader import get_template
from django.core.validators import validate_email, ValidationError

from core import models as core_models, files
from journal import models as journal_models, issue_forms
from identifiers import models as identifier_models
from utils import render_template, notify_helpers
from utils.notify_plugins import notify_email
from events import logic as event_logic


def install_cover(journal, request):
    """ Installs the default cover for the journal (stored in Files/journal/<id>/cover.png)

    :param journal: the journal object
    :param request: the current request or None
    :return: None
    """
    owner = request.user if request.user is not None else core_models.Account(id=1)

    thumbnail_file = core_models.File(
        mime_type="image/png",
        original_filename="cover.png",
        uuid_filename="cover.png",
        label="Journal logo",
        description="Logo for the journal",
        owner=owner
    )

    thumbnail_file.save()

    journal.thumbnail_image = thumbnail_file
    journal.save()


def list_scss(journal):
    """ Lists the SCSS override files for a journal

    :param journal: the journal in question
    :return: a list of SCSS files
    """
    try:
        scss_path = join(settings.BASE_DIR, 'files', 'styling', 'journals', str(journal.id))
        makedirs(scss_path, exist_ok=True)
        return [join(scss_path, f) for f in listdir(scss_path) if isfile(join(scss_path, f))]
    except FileNotFoundError:
        return []


def get_best_galley(article, galleys):
    """
    Attempts to get the best alley possible for an article
    :param article: Article object
    :param galleys: list of Galley objects
    :return: Galley object
    """
    if article.render_galley:
        return article.render_galley

    try:
        try:
            html_galley = galleys.get(file__mime_type__in=files.HTML_MIMETYPES)
            return html_galley
        except core_models.Galley.DoesNotExist:
            pass

        try:
            xml_galley = galleys.get(file__mime_type__in=files.XML_MIMETYPES)
            return xml_galley
        except core_models.Galley.DoesNotExist:
            pass
    except core_models.Galley.MultipleObjectsReturned:
        pass

    return None


def get_galley_content(article, galleys):
    """
    Gets the best galley and returns its content
    :param article: Article object
    :param galleys: list of Galley objects
    :return: Inline content of the galley, HTML
    """
    galley = get_best_galley(article, galleys)
    return galley.file_content()


def get_doi_data(article):
    try:
        doi = identifier_models.Identifier.objects.get(id_type='doi', article=article)
        r = requests.get(doi.get_doi_url())
        return [r, doi]
    except BaseException:
        return [None, None]

<commit_msg>In the event no galley is found, return a blank string.<commit_after>__copyright__ = "Copyright 2017 Birkbeck, University of London"
__author__ = "<NAME> & <NAME>"
__license__ = "AGPL v3"
__maintainer__ = "Birkbeck Centre for Technology and Publishing"

from os import listdir, makedirs
from os.path import isfile, join
import requests
from dateutil import parser as dateparser
from bs4 import BeautifulSoup
import csv

from django.contrib import messages
from django.conf import settings
from django.shortcuts import redirect, get_object_or_404
from django.urls import reverse
from django.template.loader import get_template
from django.core.validators import validate_email, ValidationError

from core import models as core_models, files
from journal import models as journal_models, issue_forms
from identifiers import models as identifier_models
from utils import render_template, notify_helpers
from utils.notify_plugins import notify_email
from events import logic as event_logic


def install_cover(journal, request):
    """ Installs the default cover for the journal (stored in Files/journal/<id>/cover.png)

    :param journal: the journal object
    :param request: the current request or None
    :return: None
    """
    owner = request.user if request.user is not None else core_models.Account(id=1)

    thumbnail_file = core_models.File(
        mime_type="image/png",
        original_filename="cover.png",
        uuid_filename="cover.png",
        label="Journal logo",
        description="Logo for the journal",
        owner=owner
    )

    thumbnail_file.save()

    journal.thumbnail_image = thumbnail_file
    journal.save()


def list_scss(journal):
    """ Lists the SCSS override files for a journal

    :param journal: the journal in question
    :return: a list of SCSS files
    """
    try:
        scss_path = join(settings.BASE_DIR, 'files', 'styling', 'journals', str(journal.id))
        makedirs(scss_path, exist_ok=True)
        return [join(scss_path, f) for f in listdir(scss_path) if isfile(join(scss_path, f))]
    except FileNotFoundError:
        return []


def get_best_galley(article, galleys):
    """
    Attempts to get the best alley possible for an article
    :param article: Article object
    :param galleys: list of Galley objects
    :return: Galley object
    """
    if article.render_galley:
        return article.render_galley

    try:
        try:
            html_galley = galleys.get(file__mime_type__in=files.HTML_MIMETYPES)
            return html_galley
        except core_models.Galley.DoesNotExist:
            pass

        try:
            xml_galley = galleys.get(file__mime_type__in=files.XML_MIMETYPES)
            return xml_galley
        except core_models.Galley.DoesNotExist:
            pass
    except core_models.Galley.MultipleObjectsReturned:
        pass

    return None


def get_galley_content(article, galleys):
    """
    Gets the best galley and returns its content
    :param article: Article object
    :param galleys: list of Galley objects
    :return: Inline content of the galley, HTML, or a blank string
    """
    galley = get_best_galley(article, galleys)
    if galley:
        return galley.file_content()
    else:
        return ''


def get_doi_data(article):
    try:
        doi = identifier_models.Identifier.objects.get(id_type='doi', article=article)
        r = requests.get(doi.get_doi_url())
        return [r, doi]
    except BaseException:
        return [None, None]


{'fraction_non_alphanumeric': 0.07402559627690518, 'fraction_numerical': 0.0029086678301337987, 'mean_word_length': 3.338801261829653, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3908392', 'n_tokens_mistral': 2056, 'n_tokens_neox': 1990, 'n_words': 622}