# Path: apps/page/models.py
# class Page(models.Model):
# 
#     team        = models.ForeignKey("team.Team", related_name="pages")
#     link        = models.SlugField(unique=True)
#     name        = models.CharField(max_length=1024)
#     content     = SanitizedCharField(
#                     max_length=50000, allowed_tags=SANITIZER_ALLOWED_TAGS, 
#                     allowed_attributes=SANITIZER_ALLOWED_ATTRIBUTES, strip=False
#                 )
# 
#     order       = models.IntegerField(blank=True, null=True)
# 
#     # meta
#     created_by  = models.ForeignKey(Account, related_name="pages_created")
#     created_on  = models.DateTimeField(auto_now_add=True)
#     updated_by  = models.ForeignKey(Account, related_name="pages_updated")
#     updated_on  = models.DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return u"%s: %s" % (self.team.name, self.name)
# 
#     class Meta:                                                                                                 
#                                                                                                                 
#         unique_together = (("team", "name"), ("team", "link")) 
#         ordering = ["order", "name"]
# 
# Path: config/settings.py
# SANITIZER_ALLOWED_TAGS = [
#     'a', 'p', 'b', 'u', 'i', 'h1', 'h2', 'h3', 'img', 'br', 'table', 'tbody',
#     'tr', 'td', 'span', 'li', 'ul'
# ]
# 
# SANITIZER_ALLOWED_ATTRIBUTES = [ 'href', 'src', 'style', 'width', 'height']


from django import forms
from django.utils.translation import ugettext as _
from sanitizer.forms import SanitizedCharField
from apps.common.shortcuts import uslugify
from apps.page.models import Page
from config.settings import SANITIZER_ALLOWED_TAGS, SANITIZER_ALLOWED_ATTRIBUTES
            raise forms.ValidationError(_("ERROR_NAME_USED"))
        if bool(len(Page.objects.filter(link=link, team=self.team))):
            raise forms.ValidationError(_("ERROR_NAME_USED"))
        return cleaned_data


class EditPageForm(forms.Form):

    name = forms.CharField(label=_("PAGE_NAME"))
    content = SanitizedCharField(label=_("CONTENT"), widget=forms.Textarea,
                                 max_length=50000, allowed_tags=SANITIZER_ALLOWED_TAGS,
                                 allowed_attributes=SANITIZER_ALLOWED_ATTRIBUTES, 
                                 strip=False)
    order = forms.IntegerField(label=_("ORDER"), required=False)

    def __init__(self, *args, **kwargs):
        self.page = kwargs.pop("page")
        super(EditPageForm, self).__init__(*args, **kwargs)
        self.fields["name"].initial = self.page.name
        self.fields["content"].initial = self.page.content
        self.fields["order"].initial = self.page.order

    def clean(self):
        cleaned_data = super(EditPageForm, self).clean()
        name = cleaned_data.get("name").strip()
        link = uslugify(name)
        if len(link) < 3:
            raise forms.ValidationError(_("ERROR_NAME_TO_SHORT"))
        if link in _RESERVED_NAMES:
            raise forms.ValidationError(_("ERROR_NAME_RESERVED"))

# Path: apps/page/models.py
# class Page(models.Model):
# 
#     team        = models.ForeignKey("team.Team", related_name="pages")
#     link        = models.SlugField(unique=True)
#     name        = models.CharField(max_length=1024)
#     content     = SanitizedCharField(
#                     max_length=50000, allowed_tags=SANITIZER_ALLOWED_TAGS, 
#                     allowed_attributes=SANITIZER_ALLOWED_ATTRIBUTES, strip=False
#                 )
# 
#     order       = models.IntegerField(blank=True, null=True)
# 
#     # meta
#     created_by  = models.ForeignKey(Account, related_name="pages_created")
#     created_on  = models.DateTimeField(auto_now_add=True)
#     updated_by  = models.ForeignKey(Account, related_name="pages_updated")
#     updated_on  = models.DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return u"%s: %s" % (self.team.name, self.name)
# 
#     class Meta:                                                                                                 
#                                                                                                                 
#         unique_together = (("team", "name"), ("team", "link")) 
#         ordering = ["order", "name"]
# 
# Path: config/settings.py
# SANITIZER_ALLOWED_TAGS = [
#     'a', 'p', 'b', 'u', 'i', 'h1', 'h2', 'h3', 'img', 'br', 'table', 'tbody',
#     'tr', 'td', 'span', 'li', 'ul'
# ]
# 
# SANITIZER_ALLOWED_ATTRIBUTES = [ 'href', 'src', 'style', 'width', 'height']


# Path: apps/page/forms.py
from django import forms
from django.utils.translation import ugettext as _
from sanitizer.forms import SanitizedCharField
from apps.common.shortcuts import uslugify
from apps.page.models import Page
from config.settings import SANITIZER_ALLOWED_TAGS, SANITIZER_ALLOWED_ATTRIBUTES

            raise forms.ValidationError(_("ERROR_NAME_USED"))
        if bool(len(Page.objects.filter(link=link, team=self.team))):
            raise forms.ValidationError(_("ERROR_NAME_USED"))
        return cleaned_data


class EditPageForm(forms.Form):

    name = forms.CharField(label=_("PAGE_NAME"))
    content = SanitizedCharField(label=_("CONTENT"), widget=forms.Textarea,
                                 max_length=50000, allowed_tags=SANITIZER_ALLOWED_TAGS,
                                 allowed_attributes=SANITIZER_ALLOWED_ATTRIBUTES, 
                                 strip=False)
    order = forms.IntegerField(label=_("ORDER"), required=False)

    def __init__(self, *args, **kwargs):
        self.page = kwargs.pop("page")
        super(EditPageForm, self).__init__(*args, **kwargs)
        self.fields["name"].initial = self.page.name
        self.fields["content"].initial = self.page.content
        self.fields["order"].initial = self.page.order

    def clean(self):
        cleaned_data = super(EditPageForm, self).clean()
        name = cleaned_data.get("name").strip()
        link = uslugify(name)
        if len(link) < 3:
            raise forms.ValidationError(_("ERROR_NAME_TO_SHORT"))
        if link in _RESERVED_NAMES:
            raise forms.ValidationError(_("ERROR_NAME_RESERVED"))

        if len(Page.objects.filter(name=name, team=self.page.team)) > 1: