def get_systematics(question_id): mysql = Mysql() mysql.connect_master() query_sql = "select D.name as module_name,C.name as unit_name,B.name as topic_name from (select topic_id from link_question_topic where question_id=%(question_id)d)A left outer join (select id,name,unit_id from entity_topic)B on (A.topic_id=B.id) left outer join (select id,name,module_id from entity_unit)C on (B.unit_id=C.id) left outer join (select id,name from entity_module)D on (C.module_id=D.id);" try: if mysql.query(query_sql,question_id = int(question_id)): res = mysql.fetchall() systematics_list = [] for line in res: module = line[0] unit = line[1] topic = line[2] systematics_dict = {'module':module,'unit':unit,'topic':topic} systematics_list.append(systematics_dict) return systematics_list else: return False except DBException as e: LOG.error('get systematics error [%s]' % e) raise CKException('get systematics error')
def q_mark_list(): mysql = Mysql() mysql.connect_master() query_sql = "select id,name from link_question_mark where enable=1;" mark_list = [] try: if mysql.query(query_sql): mark_tuple = mysql.fetchall() for mark in mark_tuple: tmp_tuple = (mark[0],mark[1]) mark_list.append(tmp_tuple) return mark_list else: return None except DBException as e: LOG.error('get mark error [%s]' % e) raise CkException('get mark error')
def _new_question(data): for i in range(1): mysql = Mysql() try: mysql.connect_master() search_sql = "select id,json,subject,type from entity_question_new where oldid = %(oldid)d;" if 0 == mysql.query(search_sql,oldid = int(data)): return None else: question_set = mysql.fetch() except DBException as e: break newid = question_set[0] question_json = question_set[1] question_subject = question_set[2] question_type = question_set[3] new_question_dict = {} new_question_dict['q_new_id'] = newid new_question_dict['new_question'],new_question_dict['blank_num'] = Business.q_json_parse(question_type,question_json) new_question_dict['subject'] = question_subject #print new_question_dict return new_question_dict
def q_subject_list(): mysql = Mysql() mysql.connect_master() query_sql = "select distinct subject from entity_question_new where type is not null;" subject_list = [] try: if mysql.query(query_sql): subject_tuple = mysql.fetchall() for type in subject_tuple: tmp_tuple = (type[0]) subject_list.append(tmp_tuple) return subject_list else: return None except DBException as e: LOG.error('get subject error [%s]' % e) raise CkException('get subject error')
def get_question_by_id(self,question_id): mysql = Mysql() try: mysql.connect_master() search_sql = "select A.Knowledge,B.TypeValue,A.QuesAbility,A.QuesDiff,A.QuesBody,A.QuesAnswer,A.QuesParse from(select Knowledge,QuesType,QuesAbility,QuesDiff,QuesBody,QuesAnswer,QuesParse from paper.paper where ID=%(question_id)d union all select Knowledge,QuesType,QuesAbility,QuesDiff,QuesBody,QuesAnswer,QuesParse from paper.cz_paper where ID=%(question_id)d)A left outer join (select TypeId,TypeValue from paper.questype)B on (A.QuesType=B.TypeId);" if 0 == mysql.query(search_sql,question_id = int(question_id)): return None else: question_set = mysql.fetch() except DBException as e: pass question_ability_dict ={'1':'了解和识记','2':'理解和掌握','3':'简单应用','4':'综合应用'} question_diff_dict = {'1':'容易','2':'较易','3':'一般','4':'较难','5':'困难'} question_dict = {} question_dict['knowledge'] = question_set[0] question_dict['type'] = question_set[1] question_dict['ability'] = question_ability_dict[question_set[2]] question_dict['diff'] = question_diff_dict[question_set[3]] question_dict['body'] = question_set[4] question_dict['answer'] = question_set[5] question_dict['parse'] = question_set[6] return question_dict
def get_group_list(system_id): mysql = Mysql() mysql.connect_master() query_sql = "select A.id,A.name,B.num from (select id,name from entity_group where system_id=%(system_id)d or id=0)A left outer join (select question_group,count(1) as num from entity_question where upload_id=%(system_id)d group by question_group)B on (A.id=B.question_group);" try: if mysql.query(query_sql,system_id = system_id): res = mysql.fetchall() group_list = [] for line in res: group_id = line[0] group_name = line[1] question_num = int(line[2]) if line[2] else 0 group_dict = {'id':int(group_id),'name':group_name,'num':int(question_num)} group_list.append(group_dict) return group_list else: return False except DBException as e: LOG.error('check topic error [%s]' % e) raise CKException('check topic error')
def _new_question(data): for i in range(1): mysql = Mysql() try: mysql.connect_master() search_sql = "select id,json,subject,type from entity_question_new where oldid = %(oldid)d;" if 0 == mysql.query(search_sql, oldid=int(data)): return None else: question_set = mysql.fetch() except DBException as e: break newid = question_set[0] question_json = question_set[1] question_subject = question_set[2] question_type = question_set[3] new_question_dict = {} new_question_dict['q_new_id'] = newid new_question_dict['new_question'], new_question_dict[ 'blank_num'] = Business.q_json_parse(question_type, question_json) new_question_dict['subject'] = question_subject #print new_question_dict return new_question_dict
def get_group_list(system_id): mysql = Mysql() mysql.connect_master() query_sql = "select A.id,count(1) from entity_group A,entity_question B where (A.system_id=%(system_id)d or A.id=0) and A.enable=1 and B.upload_id=%(system_id)d and A.id=B.question_group group by B.question_group;" group_sql = "select id,name from entity_group where (system_id=%(system_id)d or id=0) order by create_time desc;" try: group_dict = OrderedDict() group_list = [] default_num = 0 mysql.query(group_sql, system_id=system_id) group_res = mysql.fetchall() mysql.query(query_sql, system_id=system_id) num_res = mysql.fetchall() for group in group_res: group_id = group[0] group_name = group[1] group_dict[group_id] = { 'id': int(group_id), 'name': group_name, 'num': 0 } for num in num_res: gid = num[0] num = num[1] group_dict[gid]['num'] = num for gid in group_dict: if 0 == gid: default_num = group_dict[gid]['num'] continue group_list.append(group_dict[gid]) return group_list, default_num except DBException as e: LOG.error('check topic error [%s]' % e) raise CKException('check topic error')
def is_seriess(seriess_id): mysql = Mysql() mysql.connect_master() query_sql = "select 1 from entity_seriess where id = %(seriess_id)d;" try: if mysql.query(query_sql,seriess_id = int(seriess_id)): return True else: return False except DBException as e: LOG.error('check seriess error [%s]' % e) raise CkException('check seriess error')
def is_topic(topic_id): mysql = Mysql() mysql.connect_master() query_sql = "select 1 from entity_topic where id = %(topic_id)d;" try: if mysql.query(query_sql,topic_id = int(topic_id)): return True else: return False except DBException as e: LOG.error('check topic error [%s]' % e) raise CKException('check topic error')
def chapter_id_exist(chapter_id): mysql = Mysql() mysql.connect_master() query_sql = "select 1 from entity_teaching_chapter where id='%(chapter_id)d';" try: if mysql.query(query_sql,chapter_id = int(chapter_id)): return True else: return False except DBException as e: LOG.error('check chapter error [%s]' % e) raise CKException('check chapter error')
def update_json_by_id(oldid,json): mysql = Mysql() mysql.connect_master() query_sql = "update entity_question_new set json='%(question_json)s' where oldid=%(oldid)d;" try: if mysql.query(query_sql,oldid = int(oldid),question_json = json): return True else: return False except DBException as e: LOG.error('update json error [%s]' % e) raise CkException('update json error')
def chapter_id_exist(chapter_id): mysql = Mysql() mysql.connect_master() query_sql = "select 1 from entity_teaching_chapter where id='%(chapter_id)d';" try: if mysql.query(query_sql, chapter_id=int(chapter_id)): return True else: return False except DBException as e: LOG.error('check chapter error [%s]' % e) raise CKException('check chapter error')
def group_id_exist(group_id): mysql = Mysql() mysql.connect_master() query_sql = "select 1 from entity_group where id = '%(group_id)d';" try: if mysql.query(query_sql,group_id = int(group_id)): return True else: return False except DBException as e: LOG.error('check topic error [%s]' % e) raise CKException('check topic error')
def is_type(type_id): mysql = Mysql() mysql.connect_master() query_sql = "select name from entity_question_type where type_id = %(type_id)d and enable = 1;" try: if mysql.query(query_sql,type_id = int(type_id)): return mysql.fetch()[0] else: return False except DBException as e: LOG.error('check type error [%s]' % e) raise CkException('check type error')
def q_subject_filter_num(type): mysql = Mysql() mysql.connect_master() query_sql = "select count(*) from entity_question_new where subject = '%(type)s';" try: if mysql.query(query_sql,type = type): return mysql.fetchall()[0][0] else: return None except DBException as e: LOG.error('filtet type error [%s]' % e) raise CkException('filter type error')
def get_json_by_id(oldid): mysql = Mysql() mysql.connect_master() query_sql = "select json from entity_question_new where oldid=%(oldid)d;" try: if mysql.query(query_sql,oldid = int(oldid)): json = mysql.fetch()[0] return json else: return False except DBException as e: LOG.error('get json error [%s]' % e) raise CkException('get json error')
def add_mark(name): mysql = Mysql() mysql.connect_master() query_sql = "insert into link_question_mark (name,mark_time) values ('%(name)s',now());" try: if mysql.query(query_sql,name = name): return mysql.get_last_id() else: return None except DBException as e: LOG.error('add mark error [%s]' % e) raise CkException('add mark error')
def verify(username,oldid,newid,verify): mysql = Mysql() mysql.connect_master() query_sql = "insert into entity_verify (username,oldid,newid,state) values ('%(username)s',%(oldid)d,%(newid)d,%(verify)d);" try: if mysql.query(query_sql,username = username,oldid = int(oldid),newid = int(newid),verify = int(verify)): return mysql.get_last_id() else: return None except DBException as e: LOG.error('add mark error [%s]' % e) raise CkException('add mark error')
def q_mark(oldid,newid,mark): mysql = Mysql() mysql.connect_master() query_sql = "insert into entity_question_mark (oldid,newid,mark,mark_time) values (%(oldid)d,%(newid)d,%(mark)d,now());" try: if mysql.query(query_sql,oldid = oldid,newid = newid,mark = mark): return 'success' else: return None except DBException as e: LOG.error('mark error [%s]' % e) raise CkException('mark error')
def q_subject_filter(type,start,num): mysql = Mysql() mysql.connect_master() query_sql = "select oldid,subject from entity_question_new where subject = '%(type)s' limit %(start)d,%(num)d;" try: if mysql.query(query_sql,type = type,start = start,num = num): return mysql.fetchall() else: return None except DBException as e: LOG.error('filtet type error [%s]' % e) raise CkException('filter type error')
def get_group_list(system_id): mysql = Mysql() mysql.connect_master() query_sql = "select A.id,count(1) from entity_group A,entity_question B where (A.system_id=%(system_id)d or A.id=0) and A.enable=1 and B.upload_id=%(system_id)d and A.id=B.question_group group by B.question_group;" group_sql = "select id,name from entity_group where (system_id=%(system_id)d or id=0) order by create_time desc;" try: group_dict = OrderedDict() group_list = [] default_num = 0 mysql.query(group_sql,system_id = system_id) group_res = mysql.fetchall() mysql.query(query_sql,system_id = system_id) num_res = mysql.fetchall() for group in group_res: group_id = group[0] group_name = group[1] group_dict[group_id] = {'id':int(group_id),'name':group_name,'num':0} for num in num_res: gid = num[0] num = num[1] group_dict[gid]['num'] = num for gid in group_dict: if 0 == gid: default_num = group_dict[gid]['num'] continue group_list.append(group_dict[gid]) return group_list,default_num except DBException as e: LOG.error('check topic error [%s]' % e) raise CKException('check topic error')
def get_question_by_id(self, question_id): mysql = Mysql() try: mysql.connect_master() search_sql = "select A.Knowledge,B.TypeValue,A.QuesAbility,A.QuesDiff,A.QuesBody,A.QuesAnswer,A.QuesParse from(select Knowledge,QuesType,QuesAbility,QuesDiff,QuesBody,QuesAnswer,QuesParse from paper.paper where ID=%(question_id)d union all select Knowledge,QuesType,QuesAbility,QuesDiff,QuesBody,QuesAnswer,QuesParse from paper.cz_paper where ID=%(question_id)d)A left outer join (select TypeId,TypeValue from paper.questype)B on (A.QuesType=B.TypeId);" if 0 == mysql.query(search_sql, question_id=int(question_id)): return None else: question_set = mysql.fetch() except DBException as e: pass question_ability_dict = { '1': '了解和识记', '2': '理解和掌握', '3': '简单应用', '4': '综合应用' } question_diff_dict = { '1': '容易', '2': '较易', '3': '一般', '4': '较难', '5': '困难' } question_dict = {} question_dict['knowledge'] = question_set[0] question_dict['type'] = question_set[1] question_dict['ability'] = question_ability_dict[question_set[2]] question_dict['diff'] = question_diff_dict[question_set[3]] question_dict['body'] = question_set[4] question_dict['answer'] = question_set[5] question_dict['parse'] = question_set[6] return question_dict
def add_user(username,password): password = Base.md5(password) mysql = Mysql() mysql.connect_master() query_sql = "insert into verify_user (username,password) values ('%(username)s','%(password)s');" try: if mysql.query(query_sql,username = username,password = password): return mysql.get_last_id() else: return None except DBException as e: LOG.error('add user error [%s]' % e) raise CkException('add user error')
def check_user(username,password): password = Base.md5(password) mysql = Mysql() mysql.connect_master() query_sql = "select password from verify_user where username='******';" try: if mysql.query(query_sql,username = username): pwd = mysql.fetch()[0] if password == pwd: return True else: return False except DBException as e: LOG.error('check user error [%s]' % e) raise CkException('check user error')
def load(): configer = Configer('../config.ini') log_info = configer.get_configer('LOG','info') log_path = configer.get_configer('LOG','path') log_format = '%(asctime)s - %(name)s - %(levelname)s - %(filename)s - %(funcName)s - %(message)s' logger = Logger(info = log_info,path = log_path,format = log_format) LOG = logger.get_logger() ''' from mongo import Mongo mongo = Mongo() mongo.connect('resource') ''' from mysql import Mysql mysql = Mysql() mysql.connect_master() '''
def post(self): for i in range(1): LOG.info('API IN[%s]' % (self.__class__.__name__)) LOG.info('PARAMETER IN[%s]' % self.request.arguments) ret = {'code':'','message':''} essential_keys = set(['question_id','group_id']) if Base.check_parameter(set(self.request.arguments.keys()),essential_keys): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERROR[in parameter invalid]') break question_id = ''.join(self.request.arguments['question_id']) group_id = ''.join(self.request.arguments['group_id']) if Base.empty(question_id) or Base.empty(group_id): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERROR[parameter empty]') break configer = Configer() remote_host = configer.get_configer('REMOTE','host') remote_port = configer.get_configer('REMOTE','port') remote_uri = configer.get_configer('REMOTE','uri') remote_url = "http://%s:%s/%s" % (remote_host,remote_port,remote_uri) token = self.get_cookie("teacher_id") LOG.info('TOKEN[%s]' % token) if token is None: ret['code'] = 6 ret['message'] = '无效参数' LOG.error('ERROR[token empty]') break post_data = {'token' : token} client = httpclient.AsyncHTTPClient() response = yield gen.Task(client.fetch,remote_url,method = 'POST',body = urllib.urlencode(post_data)) #response = Http.post(remote_url,post_data) LOG.info('REMOTE RES CODE[%d]' % response.code) if 200 == response.code: encode_body = json.loads(response.body) if 0 == encode_body['code'] or 2 == encode_body['code']: ret['code'] = 7 ret['message'] = '无效参数' LOG.error('ERROR[token not exist]') break if 1 == encode_body['code']: subject_id = encode_body['subject_id'] grade_id = encode_body['grade_id'] system_id = encode_body['system_id'] org_type = encode_body['org_type'] db = Mysql() question_sql = "update entity_question set question_group=%(group_id)d where upload_id=%(system_id)d and id=%(question_id)d;" try: db.connect_master() question_res = db.query(question_sql,question_id = int(question_id),system_id = int(system_id),group_id=int(group_id)) if not question_res: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[mysql error]') break question_sql = db.get_last_sql() LOG.info('SQL[%s] - RES[%s]' % (question_sql,question_res)) except DBException as e: db.rollback() ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[mysql error]') break else: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[remote error]') break else: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[remote error]') break ret['code'] = 0 ret['message'] = 'success' break self.write(json.dumps(ret)) self.finish() LOG.info('PARAMETER OUT[%s]' % ret) LOG.info('API OUT[%s]' % (self.__class__.__name__))
def post(self): for i in range(1): LOG.info('API IN[%s]' % (self.__class__.__name__)) LOG.info('PARAMETER IN[%s]' % self.request.arguments) ret = {'code':'','message':''} essential_keys = set(['name']) if Base.check_parameter(set(self.request.arguments.keys()),essential_keys): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERROR[in parameter invalid]') break group_name = ''.join(self.request.arguments['name']) #timestamp = ''.join(self.request.arguments['timestamp']) #secret = ''.join(self.request.arguments['secret']) if Base.empty(group_name): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERROR[parameter empty]') break #key = group_name + timestamp; #secret_key = sha1(key).hexdigest() configer = Configer() remote_host = configer.get_configer('REMOTE','host') remote_port = configer.get_configer('REMOTE','port') remote_uri = configer.get_configer('REMOTE','uri') remote_url = "http://%s:%s/%s" % (remote_host,remote_port,remote_uri) token = self.get_cookie("teacher_id") LOG.info('TOKEN[%s]' % token) if token is None: ret['code'] = 6 ret['message'] = '无效参数' LOG.error('ERROR[token empty]') break post_data = {'token' : token} client = httpclient.AsyncHTTPClient() response = yield gen.Task(client.fetch,remote_url,method = 'POST',body = urllib.urlencode(post_data)) #response = Http.post(remote_url,post_data) LOG.info('REMOTE RES CODE[%d]' % response.code) if 200 == response.code: encode_body = json.loads(response.body) if 0 == encode_body['code'] or 2 == encode_body['code']: ret['code'] = 7 ret['message'] = '无效参数' LOG.error('ERROR[token not exist]') break if 1 == encode_body['code']: subject_id = encode_body['subject_id'] grade_id = encode_body['grade_id'] system_id = encode_body['system_id'] org_type = encode_body['org_type'] if Business.group_name_exist(group_name,system_id): ret['code'] = 6 ret['message'] = '组名已存在' LOG.error('ERROR[group exist]') break db = Mysql() group_sql = "insert into entity_group (name,system_id) values ('%(group_name)s',%(system_id)d);" try: db.connect_master() group_res = db.query(group_sql,group_name = group_name,system_id = system_id) group_sql = db.get_last_sql() group_id = db.get_last_id() LOG.info('SQL[%s] - RES[%s] - INS[%d]' % (group_sql,group_res,group_id)) except DBException as e: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[mysql error]') break else: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[remote error]') break else: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[remote error]') break ret['id'] = group_id ret['name'] = group_name ret['code'] = 0 ret['message'] = 'success' break self.write(json.dumps(ret)) self.finish() LOG.info('PARAMETER OUT[%s]' % ret) LOG.info('API OUT[%s]' % (self.__class__.__name__))
def _old_question(data): for i in range(1): mysql = Mysql() try: mysql.connect_master() search_sql = "select id,question_body,question_options,question_answer,question_analysis,question_type,difficulty from entity_question_old where id = %(question_id)d;" if 0 == mysql.query(search_sql, question_id=int(data)): return None else: question_set = mysql.fetch() children_sql = "select id,question_body,question_options,question_answer,question_analysis,question_type,difficulty from entity_question_old where parent_question_id = %(parent_id)d order by id;" if 0 == mysql.query(children_sql, parent_id=int(data)): children_set = None else: children_set = mysql.fetchall() except DBException as e: break domain = "http://%s.okjiaoyu.cn/%s" question_body = question_set[1] question_option = question_set[2] question_answer = question_set[3] question_analysis = question_set[4] question_type = question_set[5] question_level = question_set[6] url_list = [] body_url = '' option_url = '' answer_url = '' analysis_url = '' if question_body is not None: body_bucket = question_body[0:2] body_url = domain % (body_bucket, question_body) url_list.append(body_url) if question_option is not None: option_bucket = question_option[0:2] option_url = domain % (option_bucket, question_option) url_list.append(option_url) if question_answer is not None: answer_bucket = question_answer[0:2] answer_url = domain % (answer_bucket, question_answer) url_list.append(answer_url) if question_analysis is not None: analysis_bucket = question_analysis[0:2] analysis_url = domain % (analysis_bucket, question_analysis) url_list.append(analysis_url) if children_set is not None: for children in children_set: for i in range(1, 5): if children[i] is not None: children_bucket = children[i][0:2] children_url = domain % (children_bucket, children[i]) url_list.append(children_url) return { 'url_list': url_list, 'type': question_type, 'level': question_level, 'q_old_id': data }
def _old_question(data): for i in range(1): mysql = Mysql() try: mysql.connect_master() search_sql = "select id,question_body,question_options,question_answer,question_analysis,question_type,difficulty from entity_question_old where id = %(question_id)d;" if 0 == mysql.query(search_sql,question_id = int(data)): return None else: question_set = mysql.fetch() children_sql = "select id,question_body,question_options,question_answer,question_analysis,question_type,difficulty from entity_question_old where parent_question_id = %(parent_id)d order by id;" if 0 == mysql.query(children_sql,parent_id = int(data)): children_set = None else: children_set = mysql.fetchall() except DBException as e: break domain = "http://%s.okjiaoyu.cn/%s" question_body = question_set[1] question_option = question_set[2] question_answer = question_set[3] question_analysis = question_set[4] question_type = question_set[5] question_level = question_set[6] url_list = [] body_url = '' option_url = '' answer_url = '' analysis_url = '' if question_body is not None: body_bucket = question_body[0:2] body_url = domain % (body_bucket,question_body) url_list.append(body_url) if question_option is not None: option_bucket = question_option[0:2] option_url = domain % (option_bucket,question_option) url_list.append(option_url) if question_answer is not None: answer_bucket = question_answer[0:2] answer_url = domain % (answer_bucket,question_answer) url_list.append(answer_url) if question_analysis is not None: analysis_bucket = question_analysis[0:2] analysis_url = domain % (analysis_bucket,question_analysis) url_list.append(analysis_url) if children_set is not None: for children in children_set: for i in range(1,5): if children[i] is not None: children_bucket = children[i][0:2] children_url = domain % (children_bucket,children[i]) url_list.append(children_url) return {'url_list' : url_list,'type' : question_type,'level' : question_level,'q_old_id' : data}
def post(self): for i in range(1): self.set_header("Access-Control-Allow-Origin", "*") LOG.info('API IN[%s]' % (self.__class__.__name__)) LOG.info('PARAMETER IN[%s]' % self.request.arguments) ret = {'code': '', 'message': ''} essential_keys = set([ 'json', 'html', 'topic', 'level', 'type', 'group', 'chapter', 'ref' ]) if Base.check_parameter(set(self.request.arguments.keys()), essential_keys): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[in parameter invalid]') break question_json = ''.join(self.request.arguments['json']) question_html = ''.join(self.request.arguments['html']) question_topic = ''.join(self.request.arguments['topic']) question_level = ''.join(self.request.arguments['level']) question_type = ''.join(self.request.arguments['type']) question_group = ''.join(self.request.arguments['group']) question_chapter = ''.join(self.request.arguments['chapter']) ref = ''.join(self.request.arguments['ref']) if Business.is_level(question_level) is False: ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[level is invalid]') break try: #question_json = urllib.unquote(question_json) #question_json = question_json.replace("'","\"") encode_json = json.loads(question_json, encoding='utf-8') #question_html = urllib.unquote(question_html) #question_html = question_html.replace("'","\"") encode_html = json.loads(question_html, encoding='utf-8') LOG.info('Json Loads Successful') answer_num = 0 if Base.empty(question_topic) and Base.empty(question_chapter): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[topic and chapter empty]') break if Base.empty(question_group): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[group empty]') break if Base.empty(question_topic) is False: topic_list = question_topic.split(',') for question_theme in topic_list: if Business.is_topic(question_theme) is False: ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[topic %s invalid]' % question_theme) break type_name = Business.is_type(question_type) if type_name is False: ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[type is invalid]') break option_num = 0 LOG.info('Json Parse Start') if type_name == '选择题'.decode('utf-8'): if 'answer' in encode_json.keys(): answer_num = len(encode_json['answer']) option_num = len(encode_json['options']) if type_name == '填空题'.decode('utf-8'): if 'answer' in encode_json.keys(): answer_num = max([ int(group['index']) for group in encode_json['answer'] ]) LOG.info('Json Parse End') if not Base.empty(question_chapter): if Business.chapter_id_exist(question_chapter) is False: ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[seriess %s invalid]' % question_theme) break except (ValueError, KeyError, TypeError): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[json format invalid]') break except CKException: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERR[mysql exception]') break key = question_topic + question_level + question_type + question_group secret_key = hashlib.sha1(key).hexdigest() qiniu = QiniuWrap() json_key = 'tmp_' + secret_key + '.json' if not qiniu.upload_data("temp", json_key, question_json): ret['code'] = 4 ret['message'] = '服务器错误' LOG.error('ERR[json upload qiniu exception]') break html_key = 'tmp_' + secret_key + '.html' if not qiniu.upload_data("temp", html_key, question_html): ret['code'] = 4 ret['message'] = '服务器错误' LOG.error('ERR[html upload qiniu exception]') break configer = Configer() remote_host = configer.get_configer('REMOTE', 'host') remote_port = configer.get_configer('REMOTE', 'port') remote_uri = configer.get_configer('REMOTE', 'uri') remote_timeout = configer.get_configer('REMOTE', 'timeout') remote_url = "http://%s:%s/%s" % (remote_host, remote_port, remote_uri) token = self.get_cookie("teacher_id") LOG.info('TOKEN[%s]' % token) if token is None: ret['code'] = 6 ret['message'] = 'token失效' LOG.error('ERROR[token empty]') break post_data = {'token': token} client = httpclient.AsyncHTTPClient() response = yield gen.Task(client.fetch, remote_url, request_timeout=int(remote_timeout), method='POST', body=urllib.urlencode(post_data)) #response = Http.post(remote_url,post_data) if 200 == response.code: encode_body = json.loads(response.body, encoding='utf-8') if 0 == encode_body['code'] or 2 == encode_body['code']: ret['code'] = 7 ret['message'] = 'token失效' LOG.error('ERR[token not exist]') break if 1 == encode_body['code']: subject_id = encode_body['subject_id'] grade_id = encode_body['grade_id'] system_id = encode_body['system_id'] org_type = encode_body['org_type'] if 0 != int(question_group): if Business.group_id_exist(question_group, system_id) is False: ret['code'] = 8 ret['message'] = '无效参数' LOG.error('ERROR[group not exist]') break db = Mysql() question_sql = "insert into entity_question (difficulty,question_docx,html,upload_time,update_time,question_type,subject_id,new_format,upload_id,upload_src,question_group,grade_id,state,is_single,question_type_id,answer_num,count_ref,paper_year,parent_question_id,count_options) values (%(level)d,'%(json)s','%(html)s',now(),now(),'%(type)s',%(subject_id)d,1,%(upload_id)d,%(upload_src)d,%(question_group)d,%(grade_id)d,'ENABLED',1,%(question_type_id)d,%(answer_num)d,0,0,0,%(count_options)d);" link_topic_sql = "insert into link_question_topic (question_id,topic_id) values (%(q_id)d,%(t_id)d);" link_chapter_sql = "insert into link_question_chapter (question_id,chapter_id) values (%(q_id)d,%(c_id)d);" try: db.connect_master() db.start_event() question_res = db.exec_event( question_sql, level=int(question_level), json=json_key, html=html_key, type=type_name, subject_id=int(subject_id), upload_id=int(system_id), upload_src=int(org_type), question_group=int(question_group), grade_id=int(grade_id), question_type_id=int(question_type), answer_num=answer_num, count_options=option_num) question_sql = db.get_last_sql() question_id = db.get_last_id() LOG.info('RES[%s] - INS[%d]' % (question_res, question_id)) if Base.empty(question_topic) is False: topic_list = question_topic.split(',') for question_theme in topic_list: topic_res = db.exec_event( link_topic_sql, q_id=int(question_id), t_id=int(question_theme)) topic_sql = db.get_last_sql() topic_id = db.get_last_id() LOG.info('RES[%s] - INS[%d]' % (topic_res, topic_id)) if not Base.empty(question_chapter): chapter_res = db.exec_event( link_chapter_sql, q_id=int(question_id), c_id=int(question_chapter)) chapter_sql = db.get_last_sql() chapter_id = db.get_last_id() LOG.info('RES[%s] - INS[%d]' % (chapter_res, chapter_id)) except DBException as e: db.rollback() db.end_event() ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERR[insert mysql error]') break else: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[remote error]') break mongo = Mongo() try: mongo.connect('resource') mongo.select_collection('mongo_question_json') json_id = mongo.insert_one({ "content": encode_json, "question_id": question_id }) LOG.info( 'MONGO[insert json] - DATA[%s] - INS[%s] - Question Id[%d]' % (json.dumps(encode_json), json_id, question_id)) mongo.select_collection('mongo_question_html') html_id = mongo.insert_one({ "content": encode_html, "question_id": question_id }) LOG.info( 'MONGO[insert html] - DATA[%s] - INS[%s] - Question Id[%d]' % (json.dumps(encode_html), html_id, question_id)) except DBException as e: db.rollback() db.end_event() ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERR[mongo exception]') break db.end_event() if int(ref): doit = Doit() doit.local_img(str(question_id)) LOG.info('Local Img [%s]' % str(question_id)) ret['code'] = 0 ret['message'] = 'success' ret['id'] = question_id LOG.info('PARAMETER OUT[%s]' % ret) LOG.info('API OUT[%s]' % (self.__class__.__name__)) self.write(json.dumps(ret)) self.finish()
def post(self): for i in range(1): LOG.info('API IN[%s]' % (self.__class__.__name__)) LOG.info('PARAMETER IN[%s]' % self.request.arguments) ret = {'code': '', 'message': ''} essential_keys = set(['name']) if Base.check_parameter(set(self.request.arguments.keys()), essential_keys): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERROR[in parameter invalid]') break group_name = ''.join(self.request.arguments['name']) #timestamp = ''.join(self.request.arguments['timestamp']) #secret = ''.join(self.request.arguments['secret']) if Base.empty(group_name): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERROR[parameter empty]') break #key = group_name + timestamp; #secret_key = sha1(key).hexdigest() configer = Configer() remote_host = configer.get_configer('REMOTE', 'host') remote_port = configer.get_configer('REMOTE', 'port') remote_uri = configer.get_configer('REMOTE', 'uri') remote_url = "http://%s:%s/%s" % (remote_host, remote_port, remote_uri) token = self.get_cookie("teacher_id") LOG.info('TOKEN[%s]' % token) if token is None: ret['code'] = 6 ret['message'] = '无效参数' LOG.error('ERROR[token empty]') break post_data = {'token': token} client = httpclient.AsyncHTTPClient() response = yield gen.Task(client.fetch, remote_url, method='POST', body=urllib.urlencode(post_data)) #response = Http.post(remote_url,post_data) LOG.info('REMOTE RES CODE[%d]' % response.code) if 200 == response.code: encode_body = json.loads(response.body) if 0 == encode_body['code'] or 2 == encode_body['code']: ret['code'] = 7 ret['message'] = '无效参数' LOG.error('ERROR[token not exist]') break if 1 == encode_body['code']: subject_id = encode_body['subject_id'] grade_id = encode_body['grade_id'] system_id = encode_body['system_id'] org_type = encode_body['org_type'] if Business.group_name_exist(group_name, system_id): ret['code'] = 6 ret['message'] = '组名已存在' LOG.error('ERROR[group exist]') break db = Mysql() group_sql = "insert into entity_group (name,system_id) values ('%(group_name)s',%(system_id)d);" try: db.connect_master() group_res = db.query(group_sql, group_name=group_name, system_id=system_id) group_sql = db.get_last_sql() group_id = db.get_last_id() LOG.info('SQL[%s] - RES[%s] - INS[%d]' % (group_sql, group_res, group_id)) except DBException as e: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[mysql error]') break else: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[remote error]') break else: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[remote error]') break ret['id'] = group_id ret['name'] = group_name ret['code'] = 0 ret['message'] = 'success' break self.write(json.dumps(ret)) self.finish() LOG.info('PARAMETER OUT[%s]' % ret) LOG.info('API OUT[%s]' % (self.__class__.__name__))
def post(self): for i in range(1): LOG.info('API IN[%s]' % (self.__class__.__name__)) LOG.info('PARAMETER IN[%s]' % self.request.arguments) ret = {'code': '', 'message': ''} essential_keys = set(['question_id', 'group_id']) if Base.check_parameter(set(self.request.arguments.keys()), essential_keys): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERROR[in parameter invalid]') break question_id = ''.join(self.request.arguments['question_id']) group_id = ''.join(self.request.arguments['group_id']) if Base.empty(question_id) or Base.empty(group_id): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERROR[parameter empty]') break configer = Configer() remote_host = configer.get_configer('REMOTE', 'host') remote_port = configer.get_configer('REMOTE', 'port') remote_uri = configer.get_configer('REMOTE', 'uri') remote_url = "http://%s:%s/%s" % (remote_host, remote_port, remote_uri) token = self.get_cookie("teacher_id") LOG.info('TOKEN[%s]' % token) if token is None: ret['code'] = 6 ret['message'] = '无效参数' LOG.error('ERROR[token empty]') break post_data = {'token': token} client = httpclient.AsyncHTTPClient() response = yield gen.Task(client.fetch, remote_url, method='POST', body=urllib.urlencode(post_data)) #response = Http.post(remote_url,post_data) LOG.info('REMOTE RES CODE[%d]' % response.code) if 200 == response.code: encode_body = json.loads(response.body) if 0 == encode_body['code'] or 2 == encode_body['code']: ret['code'] = 7 ret['message'] = '无效参数' LOG.error('ERROR[token not exist]') break if 1 == encode_body['code']: subject_id = encode_body['subject_id'] grade_id = encode_body['grade_id'] system_id = encode_body['system_id'] org_type = encode_body['org_type'] db = Mysql() question_sql = "update entity_question set question_group=%(group_id)d where upload_id=%(system_id)d and id=%(question_id)d;" try: db.connect_master() question_res = db.query(question_sql, question_id=int(question_id), system_id=int(system_id), group_id=int(group_id)) if not question_res: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[mysql error]') break question_sql = db.get_last_sql() LOG.info('SQL[%s] - RES[%s]' % (question_sql, question_res)) except DBException as e: db.rollback() ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[mysql error]') break else: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[remote error]') break else: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[remote error]') break ret['code'] = 0 ret['message'] = 'success' break self.write(json.dumps(ret)) self.finish() LOG.info('PARAMETER OUT[%s]' % ret) LOG.info('API OUT[%s]' % (self.__class__.__name__))
def post(self): for i in range(1): self.set_header("Access-Control-Allow-Origin", "*") LOG.info('API IN[%s]' % (self.__class__.__name__)) LOG.info('PARAMETER IN[%s]' % self.request.arguments) ret = {'code':'','message':''} essential_keys = set(['json','html','topic','level','type','group','chapter','ref']) if Base.check_parameter(set(self.request.arguments.keys()),essential_keys): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[in parameter invalid]') break question_json = ''.join(self.request.arguments['json']) question_html = ''.join(self.request.arguments['html']) question_topic = ''.join(self.request.arguments['topic']) question_level = ''.join(self.request.arguments['level']) question_type = ''.join(self.request.arguments['type']) question_group = ''.join(self.request.arguments['group']) question_chapter = ''.join(self.request.arguments['chapter']) ref = ''.join(self.request.arguments['ref']) if Business.is_level(question_level) is False: ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[level is invalid]') break try: #question_json = urllib.unquote(question_json) #question_json = question_json.replace("'","\"") encode_json = json.loads(question_json,encoding = 'utf-8') #question_html = urllib.unquote(question_html) #question_html = question_html.replace("'","\"") encode_html = json.loads(question_html,encoding = 'utf-8') LOG.info('Json Loads Successful') answer_num = 0 if Base.empty(question_topic) and Base.empty(question_chapter): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[topic and chapter empty]') break if Base.empty(question_group): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[group empty]') break if Base.empty(question_topic) is False: topic_list = question_topic.split(',') for question_theme in topic_list: if Business.is_topic(question_theme) is False: ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[topic %s invalid]' % question_theme) break type_name = Business.is_type(question_type) if type_name is False: ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[type is invalid]') break option_num = 0 LOG.info('Json Parse Start') if type_name == '选择题'.decode('utf-8'): if 'answer' in encode_json.keys(): answer_num = len(encode_json['answer']) option_num = len(encode_json['options']) if type_name == '填空题'.decode('utf-8'): if 'answer' in encode_json.keys(): answer_num = max([int(group['index']) for group in encode_json['answer']]) LOG.info('Json Parse End') if not Base.empty(question_chapter): if Business.chapter_id_exist(question_chapter) is False: ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[seriess %s invalid]' % question_theme) break except (ValueError,KeyError,TypeError): ret['code'] = 1 ret['message'] = '无效参数' LOG.error('ERR[json format invalid]') break except CKException: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERR[mysql exception]') break key = question_topic + question_level + question_type + question_group secret_key = hashlib.sha1(key).hexdigest() qiniu = QiniuWrap() json_key = 'tmp_' + secret_key + '.json' if not qiniu.upload_data("temp",json_key,question_json): ret['code'] = 4 ret['message'] = '服务器错误' LOG.error('ERR[json upload qiniu exception]') break html_key = 'tmp_' + secret_key + '.html' if not qiniu.upload_data("temp",html_key,question_html): ret['code'] = 4 ret['message'] = '服务器错误' LOG.error('ERR[html upload qiniu exception]') break configer = Configer() remote_host = configer.get_configer('REMOTE','host') remote_port = configer.get_configer('REMOTE','port') remote_uri = configer.get_configer('REMOTE','uri') remote_timeout = configer.get_configer('REMOTE','timeout') remote_url = "http://%s:%s/%s" % (remote_host,remote_port,remote_uri) token = self.get_cookie("teacher_id") LOG.info('TOKEN[%s]' % token) if token is None: ret['code'] = 6 ret['message'] = 'token失效' LOG.error('ERROR[token empty]') break post_data = {'token' : token} client = httpclient.AsyncHTTPClient() response = yield gen.Task(client.fetch,remote_url,request_timeout = int(remote_timeout),method = 'POST',body = urllib.urlencode(post_data )) #response = Http.post(remote_url,post_data) if 200 == response.code: encode_body = json.loads(response.body,encoding = 'utf-8') if 0 == encode_body['code'] or 2 == encode_body['code']: ret['code'] = 7 ret['message'] = 'token失效' LOG.error('ERR[token not exist]') break if 1 == encode_body['code']: subject_id = encode_body['subject_id'] grade_id = encode_body['grade_id'] system_id = encode_body['system_id'] org_type = encode_body['org_type'] if 0 != int(question_group): if Business.group_id_exist(question_group,system_id) is False: ret['code'] = 8 ret['message'] = '无效参数' LOG.error('ERROR[group not exist]') break db = Mysql() question_sql = "insert into entity_question (difficulty,question_docx,html,upload_time,update_time,question_type,subject_id,new_format,upload_id,upload_src,question_group,grade_id,state,is_single,question_type_id,answer_num,count_ref,paper_year,parent_question_id,count_options) values (%(level)d,'%(json)s','%(html)s',now(),now(),'%(type)s',%(subject_id)d,1,%(upload_id)d,%(upload_src)d,%(question_group)d,%(grade_id)d,'ENABLED',1,%(question_type_id)d,%(answer_num)d,0,0,0,%(count_options)d);" link_topic_sql = "insert into link_question_topic (question_id,topic_id) values (%(q_id)d,%(t_id)d);" link_chapter_sql = "insert into link_question_chapter (question_id,chapter_id) values (%(q_id)d,%(c_id)d);" try: db.connect_master() db.start_event() question_res = db.exec_event(question_sql,level = int(question_level),json = json_key,html = html_key,type = type_name,subject_id = int(subject_id),upload_id = int(system_id),upload_src = int(org_type),question_group = int(question_group),grade_id = int(grade_id),question_type_id = int(question_type),answer_num = answer_num,count_options = option_num) question_sql = db.get_last_sql() question_id = db.get_last_id() LOG.info('RES[%s] - INS[%d]' % (question_res,question_id)) if Base.empty(question_topic) is False: topic_list = question_topic.split(',') for question_theme in topic_list: topic_res = db.exec_event(link_topic_sql,q_id = int(question_id),t_id = int(question_theme)) topic_sql = db.get_last_sql() topic_id = db.get_last_id() LOG.info('RES[%s] - INS[%d]' % (topic_res,topic_id)) if not Base.empty(question_chapter): chapter_res = db.exec_event(link_chapter_sql,q_id = int(question_id),c_id = int(question_chapter)) chapter_sql = db.get_last_sql() chapter_id = db.get_last_id() LOG.info('RES[%s] - INS[%d]' % (chapter_res,chapter_id)) except DBException as e: db.rollback() db.end_event() ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERR[insert mysql error]') break else: ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERROR[remote error]') break mongo = Mongo() try: mongo.connect('resource') mongo.select_collection('mongo_question_json') json_id = mongo.insert_one({"content":encode_json,"question_id":question_id}) LOG.info('MONGO[insert json] - DATA[%s] - INS[%s] - Question Id[%d]' % (json.dumps(encode_json),json_id,question_id)) mongo.select_collection('mongo_question_html') html_id = mongo.insert_one({"content":encode_html,"question_id":question_id}) LOG.info('MONGO[insert html] - DATA[%s] - INS[%s] - Question Id[%d]' % (json.dumps(encode_html),html_id,question_id)) except DBException as e: db.rollback() db.end_event() ret['code'] = 3 ret['message'] = '服务器错误' LOG.error('ERR[mongo exception]') break db.end_event() if int(ref): doit = Doit() doit.local_img(str(question_id)) LOG.info('Local Img [%s]' % str(question_id)) ret['code'] = 0 ret['message'] = 'success' ret['id'] = question_id LOG.info('PARAMETER OUT[%s]' % ret) LOG.info('API OUT[%s]' % (self.__class__.__name__)) self.write(json.dumps(ret)) self.finish()