# Path: apps/team/models.py
# class Team(Model):
# 
#     link        = SlugField(unique=True) # team url name 
#     links       = ManyToManyField('link.Link', null=True, blank=True) # team social media links
#     name        = CharField(max_length=1024, unique=True)
#     country     = CountryField()
#     members     = ManyToManyField(Account, null=True, blank=True, 
#                                   related_name="teams") 
#     logo        = ProcessedImageField(upload_to=_upload_to, 
#                                       processors=[ResizeToFill(525, 100)],
#                                       format='PNG', options={'quality': 90})
#     active      = BooleanField(default=False)
#     application = TextField() 
# 
#     # meta
#     created_by  = ForeignKey(Account, related_name='team_created')
#     created_on  = DateTimeField(auto_now_add=True)
#     updated_by  = ForeignKey(Account, related_name='team_updated')
#     updated_on  = DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return u"%s (%s)" % (self.name, self.country.name)
# 
#     class Meta:
#         
#         ordering = ['name']
# 
# Path: apps/team/models.py
# class JoinRequest(Model):
# 
#     # main data
#     team = ForeignKey( # team user wants to join
#         'team.Team', related_name='join_requests'
#     )
#     requester = ForeignKey( # user who is requesting to join
#         Account, related_name='join_requests_made'
#     ) 
#     processor = ForeignKey( # user who answerd the request
#         Account, related_name='join_requests_processed', 
#         null=True, blank=True
#     )
#     status = CharField(
#         max_length=256, choices=STATUS_CHOICES, default='PENDING'
#     )
#     application = TextField() # reason given by user to join
#     response = TextField(blank=True) # reason given by processor
# 
#     # meta
#     # created_by = requester
#     # updated_by = processor
#     created_on = DateTimeField(auto_now_add=True)
#     updated_on = DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return u"%s > %s (%s)" % (self.requester, self.team, self.status)
# 
#     class Meta:
#         
#         ordering = ['-status', 'created_on']
# 
# Path: apps/team/models.py
# class RemoveRequest(Model):
# 
#     # main data
#     team = ForeignKey( # team to remove user from
#         'team.Team', 
#         related_name='remove_requests'
#     )
#     concerned = ForeignKey( # user to be removed
#         Account, related_name='remove_requests_concerned'
#     )
#     requester = ForeignKey( # user who is requesting the removel
#         Account, related_name='remove_requests_made'
#     )
#     processor = ForeignKey( # user who processed the request
#         Account, related_name='remove_requests_processed', 
#         null=True, blank=True
#     )
#     status = CharField(
#         max_length=256, choices=STATUS_CHOICES, default='PENDING'
#     )
#     reason = TextField() # reason given by by the requester
#     response = TextField(blank=True) # reason given by processor
# 
#     # meta
#     # created_by = requester
#     # updated_by = processor
#     created_on = DateTimeField(auto_now_add=True)
#     updated_on = DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return u"%s < %s (%s)" % (self.concerned, self.team, self.status)
# 
#     class Meta:
# 
#         ordering = ['-status', 'created_on']
# 
# 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/team/signals.py

# 
# 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):


import os
from django.shortcuts import get_object_or_404
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.dispatch import receiver
from allauth.account.models import EmailAddress
from apps.team.models import Team
from apps.team.models import JoinRequest
from apps.common.shortcuts import uslugify
from apps.team.models import RemoveRequest
from apps.link import control as link_control
from apps.team import signals
from apps.common.shortcuts import send_mail
from apps.account import control as account_control
        remove_request.status = status
        remove_request.save()
        if status == "ACCEPTED":
            remove_request.team.members.remove(remove_request.concerned)
    signals.remove_request_processed.send(sender=process_remove_request, 
                                          remove_request=remove_request)
    return remove_request


#########
# LINKS #
#########


def site_link_exists(team, site):
    return bool(list(team.links.filter(site=site)))


def can_create_link(account, team, site, profile):
    return (link_control.valid_profile_format(profile, site) and 
            not site_link_exists(team, site) and is_member(account, team))


def can_delete_link(account, team, link):
    return link in team.links.all() and is_member(account, team)


def link_create(account, team, site, profile):
    if not can_create_link(account, team, site, profile):
        raise PermissionDenied

# Path: apps/team/models.py
# class Team(Model):
# 
#     link        = SlugField(unique=True) # team url name 
#     links       = ManyToManyField('link.Link', null=True, blank=True) # team social media links
#     name        = CharField(max_length=1024, unique=True)
#     country     = CountryField()
#     members     = ManyToManyField(Account, null=True, blank=True, 
#                                   related_name="teams") 
#     logo        = ProcessedImageField(upload_to=_upload_to, 
#                                       processors=[ResizeToFill(525, 100)],
#                                       format='PNG', options={'quality': 90})
#     active      = BooleanField(default=False)
#     application = TextField() 
# 
#     # meta
#     created_by  = ForeignKey(Account, related_name='team_created')
#     created_on  = DateTimeField(auto_now_add=True)
#     updated_by  = ForeignKey(Account, related_name='team_updated')
#     updated_on  = DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return u"%s (%s)" % (self.name, self.country.name)
# 
#     class Meta:
#         
#         ordering = ['name']
# 
# Path: apps/team/models.py
# class JoinRequest(Model):
# 
#     # main data
#     team = ForeignKey( # team user wants to join
#         'team.Team', related_name='join_requests'
#     )
#     requester = ForeignKey( # user who is requesting to join
#         Account, related_name='join_requests_made'
#     ) 
#     processor = ForeignKey( # user who answerd the request
#         Account, related_name='join_requests_processed', 
#         null=True, blank=True
#     )
#     status = CharField(
#         max_length=256, choices=STATUS_CHOICES, default='PENDING'
#     )
#     application = TextField() # reason given by user to join
#     response = TextField(blank=True) # reason given by processor
# 
#     # meta
#     # created_by = requester
#     # updated_by = processor
#     created_on = DateTimeField(auto_now_add=True)
#     updated_on = DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return u"%s > %s (%s)" % (self.requester, self.team, self.status)
# 
#     class Meta:
#         
#         ordering = ['-status', 'created_on']
# 
# Path: apps/team/models.py
# class RemoveRequest(Model):
# 
#     # main data
#     team = ForeignKey( # team to remove user from
#         'team.Team', 
#         related_name='remove_requests'
#     )
#     concerned = ForeignKey( # user to be removed
#         Account, related_name='remove_requests_concerned'
#     )
#     requester = ForeignKey( # user who is requesting the removel
#         Account, related_name='remove_requests_made'
#     )
#     processor = ForeignKey( # user who processed the request
#         Account, related_name='remove_requests_processed', 
#         null=True, blank=True
#     )
#     status = CharField(
#         max_length=256, choices=STATUS_CHOICES, default='PENDING'
#     )
#     reason = TextField() # reason given by by the requester
#     response = TextField(blank=True) # reason given by processor
# 
#     # meta
#     # created_by = requester
#     # updated_by = processor
#     created_on = DateTimeField(auto_now_add=True)
#     updated_on = DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return u"%s < %s (%s)" % (self.concerned, self.team, self.status)
# 
#     class Meta:
# 
#         ordering = ['-status', 'created_on']
# 
# 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/team/signals.py

# 
# 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/team/control.py
import os
from django.shortcuts import get_object_or_404
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.dispatch import receiver
from allauth.account.models import EmailAddress
from apps.team.models import Team
from apps.team.models import JoinRequest
from apps.common.shortcuts import uslugify
from apps.team.models import RemoveRequest
from apps.link import control as link_control
from apps.team import signals
from apps.common.shortcuts import send_mail
from apps.account import control as account_control

        remove_request.status = status
        remove_request.save()
        if status == "ACCEPTED":
            remove_request.team.members.remove(remove_request.concerned)
    signals.remove_request_processed.send(sender=process_remove_request, 
                                          remove_request=remove_request)
    return remove_request


#########
# LINKS #
#########


def site_link_exists(team, site):
    return bool(list(team.links.filter(site=site)))


def can_create_link(account, team, site, profile):
    return (link_control.valid_profile_format(profile, site) and 
            not site_link_exists(team, site) and is_member(account, team))


def can_delete_link(account, team, link):
    return link in team.links.all() and is_member(account, team)


def link_create(account, team, site, profile):
    if not can_create_link(account, team, site, profile):
        raise PermissionDenied

    link = link_control.create(account, site, profile)