# -*- coding: utf-8 -*-
'''活动管理接口'''

from flask import request
from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User
from model.role import identity

from flask_jwt_extended import (fresh_jwt_required)


def demand_activity_add(activity_id, data):
    '''添加活动需求'''
    for demand_id in data:
        demand = Demand.get(Demand.id == demand_id)
        if not demand.activityId:
            demand.activityId = activity_id
            # Demand.update(activityId=activity_id).where(Demand.id == demand_id).execute()
            demand.save()


def demand_activity_del(activity_id, data):
    '''删除活动需求'''
    for demand_id in data:
        demand = Demand.get(Demand.id == demand_id)
        if demand.activityId == activity_id:
            demand.activityId = None
            # Demand.update(activityId=activity_id).where(Demand.id == demand_id).execute()
            demand.save()


def demand_activity_done(activity_id, data):
    '''更新活动需求'''
    for demand_id in data:
        demand = Demand.get(Demand.id == demand_id)
        if demand.activityId == activity_id:
            demand.status = 1
            # Demand.update(activityId=activity_id).where(Demand.id == demand_id).execute()
            demand.save()


@fresh_jwt_required
@identity.check_permission("create", 'task')
def activity_add():
    '''创建项目活动'''
    data = request.json
    if 'memberId' in data and data['memberId']:
        data['status'] = 'dev-ing'
    with database.atomic():
        activity_id = ActivityBase.create(**data).id
        if 'memberId' in data and data['memberId']:
            for member_id in data['memberId']:
                role = ProjectMember.get(
                    ProjectMember.projectId == data['projectId'],
                    ProjectMember.memberId == member_id).role
                ActivityMember.create(**{
                    'activityId': activity_id,
                    'memberId': member_id,
                    'role': role
                })
        demand_activity_add(activity_id, data['demand'])
    return {"msg": 'ok'}


@fresh_jwt_required
@identity.check_permission("update", 'task')
def activity_update():
    '''更新项目活动'''
    data = request.json
    activity_id = data.pop('activityId')
    with database.atomic():
        if 'del_memberId' in data:
            for member_id in data.pop('del_memberId'):
                ActivityMember.delete().where(
                    (ActivityMember.activityId == activity_id) &
                    (ActivityMember.memberId == member_id)).execute()
        if 'memberId' in data:
            if not 'status' in data or not data['status']:
                data['status'] = 'dev-ing'
            for member_id in data.pop('memberId'):
                ActivityMember.get_or_create(
                    activityId=activity_id,
                    memberId=member_id,
                    role=ProjectMember.get(
                        (ProjectMember.projectId == data['projectId'])
                        & (ProjectMember.memberId == member_id)).role)
        if 'done_demand' in data:
            demand_activity_done(activity_id, data.pop('done_demand'))
        if 'demand' in data:
            demand_activity_add(activity_id, data.pop('demand'))
        if 'del_demand' in data:
            demand_activity_del(activity_id, data.pop('del_demand'))
        Activity.update(**data).where(Activity.id == activity_id).execute()
    return {"msg": 'ok'}


@fresh_jwt_required
def activity_detail(activity_id):
    '''查询活动详情
        GET /api/activity/<int:activity_id>
    '''
    activity = Activity.findOne(Activity.id == activity_id)
    activity['member'] = list(
        ActivityMember.find(ActivityMember.role, User.username,
                            User.email, User.id).join(User)
        .where(ActivityMember.activityId == activity_id))
    activity['demand'] = list(
        Demand.find().where(Demand.activityId == activity_id))
    return activity


@fresh_jwt_required
def project_user(project_id):
    '''查询项目成员'''
    return {
        "data":
        list(
            ProjectMember.find(
                ProjectMember.role,
                User).join(User).where(ProjectMember.projectId == project_id))
    }

[((1287, 1330), 'model.role.identity.check_permission', 'identity.check_permission', (['"""create"""', '"""task"""'], {}), "('create', 'task')\n", (1312, 1330), False, 'from model.role import identity\n'), ((2114, 2157), 'model.role.identity.check_permission', 'identity.check_permission', (['"""update"""', '"""task"""'], {}), "('update', 'task')\n", (2139, 2157), False, 'from model.role import identity\n'), ((3602, 3646), 'model.db.Activity.findOne', 'Activity.findOne', (['(Activity.id == activity_id)'], {}), '(Activity.id == activity_id)\n', (3618, 3646), False, 'from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User\n'), ((355, 389), 'model.db.Demand.get', 'Demand.get', (['(Demand.id == demand_id)'], {}), '(Demand.id == demand_id)\n', (365, 389), False, 'from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User\n'), ((693, 727), 'model.db.Demand.get', 'Demand.get', (['(Demand.id == demand_id)'], {}), '(Demand.id == demand_id)\n', (703, 727), False, 'from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User\n'), ((1036, 1070), 'model.db.Demand.get', 'Demand.get', (['(Demand.id == demand_id)'], {}), '(Demand.id == demand_id)\n', (1046, 1070), False, 'from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User\n'), ((1484, 1501), 'model.db.database.atomic', 'database.atomic', ([], {}), '()\n', (1499, 1501), False, 'from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User\n'), ((2272, 2289), 'model.db.database.atomic', 'database.atomic', ([], {}), '()\n', (2287, 2289), False, 'from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User\n'), ((1525, 1552), 'model.db.ActivityBase.create', 'ActivityBase.create', ([], {}), '(**data)\n', (1544, 1552), False, 'from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User\n'), ((1841, 1934), 'model.db.ActivityMember.create', 'ActivityMember.create', ([], {}), "(**{'activityId': activity_id, 'memberId': member_id,\n    'role': role})\n", (1862, 1934), False, 'from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User\n'), ((3899, 3912), 'model.db.Demand.find', 'Demand.find', ([], {}), '()\n', (3910, 3912), False, 'from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User\n'), ((1678, 1783), 'model.db.ProjectMember.get', 'ProjectMember.get', (["(ProjectMember.projectId == data['projectId'])", '(ProjectMember.memberId == member_id)'], {}), "(ProjectMember.projectId == data['projectId'], \n    ProjectMember.memberId == member_id)\n", (1695, 1783), False, 'from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User\n'), ((3372, 3395), 'model.db.Activity.update', 'Activity.update', ([], {}), '(**data)\n', (3387, 3395), False, 'from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User\n'), ((3686, 3762), 'model.db.ActivityMember.find', 'ActivityMember.find', (['ActivityMember.role', 'User.username', 'User.email', 'User.id'], {}), '(ActivityMember.role, User.username, User.email, User.id)\n', (3705, 3762), False, 'from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User\n'), ((2902, 3012), 'model.db.ProjectMember.get', 'ProjectMember.get', (["((ProjectMember.projectId == data['projectId']) & (ProjectMember.memberId ==\n    member_id))"], {}), "((ProjectMember.projectId == data['projectId']) & (\n    ProjectMember.memberId == member_id))\n", (2919, 3012), False, 'from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User\n'), ((4098, 4142), 'model.db.ProjectMember.find', 'ProjectMember.find', (['ProjectMember.role', 'User'], {}), '(ProjectMember.role, User)\n', (4116, 4142), False, 'from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User\n'), ((2397, 2420), 'model.db.ActivityMember.delete', 'ActivityMember.delete', ([], {}), '()\n', (2418, 2420), False, 'from model.db import database, Activity, ActivityMember, Demand, ActivityBase, ProjectMember, User\n')]