# Path: apps/bike/models.py
# class Bike(Model):
# 
#     # main data
#     team = ForeignKey('team.Team', related_name='bikes')
#     name = CharField(max_length=1024)
#     gallery = ForeignKey('gallery.Gallery')
#     description = TextField()
#     active = BooleanField(default=True)
#     reserve = BooleanField(default=False) # not requestable
#     station = ForeignKey('station.Station', related_name="bikes")
#     lockcode = CharField(max_length=1024)
#     size = CharField(max_length=64, choices=SIZE_CHOICES, default='MEDIUM')
#     lights = BooleanField(default=False) # to cycle when dark
#     
#     # metadata
#     created_by  = ForeignKey(Account, related_name="bikes_created")
#     created_on  = DateTimeField(auto_now_add=True)
#     updated_by  = ForeignKey(Account, related_name="bikes_updated")
#     updated_on  = DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     class Meta:
#         
#         ordering = ['name']
# 
# Path: apps/team/control.py
# def get_team_emails(team, excludes=None):
# def get_teams(account):
# def get_or_404(link):
# def is_member(account, team):
# def create(account, name, country, logo, application):
# def notify_staff_team_created(sender, **kwargs):
# def notify_team_join_request_created(sender, **kwargs):
# def notify_team_join_request_processed(sender, **kwargs):
# def notify_concerned_remove_request_created(sender, **kwargs):
# def notify_team_remove_request_created(sender, **kwargs):
# def notify_team_remove_request_processed(sender, **kwargs):
# def can_replace_logo(account, team):
# def replace_logo(account, team, logo):
# def can_join(account, team):
# def create_join_request(account, team, application):
# def can_process_join_request(account, join_request):
# def process_join_request(account, join_request, response, status):
# def has_remove_request(concerned, team):
# def can_create_remove_request(requester, concerned, team):
# def can_process_remove_request(account, remove_request):
# def create_remove_request(requester, concerned, team, reason):
# def process_remove_request(account, remove_request, response, status):
# def site_link_exists(team, site):
# def can_create_link(account, team, site, profile):
# def can_delete_link(account, team, link):
# def link_create(account, team, site, profile):
# def link_delete(account, team, link):
# 
# Path: apps/team/utils.py
# def assert_member(account, team):
#     if not control.is_member(account, team):
#         raise PermissionDenied
# 
# Path: apps/gallery/control.py
# def create(account, image, team):
#     """ Create a new gallery. """
#     if team:
#         assert_member(account, team)
#     gallery = Gallery()
#     gallery.created_by = account
#     gallery.updated_by = account
#     gallery.team = team
#     gallery.save()
#     picture = add(account, image, gallery)
#     gallery.primary = picture
#     gallery.save()
#     return gallery
# 
# Path: apps/gallery/control.py
# def delete(account, gallery):
#     """ Delete gallery and all pictures belonging to it. """
#     _assert_can_edit(account, gallery)
#     for picture in gallery.pictures.all():
#         remove(account, picture)
#     gallery.delete()


import datetime
from django.core.exceptions import PermissionDenied
from apps.bike.models import Bike
from apps.team import control as team_control
from apps.borrow import control as borrow_control
from apps.team.utils import assert_member
from apps.gallery.control import create as create_gallery
from apps.gallery.control import delete as delete_gallery

def has_future_borrows(bike):
    """ Checks if bike is borrowed in the future. """
    today = datetime.datetime.now().date()
    return bool(len(bike.borrows.filter(active=True, finish__gte=today)))


def currently_borrowed(bike):
    """ Checks if bike is currently borrowed. """
    today = datetime.datetime.now().date()
    qs = bike.borrows.filter(active=True)
    qs = qs.exclude(finish__lt=today)
    qs = qs.exclude(start__gt=today)
    return bool(len(qs))


def can_change_station(account, bike, station):
    """ Check if an account can change the bike station.
    Account must be a team member, bike not currently borrowed and
    station must be active.
    """
    return (station and station.active and bike.team == station.team and 
            team_control.is_member(account, bike.team) and 
            not currently_borrowed(bike))


def can_deactivate(account, bike):
    """ Check if an account can deactivate a bike.
    Account must be a team member and bike not borrowed in the future.
    """

# Path: apps/bike/models.py
# class Bike(Model):
# 
#     # main data
#     team = ForeignKey('team.Team', related_name='bikes')
#     name = CharField(max_length=1024)
#     gallery = ForeignKey('gallery.Gallery')
#     description = TextField()
#     active = BooleanField(default=True)
#     reserve = BooleanField(default=False) # not requestable
#     station = ForeignKey('station.Station', related_name="bikes")
#     lockcode = CharField(max_length=1024)
#     size = CharField(max_length=64, choices=SIZE_CHOICES, default='MEDIUM')
#     lights = BooleanField(default=False) # to cycle when dark
#     
#     # metadata
#     created_by  = ForeignKey(Account, related_name="bikes_created")
#     created_on  = DateTimeField(auto_now_add=True)
#     updated_by  = ForeignKey(Account, related_name="bikes_updated")
#     updated_on  = DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     class Meta:
#         
#         ordering = ['name']
# 
# Path: apps/team/control.py
# def get_team_emails(team, excludes=None):
# def get_teams(account):
# def get_or_404(link):
# def is_member(account, team):
# def create(account, name, country, logo, application):
# def notify_staff_team_created(sender, **kwargs):
# def notify_team_join_request_created(sender, **kwargs):
# def notify_team_join_request_processed(sender, **kwargs):
# def notify_concerned_remove_request_created(sender, **kwargs):
# def notify_team_remove_request_created(sender, **kwargs):
# def notify_team_remove_request_processed(sender, **kwargs):
# def can_replace_logo(account, team):
# def replace_logo(account, team, logo):
# def can_join(account, team):
# def create_join_request(account, team, application):
# def can_process_join_request(account, join_request):
# def process_join_request(account, join_request, response, status):
# def has_remove_request(concerned, team):
# def can_create_remove_request(requester, concerned, team):
# def can_process_remove_request(account, remove_request):
# def create_remove_request(requester, concerned, team, reason):
# def process_remove_request(account, remove_request, response, status):
# def site_link_exists(team, site):
# def can_create_link(account, team, site, profile):
# def can_delete_link(account, team, link):
# def link_create(account, team, site, profile):
# def link_delete(account, team, link):
# 
# Path: apps/team/utils.py
# def assert_member(account, team):
#     if not control.is_member(account, team):
#         raise PermissionDenied
# 
# Path: apps/gallery/control.py
# def create(account, image, team):
#     """ Create a new gallery. """
#     if team:
#         assert_member(account, team)
#     gallery = Gallery()
#     gallery.created_by = account
#     gallery.updated_by = account
#     gallery.team = team
#     gallery.save()
#     picture = add(account, image, gallery)
#     gallery.primary = picture
#     gallery.save()
#     return gallery
# 
# Path: apps/gallery/control.py
# def delete(account, gallery):
#     """ Delete gallery and all pictures belonging to it. """
#     _assert_can_edit(account, gallery)
#     for picture in gallery.pictures.all():
#         remove(account, picture)
#     gallery.delete()


# Path: apps/bike/control.py
import datetime
from django.core.exceptions import PermissionDenied
from apps.bike.models import Bike
from apps.team import control as team_control
from apps.borrow import control as borrow_control
from apps.team.utils import assert_member
from apps.gallery.control import create as create_gallery
from apps.gallery.control import delete as delete_gallery


def has_future_borrows(bike):
    """ Checks if bike is borrowed in the future. """
    today = datetime.datetime.now().date()
    return bool(len(bike.borrows.filter(active=True, finish__gte=today)))


def currently_borrowed(bike):
    """ Checks if bike is currently borrowed. """
    today = datetime.datetime.now().date()
    qs = bike.borrows.filter(active=True)
    qs = qs.exclude(finish__lt=today)
    qs = qs.exclude(start__gt=today)
    return bool(len(qs))


def can_change_station(account, bike, station):
    """ Check if an account can change the bike station.
    Account must be a team member, bike not currently borrowed and
    station must be active.
    """
    return (station and station.active and bike.team == station.team and 
            team_control.is_member(account, bike.team) and 
            not currently_borrowed(bike))


def can_deactivate(account, bike):
    """ Check if an account can deactivate a bike.
    Account must be a team member and bike not borrowed in the future.
    """

    return (team_control.is_member(account, bike.team) and 