<commit_before>"""
Forms for the organization application
"""

from django import forms
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.template.defaultfilters import slugify

from muckrock.organization.models import Organization
from muckrock.settings import ORG_MIN_SEATS

import autocomplete_light

class CreateForm(forms.ModelForm):
    """Allows ordinary users to create an organization"""
    class Meta:
        model = Organization
        fields = ['name']

    def clean_name(self):
        """Ensures name is unique"""
        name = self.cleaned_data['name']
        slug = slugify(name)
        try:
            Organization.objects.get(slug=slug)
        except ObjectDoesNotExist:
            return name
        raise forms.ValidationError('Organization already exists with this name.')


class StaffCreateForm(CreateForm):
    """Allows staff more control over the creation of an organization"""
    class Meta:
        model = Organization
        fields = ['name', 'owner', 'monthly_requests', 'monthly_cost', 'max_users']
        widgets = {'owner': autocomplete_light.ChoiceWidget('UserAutocomplete')}

    def clean_max_users(self):
        """Ensures that max_users is not below the minimum value."""
        max_users = self.cleaned_data['max_users']
        if max_users < ORG_MIN_SEATS:
            err_msg = 'Organizations have a %d-seat minimum' % ORG_MIN_SEATS
            raise forms.ValidationError(err_msg)
        return max_users


class UpdateForm(forms.ModelForm):
    """Allows owner to update the number of seats in their organization."""
    class Meta:
        model = Organization
        fields = ['max_users']

    def clean_max_users(self):
        """Ensures that max_users is not below the minimum value."""
        max_users = self.cleaned_data['max_users']
        if max_users < ORG_MIN_SEATS:
            err_msg = 'Organizations have a %d-seat minimum' % ORG_MIN_SEATS
            raise forms.ValidationError(err_msg)
        return max_users


class StaffUpdateForm(UpdateForm):
    """Allows staff more control over the updating of an organization"""
    class Meta:
        model = Organization
        fields = ['monthly_requests', 'monthly_cost', 'max_users']


class AddMembersForm(forms.Form):
    """A form that uses autocomplete to suggest users to add to an organization"""
    members = forms.ModelMultipleChoiceField(
        required=True,
        queryset=User.objects.all(),
        widget=autocomplete_light.MultipleChoiceWidget('UserOrganizationAutocomplete')
    )
<commit_msg>sets a minimum value on the max_users input<commit_after>"""
Forms for the organization application
"""

from django import forms
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.template.defaultfilters import slugify

from muckrock.organization.models import Organization
from muckrock.settings import ORG_MIN_SEATS

import autocomplete_light

class CreateForm(forms.ModelForm):
    """Allows ordinary users to create an organization"""
    class Meta:
        model = Organization
        fields = ['name']

    def clean_name(self):
        """Ensures name is unique"""
        name = self.cleaned_data['name']
        slug = slugify(name)
        try:
            Organization.objects.get(slug=slug)
        except ObjectDoesNotExist:
            return name
        raise forms.ValidationError('Organization already exists with this name.')


class StaffCreateForm(CreateForm):
    """Allows staff more control over the creation of an organization"""
    class Meta:
        model = Organization
        fields = ['name', 'owner', 'monthly_requests', 'monthly_cost', 'max_users']
        widgets = {'owner': autocomplete_light.ChoiceWidget('UserAutocomplete')}

    def clean_max_users(self):
        """Ensures that max_users is not below the minimum value."""
        max_users = self.cleaned_data['max_users']
        if max_users < ORG_MIN_SEATS:
            err_msg = 'Organizations have a %d-seat minimum' % ORG_MIN_SEATS
            raise forms.ValidationError(err_msg)
        return max_users


class UpdateForm(forms.ModelForm):
    """Allows owner to update the number of seats in their organization."""
    class Meta:
        model = Organization
        fields = ['max_users']
        widgets = {'max_users': forms.NumberInput(attrs={'min': ORG_MIN_SEATS})}

    def clean_max_users(self):
        """Ensures that max_users is not below the minimum value."""
        max_users = self.cleaned_data['max_users']
        if max_users < ORG_MIN_SEATS:
            err_msg = 'Organizations have a %d-seat minimum' % ORG_MIN_SEATS
            raise forms.ValidationError(err_msg)
        return max_users


class StaffUpdateForm(UpdateForm):
    """Allows staff more control over the updating of an organization"""
    class Meta:
        model = Organization
        fields = ['monthly_requests', 'monthly_cost', 'max_users']


class AddMembersForm(forms.Form):
    """A form that uses autocomplete to suggest users to add to an organization"""
    members = forms.ModelMultipleChoiceField(
        required=True,
        queryset=User.objects.all(),
        widget=autocomplete_light.MultipleChoiceWidget('UserOrganizationAutocomplete')
    )

{'fraction_non_alphanumeric': 0.08841749577623428, 'fraction_numerical': 0.0, 'mean_word_length': 3.269230769230769, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2737763', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1424, 'n_words': 427}