<commit_before>from django import forms
from django.utils.translation import pgettext_lazy

from ...account.models import User
from ...core.permissions import get_permissions
from ..customer.forms import get_name_placeholder
from ..forms import PermissionMultipleChoiceField


class StaffForm(forms.ModelForm):
    user_permissions = PermissionMultipleChoiceField(
        queryset=get_permissions(),
        widget=forms.CheckboxSelectMultiple, required=False,
        label=pgettext_lazy(
            'Label above the permissions choicefield', 'Permissions'))

    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'email',
                  'user_permissions', 'is_active', 'is_staff']
        labels = {
            'first_name': pgettext_lazy(
                'Customer form: Given name field', 'Given name'),
            'last_name': pgettext_lazy(
                'Customer form: Family name field', 'Family name'),
            'email': pgettext_lazy(
                'Email', 'Email'),
            'is_active': pgettext_lazy(
                'User active toggle', 'User is active'),
            'is_staff': pgettext_lazy(
                'User staff toggle', 'User is staff')}

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

        # Non-superuser couldn't edit superuser's profile
        if (self.user and not self.user.is_superuser
                and self.instance.is_superuser):
            self.fields['email'].disabled = True
            self.fields['user_permissions'].disabled = True
            self.fields['is_active'].disabled = True
            self.fields['is_staff'].disabled = True

        # Disable editing other staff's email for non superuser staff
        if (self.user and not self.user.is_superuser
                and self.instance.is_staff):
            self.fields['email'].disabled = True

        # Disable users editing their own following fields
        if self.user == self.instance:
            self.fields['user_permissions'].disabled = True
            self.fields['is_active'].disabled = True
            self.fields['is_staff'].disabled = True

        address = self.instance.default_billing_address
        if not address:
            return
        if address.first_name:
            placeholder = get_name_placeholder(address.first_name)
            self.fields['first_name'].widget.attrs['placeholder'] = placeholder
        if address.last_name:
            placeholder = get_name_placeholder(address.last_name)
            self.fields['last_name'].widget.attrs['placeholder'] = placeholder

    def clean(self):
        cleaned_data = super().clean()

        # Remove all permissions if user is not staff
        if not cleaned_data['is_staff']:
            cleaned_data['user_permissions'] = []

        return cleaned_data
<commit_msg>Always enable user's own email address<commit_after>from django import forms
from django.utils.translation import pgettext_lazy

from ...account.models import User
from ...core.permissions import get_permissions
from ..customer.forms import get_name_placeholder
from ..forms import PermissionMultipleChoiceField


class StaffForm(forms.ModelForm):
    user_permissions = PermissionMultipleChoiceField(
        queryset=get_permissions(),
        widget=forms.CheckboxSelectMultiple, required=False,
        label=pgettext_lazy(
            'Label above the permissions choicefield', 'Permissions'))

    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'email',
                  'user_permissions', 'is_active', 'is_staff']
        labels = {
            'first_name': pgettext_lazy(
                'Customer form: Given name field', 'Given name'),
            'last_name': pgettext_lazy(
                'Customer form: Family name field', 'Family name'),
            'email': pgettext_lazy(
                'Email', 'Email'),
            'is_active': pgettext_lazy(
                'User active toggle', 'User is active'),
            'is_staff': pgettext_lazy(
                'User staff toggle', 'User is staff')}

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

        # Non-superuser couldn't edit superuser's profile
        if (self.user and not self.user.is_superuser
                and self.instance.is_superuser):
            self.fields['email'].disabled = True
            self.fields['user_permissions'].disabled = True
            self.fields['is_active'].disabled = True
            self.fields['is_staff'].disabled = True

        # Disable editing other staff's email for non superuser staff
        if (self.user and not self.user.is_superuser
                and self.instance.is_staff):
            self.fields['email'].disabled = True

        # Disable users editing their own following fields except for email
        if self.user == self.instance:
            self.fields['email'].disabled = False
            self.fields['user_permissions'].disabled = True
            self.fields['is_active'].disabled = True
            self.fields['is_staff'].disabled = True

        address = self.instance.default_billing_address
        if not address:
            return
        if address.first_name:
            placeholder = get_name_placeholder(address.first_name)
            self.fields['first_name'].widget.attrs['placeholder'] = placeholder
        if address.last_name:
            placeholder = get_name_placeholder(address.last_name)
            self.fields['last_name'].widget.attrs['placeholder'] = placeholder

    def clean(self):
        cleaned_data = super().clean()

        # Remove all permissions if user is not staff
        if not cleaned_data['is_staff']:
            cleaned_data['user_permissions'] = []

        return cleaned_data

{'fraction_non_alphanumeric': 0.1011541072640869, 'fraction_numerical': 0.0, 'mean_word_length': 2.414252607184241, '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': '3287209', 'n_tokens_mistral': 1551, 'n_tokens_neox': 1508, 'n_words': 384}