def yw_paper_unit_status(paper_ids, unit_ids, user_id): """ :param paper_ids: :param unit_ids: :param user_id: :return: """ sql = """ select distinct object_id, c.unit_class_id from yw_task_new t, yw_task_class_new c where t.type = 103 and t.add_user = %s and t.id = c.task_id and t.object_id in (%s) and c.unit_class_id in (%s) and c.status>-1 """ % (user_id, join(paper_ids), join(unit_ids)) rows = db.tbkt_yuwen.fetchall_dict(sql) paper_map = defaultdict(list) for i in rows: paper_map[i.object_id].append(i.unit_class_id) out = {} for i in paper_ids: send_unit = paper_map.get(i, []) unsent = set(send_unit) ^ set(unit_ids) for u in unit_ids: if not send_unit or u in unsent: out[u] = 1 num = r_class_num(u) if num == 0: out[u] = -1 return out
def sx_paper_unit_status(paper_ids, unit_ids, user_id): """ :param paper_ids: :param unit_ids: :param user_id: :return: """ sql = """ select s.object_id, c.unit_class_id from sx_task s inner join sx_task_class c on s.id=c.task_id where s.add_user=%s and s.type=103 and c.status>-1 and c.unit_class_id in (%s) and s.object_id in (%s) and s.status>-1 """ % (user_id, join(unit_ids), join(paper_ids)) rows = db.tbkt_shuxue.fetchall_dict(sql) paper_map = defaultdict(list) for i in rows: paper_map[i.object_id].append(i.unit_class_id) out = {} for i in paper_ids: send_unit = paper_map.get(i, []) unsent = set(send_unit) ^ set(unit_ids) for u in unit_ids: if not send_unit or u in unsent: out[u] = 1 num = r_class_num(u) if num == 0: out[u] = -1 return out
def yy_task_info(user, tea_unit_ids): """ 英语作业 """ if tea_unit_ids: active_time = db.tbkt_active.active.select("begin_time", "end_time").get(name=u"教师发作业领金币") begin_time = active_time.begin_time end_time = active_time.end_time sql = """ select tc.task_id, t.begin_time, t.end_time, t.status,tc.unit_class_id from yy_task t inner join yy_task_class tc on tc.task_id=t.id and tc.unit_class_id in (%s) where t.add_user=%s and t.status!=-1 and t.begin_time>%s and t.end_time<%s """ % (join(tea_unit_ids), user.id, begin_time, end_time) tasks = db.tbkt_yingyu.fetchall_dict(sql) for i in tea_unit_ids: class_students_data = db.ketang.mobile_order_region.select("user_id").filter( unit_class_id=i, user_type=1)[:] if len(class_students_data) < 20: continue user_ids = [u.user_id for u in class_students_data] for t in tasks: if t.unit_class_id != i: continue finish = db.tbkt_yingyu.yy_task_progress.filter(task_id=t.task_id, user_id__in=user_ids, status=1)[:] finish_num = len(finish) percent = ((finish_num) * 100 / len(class_students_data)) if len(class_students_data) != 0 else 0 if percent < 90: continue return True return False
def class_stu_rank(user, unit_id, stu_active_id): """ 获取班级学生信息 ——————————————————— 王世成 2017.9.12 ——————————————————— :param unit_id: :param request: :param stu_active_id: :param user :return: """ stu_ids = user.get_mobile_order_region().select("user_id") \ .filter(unit_class_id=unit_id, user_type=1).flat('user_id')[:] if not stu_ids: return [] sql = """ select a.id, a.real_name,a.id from auth_user a, auth_profile p where a.id = p.user_id and user_id in (%s) """ % join(stu_ids) rows = db.tbkt_user_slave.fetchall_dict(sql) stu_ids = [i.id for i in rows] stu_score = db.tbkt_active_slave.active_score_user.select('user_id', 'score')\ .filter(user_id__in=stu_ids, active_id=stu_active_id).order_by('score desc')[:] score_map = {i.user_id: i.score for i in stu_score} for i in rows: i.score = score_map.get(i.id, 0) rows.sort(key=lambda x: (-x.score,x.id)) return rows
def unit_stu_rank(user, stu_active_id, unit_id): """ 获取班级学生信息, 分班返回 """ stu_ids = user.get_mobile_order_region().select("user_id") \ .filter(unit_class_id=unit_id, user_type=1).flat('user_id')[:] if not stu_ids: return [] sql = """ select a.id, a.real_name,a.id from auth_user a, auth_profile p where a.id = p.user_id and user_id in (%s) """ % join(stu_ids) rows = db.tbkt_user_slave.fetchall_dict(sql) stu_ids = [i.id for i in rows] stu_score = db.tbkt_active_slave.active_score_user.select('user_id', 'score', 'id') \ .filter(user_id__in=stu_ids, active_id=stu_active_id)[:] score_map = {i.user_id: i.score for i in stu_score} # 积分相同时按用户参加活动顺序排序 score_id_map = {i.user_id: i.id for i in stu_score} for i in rows: i.score = score_map.get(i.id, 0) i.score_id = score_id_map.get(i.id, 0) rows.sort(key=lambda x: (-x.score, x.score_id)) return rows
def message_send_sms(unit_ids, stu_ids, content, platform_id): """ 发送短信 :param unit_ids: :param stu_ids: :param content: :param platform_id: :return: """ try: hub = tbktapi.Hub() d = { 'unit_id': join(unit_ids), 'user_id': join(stu_ids), 'content': content, 'platform_id': platform_id } hub.sms.post('/sms/send', d) except Exception as e: logging.error("send msg ERROR %s:%s" % (inspect.stack()[0][3], e))
def get_wrong_question(user_id, week_id, grade_id): """ 获取高频试题资源 :param week_id: :param grade_id: :param user_id: :return: """ # 获取题目信息 data = get_wrong_source(week_id, grade_id) if not data: return [] test = db.tbkt_active.full_score_test.select('text').get(user_id=user_id, week_id=week_id, grade_id=grade_id, type=WRONG_TYPE) if test and test.status == 1: # 用户已完成当前练习 return [] text = json.loads(test.text) if test and test.text else [] text_map = defaultdict(dict) for i in text: temp = Struct(i) text_map[str(temp.qid)].update({temp.aid: (temp.option, temp.result)}) qids = [str(i.get("qid")).strip() for i in text] qid_map = {str(i.get("qid")).strip(): (i.get("count"), i.get("ratio", "75%")) for i in data if i.get("qid")} source_qids = [str(i.get("qid")).strip() for i in data] r = Hub().sx.get("/sx/questions", dict(qid=join(source_qids))) if not r or r.response != "ok": return [] questions = map(Struct, r.data) questions = [i for i in questions if i not in qids] print [i.id for i in questions] out = [] for i in questions: q = text_map.get(str(i.id)) asks = map(Struct, i.asks) if q and len(asks) == len(q): continue if not qid_map.get(str(i.id)): continue i.count, i.ratio = qid_map.get(str(i.id)) temp = int(i.ratio.replace("%", "")) i.ratio = "%s%%" % (temp + 20) if temp <= 55 else i.ratio i.count = random.randint(217, 500) + i.count for ask in asks: option, result = q.get(ask.id, ("", "")) if q else ("", "") ask.user_answer = option ask.user_result = result i.asks = asks out.append(i) return out
def p_sendmany(request): """ @api {post} /account/sendmany [短信]发多条短信 @apiGroup account @apiParamExample {json} 请求示例 {"phone":"15981867201,15981867202", "content":"您的验证码是111", "schedule_time":"140415112622"} *schedule_time: 可选, 定时发送时间, 如: 140415112622 代表14年04月15日11点26分22秒 @apiSuccessExample {json} 成功返回 { "message": "", "next": "", "data": { "id": 2787448, # 短信ID "success": true }, "response": "ok", "error": "" } @apiSuccessExample {json} 失败返回 { "message": "请填写短信内容", "next": "", "data": "", "response": "fail", "error": "" } """ args = request.QUERY.casts(phone=str, content=unicode, schedule_time=str) phones = args.phone or '' content = args.content or '' schedule_time = args.schedule_time or '' phones = phones.split(',') phones = [s for s in phones if is_chinamobile(s)] if not phones: return ajax.jsonp_fail(request, message='请输入移动手机号') if not content: return ajax.jsonp_fail(request, message='请填写短信内容') hub = tbktapi.Hub(request) url = '/sms/send' d = dict(phone=join(phones), content=content, schedule_time=schedule_time, platform_id=request.user.platform_id) r = hub.sms.post(url, d) if not r: return ajax.jsonp_fail(request, message='服务器开小差') return ajax.jsonp_ok(request, r.data)
def sx_task_info(user, tea_unit_ids): """ 数学作业 """ # tea_unit_ids = [u.id for u in user.units] # 活动期间教师布置的作业 if tea_unit_ids: active_time = db.tbkt_active.active.select("begin_time", "end_time").get(name=u"教师发作业领金币") sx_begin_time = active_time.begin_time sx_end_time = active_time.end_time sql = """ select tc.task_id, t.begin_time, t.end_time, t.add_time, tc.unit_class_id,tc.status from sx_task t inner join sx_task_class tc on tc.task_id=t.id and tc.unit_class_id in (%s) where t.add_user=%s and t.status!=-1 and tc.status != -1 and t.begin_time>%s and t.end_time<%s """ % (join(tea_unit_ids), user.id, sx_begin_time, sx_end_time) tasks = db.tbkt_shuxue.fetchall_dict(sql) task_ids = [i.task_id for i in tasks] task_info = db.tbkt_shuxue.sx_task_content.select("text", "task_id").filter(task_id__in=task_ids)[:] for i in tea_unit_ids: class_students_data = db.ketang.mobile_order_region.select("user_id").filter( unit_class_id=i, user_type=1)[:] if len(class_students_data) < 40: continue user_ids = [u.user_id for u in class_students_data] if task_info: for f in task_info: # if len(json.loads(f.text)) < 3: if len(f["text"]) < 3: continue for s in tasks: if s.unit_class_id != i and s.task_id not in task_ids: continue finish = db.tbkt_shuxue.sx_task_test.filter(task_id=s.task_id, user_id__in=user_ids, status=1)[:] finish_num = len(finish) percent = ((finish_num) * 100 / len(class_students_data)) if len( class_students_data) != 0 else 0 if percent < 90: continue return True return False
def p_givesms(request): """ 老板语文 作业短信接口 :param request: :return: """ args = request.QUERY.casts(classes='json', content=unicode, begin_time='datetime') classes = args.classes or [] content = args.content or u'' begin_time = args.begin_time if not classes: return ajax.jsonp_fail(request, message='请选择您要发布的班级') content = content.strip() if not content: return ajax.jsonp_fail(request, message='短信内容不能为空') if len(content) > 200: return ajax.jsonp_fail(request, message='短信内容不能超过200字') unit_ids = [] user_ids = [] for i in classes: unit_id = str(i.get('unit_id')) if unit_id in unit_ids: continue unit_ids.append(unit_id) bind_ids = i.get('bind_id', '') user_ids += [str(i) for i in bind_ids.split(',') if i] hub = tbktapi.Hub(request) url = '/sms/send' if unit_ids and not user_ids: user_ids += get_student_ids(unit_ids) args = dict(user_id=join(user_ids)) r = hub.sms.post(url, args) if not r: return ajax.jsonp_fail(request, message='服务器开小差') com_sys.send_im(user_ids, 2, '新作业', '你的老师布置作业了,快来看看吧!', {'type': 1}) return ajax.jsonp_ok(request, r.data)
def get_ask(question_ids): """ Under the big question ask info :param question_ids :return: {} """ if not isinstance(question_ids, (list, tuple)): return {} sql = """ select a.id, a.question_id, a.content, a.video, a.image, a.parse, if(a.type, a.type, q.type) type, a.question_title, q.category, q.show_parse from yw_question q, yw_question_ask a where q.id = a.question_id and q.id in (%s) order by a.question_number """ % (join(question_ids)) asks = db.tbkt_yw.fetchall_dict(sql) if not asks: return {} ask_id = [i.id for i in asks] options = db.tbkt_yw.yw_question_options.filter(question_id__in=ask_id)[:] options_map = defaultdict(list) for i in options: options_map[i.question_id].append(i) out = defaultdict(list) for i in asks: i.image = get_absurl(i.image).replace("//", "/") i.video = get_absurl(i.video).replace("//", "/") i.content = i.content if i.content not in ("<p><br></p>", "<p><br/></p>") else "" i.question_title = format_question_title(i.question_title) i.parse = format_parse(i.parse) if not i.show_parse: i.parse = "" del i.show_parse del i.sequence del i.question_number out[i.question_id].append(i) for qid, asks in out.iteritems(): ask_len = len(asks) for i, a in enumerate(asks): a.no = "(%s)" % (i + 1) if ask_len > 1 else "" op = options_map.get(a.id) get_options(a, op) del a.category return out
def users_info_temp(user_ids): """ 获取用户姓名头像边框 :param user_ids:批量获取用户信息id :return: """ sql = """ select a.id, a.real_name, p.portrait from auth_user a, auth_profile p where a.id = p.user_id and user_id in (%s) """ % join(user_ids) user_info = db.tbkt_user.fetchall_dict(sql) user_map = {i.id: i for i in user_info} user_ids = [i.id for i in user_info] school_info = db.ketang.mobile_order_region.select( 'school_name', 'user_id', 'unit_class_id unit_id').filter( user_id__in=user_ids).group_by('user_id')[:] school_map = {i.user_id: i for i in school_info} unit_ids = [i.unit_id for i in school_info] unit_names = db.default.school_unit_class.filter(id__in=unit_ids).select( "unit_name", "id")[:] unit_map = {i.id: i.unit_name for i in unit_names} user_borders = db.default.user_border_detail.filter(user_id__in=user_ids, status=1)[:] border_map = {i.user_id: i.border_id for i in user_borders} users = [] for i in user_info: user = user_map.get(i.id) region = school_map.get(i.id, Struct()) border_id = border_map.get(i.id, 0) border_url = "" if border_id != 0: border_info = db.default.border.get(id=border_id) if border_info: border_url = border_info.border_url if not region: continue users.append( dict(user_id=i["id"], real_name=user.real_name, portrait=math_com.get_portrait(user.portrait, 1), school_name=region.school_name, unit_name=unit_map.get(region.unit_id), border_url=get_absurl(border_url))) out = {i["user_id"]: i for i in users} return out
def get_source(week_id, grade_id): """ 获取知识点信息 :param week_id: :param grade_id: :return: """ source = db.tbkt_active.full_score_week_detail.select("knowledge_id", "week_id") \ .get(week_id=week_id, grade_id=grade_id) if not source: return [] kids = set(source.knowledge_id.split(",")) hub = tbktapi.Hub() url = "/sx/knowledges" r = hub.sx.post(url, {"kid": join(list(kids))}) if not r or r.response != "ok": return [] return map(Struct, r.data)
def video_submit(user_id, knowledge_id, week_id, city): """ 保存用户提交看视频的记录 :param user_id: :param knowledge_id: 用户观看知识点视频的knowledge_id :param week_id: 知识训练场的week_id :param city: 保存用户地市 :return: """ grade_id = user_grade(user_id) # 获取用户设置教材id nowt = int(time.time()) # 查看用户查看视频记录 info = db.tbkt_active.full_score_test.get(user_id=user_id, week_id=week_id, grade_id=grade_id, type=1) if info and info.status == 1: return source = get_source(week_id, grade_id) if not source: return num = len(source) with db.tbkt_active as active: if not info: status = 1 if num == 1 else 0 active.full_score_test.create(grade_id=grade_id, week_id=week_id, status=status, add_time=nowt, user_id=user_id, type=1, city=city, text=knowledge_id), return added = info.text.split(",") if str(knowledge_id) in added: return status = 1 if num - len(added) == 1 else 0 added.append(knowledge_id) active.full_score_test.filter(id=info.id).update(text=join(added), status=status)
def wrong_result(user_id, week_id, grade_id): """ 高频错题结果页 :param user_id: :param week_id: :param grade_id: :return: """ data = get_wrong_source(week_id, grade_id) if not data: return [] test = db.tbkt_active.full_score_test.select('text').get(user_id=user_id, week_id=week_id, grade_id=grade_id, type=WRONG_TYPE) if not test or not test.text: return [] text = map(Struct, json.loads(test.text)) if test and test.text else [] result_map = {} for i in text: result_map[i.aid] = (i.option, i.result) qids = [i.get("qid") for i in data] r = Hub().sx.get("/sx/questions", dict(qid=join(qids))) if not r or r.response != "ok": return [] questions = map(Struct, r.data) for i in questions: for a in i["asks"]: option, result = result_map.get(a['id'], ("", "")) a["user_answer"] = option a["user_result"] = result # 对结果页进行排序 与题目返回数据顺序一致 # questions = sorted(questions,key=lambda x:(-i for i,v in enumerate(questions))) return questions
def yw_task_info(user, tea_unit_ids): """ 语文作业 """ # 活动期间教师布置的作业 if tea_unit_ids: active_time = db.tbkt_active.active.select("begin_time", "end_time").get(name=u"教师发作业领金币") begin_time = active_time.begin_time end_time = active_time.end_time sql = """ select u.task_id ,t.add_user,t.type, t.add_time,t.begin_time,t.end_time,t.title,t.chapter_id,u.unit_class_id,t.status, u.status class_status from yw_task_new t inner join yw_task_class_new u on u.task_id=t.id and t.add_user=%s where u.unit_class_id in (%s) and t.status =1 and u.status > -1 and t.begin_time>%s and t.end_time<%s """ % (user.id, join(tea_unit_ids), begin_time, end_time) yw_task = db.tbkt_yuwen.fetchall_dict(sql) for i in tea_unit_ids: class_students_data = db.ketang.mobile_order_region.select("user_id").filter( unit_class_id=i, user_type=1)[:] if len(class_students_data) < 20: continue user_ids = [u.user_id for u in class_students_data] for s in yw_task: if s.unit_class_id != i: continue finish = db.tbkt_yuwen.yw_test_new.filter(task_id=s.task_id, user_id__in=user_ids, status=1)[:] finish_num = len(finish) percent = ((finish_num) * 100 / len(class_students_data)) if len(class_students_data) != 0 else 0 if percent < 90: continue return True return False
def r_sx_send(request): """ @api {post} huodong/terminal/math/send [期末提分试卷]数学布置作业 @apiGroup terminal @apiParamExample {json} 请求示例 { "type": 103 "unit_id": "555428,515192", # 班级ID "title": "作业标题", # 对应paper_id 多个title以竖线|分割 "content": "短信内容", "begin_time": "2017-01-05 09:10:00", # (可选)定时作业时间 "end_time": "2017-02-05 23:59:59", # 作业截止时间(必须大于当前时间和begin_time) "sendpwd": 1/0, # 是否下发帐号密码 "sendscope": 1/0, # 短信发送范围 0给所有学生发短信 1只给开通的学生发短信 "object_id":"试卷ID,可多个" # 需要与title对应,多个以逗号分割 } @apiSuccessExample {json} 成功返回 { "message": "", "next": "", "data": "", "response": "ok", "error": "" } @apiSuccessExample {json} 失败返回 { "message": "请设置作业的完成时间", "next": "", "data": "", "response": "fail", "error": "" } """ user = request.user grade_id = user.grade_id args = request.QUERY.casts(type=int, unit_id=str, title=unicode, content=unicode, object_id=str, begin_time='timestamp', end_time='timestamp', sendpwd=int, sendscope=int, details='json', agent=int) task_type = 103 # 期末试卷 paper_id = args.object_id # 如果为试卷作业 则为试卷id 否则 为0 paper_ids = [int(s) for s in paper_id.split(',') if s] unit_id = args.unit_id or '' # 发送作业班级 作业必须要班级id unit_ids = [int(s) for s in unit_id.split(',') if s] content = args.content or u'' agent = args.agent or 2 now = int(time.time()) # 当前时间 begin_time = args.begin_time or now # 作业开始时间 如果不是定时作业开始时间=当前时间 end_time = args.end_time # 作业结束时间 sendpwd = args.sendpwd # 是否下发密码 sendscope = args.sendscope # 短信发送范围 0全部 1仅开通 tea_name = user.name open_content = get_text_content(grade_id, tea_name, end_time, subject_id=21) # 作业标题 如果为空 则按时间进行保存展示 title = [s for s in args.title.split('|') if s] if not unit_id: return ajax.jsonp_fail(request, message='请选择您要发布的班级') if not content: return ajax.jsonp_fail(request, message='短信内容不能为空') if len(content) > 200: return ajax.jsonp_fail(request, message='短信内容不能超过200字') if not end_time: return ajax.jsonp_fail(request, message='请设置作业的完成时间') if begin_time < now: return ajax.jsonp_fail(request, message='定时发布时间不得小于当前系统时间') if begin_time >= end_time: return ajax.jsonp_fail(request, message='定时发布时间不得大于完成时间') # 1:实时发送展示 2:定时作业 status = 1 if begin_time <= now else 2 sql = """ select distinct object_id, c.unit_class_id from sx_task t, sx_task_class c where t.type = 103 and t.add_user = %s and t.id = c.task_id and t.status > -1 and c.status > -1 and t.object_id in (%s) """ % (user.id, join(paper_ids)) rows = db.tbkt_shuxue.fetchall_dict(sql) # {(paper_id, unit_id): 1} done_map = {(i.object_id, i.unit_class_id): 1 for i in rows} # 使用事务保证 数据完整 with db.tbkt_shuxue as dt: for k, i in enumerate(paper_ids): # create task task_id = dt.sx_task.create( type=task_type, object_id=i, title=title[k], add_user=user.id, sms_content=content, status=status, add_time=now, begin_time=begin_time, end_time=end_time, is_sendpwd=sendpwd, ) # create task_class class_details = [] unsent = set() for unit_id in unit_ids: if done_map.get((i, unit_id)): continue d = {'task_id': task_id, 'unit_class_id': unit_id, 'type': sendscope, 'add_time': now, 'student_id': '', 'status': status} unsent.add(unit_id) class_details.append(d) if class_details: dt.sx_task_class.bulk_create(class_details, ignore=True) sql = """ select DISTINCT d.paper_id,d.question_id from sx_paper_question_number n inner join sx_paper_detail d on n.paper_id = %s and n.paper_id = d.paper_id and n.type = 2 and n.object_id = d.question_id """ % i papers = db.ziyuan_slave.fetchall_dict(sql) create_ordinary_task(task_id, papers, user.id, now, dt) create_message(task_id, user.id, title[k], content, begin_time, end_time, unsent, agent, 21) # send sms or password if status == 1: students = [] for u in unit_ids: students += get_unit_students(request, u) user_ids = ','.join(str(s.id) for s in students if s and (sendscope == 0 or s.is_open)) # send sms send_sms_user_id(request, user_ids, open_content) # send password if sendpwd: send_pwd(request, user_ids) # send im send_im(request, '', user_ids, '新作业', '你的老师布置作业了,快来看看吧!', 2) return ajax.jsonp_ok(request)
def r_yw_send(request): """ @api {post} /huodong/terminal/yw/send [期末提分试卷]语文试卷发送 @apiGroup terminal @apiParamExample {json} 请求示例 { "unit_id": 班级id(多个逗号分隔), "title": "试卷名字(多个逗号分隔)", "content": "短信内容", "begin_time": "开始时间(选填定时作业需要)", "end_time": "结束时间", "sendpwd": 是否下发账号密码 1/0 (选填), "sendscope": "短信发送范围 1 开通 0 全部 (选填)", "paper_id": "多个试卷逗号分隔", } @apiSuccessExample {json} 成功返回 { "message": "", "next": "", "data": "", "response": "ok", "error": "" } """ args = request.QUERY.casts(unit_id=str, title=unicode, content=unicode, begin_time='timestamp', end_time='timestamp', sendpwd=int, sendscope=int, paper_id=str, agent=int) now = int(time.time()) try: unit_id = args.unit_id or "" unit_ids = map(int, unit_id.split(",")) if not unit_ids: return ajax.jsonp_fail(request, message="请选择班级!") title = args.title or "" title = title.split(",") if not title: return ajax.jsonp_fail(request, message="没有找到试卷标题!") content = args.content if not content: return ajax.jsonp_fail(request, message='短信内容不能为空') if len(content) > 200: return ajax.jsonp_fail(request, message='短信内容不能超过200字') begin_time = args.begin_time or now end_time = args.end_time if not end_time: return ajax.jsonp_fail(request, message='请设置作业的完成时间') if begin_time < now: return ajax.jsonp_fail(request, message='定时发布时间不得小于当前系统时间') if begin_time >= end_time: return ajax.jsonp_fail(request, message='定时发布时间不得大于完成时间') paper_id = args.paper_id or "" paper_ids = map(int, paper_id.split(",")) if not paper_id: return ajax.jsonp_fail(request, message="请选择发布的试卷") agent = args.agent or 2 except Exception as e: print e return ajax.jsonp_fail(request, message="参数异常!") user = request.user content = get_text_content(user.grade_id, user.name, end_time, 51) status = 1 if begin_time <= now else 0 send_pwd = args.sendpwd # 是否下发密码 send_scope = args.sendscope # 短信发送范围 0全部 1仅开通 sql = """ select distinct object_id, c.unit_class_id from yw_task_new t, yw_task_class_new c where t.type = %s and t.add_user = %s and t.id = c.task_id and t.object_id in (%s) and t.status > -1 and c.status > -1 """ % (YW_TASK_TYPE, user.id, join(paper_ids)) rows = db.tbkt_yuwen.fetchall_dict(sql) done_map = {(i.object_id, i.unit_class_id): 1 for i in rows} with db.tbkt_yuwen as dt: for k, i in enumerate(paper_ids): # create task task_id = dt.yw_task_new.create(add_user=user.id, chapter_id=0, type=YW_TASK_TYPE, status=status, add_time=now, begin_time=begin_time, end_time=end_time, title=title[k], object_id=i) # create task_class class_details = [] send_unit = set() for unit_id in unit_ids: if done_map.get((i, unit_id)): continue d = { 'task_id': task_id, 'unit_class_id': unit_id, 'stu_ids': '', 'is_password': send_pwd, 'is_all': send_scope, 'status': status } send_unit.add(unit_id) class_details.append(d) if class_details: dt.yw_task_class_new.bulk_create(class_details, ignore=True) question_ids = PAPER_QUESTION.get(i, ()) dt.yw_task_detail_new.create(task_id=task_id, question_id=join(question_ids), type=YW_TASK_TYPE) create_message(task_id, user.id, title[k], content, begin_time, end_time, send_unit, agent, 51) # send sms or password if status == 1: students = [] for u in unit_ids: students += get_unit_students(request, u) user_ids = ','.join( str(s.id) for s in students if s and (send_scope == 0 or s.is_open)) # send sms send_sms_user_id(request, user_ids, content) # send password if send_pwd: send_pwd(request, user_ids) # send im send_im(request, '', user_ids, '新作业', '你的老师布置作业了,快来看看吧!', 2) return ajax.jsonp_ok(request)
def r_yy_send(request): """ @api {post} /huodong/terminal/yy/send [期末提分试卷英语]教师发试卷 @apiGroup terminal @apiParamExample {json} 请求示例 { "type": 103, # 期末作业类型 "unit_id": "622216,578298", # 班级id 多个以逗号分割 "title": "试卷标题", # 试卷标题 以竖线|分割 "content": "短信内容", # 短信内容 "begin_time": "2017-04-8 09:10:00", # 开始时间 "end_time": "2017-04-9 23:59:59", # 结束时间 "sendpwd": 0, # 是否下发账号密码 "sendscope": 0, # 短信发送范围 0给所有学生发短信 1只给开通的学生发短信 "ids":"1" # 试卷id,多个试卷id以逗号分割 } @apiSuccessExample {json} 成功返回 { "message": "", "next": "", "data": {}, "response": "ok", "error": "" } @apiSuccessExample {json} 失败返回 {"message": "", "error": "", "data": "", "response": "fail", "next": ""} """ user = request.user grade_id = request.user.grade_id args = request.QUERY.casts(type=int, unit_id=str, title=unicode, content=unicode, begin_time='timestamp', end_time='timestamp', sendpwd=int, sendscope=int, ids=str, agent=int) typ = args.type # 1网上作业 2试卷作业 (本函数都是2) unit_id = args.unit_id or '' # 班级id unit_ids = [int(s) for s in unit_id.split(',') if s] content = args.content or u'' # 短信内容 now = int(time.time()) # 现在的时间 begin_time = args.begin_time or now # 发布时间 end_time = args.end_time # 完成时间 sendpwd = args.sendpwd or 0 # 下发账号密码 0/1 sendscope = args.sendscope or 0 # 短信发送范围 0给所有学生发短信 1只给开通的学生发短信 ids = args.ids or '' ids = [int(id) for id in ids.split(',') if id] # 试卷 title = [s for s in args.title.split('|') if s] agent = args.agent or 2 tea_name = user.name open_content = get_text_content(grade_id, tea_name, end_time, subject_id=91) if not ids: return jsonp_fail(request, message='请选择试卷') if not typ and typ != 2: return jsonp_fail(request, message='作业类型不能为空') if not unit_id: return jsonp_fail(request, message='请选择您要发布的班级') if not content: return jsonp_fail(request, message='短信内容不能为空') if len(content) > 200: return jsonp_fail(request, message='短信内容不能超过200字') if not end_time: return jsonp_fail(request, message='请设置作业的完成时间') if begin_time < now: return jsonp_fail(request, message='定时发布时间不得小于当前系统时间') if begin_time >= end_time: return jsonp_fail(request, message='定时发布时间不得大于完成时间') title = title or "%s试卷" % begin_time.strftime("%m月%d日") # 1已发 2待发 status = 1 if begin_time <= now else 2 # 查询试卷发送作业状态 sql = """ select distinct object_id, c.unit_class_id from yy_task t, yy_task_class c where t.type = 103 and t.add_user = %s and t.id = c.task_id and t.status > -1 and c.status > -1 and t.object_id in (%s) """ % (user.id, join(ids)) rows = db.tbkt_yingyu.fetchall_dict(sql) done_map = {(i.object_id, i.unit_class_id): 1 for i in rows} with db.tbkt_yingyu as dt: for k, i in enumerate(ids): # create task task_id = dt.yy_task.create( type=103, object_id=i, add_user=user.id, title=title[k], sms_content=content, status=status, add_time=now, begin_time=begin_time, end_time=end_time, is_sendpwd=sendpwd, ) # create task_class # 查询试卷是否发送过 和发送的班级 class_details = [] unsent = set() for unit_id in unit_ids: if done_map.get((i, unit_id)): continue d = { 'task_id': task_id, 'unit_class_id': unit_id, 'type': sendscope, 'add_time': now, 'student_id': '', 'status': status } # 判断用户试卷发送过的班级 如果发送过 跳过 unsent.add(unit_id) class_details.append(d) if class_details: dt.yy_task_class.bulk_create(class_details) # create task_details task_details = [] questions = PAPER_QUESTION[i] # questions.sort() questions_array = [] d = {} d['task_id'] = task_id # content_type 学生端作业列表显示 d['content_type'] = 11 for qid in questions: questions_array.append({"cid": i, "tcid": i, "qid": qid}) d['text'] = json.dumps(questions_array) task_details.append(d) dt.yy_task_detail.bulk_create(task_details) pub_ids = [] questions = PAPER_QUESTION[i] for qid in questions: d = {} d['user_id'] = user.id d['subject_id'] = 91 d['question_id'] = qid d['add_time'] = time.time() pub_ids.append(d) dt.yy_publish_question.bulk_create( pub_ids, ignore=True) # !!!!!!!!!!!!!!!!!!!!!!!! create_message(task_id, user.id, title[k], content, begin_time, end_time, unsent, agent, 91) user_phone = db.tbkt_user_slave.auth_user.select('phone').get( id=user.id) # send sms or password if status == 1 or sendpwd: call(send_sms_method, request, unit_ids, status, user_phone, open_content, content, sendpwd) data = {} return jsonp_ok(request, data)