# Path: users/models.py
# class User(usermodels.AbstractUser):
# 
#     # The first element in each tuple is the actual value to be stored,
#     # and the second element is the human-readable name.
#     USER_STATUS_CHOICES = (
#         ("User", "Regular User"),
#         ("Staff", "EUSA Staff"),
#         ("Candidate", "EUSA Candidate"),
#         ("Officeholder", "EUSA Officeholder")
#     )
#     sid = models.CharField("student ID", max_length=20, unique=True)
#     slug = models.SlugField(default="slug")
#     userStatus = models.CharField("user status",
#                                   max_length=12,
#                                   choices=USER_STATUS_CHOICES,
#                                   default="User")
#     title = models.CharField(max_length=100, blank=True)
#     isModerator = models.BooleanField("moderator", default=False)
#     hasProfile = models.BooleanField("public profile", default=False)
#     follows_tags = models.ManyToManyField(Tag, related_name="followers",
#                                           blank=True)
# 
#     """
#     Days between email notifications. 0 means no email notifications.
#     """
#     email_notification_frequency = models.IntegerField("days between email"
#                                                        " notifications",
#                                                        choices=[
#                                                            (3, "Every 3 days"),
#                                                            (7, "Weekly"),
#                                                            (0, "Never")
#                                                        ],
#                                                        default=3)
#     last_notification_email = models.DateField("last notification email"
#                                                "date",
#                                                default=datetime.now)
# 
#     # Use UserManager to get the create_user method, etc.
#     objects = UserManager()
# 
#     def proposed(self):
#         return Proposal.objects.all().filter(user=self)
# 
#     def comments(self):
#         return Comment.objects.all().filter(user=self)
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.username, max_length=100)
#         super(User, self).save(*args, **kwargs)
# 
#     def get_absolute_url(self):
#         return reverse("user", kwargs={"slug": self.slug})
# 
#     def get_votes(self):
#         return Vote.objects.filter(user=self)
# 
#     def get_vote_on(self, content):
#         content_type = ContentType.objects.get_for_model(content)
#         try:
#             vote = Vote.objects.get(user=self, content_type=content_type,
#                                     object_id=content.id)
#             if vote.isVoteUp:
#                 return 1
#             else:
#                 return -1
#         except Vote.DoesNotExist:
#             return 0
# 
#     def get_proposals_voted_for(self):
#         """
#         Returns a list (formerly QuerySet) of proposals that the user
#         has voted for.
#         """
#         user_votes = Vote.objects.filter(user=self)\
#                                  .filter(content_type=Proposal
#                                          .get_content_type())\
#                                  .filter(isVoteUp=True)
#         return [vote.content for vote in user_votes if vote.content]
# 
#     def get_proposals_voted_against(self):
#         """
#         Returns a list (formerly QuerySet) of proposals that
#         the user has voted against.
#         """
#         user_votes = Vote.objects.filter(user=self)\
#                                  .filter(content_type=Proposal
#                                          .get_content_type())\
#                                  .filter(isVoteUp=False)
#         return [vote.content for vote in user_votes if vote.content]
# 
#     def has_unread_notifications(self):
#         return bool(Notification.objects.get_unread(self))
# 
#     def can_write_response(self):
#         return self.userStatus == "Staff" or self.userStatus == "Officeholder"
# 
#     def __unicode__(self):
#         return self.username + " (" + self.sid + ")"
# 
# Path: core/utils.py
# def contains_swear_words(text):
#     words = re.sub(r"[^\w]", " ", text).split()
#     bad_words = [w for w in words if w.lower() in settings.PROFANITIES_LIST]
#     return bool(bad_words)
# 
# def is_sid(value):
#     sid = re.compile(r"^s\d{7}$")
#     return sid.match(value)
# 
# Path: tags/models.py
# class Tag(models.Model):
#     TAG_GROUPS = (
#         (1, "School"),
#         (2, "Liberation"),
#         (3, "Other")
#     )
#     id = models.AutoField(primary_key=True)
#     name = models.CharField(max_length=100, unique=True)
#     slug = models.SlugField(blank=False, max_length=100)
#     group = models.IntegerField(choices=TAG_GROUPS, default=1)
#     description = models.TextField(blank=True)
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.name, max_length=100)
#         super(Tag, self).save(*args, **kwargs)
# 
#     def get_absolute_url(self):
#         return reverse("tag", kwargs={"tagId": self.id, "slug": self.slug})
# 
#     def __unicode__(self):
#         return self.name
# 
#     def __str__(self):
#         return self.name


from slugify import slugify
from django import forms
from users.models import User
from core.utils import contains_swear_words, is_sid
from tags.models import Tag
    the username is blank, if the username slug already exists (and therefore
    if the username already exists), and if the username contains swear words.
    :param username: the form with the username to be checked
    :return: the same username, if it passes all checks
    """
    cleaned_username = form.cleaned_data["username"]

    # don't allow blank usernames
    if cleaned_username == "":
        raise forms.ValidationError("Username cannot be blank.")

    # don't allow usernames where the slug already exists
    slug = slugify(cleaned_username, max_length=100)
    if User.objects.exclude(sid=user.sid).filter(slug=slug)\
                                                  .exists():
        raise forms.ValidationError("User slug already exists.")

    # don't allow swear words
    if contains_swear_words(cleaned_username):
        raise forms.ValidationError("Username cannot contain swear words.")

    # don't allow SIDs as username
    if is_sid(cleaned_username):
        raise forms.ValidationError("Username cannot be a student ID.")

    return cleaned_username


class UserForm(forms.ModelForm):
    class Meta:

# Path: users/models.py
# class User(usermodels.AbstractUser):
# 
#     # The first element in each tuple is the actual value to be stored,
#     # and the second element is the human-readable name.
#     USER_STATUS_CHOICES = (
#         ("User", "Regular User"),
#         ("Staff", "EUSA Staff"),
#         ("Candidate", "EUSA Candidate"),
#         ("Officeholder", "EUSA Officeholder")
#     )
#     sid = models.CharField("student ID", max_length=20, unique=True)
#     slug = models.SlugField(default="slug")
#     userStatus = models.CharField("user status",
#                                   max_length=12,
#                                   choices=USER_STATUS_CHOICES,
#                                   default="User")
#     title = models.CharField(max_length=100, blank=True)
#     isModerator = models.BooleanField("moderator", default=False)
#     hasProfile = models.BooleanField("public profile", default=False)
#     follows_tags = models.ManyToManyField(Tag, related_name="followers",
#                                           blank=True)
# 
#     """
#     Days between email notifications. 0 means no email notifications.
#     """
#     email_notification_frequency = models.IntegerField("days between email"
#                                                        " notifications",
#                                                        choices=[
#                                                            (3, "Every 3 days"),
#                                                            (7, "Weekly"),
#                                                            (0, "Never")
#                                                        ],
#                                                        default=3)
#     last_notification_email = models.DateField("last notification email"
#                                                "date",
#                                                default=datetime.now)
# 
#     # Use UserManager to get the create_user method, etc.
#     objects = UserManager()
# 
#     def proposed(self):
#         return Proposal.objects.all().filter(user=self)
# 
#     def comments(self):
#         return Comment.objects.all().filter(user=self)
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.username, max_length=100)
#         super(User, self).save(*args, **kwargs)
# 
#     def get_absolute_url(self):
#         return reverse("user", kwargs={"slug": self.slug})
# 
#     def get_votes(self):
#         return Vote.objects.filter(user=self)
# 
#     def get_vote_on(self, content):
#         content_type = ContentType.objects.get_for_model(content)
#         try:
#             vote = Vote.objects.get(user=self, content_type=content_type,
#                                     object_id=content.id)
#             if vote.isVoteUp:
#                 return 1
#             else:
#                 return -1
#         except Vote.DoesNotExist:
#             return 0
# 
#     def get_proposals_voted_for(self):
#         """
#         Returns a list (formerly QuerySet) of proposals that the user
#         has voted for.
#         """
#         user_votes = Vote.objects.filter(user=self)\
#                                  .filter(content_type=Proposal
#                                          .get_content_type())\
#                                  .filter(isVoteUp=True)
#         return [vote.content for vote in user_votes if vote.content]
# 
#     def get_proposals_voted_against(self):
#         """
#         Returns a list (formerly QuerySet) of proposals that
#         the user has voted against.
#         """
#         user_votes = Vote.objects.filter(user=self)\
#                                  .filter(content_type=Proposal
#                                          .get_content_type())\
#                                  .filter(isVoteUp=False)
#         return [vote.content for vote in user_votes if vote.content]
# 
#     def has_unread_notifications(self):
#         return bool(Notification.objects.get_unread(self))
# 
#     def can_write_response(self):
#         return self.userStatus == "Staff" or self.userStatus == "Officeholder"
# 
#     def __unicode__(self):
#         return self.username + " (" + self.sid + ")"
# 
# Path: core/utils.py
# def contains_swear_words(text):
#     words = re.sub(r"[^\w]", " ", text).split()
#     bad_words = [w for w in words if w.lower() in settings.PROFANITIES_LIST]
#     return bool(bad_words)
# 
# def is_sid(value):
#     sid = re.compile(r"^s\d{7}$")
#     return sid.match(value)
# 
# Path: tags/models.py
# class Tag(models.Model):
#     TAG_GROUPS = (
#         (1, "School"),
#         (2, "Liberation"),
#         (3, "Other")
#     )
#     id = models.AutoField(primary_key=True)
#     name = models.CharField(max_length=100, unique=True)
#     slug = models.SlugField(blank=False, max_length=100)
#     group = models.IntegerField(choices=TAG_GROUPS, default=1)
#     description = models.TextField(blank=True)
# 
#     def save(self, *args, **kwargs):
#         self.slug = slugify(self.name, max_length=100)
#         super(Tag, self).save(*args, **kwargs)
# 
#     def get_absolute_url(self):
#         return reverse("tag", kwargs={"tagId": self.id, "slug": self.slug})
# 
#     def __unicode__(self):
#         return self.name
# 
#     def __str__(self):
#         return self.name


# Path: users/forms.py
from slugify import slugify
from django import forms
from users.models import User
from core.utils import contains_swear_words, is_sid
from tags.models import Tag

    the username is blank, if the username slug already exists (and therefore
    if the username already exists), and if the username contains swear words.
    :param username: the form with the username to be checked
    :return: the same username, if it passes all checks
    """
    cleaned_username = form.cleaned_data["username"]

    # don't allow blank usernames
    if cleaned_username == "":
        raise forms.ValidationError("Username cannot be blank.")

    # don't allow usernames where the slug already exists
    slug = slugify(cleaned_username, max_length=100)
    if User.objects.exclude(sid=user.sid).filter(slug=slug)\
                                                  .exists():
        raise forms.ValidationError("User slug already exists.")

    # don't allow swear words
    if contains_swear_words(cleaned_username):
        raise forms.ValidationError("Username cannot contain swear words.")

    # don't allow SIDs as username
    if is_sid(cleaned_username):
        raise forms.ValidationError("Username cannot be a student ID.")

    return cleaned_username


class UserForm(forms.ModelForm):
    class Meta:

        model = User