<commit_before>from django import forms
from django.utils.translation import ugettext_lazy as _

from projects.models import *

# @@@ this is based on Tribes -- can we re-use anything?

class ProjectForm(forms.ModelForm):
    
    slug = forms.RegexField(max_length=20, regex=r'^\w+$',
        help_text = _("a short version of the name consisting only of letters, numbers and underscores."),
        error_message = _("This value must contain only letters, numbers and underscores."))
            
    def clean_slug(self):
        if Project.objects.filter(slug__iexact=self.cleaned_data["slug"]).count() > 0:
            raise forms.ValidationError(_("A project already exists with that slug."))
        return self.cleaned_data["slug"].lower()
    
    def clean_name(self):
        if Project.objects.filter(name__iexact=self.cleaned_data["name"]).count() > 0:
            raise forms.ValidationError(_("A project already exists with that name."))
        return self.cleaned_data["name"]
    
    class Meta:
        model = Project
        fields = ('name', 'slug', 'description', 'tags')


# @@@ is this the right approach, to have two forms where creation and update fields differ?

class ProjectUpdateForm(forms.ModelForm):
    
    def clean_name(self):
        if Project.objects.filter(name__iexact=self.cleaned_data["name"]).count() > 0:
            if self.cleaned_data["name"] == self.instance.name:
                pass # same instance
            else:
                raise forms.ValidationError(_("A project already exists with that name."))
        return self.cleaned_data["name"]
    
    class Meta:
        model = Project
        fields = ('name', 'description', 'tags')

class TopicForm(forms.ModelForm):
    
    class Meta:
        model = Topic
        fields = ('title', 'body', 'tags')


class TaskForm(forms.ModelForm):
    def __init__(self, project, *args, **kwargs):
        super(TaskForm, self).__init__(*args, **kwargs)
        self.fields["assignee"].queryset = self.fields["assignee"].queryset.filter(project=project)
    
    class Meta:
        model = Task
        fields = ('summary', 'detail', 'assignee', 'tags')


class AssignForm(TaskForm):
    """
    a form for changing the assignee of a task
    """
    class Meta(TaskForm.Meta):
        fields = ('assignee',)


class StatusForm(forms.ModelForm):
    """
    a form for changing the status of a task
    """
    status = forms.CharField(widget=forms.TextInput(attrs={'size':'40'}))
    
    class Meta:
        model = Task
        fields = ('status',)


class AddUserForm(forms.Form):
    
    recipient = forms.CharField(label=_(u"User"))
    
    def __init__(self, project, *args, **kwargs):
        super(AddUserForm, self).__init__(*args, **kwargs)
        self.project = project
    
    def clean_recipient(self):
        try:
            user = User.objects.get(username__exact=self.cleaned_data['recipient'])
        except User.DoesNotExist:
            raise forms.ValidationError(_("There is no user with this username."))
            
        if ProjectMember.objects.filter(project=project, user=user).count() > 0:
            raise forms.ValidationError(_("User is already a member of this project."))
        
        return self.cleaned_data['recipient']
    
    # @@@ we don't need to pass in project any more as we have self.project
    def save(self, project, user):
        new_member = User.objects.get(username__exact=self.cleaned_data['recipient'])
        project_member = ProjectMember(project=project, user=new_member)
        project_member.save()
        project.members.add(project_member)
        if notification:
            notification.send(project.member_users.all(), "projects_new_member", {"new_member": new_member, "project": project})
            notification.send([new_member], "projects_added_as_member", {"adder": user, "project": project})
        user.message_set.create(message="added %s to project" % new_member)


class AwayForm(forms.Form):
    
    away_message = forms.CharField(label=_(u"Message"))
    
    def save(self, project_member):
        project_member.away = True
        project_member.away_message = self.cleaned_data['away_message']
        project_member.away_since = datetime.now()
        project_member.save()

<commit_msg>Fixed a small bug in project member inviting.<commit_after>from django import forms
from django.utils.translation import ugettext_lazy as _

from projects.models import *

# @@@ this is based on Tribes -- can we re-use anything?

class ProjectForm(forms.ModelForm):
    
    slug = forms.RegexField(max_length=20, regex=r'^\w+$',
        help_text = _("a short version of the name consisting only of letters, numbers and underscores."),
        error_message = _("This value must contain only letters, numbers and underscores."))
            
    def clean_slug(self):
        if Project.objects.filter(slug__iexact=self.cleaned_data["slug"]).count() > 0:
            raise forms.ValidationError(_("A project already exists with that slug."))
        return self.cleaned_data["slug"].lower()
    
    def clean_name(self):
        if Project.objects.filter(name__iexact=self.cleaned_data["name"]).count() > 0:
            raise forms.ValidationError(_("A project already exists with that name."))
        return self.cleaned_data["name"]
    
    class Meta:
        model = Project
        fields = ('name', 'slug', 'description', 'tags')


# @@@ is this the right approach, to have two forms where creation and update fields differ?

class ProjectUpdateForm(forms.ModelForm):
    
    def clean_name(self):
        if Project.objects.filter(name__iexact=self.cleaned_data["name"]).count() > 0:
            if self.cleaned_data["name"] == self.instance.name:
                pass # same instance
            else:
                raise forms.ValidationError(_("A project already exists with that name."))
        return self.cleaned_data["name"]
    
    class Meta:
        model = Project
        fields = ('name', 'description', 'tags')

class TopicForm(forms.ModelForm):
    
    class Meta:
        model = Topic
        fields = ('title', 'body', 'tags')


class TaskForm(forms.ModelForm):
    def __init__(self, project, *args, **kwargs):
        super(TaskForm, self).__init__(*args, **kwargs)
        self.fields["assignee"].queryset = self.fields["assignee"].queryset.filter(project=project)
    
    class Meta:
        model = Task
        fields = ('summary', 'detail', 'assignee', 'tags')


class AssignForm(TaskForm):
    """
    a form for changing the assignee of a task
    """
    class Meta(TaskForm.Meta):
        fields = ('assignee',)


class StatusForm(forms.ModelForm):
    """
    a form for changing the status of a task
    """
    status = forms.CharField(widget=forms.TextInput(attrs={'size':'40'}))
    
    class Meta:
        model = Task
        fields = ('status',)


class AddUserForm(forms.Form):
    
    recipient = forms.CharField(label=_(u"User"))
    
    def __init__(self, project, *args, **kwargs):
        super(AddUserForm, self).__init__(*args, **kwargs)
        self.project = project
    
    def clean_recipient(self):
        try:
            user = User.objects.get(username__exact=self.cleaned_data['recipient'])
        except User.DoesNotExist:
            raise forms.ValidationError(_("There is no user with this username."))
            
        if ProjectMember.objects.filter(project=self.project, user=user).count() > 0:
            raise forms.ValidationError(_("User is already a member of this project."))
        
        return self.cleaned_data['recipient']
    
    # @@@ we don't need to pass in project any more as we have self.project
    def save(self, project, user):
        new_member = User.objects.get(username__exact=self.cleaned_data['recipient'])
        project_member = ProjectMember(project=project, user=new_member)
        project_member.save()
        project.members.add(project_member)
        if notification:
            notification.send(project.member_users.all(), "projects_new_member", {"new_member": new_member, "project": project})
            notification.send([new_member], "projects_added_as_member", {"adder": user, "project": project})
        user.message_set.create(message="added %s to project" % new_member)


class AwayForm(forms.Form):
    
    away_message = forms.CharField(label=_(u"Message"))
    
    def save(self, project_member):
        project_member.away = True
        project_member.away_message = self.cleaned_data['away_message']
        project_member.away_since = datetime.now()
        project_member.save()


{'fraction_non_alphanumeric': 0.12113491603937464, 'fraction_numerical': 0.0018529241459177765, 'mean_word_length': 2.936189608021878, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 11, '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': '3193640', 'n_tokens_mistral': 2507, 'n_tokens_neox': 2452, 'n_words': 611}