<commit_before>import re

from django import forms
from django.core.mail import EmailMultiAlternatives

from mailer.models import Message, make_message


class MessageForm(forms.ModelForm):
    from_email = forms.CharField()
    to = forms.CharField()
    subject = forms.CharField()
    body = forms.CharField(widget=forms.Textarea())
    body_html = forms.CharField(required=False, widget=forms.Textarea())

    def __init__(self, *args, **kwargs):
        instance = kwargs.get('instance')
        if instance:
            if not kwargs.get('initial'):
                kwargs['initial'] = {}
            kwargs['initial']['from_email'] = instance.from_address
            kwargs['initial']['to'] = instance.to_addresses
            kwargs['initial']['subject'] = instance.subject
            kwargs['initial']['body'] = instance.body
            kwargs['initial']['body_html'] = instance.body_html
        return super(MessageForm, self).__init__(*args, **kwargs)

    def save(self, commit=True):
        instance = super(MessageForm, self).save(commit=False)
        instance = make_message(db_msg=instance,
                                from_email=self.cleaned_data['from_email'],
                                to=re.split(", *", self.cleaned_data['to']),
                                subject=self.cleaned_data['subject'],
                                body=self.cleaned_data['body'])

        body_html = self.cleaned_data['body_html']
        if body_html:
            email = instance.email
            email = EmailMultiAlternatives(email.subject, email.body, email.from_email, email.to)
            email.attach_alternative(body_html, "text/html")
            instance.email = email

        if commit:
            instance.save()
        return instance

    class Meta:
        model = Message
        fields = ('from_email', 'to', 'subject', 'body', 'when_added', 'priority')
<commit_msg>cosmetic changes to Message admin form<commit_after>import re

from django import forms
from django.core.mail import EmailMultiAlternatives
from django.contrib.admin.widgets import AdminTextInputWidget, AdminTextareaWidget

from mailer.models import Message, make_message


class MessageForm(forms.ModelForm):
    from_email = forms.CharField(widget=AdminTextInputWidget())
    to = forms.CharField(widget=AdminTextInputWidget())
    subject = forms.CharField(widget=AdminTextInputWidget())
    body = forms.CharField(widget=AdminTextareaWidget)
    body_html = forms.CharField(required=False, widget=AdminTextareaWidget)

    def __init__(self, *args, **kwargs):
        instance = kwargs.get('instance')
        if instance:
            if not kwargs.get('initial'):
                kwargs['initial'] = {}
            kwargs['initial']['from_email'] = instance.from_address
            kwargs['initial']['to'] = instance.to_addresses
            kwargs['initial']['subject'] = instance.subject
            kwargs['initial']['body'] = instance.body
            kwargs['initial']['body_html'] = instance.body_html
        return super(MessageForm, self).__init__(*args, **kwargs)

    def save(self, commit=True):
        instance = super(MessageForm, self).save(commit=False)
        instance = make_message(db_msg=instance,
                                from_email=self.cleaned_data['from_email'],
                                to=re.split(", *", self.cleaned_data['to']),
                                subject=self.cleaned_data['subject'],
                                body=self.cleaned_data['body'])

        body_html = self.cleaned_data['body_html']
        if body_html:
            email = instance.email
            email = EmailMultiAlternatives(email.subject, email.body, email.from_email, email.to)
            email.attach_alternative(body_html, "text/html")
            instance.email = email

        if commit:
            instance.save()
        return instance

    class Meta:
        model = Message
        fields = ('from_email', 'to', 'subject', 'body', 'body_html', 'when_added', 'priority')

{'fraction_non_alphanumeric': 0.11942517343904856, 'fraction_numerical': 0.0, 'mean_word_length': 2.563106796116505, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2077898', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1058, 'n_words': 207}