<commit_before>from django.db.utils import OperationalError
from django.contrib.contenttypes.models import ContentType

from utils import models

PLUGIN_NAME = 'News'
SHORT_NAME = 'news'
DESCRIPTION = 'This is a homepage element that renders News section.'
AUTHOR = '<NAME> & <NAME>'
VERSION = '1.0'


def install():
    import core.models as core_models
    import journal.models as journal_models
    import press.models as press_models

    # check whether this homepage element has already been installed for all journals
    journals = journal_models.Journal.objects.all()

    for journal in journals:
        content_type = ContentType.objects.get_for_model(journal)
        element, created = core_models.HomepageElement.objects.get_or_create(
            name=PLUGIN_NAME,
            configure_url='news_config',
            template_path='journal/homepage_elements/news.html',
            content_type=content_type,
            object_id=journal.pk,
            has_config=True,
            defaults={'available_to_press': True})

        element.save()

    presses = press_models.Press.objects.all()

    for press in presses:
        content_type = ContentType.objects.get_for_model(press)
        element, created = core_models.HomepageElement.objects.get_or_create(
            name=PLUGIN_NAME,
            configure_url='news_config',
            template_path='journal/homepage_elements/news.html',
            content_type=content_type,
            object_id=press.pk,
            has_config=True,
            defaults={'available_to_press': True})

        element.save()

    models.Plugin.objects.get_or_create(
        name=PLUGIN_NAME,
        version=VERSION,
        enabled=True,
        display_name='News',
        press_wide=True,
    )


def hook_registry():
    try:
        install()
        return {'yield_homepage_element_context': {'module': 'core.homepage_elements.news.hooks',
                                                   'function': 'yield_homepage_element_context'}
                }
    except OperationalError:
        # if we get here the database hasn't yet been created
        return {}
    except BaseException:
        return {}
<commit_msg>#696: Sets news items default at Plugin install<commit_after>from django.db.utils import OperationalError
from django.contrib.contenttypes.models import ContentType

from utils import models, setting_handler

PLUGIN_NAME = 'News'
SHORT_NAME = 'news'
DESCRIPTION = 'This is a homepage element that renders News section.'
AUTHOR = '<NAME> & <NAME>'
VERSION = '1.0'
DEFAULT_NEWS = 5 #Defines how many news are to be displayed by default


def install():
    import core.models as core_models
    import journal.models as journal_models
    import press.models as press_models

    # check whether this homepage element has already been installed for all journals
    journals = journal_models.Journal.objects.all()
    plugin, created = models.Plugin.objects.get_or_create(
        name=PLUGIN_NAME,
        version=VERSION,
        enabled=True,
        display_name='News',
        press_wide=True,
    )

    for journal in journals:
        content_type = ContentType.objects.get_for_model(journal)
        element, created = core_models.HomepageElement.objects.get_or_create(
            name=PLUGIN_NAME,
            configure_url='news_config',
            template_path='journal/homepage_elements/news.html',
            content_type=content_type,
            object_id=journal.pk,
            has_config=True,
            defaults={'available_to_press': True})

        element.save()
        number_of_articles = setting_handler.get_plugin_setting(
                plugin=plugin,
                setting_name='number_of_articles',
                journal=journal,
                create=True,
                pretty='Number of Articles',
        ).value
        if number_of_articles in {None, " ", ""}:
            setting_handler.save_plugin_setting(
                    plugin=plugin,
                    setting_name='number_of_articles',
                    value=DEFAULT_NEWS,
                    journal=journal,
            )

    presses = press_models.Press.objects.all()

    for press in presses:
        content_type = ContentType.objects.get_for_model(press)
        element, created = core_models.HomepageElement.objects.get_or_create(
            name=PLUGIN_NAME,
            configure_url='news_config',
            template_path='journal/homepage_elements/news.html',
            content_type=content_type,
            object_id=press.pk,
            has_config=True,
            defaults={'available_to_press': True})

        element.save()



def hook_registry():
    try:
        install()
        return {'yield_homepage_element_context': {'module': 'core.homepage_elements.news.hooks',
                                                   'function': 'yield_homepage_element_context'}
                }
    except OperationalError:
        # if we get here the database hasn't yet been created
        return {}

{'fraction_non_alphanumeric': 0.08237395791980945, 'fraction_numerical': 0.0015879317189360857, 'mean_word_length': 2.4093369418132613, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7144495', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1322, 'n_words': 296}