def p_send_sms(request): """ @api {post} /huodong/sx_summer/t/send_sms [暑假活动 教师]发送短信 @apiGroup sx_summer @apiParamExample {json} 请求示例 {'content':'家长您好....'} @apiSuccessExample {json} 成功返回 {"message": "", "error": "", "data": ', "response": "ok", "next": ""} @apiSuccessExample {json} 失败返回 {"message": "", "error": "", "data": "", "response": "fail", "next": ""} """ user = request.user args = request.QUERY.casts(content=unicode) if not args.content.strip(): return ajax_json.jsonp_fail(request, message=u'请输入短息内容') open_num = common.get_score_add_num(APP_ID_TEA, user.id, 'send_sms') if open_num < SEND_SMS_NUM: common.add_score(APP_ID_TEA, user.id, SEND_SMS_SCORE, 'send_sms', u'发送通知短信', user.city or 0) content = args.content unit_class_id = ','.join(str(u.id) for u in user.units) hub = tbktapi.Hub(request) hub.sms.post('/sms/send', {'unit_id': unit_class_id, 'content': content}) return ajax_json.jsonp_ok(request)
def add_tea_score(request): """ @api {post} /huodong/sx_summer/s/add_tea_score [暑假活动 学生]学生登录教师加积分 @apiGroup sx_summer @apiParamExample {json} 请求示例 {} @apiSuccessExample {json} 成功返回 { "message": "", "error": "", "data": {} "response": "ok", "next": "" } @apiSuccessExample {json} 失败返回 {"message": "", "error": "no_app_id", "data": "", "response": "fail", "next": ""} """ user = request.user if user.is_teacher: return ajax_json.jsonp_fail(request, message=u"学生用户才能登录") tea = com_stu.get_user_tea(user.unit.id) if user.unit else 0 if not tea: return ajax_json.jsonp_ok(request) score = STU_LOGIN_SCORE if not user.openstatus(2) else STU_LOGIN_SCORE * 2 for t in tea: num = common.get_score_add_num(APP_ID_TEA, t.id, 'stu_login_%s' % user.id) if num: return ajax_json.jsonp_ok(request) common.add_score(APP_ID_TEA, t.id, score, 'stu_login_%s' % user.id, u'学生%s登录活动,加%s积分' % (user.name, score), t.city or 0) return ajax_json.jsonp_ok(request)
def user_share(request): """ @api {post} /huodong/sx_summer/s/share [暑假活动 学生]分享 @apiGroup sx_summer @apiParamExample {json} 请求示例 {} @apiSuccessExample {json} 成功返回 { "message": "分享成功,加10分", "error": "", "data": "", "response": "ok", "next": "" } @apiSuccessExample {json} 失败返回 {"message": "每天只能分享一次", "error": "", "data": "", "response": "fail", "next": ""} """ user = request.user if user.is_teacher: return ajax_json.jsonp_fail(request, message=u"学生用户才能加分") share_num = common.get_score_add_num(APP_ID_STU, user.id, 'share') score = SHARE_SCORE if share_num < SHARE_NUM: unit = user.unit.id if user.unit else 0 common.add_score(APP_ID_STU, user.id, score, 'share', u'分享', user.city or 0, unit or 0) return ajax_json.jsonp_ok(request, message=u'分享成功,加%s分' % score) return ajax_json.jsonp_fail(request, message=u"每天只能分享一次")
def submit(user, data): if not data or not user: return False, 'no_user' exist = db.tbkt_web.active_questionnaire.get(user_id=user.id) if exist: return False, u'请勿重复提交' # 统计 count_num(data) # 保存用户记录: save_detail(user, data) # 加分 unit = user.unit.id if user.unit else 0 add_score(APP_ID_TEA, user.id, 3000, 'questionnaire', u'添加问卷调查加分', user.city or 0, unit) return True, ''
def p_send_open(request): """ @api {post} /huodong/sx_summer/t/send_open [暑假活动 教师]发送开通短信 @apiGroup sx_summer @apiParamExample {json} 请求示例 {} @apiSuccessExample {json} 成功返回 {"message": "", "error": "", "data": ', "response": "ok", "next": ""} @apiSuccessExample {json} 失败返回 {"message": "", "error": "", "data": "", "response": "fail", "next": ""} """ user = request.user args = request.QUERY.casts(content=unicode) if not args.content.strip(): return ajax_json.jsonp_fail(request, message=u'请输入短息内容') open_num = common.get_score_add_num(APP_ID_TEA, user.id, 'send_open') if open_num < SEND_OPEN_NUM: common.add_score(APP_ID_TEA, user.id, SEND_OPEN_SCORE, 'send_open', u'发送开通短信', user.city or 0) content = args.content unit_class_id = [u.id for u in user.units] hub = tbktapi.Hub(request) student = [] for i in unit_class_id: r = hub.com.post('/class/students', {"unit_id": i}) if r and r.response == 'ok': data = r.data student += (data.get('students', [])) platform_id = int(user.platform_id) if user and user.platform_id else 1 phones = ','.join(str(s['phone_number']) for s in student if s.get('status',0) not in [2,9]) r = hub.bank.post('/cmcc/open', {'phone_number': phones, 'subject_id': 2, "platform_id": platform_id}) # if not r: # return ajax_json.jsonp_fail(request, message='服务器开小差了') # if r.response == 'fail': # return ajax_json.jsonp_fail(request, message=r.message) hub = tbktapi.Hub(request) hub.sms.post('/sms/send', {'phone': phones, 'unit_id': unit_class_id, 'content': content}) return ajax_json.jsonp_ok(request)
def add_open_score(request): """ @api {post} /huodong/sx_summer/s/add_open_score [暑假活动 学生]学生开通加积分 @apiGroup sx_summer @apiParamExample {json} 请求示例 {} @apiSuccessExample {json} 成功返回 { "message": "", "error": "", "data": { "add_score":0 # 1 加1000 积分 0 未开通不加积分 } "response": "ok", "next": "" } @apiSuccessExample {json} 失败返回 {"message": "", "error": "", "data": "", "response": "fail", "next": ""} """ global user_info_locks user = request.user if user.is_teacher: return ajax_json.jsonp_fail(request, message=u"学生用户才能加分") add_score = 0 if user.id in user_info_locks: return ajax_json.jsonp_ok(request, {'add_score': add_score}) else: user_info_locks[user.id] = 1 try: if user.openstatus(2): if not db.default.score_user_detail.filter(app_id=APP_ID_STU, user_id=user.id, item_no='open').exists(): add_score = 1 unit = user.unit.id if user.unit else 0 common.add_score(APP_ID_STU, user.id, OPEN_USER_ADD_SCORE, 'open', u'开通学科获得%s积分' % OPEN_USER_ADD_SCORE, user.city or 0, unit) finally: user_info_locks.pop(user.id, None) return ajax_json.jsonp_ok(request, {'add_score': add_score})
def _test_submit(user, paper_id, status, data): """ 测试提交 :param user_id: :param paper_id:试卷ID :param status:试卷ID :param data: 做题数据 :return: 返回错题信息 """ # check = check_user_grade(user.old_user_id, paper_id) # if not check: # return test = db.tbkt_web.active_test.get(active_id=ACTIVE_ID, user_id=user.old_user_id, object_id=paper_id) if not test or test.status == 1: return if test.status == 1: return {"test_id": test.id} if int(test.status) != 1: field = [] # 批量更新 wrong_ask_ids = [] # 错题 wrong_num = 0 # 错题数 nask = 0 # 全部小题数 all_ask_ids = [] for d in data: has_wrong = False for a in d["ask_list"]: ask_id = int(a["ask_id"]) question = int(d["question_id"]) result = int(a["result"]) option = a["option"] field.append( [ACTIVE_ID, test.id, question, ask_id, option, result]) all_ask_ids.append(ask_id) if result == 0: has_wrong = True if ask_id not in wrong_ask_ids: wrong_ask_ids.append(ask_id) nask += 1 if has_wrong: wrong_num += 1 if field: # 删除保存的记录 sql = """DELETE FROM active_test_detail WHERE test_id = %s and ask_id in (%s)""" % ( test.id, ','.join(str(i) for i in all_ask_ids)) db.tbkt_web.execute(sql) # 更新用户做题信息 sql = """ insert into active_test_detail (active_id, test_id , question_id , ask_id , answer , result ) VALUES (%s,%s,%s,%s,%s,%s) """ db.tbkt_web.execute_many(sql, field) # 保存未掌握知识点 save_knowledge_id(wrong_ask_ids, test.id, user.old_user_id) score = 0 if status == 1: # 算分 nwrong = len(wrong_ask_ids) score = int(100.0 * (nask - nwrong) / nask) if nask > 0 else 0 _score = score * 2 if user.openstatus(2) else score unit = user.unit.id if user.unit else 0 common.add_score(APP_ID_STU, user.id, _score, 'test', u'完成试卷获得%s' % _score, user.city or 0, unit) # 做完更新 状态 成绩 sql = """ update active_test set status = %s, score= %s, test_time = %s where active_id = %s and user_id = %s and object_id = %s """ % (status, score, int( time.time()), ACTIVE_ID, user.old_user_id, paper_id) db.tbkt_web.execute(sql) return {"test_id": test.id}
def p_award(request): """ @api {post} /huodong/sx_summer/t/award [暑假活动 抽奖]教师抽奖 @apiGroup sx_summer @apiParamExample {json} 请求示例 {app_id:35} 36:数学教师 42 :语文教师 , 34:英语教师 @apiSuccessExample {json} 成功返回 { "message": "很遗憾,您本次未抽中任何奖品!", "error": "", "data": {award:1} 1~4 中奖,5: 谢谢参与,6 积分不足 "response": "ok", "next": "" } @apiSuccessExample {json} 失败返回 {"message": "手速太快了,请稍后尝试", "error": "", "data": "", "response": "fail", "next": ""} """ user = request.user is_smx = 0 if user.city and int(user.city) != 411200 else 1 args = request.QUERY.casts(app_id=int) app_id = args.app_id if not app_id or app_id not in TEA_APP_IDS: return ajax_json.jsonp_fail(request, error='no_app_id') lottery_cost = TEA_LOTTERY_COST if not common.get_active_status(app_id): return ajax_json.jsonp_fail(request, message=u'活动已经结束') # 判断用户是否有抽奖资格 # 语文 if app_id == YW_APP_ID_TEA: lottery_able = common.get_yuwen_lottery_able(request, app_id, user, lottery_cost) else: lottery_able = db.default.score_user.filter(app_id=app_id, user_id=user.id, score__gte=lottery_cost).exists() if not lottery_able: return ajax_json.jsonp_ok(request, data={'award': 6}, message=u'您的积分已不足,快去赚积分吧!') # 抽奖 # 获取获奖奖项 if is_smx: award_type = probability.get_smx_user_award_type(user.old_user_id) else: award_type = probability.get_user_award_type(user.old_user_id) # 语文 if app_id == YW_APP_ID_TEA: # 抽奖扣分结果 re_result = common.yw_reduce_score(request, app_id, user, lottery_cost) if re_result: # 确认抽奖情况,保存获奖记录 award, award_name = common.get_award_result(app_id, user, award_type, is_smx) else: award, award_name = 0, 0 else: # 数学英语 抽奖扣分 unit = user.unit.id if user.unit else 0 cc = common.add_score(app_id, user.id, -lottery_cost, 'lottery', u'抽奖-%s积分' % lottery_cost, user.city or 0, unit) if cc: # 确认抽奖情况,保存获奖记录 award, award_name = common.get_award_result(app_id, user, award_type, is_smx) else: award, award_name = 0, 0 if not (award and award_name): return ajax_json.jsonp_ok(request, data={'award': 5}, message=u'很遗憾,您本次未抽中任何奖品!') return ajax_json.jsonp_ok(request, data={'award': award}, message=u'恭喜获奖,获得%s!' % award_name)
def p_award(request): """ @api {post} /huodong/sx_summer/s/award [暑假活动 抽奖]抽奖 @apiGroup sx_summer @apiParamExample {json} 请求示例 {app_id:35} 35:数学学生 43:语文学生 , 32:英语学生 @apiSuccessExample {json} 成功返回 { "message": "很遗憾,您本次未抽中任何奖品!", "error": "", "data": {award:1} 1~4 中奖,5: 谢谢参与,6 积分不足 "response": "ok", "next": "" } @apiSuccessExample {json} 失败返回 {"message": "手速太快了,请稍后尝试", "error": "", "data": "", "response": "fail", "next": ""} """ user = request.user if user.is_teacher: return ajax_json.jsonp_fail(request, message=u"学生用户才能使用") is_smx = 0 if user.city and int(user.city) != 411200 else 1 args = request.QUERY.casts(app_id=int) app_id = args.app_id if not app_id or app_id not in ALL_APP_ID: return ajax_json.jsonp_fail(request, error='no_app_id') if not common.get_active_status(app_id): return ajax_json.jsonp_fail(request, message=u'活动已经结束') # 判断 是教师还是学生活动 区分范围 lottery_cost = TEA_LOTTERY_COST if app_id in STU_APP_IDS: lottery_cost = STU_LOTTERY_COST # 判断用户是否有抽奖资格 # 语文 if app_id in [YW_APP_ID_TEA, YW_APP_ID_STU]: lottery_able = common.get_yuwen_lottery_able(request, app_id, user, lottery_cost) else: lottery_able = db.default.score_user.filter(app_id=app_id, user_id=user.id, score__gte=lottery_cost).exists() if not lottery_able: return ajax_json.jsonp_ok(request, data={'award': 6}, message=u'您的积分已不足,快去赚积分吧!') # 抽奖 global stu_lottery_locks if (user.id, app_id) in stu_lottery_locks: return ajax_json.jsonp_fail(request, message=u'手速太快了,请稍后尝试') else: stu_lottery_locks[(user.id, app_id)] = 1 try: # 获取获奖奖项 if is_smx: award_type = probability.get_smx_user_award_type(user.old_user_id) else: award_type = probability.get_user_award_type(user.old_user_id) # 判断是否开通语文 if award_type == 4: award_type = 0 if probability.get_yw_status(user) else award_type # 语文 if app_id in [YW_APP_ID_STU, YW_APP_ID_TEA]: # 抽奖扣分结果 re_result = common.yw_reduce_score(request, app_id, user, lottery_cost) if re_result: # 确认抽奖情况,保存获奖记录 award, award_name = common.get_award_result(app_id, user, award_type, is_smx) else: award, award_name = 0, 0 else: # 数学英语 抽奖扣分 unit = user.unit.id if user.unit else 0 cc = common.add_score(app_id, user.id, -lottery_cost, 'lottery', u'抽奖-%s积分' % lottery_cost, user.city or 0, unit) if cc: # 确认抽奖情况,保存获奖记录 award, award_name = common.get_award_result(app_id, user, award_type, is_smx) else: award, award_name = 0, 0 finally: stu_lottery_locks.pop((user.id, app_id), None) if not (award and award_name): return ajax_json.jsonp_ok(request, data={'award': 5}, message=u'很遗憾,您本次未抽中任何奖品!') return ajax_json.jsonp_ok(request, data={'award': award}, message=u'恭喜获奖,获得%s!' % award_name)