示例#1
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))
示例#2
0
def events():
    evs = Event.query(fetchone=False,
                      extra={"state <": Event.State.DELETED},
                      orderby='open_at asc')
    resp = []
    for each in evs:
        ev = Event(**each)
        r = ev.to_dict()
        r['creator'] = ev.get_creator().to_dict()
        r['num'] = UserEvent.count(event_id=ev.id, state=UserEvent.State.INIT)
        resp.append(r)
    return str(Response(data=resp))
示例#3
0
def published_events():
    evs = Event.query(fetchone=False,
                      creator=request.user.id,
                      extra={"state <": Event.State.DELETED},
                      orderby='id desc')
    resp = []
    for each in evs:
        ev = Event(**each)
        r = ev.to_dict()
        r['num'] = UserEvent.count(event_id=ev.id, state=UserEvent.State.INIT)
        resp.append(r)
    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 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()
示例#6
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()
示例#7
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='请微信关注服务号'))