# Path: apps/account/models.py
# SOURCE_CHOICES = [
#     ('OTHER', _('OTHER')),
#     ('COUCHSURFING', _('COUCHSURFING')),
#     ('FACEBOOK', _('FACEBOOK')),
#     ('FRIENDS', _('FRIENDS')),
#     ('GOOGLE', _('GOOGLE')),
#     ('TWITTER', _('TWITTER')),
# ]
# 
# Path: apps/account/control.py
# def get_site_account():
# def get_email_or_404(account):
# def get_staff_emails():
# def get_superuser_emails():
# def set_passport(account, passport):
# def can_view_account(current_account, view_account):
# def edit(account, username, first_name, last_name, mobile, source, description):
# def has_fullname(account):
# def has_mobile(account):
# def has_passport(account):
# def has_required_info(account):
# def site_link_exists(account, site):
# def can_create_link(account, site, profile):
# def can_delete_link(account, link):
# def link_create(account, site, profile):
# def link_delete(account, link):
# 
# Path: apps/link/models.py
# SITE_CHOICES = [
#     ("COUCHSURFING", _("COUCHSURFING")),
#     ("BEWELCOME", _("BEWELCOME")),
#     ("FACEBOOK", _("FACEBOOK")),
#     ("TWITTER", _("TWITTER")),
# ]
# 
# Path: apps/link/control.py
# def valid_profile_format(profile, site):
# def can_create(account, site, profile):
# def ensure_https(profile):
# def create(account, site, profile):


from django.utils.translation import ugettext as _
from django.forms import Form
from django.forms import CharField
from django.forms import ChoiceField
from django.forms import Textarea
from django.forms import ValidationError
from django.forms import ImageField
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from apps.account.models import SOURCE_CHOICES
from apps.account import control
from apps.link.models import SITE_CHOICES
from apps.link import control as link_control
    def clean_username(self):
        value = self.cleaned_data["username"].strip()
        if not USERNAME_REGEX.match(value):
            raise ValidationError(_("ERROR_BAD_USERNAME_FORMAT"))
        try:
            User.objects.get(username__iexact=value)
        except User.DoesNotExist:
            return value
        if value != self.account.user.username:
            raise ValidationError(_("ERROR_USERNAME_TAKEN"))
        return value


class LinkCreate(Form):

    site = ChoiceField(choices=SITE_CHOICES, label=_("SITE"), required=True)
    profile = CharField(max_length=1024, label=_("URL"), required=True)

    def __init__(self, *args, **kwargs):
        self.account = kwargs.pop("account")
        super(LinkCreate, self).__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super(LinkCreate, self).clean()
        profile = self.cleaned_data["profile"]
        site = self.cleaned_data["site"]
        if control.site_link_exists(self.account, site):
            raise ValidationError(_("ERROR_LINK_PROFILE_FOR_SITE_EXISTS"))
        if not link_control.valid_profile_format(profile, site):
            raise ValidationError(_("ERROR_BAD_PROFILE_FORMAT"))

# Path: apps/account/models.py
# SOURCE_CHOICES = [
#     ('OTHER', _('OTHER')),
#     ('COUCHSURFING', _('COUCHSURFING')),
#     ('FACEBOOK', _('FACEBOOK')),
#     ('FRIENDS', _('FRIENDS')),
#     ('GOOGLE', _('GOOGLE')),
#     ('TWITTER', _('TWITTER')),
# ]
# 
# Path: apps/account/control.py
# def get_site_account():
# def get_email_or_404(account):
# def get_staff_emails():
# def get_superuser_emails():
# def set_passport(account, passport):
# def can_view_account(current_account, view_account):
# def edit(account, username, first_name, last_name, mobile, source, description):
# def has_fullname(account):
# def has_mobile(account):
# def has_passport(account):
# def has_required_info(account):
# def site_link_exists(account, site):
# def can_create_link(account, site, profile):
# def can_delete_link(account, link):
# def link_create(account, site, profile):
# def link_delete(account, link):
# 
# Path: apps/link/models.py
# SITE_CHOICES = [
#     ("COUCHSURFING", _("COUCHSURFING")),
#     ("BEWELCOME", _("BEWELCOME")),
#     ("FACEBOOK", _("FACEBOOK")),
#     ("TWITTER", _("TWITTER")),
# ]
# 
# Path: apps/link/control.py
# def valid_profile_format(profile, site):
# def can_create(account, site, profile):
# def ensure_https(profile):
# def create(account, site, profile):


# Path: apps/account/forms.py
from django.utils.translation import ugettext as _
from django.forms import Form
from django.forms import CharField
from django.forms import ChoiceField
from django.forms import Textarea
from django.forms import ValidationError
from django.forms import ImageField
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from apps.account.models import SOURCE_CHOICES
from apps.account import control
from apps.link.models import SITE_CHOICES
from apps.link import control as link_control

    def clean_username(self):
        value = self.cleaned_data["username"].strip()
        if not USERNAME_REGEX.match(value):
            raise ValidationError(_("ERROR_BAD_USERNAME_FORMAT"))
        try:
            User.objects.get(username__iexact=value)
        except User.DoesNotExist:
            return value
        if value != self.account.user.username:
            raise ValidationError(_("ERROR_USERNAME_TAKEN"))
        return value


class LinkCreate(Form):

    site = ChoiceField(choices=SITE_CHOICES, label=_("SITE"), required=True)
    profile = CharField(max_length=1024, label=_("URL"), required=True)

    def __init__(self, *args, **kwargs):
        self.account = kwargs.pop("account")
        super(LinkCreate, self).__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super(LinkCreate, self).clean()
        profile = self.cleaned_data["profile"]
        site = self.cleaned_data["site"]
        if control.site_link_exists(self.account, site):
            raise ValidationError(_("ERROR_LINK_PROFILE_FOR_SITE_EXISTS"))
        if not link_control.valid_profile_format(profile, site):
            raise ValidationError(_("ERROR_BAD_PROFILE_FORMAT"))

        if not control.can_create_link(self.account, site, profile):