Пример #1
0
def transaction(request, t_uid):

    # 检查登录状态
    if 'login_id' not in request.session:
        return failMSG('no login')

    # 检查方法
    if request.method != 'POST':
        return failMSG('method error')

    self_uid = request.session['login_id']

    try:
        rdata = json.loads(request.body)
    except Exception as e:
        return failMSG('get json data error')

    try:
        tra_coins = rdata['coin']
    except Exception as e:
        return failMSG('parameter error')

    try:
        t_user = User.objects.filter(UserID=self_uid)
        o_user = User.objects.filter(UserID=t_uid)
    except Exception as e:
        return failMSG('db error')
    else:
        if t_user.count() == 1 and o_user.count() == 1:
            t_coin = t_user[0].coins.all()[0]
            if t_coin.Coin > tra_coins:
                o_coin = o_user[0].coins.all()[0]
                t_coin.Coin = t_coin.Coin - tra_coins
                o_coin.Coin = o_coin.Coin + tra_coins
                t_coin.save()
                o_coin.save()
                # flow
                t_flow = CoinFlow.objects.create(Uid=t_user[0],
                                                 Title='交易',
                                                 Type='transaction',
                                                 TimeStamp=time.strftime(
                                                     "%Y-%m-%d %H:%M:%S",
                                                     time.localtime()),
                                                 Flow=-tra_coins)
                # flow
                t_flow = CoinFlow.objects.create(Uid=o_user[0],
                                                 Title='交易',
                                                 Type='transaction',
                                                 TimeStamp=time.strftime(
                                                     "%Y-%m-%d %H:%M:%S",
                                                     time.localtime()),
                                                 Flow=tra_coins)
                return okMSG({'coin': t_coin.Coin})
            else:
                return failMSG('not enough coin')
        else:
            return failMSG('error when search user')

    return failMSG('fail')
Пример #2
0
def getRecent(request):
    # 检查 method
    if request.method != 'GET':
        return failMSG('wrong method')

    response, err = getAllAsgResponse()
    if err:
        return failMSG(err)

    return okMSG(response)
Пример #3
0
def getRecentByClassAndPages(request, t_class, t_pages):
    # 检查 method
    if request.method != 'GET':
        return failMSG('wrong method')

    response, err = getAsgResponseByClass((t_pages - 1) * 20, 20, t_class)
    if err:
        return failMSG(err)

    return okMSG(response)
Пример #4
0
def getRecentByClass(request, t_class):
    # 检查 method
    if request.method != 'GET':
        return failMSG('wrong method')

    response, err = getAllAsgResponseByClass(t_class)
    if err:
        return failMSG(err)

    return okMSG(response)
Пример #5
0
def getAnswerByAid(request, t_aid):
    # 检查登录状态
    if 'login_id' not in request.session:
        return failMSG('no login')

    # 从 session 获取 uid
    t_uid = request.session['login_id']
    if type(t_uid) != type(1):
        t_uid = int(t_uid)

    t_asg, err = getAsg(t_aid)
    if err:
        return failMSG(err)

    # 检查 creator
    # 只有 creator 可以查看统计答案
    if t_asg.Creator.UserID != t_uid:
        return failMSG('not creator')

    # 获取问卷信息
    response, err = getQuestionnaireResponse(t_aid)
    if err:
        return failMSG(err)

    # 获取 aid=t_aid 的 options
    try:
        t_options = Options.objects.filter(Aid=t_aid)
    except Exception as e:
        return failMSG('db error when get options')
    else:
        if t_options.count() > 0:
            for os in t_options:
                response[str(os.Oid)] = []
                t_answers = os.ans.all()
                for answer in t_answers:
                    temp = {}
                    temp['user'] = '******' % (answer.Uid.UserID,
                                              answer.Uid.Nickname)
                    temp['value'] = answer.Value
                    temp['timestamp'] = answer.TimeStamp
                    response[str(os.Oid)].append(temp)

            return okMSG(response)
        else:
            return failMSG('no options')

    return failMSG('fail')
Пример #6
0
def flow(request):
    # 检查登录状态
    if 'login_id' not in request.session:
        return failMSG('no login')

    # 检查方法
    if request.method != 'GET':
        return failMSG('method error')

    t_uid = request.session['login_id']

    t_user, err = searchUser(t_uid)
    if err:
        return failMSG(err)

    try:
        t_flow = t_user.cf.all().order_by('-TimeStamp')
    except Exception as e:
        print(e)
        return failMSG('db error when get flow')
    else:
        response = {}
        response['flows'] = []

        try:
            for t_f in t_flow:
                temp = {}
                temp['title'] = t_f.Title
                temp['type'] = t_f.Type
                temp['flow'] = t_f.Flow
                temp['timestamp'] = t_f.TimeStamp
                response['flows'].append(temp)
        except Exception as e:
            print(e)
            return failMSG('create flow fail')
        else:
            return okMSG(response)

    return failMSG('fail')
Пример #7
0
def controller(request, t_aid):

    # GET 方法, 不用登录
    # 获取问卷
    if request.method == 'GET':
        response, err = getQuestionnaireResponse(t_aid)
        if err:
            return failMSG(err)

        return okMSG(response)

    # 检查登录状态
    if 'login_id' not in request.session:
        return failMSG('no login')

    # 从 session 获取 uid
    t_uid = request.session['login_id']
    if type(t_uid) != type(1):
        t_uid = int(t_uid)

    # delete 方法, 删除问卷
    if request.method == 'DELETE':
        # 拿到 model
        t_asg, err = getAsg(t_aid)
        if err:
            return failMSG(err)

        # 检查 creator
        if t_asg.Creator.UserID == t_uid:
            t_asg.delete()
            return okMSG()
        else:
            return failMSG('not creator')

    # POST 方法, 提交问卷答案
    if request.method == 'POST':

        try:
            rdata = json.loads(request.body)
        except Exception as e:
            return failMSG('get json data error')

        try:
            t_answers = rdata['answers']
        except Exception as e:
            return failMSG('body parameter error')

        # 检查是否已经回答
        err = alreadyAnswer(t_uid, t_aid)
        if err:
            return failMSG(err)

        # 取 user asg 的 model
        t_user, err = searchUser(t_uid)
        if err:
            return failMSG(err)
        t_asg, err = getAsg(t_aid)
        if err:
            return failMSG(err)

        # 创建 answer
        for ans in t_answers:
            print(ans)
            print(ans['qid'])
            t_que, err = getQuestion(ans['qid'])
            if err:
                delAns(t_uid, t_aid)
                return failMSG(err)
            t_opt, err = getOption(ans['oid'])
            if err:
                delAns(t_uid, t_aid)
                return failMSG(err)

            try:
                t_ans = Answer.objects.create(Oid=t_opt,
                                              Qid=t_que,
                                              Aid=t_asg,
                                              Uid=t_user,
                                              Value=ans['value'],
                                              TimeStamp=ans['timestamp'])
            except Exception as e:
                print(e)
                delAns(t_uid, t_aid)
                return failMSG('db error when create answer')

        # 获得t_one.Coin闲钱报酬
        try:
            t_c = t_user.coins.all()[0]
            t_one = t_asg.qnncoin.all()[0]
            t_c.Coin += t_one.Coin
            t_asg.Coins -= t_one.Coin
            t_c.save()
            t_asg.save()
            # flow
            t_flow = CoinFlow.objects.create(Uid=t_user,
                                             Title=t_asg.Title,
                                             Type='post questionnaire',
                                             TimeStamp=time.strftime(
                                                 "%Y-%m-%d %H:%M:%S",
                                                 time.localtime()),
                                             Flow=t_one.Coin)
        except Exception as e:
            return failMSG('only get coin fail')

        return okMSG()

    return failMSG('wrong method')
Пример #8
0
def publish(request):
    # 检查登录状态
    if 'login_id' not in request.session:
        return failMSG('no login')

    # 检查 method
    if request.method != 'POST':
        return failMSG('wrong method')

    # 从 session 获取 uid
    t_uid = int(request.session['login_id'])

    try:
        rdata = json.loads(request.body)
    except Exception as e:
        return failMSG('get json data error')

    # 从 body 获取参数
    try:
        t_title = rdata['title']
        t_description = rdata['description']
        t_type = 'questionnaire'
        t_creator = t_uid
        # 单份问卷的闲钱奖励
        t_coin = int(rdata['coin'])
        # 问卷份数
        t_copy = int(rdata['copy'])
        t_createTime = rdata['createTime']
        t_startTime = rdata['startTime']
        t_endTime = rdata['endTime']
        t_questions = rdata['questions']
        t_options = rdata['options']
    except Exception as e:
        return failMSG('parameter error')

    # 检查用户和闲钱
    t_creator, err = searchUser(t_creator)
    if err:
        return failMSG(err)
    err = checkDeposit(t_creator, t_coin * t_copy)
    if err:
        return failMSG(err)

    # 创建 assignment 并 扣除相应闲钱
    try:
        t_asg = Assignment.objects.create(Title=t_title,
                                          Description=t_description,
                                          Type=t_type,
                                          Creator=t_creator,
                                          Coins=t_coin * t_copy,
                                          CreateTime=t_createTime,
                                          StartTime=t_startTime,
                                          EndTime=t_endTime)
        ctor_coin = t_creator.coins.all()[0]
        ctor_coin.Coin -= t_coin
        ctor_coin.save()
        # flow
        t_flow = CoinFlow.objects.create(Uid=t_creator,
                                         Title=t_title,
                                         Type='create questionnaire',
                                         TimeStamp=time.strftime(
                                             "%Y-%m-%d %H:%M:%S",
                                             time.localtime()),
                                         Flow=-t_coin * t_copy)
    except Exception as e:
        return failMSG('create asg fail')

    # 创建qnn coin
    try:
        t_qc = QnnCoin.objects.create(Aid=t_asg, Coin=t_coin, Copy=t_copy)
    except Exception as e:
        return failMSG('create qc fail')

    # 准备创建 question 和 option
    # q_length 有多少个 question
    # has_option 检查每个 question 是否有 option
    # opt_index 下一个创建的 option 序号
    q_length = len(t_questions)
    has_option = False
    opt_index = 0
    for index in range(q_length):
        # 创建 question
        try:
            temp = t_questions[index]
            t_que = Questions.objects.create(Aid=t_asg,
                                             Title=temp['title'],
                                             Type=temp['type'])
        except Exception as e:
            print(temp)
            print(e)
            return failMSG('create question fail')

        # 创建 option
        temp = t_options[opt_index]
        while temp['questionIndex'] == index:
            has_option = True
            try:
                t_opt = Options.objects.create(Qid=t_que,
                                               Aid=t_asg,
                                               Value=temp['value'])
            except Exception as e:
                return failMSG('create option fail')
            opt_index += 1
            if opt_index < len(t_options):
                temp = t_options[opt_index]
            else:
                break

        # 检查是否每个 question 都有 option
        if has_option == False:
            err = deleteAsgByAid(t_asg)
            if err:
                return failMSG('some questions miss option and ' + err)
            return failMSG('some questions miss option')

        has_option = False

    return okMSG({'aid': t_asg.Aid})
Пример #9
0
def getMyAsg(request, t_class):
    # 检查 method
    if request.method != 'GET':
        return failMSG('wrong method')

    # 检查登录状态
    if 'login_id' not in request.session:
        return failMSG('no login')

    # 从 session 获取 uid
    t_uid = int(request.session['login_id'])

    # 检查 get 参数
    if t_class != 'all' and t_class != 'questionnaire' and t_class != 'qa' and t_class != 'answer':
        return failMSG('parameter error')

    if t_class == 'all':
        response = {}
        response['assignments'] = []
        t_user, err = searchUser(t_uid)
        if err:
            return failMSG(err)

        # 数据库操作
        try:
            t_asg = t_user.asg.all().order_by('-CreateTime')
        except Exception as e:
            return failMSG('db error when get my asg')
        else:
            for asg in t_asg:
                temp = {}
                temp['title'] = asg.Title
                temp['description'] = asg.Description
                temp['type'] = asg.Type
                temp['aid'] = asg.Aid
                temp['creator'] = asg.Creator.Nickname
                temp['coin'] = asg.Coins
                temp['createTime'] = asg.CreateTime
                temp['startTime'] = asg.StartTime
                temp['endTime'] = asg.EndTime
                temp['answerCount'] = 0
                temp['bestCount'] = 0
                temp['unit'] = 0
                temp['copy'] = 0
                if asg.Type == 'qa':
                    temp['answerCount'] = asg.qas.all().count()
                    temp['bestCount'] = asg.qab.all().count()
                else:
                    temp['unit'] = asg.qnncoin.all()[0].Coin
                    temp['copy'] = asg.qnncoin.all()[0].Copy
                response['assignments'].append(temp)
            response['asgCount'] = t_asg.count()
            return okMSG(response)
        return failMSG('fail')

    if t_class == 'questionnaire' or t_class == 'qa':
        response = {}
        response['assignments'] = []
        t_user, err = searchUser(t_uid)
        if err:
            return failMSG(err)

        # 数据库操作
        try:
            t_asg = t_user.asg.filter(Type=t_class).order_by('-CreateTime')
        except Exception as e:
            return failMSG('db error when get my asg')
        else:
            for asg in t_asg:
                temp = {}
                temp['title'] = asg.Title
                temp['description'] = asg.Description
                temp['type'] = asg.Type
                temp['aid'] = asg.Aid
                temp['creator'] = asg.Creator.Nickname
                temp['coin'] = asg.Coins
                temp['createTime'] = asg.CreateTime
                temp['startTime'] = asg.StartTime
                temp['endTime'] = asg.EndTime
                temp['answerCount'] = 0
                temp['bestCount'] = 0
                temp['unit'] = 0
                temp['copy'] = 0
                if asg.Type == 'qa':
                    temp['answerCount'] = asg.qas.all().count()
                    temp['bestCount'] = asg.qab.all().count()
                else:
                    temp['unit'] = asg.qnncoin.all()[0].Coin
                    temp['copy'] = asg.qnncoin.all()[0].Copy
                response['assignments'].append(temp)
            response['asgCount'] = t_asg.count()
            return okMSG(response)
        return failMSG('fail')

    if t_class == 'answer':
        response = {}
        response['assignments'] = []
        t_user, err = searchUser(t_uid)
        if err:
            return failMSG(err)

        t_ans = t_user.qas.all().order_by('-TimeStamp')

        try:
            t_asg = set()

            for x in t_ans:
                t_asg.add(x.Aid)

            for asg in t_asg:
                temp = {}
                temp['title'] = asg.Title
                temp['description'] = asg.Description
                temp['type'] = asg.Type
                temp['aid'] = asg.Aid
                temp['creator'] = asg.Creator.Nickname
                temp['coin'] = asg.Coins
                temp['createTime'] = asg.CreateTime
                temp['startTime'] = asg.StartTime
                temp['endTime'] = asg.EndTime
                temp['answerCount'] = 0
                temp['bestCount'] = 0
                temp['unit'] = 0
                temp['copy'] = 0
                if asg.Type == 'qa':
                    temp['answerCount'] = asg.qas.all().count()
                    temp['bestCount'] = asg.qab.all().count()
                else:
                    temp['unit'] = asg.qnncoin.all()[0].Coin
                    temp['copy'] = asg.qnncoin.all()[0].Copy
                response['assignments'].append(temp)
            # response['asgCount'] = t_asg.count()
            return okMSG(response)
        except Exception as e:
            print(e)
            return failMSG('my answer fail')

    return failMSG('fail')
Пример #10
0
def self(request):

    # 检查登录状态
    if 'login_id' not in request.session:
        return failMSG('no login')

    t_uid = int(request.session['login_id'])

    # GET 方法
    # 获取coin数量
    if request.method == 'GET':
        try:
            t_user = User.objects.filter(UserID=t_uid)
        except Exception as e:
            return failMSG('db error')
        else:
            if t_user.count() == 1:
                t_user = t_user[0]
                t_coins = t_user.coins.all()[0].Coin
                return okMSG({'coin': t_coins})
            else:
                return failMSG('no such user')

    # POST 方法
    # 充值闲钱
    if request.method == 'POST':

        try:
            rdata = json.loads(request.body)
        except Exception as e:
            return failMSG('get json data error')

        t_c = rdata['coin']

        try:
            t_user = User.objects.filter(UserID=t_uid)
        except Exception as e:
            return failMSG('db error')
        else:
            if t_user.count() == 1:
                t_coin = t_user[0].coins.all()[0]
                t_coin.Coin = t_coin.Coin + t_c
                t_coin.save()
                # flow
                t_flow = CoinFlow.objects.create(Uid=t_user[0],
                                                 Title='充值',
                                                 Type='Recharge',
                                                 TimeStamp=time.strftime(
                                                     "%Y-%m-%d %H:%M:%S",
                                                     time.localtime()),
                                                 Flow=t_c)
                return okMSG({'coin': t_coin.Coin})
            else:
                return failMSG('no such user')

    # DELETE 方法
    # 弄丢50闲钱
    if request.method == 'DELETE':
        try:
            t_user = User.objects.filter(UserID=t_uid)
        except Exception as e:
            return failMSG('db error')
        else:
            if t_user.count() == 1:
                t_coin = t_user[0].coins.all()[0]
                if t_coin.Coin >= 50:
                    t_coin.Coin = t_coin.Coin - 50
                    t_coin.save()
                    # flow
                    t_flow = CoinFlow.objects.create(Uid=t_user[0],
                                                     Title='丢失',
                                                     Type='lose',
                                                     TimeStamp=time.strftime(
                                                         "%Y-%m-%d %H:%M:%S",
                                                         time.localtime()),
                                                     Flow=-50)
                    return okMSG({'coin': t_coin.Coin})
                return failMSG('not enough coin')
            else:
                return failMSG('no such user')

    return failMSG('method error')