| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import pymysql
- import json
- # from jsonencoder import JsonEncoder
- class ZkTools():
- db=None
- def __init__(self):
- self.db = pymysql.connect(host="localhost", user="user",
- password="password", db="zk")
-
- def get_courses(self):
- cursor=self.db.cursor()
- sql="select `data` from `course`"
- cursor.execute(sql)
- data=cursor.fetchone()[0]
- data=json.loads(data)
- self.db.commit()
- res=[]
- for i in data['results']:
- res.append({'id':i['id'], 'name':i['name']})
- return res
- def get_questions(self, course_id):
- cursor=self.db.cursor()
- sql="select `data` from `question_list` where `course_id` = %s"
- cursor.execute(sql, course_id)
- data=cursor.fetchone()[0]
- data=json.loads(data)
- self.db.commit()
- res=[]
- for i in data['results']:
- res.append({'id':i['id'], 'name':i['title']})
- return res
- def get_content(self, question_id):
- cursor=self.db.cursor()
- sql="select `content_object` from `question_content_object` where `question_id` = %s"
- cursor.execute(sql, question_id)
- data=cursor.fetchone()[0]
- data=json.loads(data)
- self.db.commit()
- return data
- def format(self, data):
- # data=data["data"]
- temp="<h1>%s</h1>\n" % data['title']
- # memo1
- if data["memo"] != "":
- temp+="<p>%s</p>\n" % data["memo"]
- # groups
- temp+="<div>\n"
-
- for group_index in range(0, len(data['groups'])):
- group=data['groups'][group_index]
- temp+="<h2>%s</h2>\n" % group['title']
- if group['memo'] != "":
- temp+="<p>%s</p>\n" % group['memo']
- # questions
- temp+="<div>\n"
- for question_index in range(0, len(group['questions'])):
- question=group['questions'][question_index]
- temp+="<p style=\"font-weight:bold;\">%s. %s</p>\n" %(question_index+1, question['title'])
- temp+="<ul>\n"
- for option in question['options']:
- temp+="<li>%s. %s</li>\n" % (option['value'], option['text'])
- temp+="</ul>\n"
- for answer_index in range(0,len(question['answer'])):
- answer=question['answer'][answer_index]
- if type(answer) == list:
- question['answer'][answer_index]=";".join(question['answer'][answer_index])
- temp+="<p><b>答案:</b>%s</p>\n" % (";".join(question['answer']))
- if 'explanation' in question:
- temp+="<p><b>题解:</b>%s</p>\n" % (question['explanation'])
- temp+= "<hr></hr>\n" if question_index < len(group['questions'])-1 else ""
- temp+="</div>\n"
- temp+="</div>\n"
- return temp
|