<commit_before># -*- encoding: utf-8 -*-

from django import forms
from django.contrib.auth.models import \
    Group   # fill in custom user info then save it
from django.contrib.auth.forms import UserCreationForm

from .models import Employee


class EmployeeForm(UserCreationForm):
    """Responsible for proper saving Employee form."""

    telephone_number = forms.CharField()
    email = forms.EmailField()

    groups = forms.ModelMultipleChoiceField(
        queryset=Group.objects.all(),
        widget=forms.CheckboxSelectMultiple,
        required=False
    )

    class Meta:
        model = Employee
        fields = (
            'username',
            'first_name',
            'last_name',
            'groups',
            'telephone_number',
            'email',
            'favorite_coffee'
        )

    def __init__(self, *args, **kwargs):
        """Init form data."""
        kwargs.setdefault('label_suffix', '')  # removes ":" from labels

        super(EmployeeForm, self).__init__(*args, **kwargs)

        self.fields['username'].label = 'Nazwa użytkownika'
        self.fields['first_name'].label = 'Imię'
        self.fields['last_name'].label = 'Nazwisko'
        self.fields['telephone_number'].label = 'Numer telefonu'
        self.fields['email'].label = 'Adres email'
        self.fields['favorite_coffee'].label = 'Twoja ulubiona kawa?'
        self.fields['groups'].label = 'Grupy uprawnień użytkownika'

        self.fields['groups'].queryset |= Group.objects.all()

        if self.instance:
            self.initial['username'] = self.instance.username
            self.initial['first_name'] = self.instance.first_name
            self.initial['last_name'] = self.instance.last_name
            self.initial['telephone_number'] = self.instance.telephone_number
            self.initial['email'] = self.instance.email
            self.initial['favorite_coffee'] = self.instance.favorite_coffee

            try: 
                self.initial['groups'] = self.instance.group.get()
            except:
                # no groups
                pass

    def save(self):
        """Override of save method, to add users groups."""
        instance = super(EmployeeForm, self).save()
        
        # clear groups of user
        try:
            instance.groups.clear()
        except:
            # user had no groups
            pass
       
        # add groups to user
        for group in self.cleaned_data['groups']:
            instance.groups.add(group)

        instance.save()

        return instance<commit_msg>style<commit_after># -*- encoding: utf-8 -*-

from django import forms
from django.contrib.auth.models import \
    Group   # fill in custom user info then save it
from django.contrib.auth.forms import UserCreationForm

from .models import Employee


class EmployeeForm(UserCreationForm):
    """Responsible for proper saving Employee form."""

    telephone_number = forms.CharField()
    email = forms.EmailField()

    groups = forms.ModelMultipleChoiceField(
        queryset=Group.objects.all(),
        widget=forms.CheckboxSelectMultiple,
        required=False
    )

    class Meta:
        model = Employee
        fields = (
            'username',
            'first_name',
            'last_name',
            'groups',
            'telephone_number',
            'email',
            'favorite_coffee'
        )

    def __init__(self, *args, **kwargs):
        """Init form data."""
        kwargs.setdefault('label_suffix', '')  # removes ":" from labels

        super(EmployeeForm, self).__init__(*args, **kwargs)

        self.fields['username'].label = 'Nazwa użytkownika'
        self.fields['first_name'].label = 'Imię'
        self.fields['last_name'].label = 'Nazwisko'
        self.fields['telephone_number'].label = 'Numer telefonu'
        self.fields['email'].label = 'Adres email'
        self.fields['favorite_coffee'].label = 'Twoja ulubiona kawa?'
        self.fields['groups'].label = 'Grupy uprawnień użytkownika'

        self.fields['groups'].queryset |= Group.objects.all()

        if self.instance:
            self.initial['username'] = self.instance.username
            self.initial['first_name'] = self.instance.first_name
            self.initial['last_name'] = self.instance.last_name
            self.initial['telephone_number'] = self.instance.telephone_number
            self.initial['email'] = self.instance.email
            self.initial['favorite_coffee'] = self.instance.favorite_coffee

            try: 
                self.initial['groups'] = self.instance.group.get()
            except:
                # no groups
                pass

    def save(self, commit=True):
        """Override of save method, to add users groups."""
        instance = super(EmployeeForm, self).save(commit=True)
        
        # clear groups of user
        try:
            instance.groups.clear()
        except:
            # user had no groups
            pass

        # add groups to user
        for group in self.cleaned_data['groups']:
            instance.groups.add(group)

        instance.save()

        return instance
{'fraction_non_alphanumeric': 0.11534720871425792, 'fraction_numerical': 0.00038902937171756465, 'mean_word_length': 2.1372788285539963, 'pattern_counts': {'":': 2, '<': 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': '4763613', 'n_tokens_mistral': 1436, 'n_tokens_neox': 1380, 'n_words': 302}