# -*- coding: utf-8 -*-

"""
bootstrap.helpers
~~~~~~~~~~~~~~~~~

:Copyright: 2006-2016 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from byceps.blueprints.user_group.models import UserGroup
from byceps.services.orga.models import OrgaFlag
from byceps.services.orga_team.models import OrgaTeam, \
    Membership as OrgaTeamMembership
from byceps.services.user.models.user import User
from byceps.services.user import service as user_service

from .util import add_to_database


# -------------------------------------------------------------------- #
# users


@add_to_database
def create_user(screen_name, email_address, *, enabled=False):
    user = user_service.build_user(screen_name, email_address)
    user.enabled = enabled
    return user


def find_user(screen_name):
    return User.query.filter_by(screen_name=screen_name).one_or_none()


def get_user(screen_name):
    return User.query.filter_by(screen_name=screen_name).one()


# -------------------------------------------------------------------- #
# orga teams


@add_to_database
def promote_orga(brand, user):
    return OrgaFlag(brand.id, user.id)


@add_to_database
def create_orga_team(party, title):
    return OrgaTeam(party.id, title.id)


@add_to_database
def assign_user_to_orga_team(user, orga_team, *, duties=None):
    membership = OrgaTeamMembership(orga_team.id, user.id)
    if duties:
        membership.duties = duties
    return membership


def get_orga_team(team_id):
    return OrgaTeam.query.get(team_id)


# -------------------------------------------------------------------- #
# user groups


@add_to_database
def create_user_group(creator, title, description=None):
    return UserGroup(creator, title, description)

@@ -172,196 +172,44 @@
 eps.
-blueprints.user_group.models import UserGroup%0Afrom byceps.services.orga.models import OrgaFlag%0Afrom byceps.services.orga_team.models import OrgaTeam, %5C%0A    Membership as OrgaTeamMembership
+services.orga.models import OrgaFlag
 %0Afro
@@ -896,13 +896,8 @@
 orga
- team
 s%0A%0A%0A
@@ -944,16 +944,16 @@
  user):%0A
+
     retu
@@ -987,596 +987,4 @@
 id)%0A
-%0A%0A@add_to_database%0Adef create_orga_team(party, title):%0A    return OrgaTeam(party.id, title.id)%0A%0A%0A@add_to_database%0Adef assign_user_to_orga_team(user, orga_team, *, duties=None):%0A    membership = OrgaTeamMembership(orga_team.id, user.id)%0A    if duties:%0A        membership.duties = duties%0A    return membership%0A%0A%0Adef get_orga_team(team_id):%0A    return OrgaTeam.query.get(team_id)%0A%0A%0A# -------------------------------------------------------------------- #%0A# user groups%0A%0A%0A@add_to_database%0Adef create_user_group(creator, title, description=None):%0A    return UserGroup(creator, title, description)%0A

Drop some bootstrap helpers (orga teams can be created via admin UI)