Пример #1
0
def delete_event(event_id=0):
    ev = Event.find(event_id)
    if ev.creator != request.user.id:
        return Response(code=ResponseCode.OPERATE_ERROR, msg='没有权限').out()
    ev.state = Event.State.DELETED
    ev.save()
    return Response().out()
Пример #2
0
def cancel_event(event_id=0):
    ev = Event.find(event_id)
    user = request.user
    user_ev = UserEvent.query(uid=user.id, event_id=ev.id)
    if not user_ev or user_ev['state'] != UserEvent.State.INIT:
        return Response(code=ResponseCode.DATA_NOT_EXIST, msg='暂无报名').out()

    user_ev = UserEvent(**user_ev)
    user_ev.state = UserEvent.State.CANCELED
    user_ev.save()
    return Response().out()
Пример #3
0
def event(event_id=0):
    ev = Event.find(event_id)
    resp = ev.to_dict()
    resp['creator'] = ev.get_creator().to_dict()
    resp['num'] = UserEvent.count(event_id=ev.id, state=UserEvent.State.INIT)
    resp['member'] = 0

    if request.user and UserEvent.query(
            event_id=ev.id, uid=request.user.id, state=UserEvent.State.INIT):
        resp['member'] = 1

    return str(Response(data=resp))
Пример #4
0
def joined_events():
    evs = UserEvent.query(fetchone=False,
                          uid=request.user.id,
                          state=UserEvent.State.INIT,
                          orderby='id desc')
    resp = []
    for each in evs:
        ev = Event.find(each['event_id'])
        r = ev.to_dict()
        r['join_at'] = dt_to_str(each['create_at'])
        resp.append(r)
    return Response(data=resp).out()
Пример #5
0
def user_event(event_id=0):
    ev = Event.find(event_id)
    ev_user = UserEvent.query(fetchone=False,
                              event_id=ev.id,
                              state=UserEvent.State.INIT,
                              orderby='id desc')
    resp = ev.to_dict()
    users = []
    for each in ev_user:
        user = User.find(each['uid'])
        r = user.json()
        r['join_at'] = dt_to_str(each['create_at'])
        users.append(r)

    resp['users'] = users
    resp['num'] = len(users)
    return Response(data=resp).out()
Пример #6
0
def join_event(event_id=0):
    ev = Event.find(event_id)
    user = request.user
    user_ev = UserEvent.query(uid=user.id, event_id=ev.id)
    if user_ev and user_ev['state'] == UserEvent.State.INIT:
        return Response(code=ResponseCode.DUPLICATE_DATA, msg='已经报名成功').out()

    if ev.fee <= 0:
        if user_ev:
            user_ev = UserEvent(**user_ev)
            user_ev.state = UserEvent.State.INIT
            user_ev.save()
        else:
            UserEvent(uid=user.id, event_id=ev.id).save()

        return Response().out()
    elif user.openid:
        order = Order(uid=user.id,
                      name=ev.title,
                      money=-ev.fee,
                      item_id=ev.id,
                      type=Order.Type.JOIN_EVENT)
        order.set_order_id()
        resp = order.save(return_keys=[Order.PKEY])
        order = Order.find(resp[Order.PKEY])

        wxorder = WXOrder(user, order)
        tokens = wxorder.get_token()
        if not tokens:
            return Response(code=ResponseCode.OPERATE_ERROR,
                            msg='订单生成失败').out()

        return Response(code=ResponseCode.LOW_BALANCE,
                        msg='余额不足',
                        data={
                            'need_money': ev.fee,
                            'order_id': order.id,
                            'order': tokens
                        }).out()
    else:
        return str(Response(code=ResponseCode.AUTH_REQUIRED, msg='请微信关注服务号'))
Пример #7
0
def edit_event(event_id=0):
    ev = Event.find(event_id)
    ev.fee = int(request.form.get('fee', 0)) * 100
    if ev.fee < 0:
        return Response(code=ResponseCode.PARAMETER_ERROR, msg='输入收费错误').out()
    ev.shop_id = request.form.get('shop_id', 0)
    ev.show_num = request.form.get('show_num') == 'on'
    ev.title = request.form.get('title')
    ev.user_limit = request.form.get('user_limit', 0)
    ev.description = request.form.get('description')
    ev.open_at = request.form.get('open_at')
    ev.close_at = request.form.get('close_at')

    file = request.files.get('poster')
    if file and file.filename != "" and allowed_file(file.filename):
        filename = '%s_%s' % (request.user.id,
                              datetime.now().strftime('%Y%m%d%H%M%S'))
        QiniuCloud.upload_file(file, filename, conf.qiniu_img_bucket)
        ev.poster_url = conf.qiniu_img_prefix + filename
    ev.save()

    return str(Response())