#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019-03-08 08:57
# @Author  : ┏ (゜ω゜)=☞ Airy
# @Email   : a532710813@gmail.com
# @File    : ActivityController.py
# @Software: PyCharm

from django.http import HttpRequest
from apps.http.db import models
from apps.Utils.validation.ParamValidation import validate_and_return
from apps.Utils import ReturnResult as rS
from apps.http.decorator.LoginCheckDecorator import request_check
from apps.http.user.controller import UtilsController
from apps.http.notification.controller.NotificationController import create_notification

@request_check()
def create(request: HttpRequest):
    """
    创建（发布）活动
    :param request:
    :return:
    """
    _param = validate_and_return(request, {
        'group_id': '',
        'title': '',
        'content': '',
        'place': '',
        'start_time': '',
        'end_time': '',
    })

    group_id = _param['group_id']
    _param.pop('group_id')
    _activity = models.Activity.objects.create(**_param)

    if _activity:
        group = models.Group.objects.get(pk=group_id)
        if group:
            mapping = models.ActivityBelongGroupMapping.objects.create(activity=_activity, group=group)
            if mapping:
                return rS.success({
                    'id': _activity.id
                })
            else:
                return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '创建活动mapping失败')
        else:
            return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '群组不存在')
    else:
        return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '创建活动失败')


@request_check()
def delete(request: HttpRequest):
    """
    删除活动
    :param request:
    :return:
    """
    _param = validate_and_return(request, {
        'activity_id': '',
        'access_token': ''
    })
    _activity = models.Activity.objects.filter(pk=_param['activity_id'])
    _user_id = UtilsController.get_id_by_token(_param['access_token'])
    if _activity.count() != 0:
        _activity.delete()
        return rS.success()
    else:
        return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '删除活动失败')


@request_check()
def modify(request: HttpRequest):
    """
    修改活动
    :param request:
    :return:
    """
    _param = validate_and_return(request, {
        'activity_id': '',
        'title': 'nullable',
        'content': 'nullable',
        'place': 'nullable',
        'start_time': 'nullable',
        'end_time': 'nullable',
    })
    # check what change you can notify them todo
    activity_id = _param['activity_id']
    if _param.get('title', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=activity_id).title + ' 更改为 ' + \
                            _param['title'])
    if _param.get('content', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=activity_id).content + ' 更改为 ' + \
                            _param['content'])
    if _param.get('place', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=activity_id).place + ' 更改为 ' + \
                            _param['place'])
    if _param.get('start_time', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=activity_id).start_time + ' 更改为 ' + \
                            _param['start_time'])
    if _param.get('end_time', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=activity_id).end_time + ' 更改为 ' + \
                            _param['end_time'])
    _param.pop('activity_id')
    models.Activity.objects.filter(pk=activity_id).update(**_param)

    return rS.success()


8759781faa82ec578f26ea88d51677c4fe89b088
<mask token>


@request_check()
def delete(request: HttpRequest):
    """
    删除活动
    :param request:
    :return:
    """
    _param = validate_and_return(request, {'activity_id': '',
        'access_token': ''})
    _activity = models.Activity.objects.filter(pk=_param['activity_id'])
    _user_id = UtilsController.get_id_by_token(_param['access_token'])
    if _activity.count() != 0:
        _activity.delete()
        return rS.success()
    else:
        return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '删除活动失败')


<mask token>

<mask token>


@request_check()
def create(request: HttpRequest):
    """
    创建（发布）活动
    :param request:
    :return:
    """
    _param = validate_and_return(request, {'group_id': '', 'title': '',
        'content': '', 'place': '', 'start_time': '', 'end_time': ''})
    group_id = _param['group_id']
    _param.pop('group_id')
    _activity = models.Activity.objects.create(**_param)
    if _activity:
        group = models.Group.objects.get(pk=group_id)
        if group:
            mapping = models.ActivityBelongGroupMapping.objects.create(activity
                =_activity, group=group)
            if mapping:
                return rS.success({'id': _activity.id})
            else:
                return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '创建活动mapping失败')
        else:
            return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '群组不存在')
    else:
        return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '创建活动失败')


@request_check()
def delete(request: HttpRequest):
    """
    删除活动
    :param request:
    :return:
    """
    _param = validate_and_return(request, {'activity_id': '',
        'access_token': ''})
    _activity = models.Activity.objects.filter(pk=_param['activity_id'])
    _user_id = UtilsController.get_id_by_token(_param['access_token'])
    if _activity.count() != 0:
        _activity.delete()
        return rS.success()
    else:
        return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '删除活动失败')


<mask token>

<mask token>


@request_check()
def create(request: HttpRequest):
    """
    创建（发布）活动
    :param request:
    :return:
    """
    _param = validate_and_return(request, {'group_id': '', 'title': '',
        'content': '', 'place': '', 'start_time': '', 'end_time': ''})
    group_id = _param['group_id']
    _param.pop('group_id')
    _activity = models.Activity.objects.create(**_param)
    if _activity:
        group = models.Group.objects.get(pk=group_id)
        if group:
            mapping = models.ActivityBelongGroupMapping.objects.create(activity
                =_activity, group=group)
            if mapping:
                return rS.success({'id': _activity.id})
            else:
                return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '创建活动mapping失败')
        else:
            return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '群组不存在')
    else:
        return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '创建活动失败')


@request_check()
def delete(request: HttpRequest):
    """
    删除活动
    :param request:
    :return:
    """
    _param = validate_and_return(request, {'activity_id': '',
        'access_token': ''})
    _activity = models.Activity.objects.filter(pk=_param['activity_id'])
    _user_id = UtilsController.get_id_by_token(_param['access_token'])
    if _activity.count() != 0:
        _activity.delete()
        return rS.success()
    else:
        return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '删除活动失败')


@request_check()
def modify(request: HttpRequest):
    """
    修改活动
    :param request:
    :return:
    """
    _param = validate_and_return(request, {'activity_id': '', 'title':
        'nullable', 'content': 'nullable', 'place': 'nullable',
        'start_time': 'nullable', 'end_time': 'nullable'})
    activity_id = _param['activity_id']
    if _param.get('title', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=
            activity_id).title + ' 更改为 ' + _param['title'])
    if _param.get('content', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=
            activity_id).content + ' 更改为 ' + _param['content'])
    if _param.get('place', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=
            activity_id).place + ' 更改为 ' + _param['place'])
    if _param.get('start_time', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=
            activity_id).start_time + ' 更改为 ' + _param['start_time'])
    if _param.get('end_time', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=
            activity_id).end_time + ' 更改为 ' + _param['end_time'])
    _param.pop('activity_id')
    models.Activity.objects.filter(pk=activity_id).update(**_param)
    return rS.success()

from django.http import HttpRequest
from apps.http.db import models
from apps.Utils.validation.ParamValidation import validate_and_return
from apps.Utils import ReturnResult as rS
from apps.http.decorator.LoginCheckDecorator import request_check
from apps.http.user.controller import UtilsController
from apps.http.notification.controller.NotificationController import create_notification


@request_check()
def create(request: HttpRequest):
    """
    创建（发布）活动
    :param request:
    :return:
    """
    _param = validate_and_return(request, {'group_id': '', 'title': '',
        'content': '', 'place': '', 'start_time': '', 'end_time': ''})
    group_id = _param['group_id']
    _param.pop('group_id')
    _activity = models.Activity.objects.create(**_param)
    if _activity:
        group = models.Group.objects.get(pk=group_id)
        if group:
            mapping = models.ActivityBelongGroupMapping.objects.create(activity
                =_activity, group=group)
            if mapping:
                return rS.success({'id': _activity.id})
            else:
                return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '创建活动mapping失败')
        else:
            return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '群组不存在')
    else:
        return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '创建活动失败')


@request_check()
def delete(request: HttpRequest):
    """
    删除活动
    :param request:
    :return:
    """
    _param = validate_and_return(request, {'activity_id': '',
        'access_token': ''})
    _activity = models.Activity.objects.filter(pk=_param['activity_id'])
    _user_id = UtilsController.get_id_by_token(_param['access_token'])
    if _activity.count() != 0:
        _activity.delete()
        return rS.success()
    else:
        return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '删除活动失败')


@request_check()
def modify(request: HttpRequest):
    """
    修改活动
    :param request:
    :return:
    """
    _param = validate_and_return(request, {'activity_id': '', 'title':
        'nullable', 'content': 'nullable', 'place': 'nullable',
        'start_time': 'nullable', 'end_time': 'nullable'})
    activity_id = _param['activity_id']
    if _param.get('title', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=
            activity_id).title + ' 更改为 ' + _param['title'])
    if _param.get('content', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=
            activity_id).content + ' 更改为 ' + _param['content'])
    if _param.get('place', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=
            activity_id).place + ' 更改为 ' + _param['place'])
    if _param.get('start_time', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=
            activity_id).start_time + ' 更改为 ' + _param['start_time'])
    if _param.get('end_time', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=
            activity_id).end_time + ' 更改为 ' + _param['end_time'])
    _param.pop('activity_id')
    models.Activity.objects.filter(pk=activity_id).update(**_param)
    return rS.success()

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019-03-08 08:57
# @Author  : ┏ (゜ω゜)=☞ Airy
# @Email   : a532710813@gmail.com
# @File    : ActivityController.py
# @Software: PyCharm

from django.http import HttpRequest
from apps.http.db import models
from apps.Utils.validation.ParamValidation import validate_and_return
from apps.Utils import ReturnResult as rS
from apps.http.decorator.LoginCheckDecorator import request_check
from apps.http.user.controller import UtilsController
from apps.http.notification.controller.NotificationController import create_notification

@request_check()
def create(request: HttpRequest):
    """
    创建（发布）活动
    :param request:
    :return:
    """
    _param = validate_and_return(request, {
        'group_id': '',
        'title': '',
        'content': '',
        'place': '',
        'start_time': '',
        'end_time': '',
    })

    group_id = _param['group_id']
    _param.pop('group_id')
    _activity = models.Activity.objects.create(**_param)

    if _activity:
        group = models.Group.objects.get(pk=group_id)
        if group:
            mapping = models.ActivityBelongGroupMapping.objects.create(activity=_activity, group=group)
            if mapping:
                return rS.success({
                    'id': _activity.id
                })
            else:
                return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '创建活动mapping失败')
        else:
            return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '群组不存在')
    else:
        return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '创建活动失败')


@request_check()
def delete(request: HttpRequest):
    """
    删除活动
    :param request:
    :return:
    """
    _param = validate_and_return(request, {
        'activity_id': '',
        'access_token': ''
    })
    _activity = models.Activity.objects.filter(pk=_param['activity_id'])
    _user_id = UtilsController.get_id_by_token(_param['access_token'])
    if _activity.count() != 0:
        _activity.delete()
        return rS.success()
    else:
        return rS.fail(rS.ReturnResult.UNKNOWN_ERROR, '删除活动失败')


@request_check()
def modify(request: HttpRequest):
    """
    修改活动
    :param request:
    :return:
    """
    _param = validate_and_return(request, {
        'activity_id': '',
        'title': 'nullable',
        'content': 'nullable',
        'place': 'nullable',
        'start_time': 'nullable',
        'end_time': 'nullable',
    })
    # check what change you can notify them todo
    activity_id = _param['activity_id']
    if _param.get('title', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=activity_id).title + ' 更改为 ' + \
                            _param['title'])
    if _param.get('content', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=activity_id).content + ' 更改为 ' + \
                            _param['content'])
    if _param.get('place', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=activity_id).place + ' 更改为 ' + \
                            _param['place'])
    if _param.get('start_time', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=activity_id).start_time + ' 更改为 ' + \
                            _param['start_time'])
    if _param.get('end_time', None) is not None:
        create_notification(4, activity_id, models.Activity.objects.get(pk=activity_id).end_time + ' 更改为 ' + \
                            _param['end_time'])
    _param.pop('activity_id')
    models.Activity.objects.filter(pk=activity_id).update(**_param)

    return rS.success()

