<commit_before>from . import models
from crispy_forms import layout
from django import forms
from django.contrib import auth
from entities import models as entities_models
from utils import forms as utils_forms


class BaseContent(utils_forms.FormMixin, forms.ModelForm):
    author = forms.ModelChoiceField(disabled=True, queryset=entities_models.Gestalt.objects.all(), widget=forms.HiddenInput)
    pinned = forms.BooleanField(label='Im Intro der Gruppe anheften', required=False)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['group'] = forms.ModelChoiceField(label='Gruppe', queryset=self.get_group_queryset(), required=False)

    def get_group_queryset(self):
        return entities_models.Gestalt.objects.get(pk=self.initial['author']).groups

    def save(self):
        content = super().save()
        if self.cleaned_data['group']:
            entities_models.GroupContent.objects.update_or_create(content=content, group=self.cleaned_data['group'], defaults={'pinned': self.cleaned_data['pinned']})
        else:
            entities_models.GroupContent.objects.filter(content=content).delete()
        return content


class Article(BaseContent):
    layout = ('author', 'group', 'title', utils_forms.EditorField('text'), 'pinned', utils_forms.Submit('Artikel erstellen'))

    class Meta:
        fields = ('author', 'text', 'title')
        model = models.Article

    def __init__(self, *args, **kwargs):
        kwargs['instance'] = models.Article(public=True)
        super().__init__(*args, **kwargs)


class Event(BaseContent):
    layout = ('author', 'group', 'pinned', 'title', layout.Field('time', data_component='date date-datetime'), 'place', utils_forms.EditorField('text'), 'public', utils_forms.Submit('Ereignis erstellen'))

    class Meta:
        fields = ('author', 'place', 'public', 'text', 'time', 'title')
        labels = {'text': 'Beschreibung'}
        model = models.Event


class Gallery(BaseContent):
    layout = ('author', 'group', 'title', utils_forms.EditorField('text'), 'public', 'pinned', utils_forms.Submit('Galerie erstellen'))

    class Meta:
        fields = ('author', 'public', 'text', 'title')
        labels = {'text': 'Beschreibung'}
        model = models.Gallery


class BaseMessage(utils_forms.FormMixin, forms.ModelForm):
    layout = ('recipient', 'sender', 'title', utils_forms.EditorField('text'), utils_forms.Submit('Nachricht senden'))
    sender = forms.EmailField(label='E-Mail-Adresse')

    class Meta:
        fields = ('text', 'title')
        labels = {'text': 'Nachricht', 'title': 'Betreff'}
        model = models.Article

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['recipient'] = forms.ModelChoiceField(disabled=True, label='Empfängerin', queryset=self.get_recipient_queryset(), widget=forms.HiddenInput)
        if self.initial.get('sender'):
            self.fields['sender'].disabled = True
            self.fields['sender'].widget = forms.HiddenInput

    def clean(self):
        if not self.fields['sender'].disabled:
            try:
                auth.get_user_model().objects.get(email=self.cleaned_data['sender'])
                if True: # TODO password is usable
                    self.add_error('sender', forms.ValidationError('Es gibt bereits ein Benutzerkonto mit dieser E-Mail-Adresse. Bitte melde Dich mit E-Mail-Adresse und Kennwort an.', code='existing'))
            except auth.get_user_model().DoesNotExist:
                pass
        return super().clean()

    def save(self):
        message = super().save(commit=False)
        auth.get_user_model().objects.get_or_create(email=self.cleaned_data['sender'])
        message.author = entities_models.Gestalt.objects.get(user__email=self.cleaned_data['sender'])
        message.save()
        return message


class GestaltMessage(BaseMessage):
    def get_recipient_queryset(self):
        return entities_models.Gestalt.objects.all()

    def save(self):
        message = super().save()
        entities_models.GestaltContent.objects.create(content=message, gestalt=self.cleaned_data['recipient'])

class GroupMessage(BaseMessage):
    def get_recipient_queryset(self):
        return entities_models.Group.objects.all()

    def save(self):
        message = super().save()
        entities_models.GroupContent.objects.create(content=message, group=self.cleaned_data['recipient'])


class ContentUpdate(utils_forms.FormMixin, forms.ModelForm):
    class Meta:
        fields = ('text', 'title')
        model = models.Content

    def __init__(self, *args, **kwargs):
        self.groupcontent = kwargs.pop('groupcontent', None)
        super().__init__(*args, **kwargs)<commit_msg>Add username and unusable password<commit_after>from . import models
from allauth.account import adapter as allauth_adapter
from crispy_forms import layout
from django import forms
from django.contrib import auth
from entities import models as entities_models
from utils import forms as utils_forms


class BaseContent(utils_forms.FormMixin, forms.ModelForm):
    author = forms.ModelChoiceField(disabled=True, queryset=entities_models.Gestalt.objects.all(), widget=forms.HiddenInput)
    pinned = forms.BooleanField(label='Im Intro der Gruppe anheften', required=False)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['group'] = forms.ModelChoiceField(label='Gruppe', queryset=self.get_group_queryset(), required=False)

    def get_group_queryset(self):
        return entities_models.Gestalt.objects.get(pk=self.initial['author']).groups

    def save(self):
        content = super().save()
        if self.cleaned_data['group']:
            entities_models.GroupContent.objects.update_or_create(content=content, group=self.cleaned_data['group'], defaults={'pinned': self.cleaned_data['pinned']})
        else:
            entities_models.GroupContent.objects.filter(content=content).delete()
        return content


class Article(BaseContent):
    layout = ('author', 'group', 'title', utils_forms.EditorField('text'), 'pinned', utils_forms.Submit('Artikel erstellen'))

    class Meta:
        fields = ('author', 'text', 'title')
        model = models.Article

    def __init__(self, *args, **kwargs):
        kwargs['instance'] = models.Article(public=True)
        super().__init__(*args, **kwargs)


class Event(BaseContent):
    layout = ('author', 'group', 'pinned', 'title', layout.Field('time', data_component='date date-datetime'), 'place', utils_forms.EditorField('text'), 'public', utils_forms.Submit('Ereignis erstellen'))

    class Meta:
        fields = ('author', 'place', 'public', 'text', 'time', 'title')
        labels = {'text': 'Beschreibung'}
        model = models.Event


class Gallery(BaseContent):
    layout = ('author', 'group', 'title', utils_forms.EditorField('text'), 'public', 'pinned', utils_forms.Submit('Galerie erstellen'))

    class Meta:
        fields = ('author', 'public', 'text', 'title')
        labels = {'text': 'Beschreibung'}
        model = models.Gallery


class BaseMessage(utils_forms.FormMixin, forms.ModelForm):
    layout = ('recipient', 'sender', 'title', utils_forms.EditorField('text'), utils_forms.Submit('Nachricht senden'))
    sender = forms.EmailField(label='E-Mail-Adresse')

    class Meta:
        fields = ('text', 'title')
        labels = {'text': 'Nachricht', 'title': 'Betreff'}
        model = models.Article

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['recipient'] = forms.ModelChoiceField(disabled=True, label='Empfängerin', queryset=self.get_recipient_queryset(), widget=forms.HiddenInput)
        if self.initial.get('sender'):
            self.fields['sender'].disabled = True
            self.fields['sender'].widget = forms.HiddenInput

    def clean(self):
        if not self.fields['sender'].disabled:
            try:
                user = auth.get_user_model().objects.get(email=self.cleaned_data['sender'])
                if user.has_usable_password():
                    self.add_error('sender', forms.ValidationError('Es gibt bereits ein Benutzerkonto mit dieser E-Mail-Adresse. Bitte melde Dich mit E-Mail-Adresse und Kennwort an.', code='existing'))
            except auth.get_user_model().DoesNotExist:
                pass
        return super().clean()

    def save(self):
        message = super().save(commit=False)
        user, created = auth.get_user_model().objects.get_or_create(email=self.cleaned_data['sender'])
        if created:
            allauth_adapter.get_adapter().populate_username(None, user)
            user.set_unusable_password()
            user.save()
        message.author = entities_models.Gestalt.objects.get(user__email=self.cleaned_data['sender'])
        message.save()
        return message


class GestaltMessage(BaseMessage):
    def get_recipient_queryset(self):
        return entities_models.Gestalt.objects.all()

    def save(self):
        message = super().save()
        entities_models.GestaltContent.objects.create(content=message, gestalt=self.cleaned_data['recipient'])

class GroupMessage(BaseMessage):
    def get_recipient_queryset(self):
        return entities_models.Group.objects.all()

    def save(self):
        message = super().save()
        entities_models.GroupContent.objects.create(content=message, group=self.cleaned_data['recipient'])


class ContentUpdate(utils_forms.FormMixin, forms.ModelForm):
    class Meta:
        fields = ('text', 'title')
        model = models.Content

    def __init__(self, *args, **kwargs):
        self.groupcontent = kwargs.pop('groupcontent', None)
        super().__init__(*args, **kwargs)
{'fraction_non_alphanumeric': 0.13850245499181668, 'fraction_numerical': 0.0, 'mean_word_length': 3.6982220086496875, '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': '606072', 'n_tokens_mistral': 2845, 'n_tokens_neox': 2808, 'n_words': 568}