import re

from django import forms
from django.core.exceptions import ValidationError

from tower import ugettext as _
from tower import ugettext_lazy as _lazy

from mozillians.groups.helpers import stringify_groups
from mozillians.groups.models import Group


class SortForm(forms.Form):
    """Group Index Sort Form."""
    sort = forms.ChoiceField(required=False,
                             choices=(('name', _lazy(u'Name A-Z')),
                                      ('-num_members', _lazy(u'Most Members')),
                                      ('num_members', _lazy(u'Fewest Members'))))

    def clean_sort(self):
        if self.cleaned_data['sort'] == '':
            return 'name'
        return self.cleaned_data['sort']


class GroupWidget(forms.TextInput):

    def render(self, name, value, attrs=None):
        if not (value is None or isinstance(value, basestring)):
            value = stringify_groups(Group.objects.get(pk=v) for v in value)

        return super(GroupWidget, self).render(name, value, attrs)


class GroupField(forms.CharField):
    widget = GroupWidget

    def clean(self, value):
        """Groups are saved in lowercase because it's easy and
        consistent.

        """
        value = super(GroupField, self).clean(value)

        if not re.match(r'^[a-zA-Z0-9 .:,-]*$', value):
            raise ValidationError(_(u'Groups can only contain alphanumeric '
                                    'characters, dashes, spaces.'))

        values = [g.strip() for g in value.lower().split(',')
                  if g and ',' not in g]

        groups = []
        for g in values:
            (group, created) = Group.objects.get_or_create(name=g)

            if group.is_visible:
                groups.append(group)

        return groups

@@ -1,15 +1,4 @@
-import re%0A%0A
 from
@@ -22,231 +22,49 @@
 rms%0A
-from django.core.exceptions import ValidationError%0A%0Afrom tower import ugettext as _%0Afrom tower import ugettext_lazy as _lazy%0A%0Afrom mozillians.groups.helpers import stringify_groups%0Afrom mozillians.groups.models import Group
+%0Afrom tower import ugettext_lazy as _lazy
 %0A%0A%0Ac
@@ -541,1052 +541,4 @@
 '%5D%0A%0A
-%0Aclass GroupWidget(forms.TextInput):%0A%0A    def render(self, name, value, attrs=None):%0A        if not (value is None or isinstance(value, basestring)):%0A            value = stringify_groups(Group.objects.get(pk=v) for v in value)%0A%0A        return super(GroupWidget, self).render(name, value, attrs)%0A%0A%0Aclass GroupField(forms.CharField):%0A    widget = GroupWidget%0A%0A    def clean(self, value):%0A        %22%22%22Groups are saved in lowercase because it's easy and%0A        consistent.%0A%0A        %22%22%22%0A        value = super(GroupField, self).clean(value)%0A%0A        if not re.match(r'%5E%5Ba-zA-Z0-9 .:,-%5D*$', value):%0A            raise ValidationError(_(u'Groups can only contain alphanumeric '%0A                                    'characters, dashes, spaces.'))%0A%0A        values = %5Bg.strip() for g in value.lower().split(',')%0A                  if g and ',' not in g%5D%0A%0A        groups = %5B%5D%0A        for g in values:%0A            (group, created) = Group.objects.get_or_create(name=g)%0A%0A            if group.is_visible:%0A                groups.append(group)%0A%0A        return groups%0A

Remove some unused code from groups/forms.py