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

from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from simplemathcaptcha.fields import MathCaptchaField
from snowpenguin.django.recaptcha2.fields import ReCaptchaField
from snowpenguin.django.recaptcha2.widgets import ReCaptchaWidget

from core import models as core_models
from journal import models as journal_models


class JournalForm(forms.ModelForm):

    class Meta:
        model = journal_models.Journal
        fields = ('code', 'domain')

    def __init__(self, *args, **kwargs):
        super(JournalForm, self).__init__(*args, **kwargs)
        if settings.URL_CONFIG == 'path':
            self.fields.pop('domain')

    def save(self, commit=True, request=None):
        journal = super(JournalForm, self).save(commit=False)

        if settings.URL_CONFIG == 'path':
            journal.domain = '{press_domain}/{path}'.format(press_domain=request.press.domain, path=journal.code)

        if commit:
            journal.save()

        return journal


class ContactForm(forms.ModelForm):

    if settings.CAPTCHA_TYPE == 'simple_math':
        question_template = _('What is %(num1)i %(operator)s %(num2)i? ')
        are_you_a_robot = MathCaptchaField(label=_('Answer this question: '))
    elif settings.CAPTCHA_TYPE == 'recaptcha':
        are_you_a_robot = ReCaptchaField(widget=ReCaptchaWidget())
    else:
        are_you_a_robot = forms.CharField(widget=forms.HiddenInput(), required=False)


    def __init__(self, *args, **kwargs):
        subject = kwargs.pop('subject', None)
        contacts = kwargs.pop('contacts', None)
        super(ContactForm, self).__init__(*args, **kwargs)

        if subject:
            self.fields['subject'].initial = subject

        if contacts:
            contact_choices = []
            for contact in contacts:
                contact_choices.append([contact.email, '{name}, {role}'.format(name=contact.name, role=contact.role)])
            self.fields['recipient'].widget = forms.Select(choices=contact_choices)
            #self.fields['recipient'].required = True

    class Meta:
        model = core_models.Contact
        fields = ('recipient', 'sender', 'subject', 'body')<commit_msg>Added form for resending logged emails.<commit_after>__copyright__ = "Copyright 2017 Birkbeck, University of London"
__author__ = "<NAME> & <NAME>"
__license__ = "AGPL v3"
__maintainer__ = "Birkbeck Centre for Technology and Publishing"

from django import forms
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.utils.safestring import mark_safe

from simplemathcaptcha.fields import MathCaptchaField
from snowpenguin.django.recaptcha2.fields import ReCaptchaField
from snowpenguin.django.recaptcha2.widgets import ReCaptchaWidget
from django_summernote.widgets import SummernoteWidget

from core import models as core_models
from journal import models as journal_models


class JournalForm(forms.ModelForm):

    class Meta:
        model = journal_models.Journal
        fields = ('code', 'domain')

    def __init__(self, *args, **kwargs):
        super(JournalForm, self).__init__(*args, **kwargs)
        if settings.URL_CONFIG == 'path':
            self.fields.pop('domain')

    def save(self, commit=True, request=None):
        journal = super(JournalForm, self).save(commit=False)

        if settings.URL_CONFIG == 'path':
            journal.domain = '{press_domain}/{path}'.format(press_domain=request.press.domain, path=journal.code)

        if commit:
            journal.save()

        return journal


class ContactForm(forms.ModelForm):

    if settings.CAPTCHA_TYPE == 'simple_math':
        question_template = _('What is %(num1)i %(operator)s %(num2)i? ')
        are_you_a_robot = MathCaptchaField(label=_('Answer this question: '))
    elif settings.CAPTCHA_TYPE == 'recaptcha':
        are_you_a_robot = ReCaptchaField(widget=ReCaptchaWidget())
    else:
        are_you_a_robot = forms.CharField(widget=forms.HiddenInput(), required=False)

    def __init__(self, *args, **kwargs):
        subject = kwargs.pop('subject', None)
        contacts = kwargs.pop('contacts', None)
        super(ContactForm, self).__init__(*args, **kwargs)

        if subject:
            self.fields['subject'].initial = subject

        if contacts:
            contact_choices = []
            for contact in contacts:
                contact_choices.append([contact.email, '{name}, {role}'.format(name=contact.name, role=contact.role)])
            self.fields['recipient'].widget = forms.Select(choices=contact_choices)
            # self.fields['recipient'].required = True

    class Meta:
        model = core_models.Contact
        fields = ('recipient', 'sender', 'subject', 'body')


class ResendEmailForm(forms.Form):
    to = forms.CharField(max_length=1000, help_text='Seperate email addresses with ;')
    subject = forms.CharField(max_length=1000)
    body = forms.CharField(widget=SummernoteWidget)

    def __init__(self, *args, **kwargs):
        log_entry = kwargs.pop('log_entry')
        super(ResendEmailForm, self).__init__(*args, **kwargs)

        self.fields['to'].initial = '{to}; '.format(to=log_entry.to)
        self.fields['subject'].initial = log_entry.subject
        self.fields['body'].initial = mark_safe(log_entry.description)

{'fraction_non_alphanumeric': 0.11625811103100216, 'fraction_numerical': 0.004686373467916366, 'mean_word_length': 3.515052888527258, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3786792', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1599, 'n_words': 360}