# Path: apps/station/models.py
# class Station(Model):
# 
#     team        = ForeignKey("team.Team", related_name="stations")
#     responsible = ForeignKey(Account)
#     active      = BooleanField(default=True)
#     street      = CharField(max_length=1024)
#     city        = CharField(max_length=1024)
#     postalcode  = CharField(max_length=1024)
#     
#     # metadata
#     created_by  = ForeignKey(Account, related_name="stations_created")
#     created_on  = DateTimeField(auto_now_add=True)
#     updated_by  = ForeignKey(Account, related_name="stations_updated")
#     updated_on  = DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         args = (
#             self.postalcode, 
#             self.city, 
#             self.street, 
#             self.responsible
#         )
#         return u"%s %s / %s (%s)" % args
# 
#     class Meta:
#         
#         ordering = ['street']
# 
# 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):


import datetime
from django.core.exceptions import PermissionDenied
from apps.station.models import Station
from apps.borrow.models import Borrow
from apps.team import control as team_control
        # bikes heading to the station in the future
        len(Borrow.objects.filter(active=True, dest=station, finish__gte=today))
    )


def can_delete(account, station):
    return can_edit(account, station) and not in_use(station)


def can_deactivate(account, station):
    return can_edit(account, station) and not in_use(station)


def create( account, team, responsible, 
            active, street, city, postalcode ):
    station = Station()
    station.created_by = account
    station.updated_by = account
    station.team = team
    station.responsible = responsible
    station.active = active
    station.street = street
    station.city = city
    station.postalcode = postalcode
    station.save()
    return station


def edit( account, station, responsible, 
          active, street, city, postalcode ):

# Path: apps/station/models.py
# class Station(Model):
# 
#     team        = ForeignKey("team.Team", related_name="stations")
#     responsible = ForeignKey(Account)
#     active      = BooleanField(default=True)
#     street      = CharField(max_length=1024)
#     city        = CharField(max_length=1024)
#     postalcode  = CharField(max_length=1024)
#     
#     # metadata
#     created_by  = ForeignKey(Account, related_name="stations_created")
#     created_on  = DateTimeField(auto_now_add=True)
#     updated_by  = ForeignKey(Account, related_name="stations_updated")
#     updated_on  = DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         args = (
#             self.postalcode, 
#             self.city, 
#             self.street, 
#             self.responsible
#         )
#         return u"%s %s / %s (%s)" % args
# 
#     class Meta:
#         
#         ordering = ['street']
# 
# 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/station/control.py
import datetime
from django.core.exceptions import PermissionDenied
from apps.station.models import Station
from apps.borrow.models import Borrow
from apps.team import control as team_control

        # bikes heading to the station in the future
        len(Borrow.objects.filter(active=True, dest=station, finish__gte=today))
    )


def can_delete(account, station):
    return can_edit(account, station) and not in_use(station)


def can_deactivate(account, station):
    return can_edit(account, station) and not in_use(station)


def create( account, team, responsible, 
            active, street, city, postalcode ):
    station = Station()
    station.created_by = account
    station.updated_by = account
    station.team = team
    station.responsible = responsible
    station.active = active
    station.street = street
    station.city = city
    station.postalcode = postalcode
    station.save()
    return station


def edit( account, station, responsible, 
          active, street, city, postalcode ):

    if not active and not can_deactivate(account, station):