Exemplo n.º 1
0
    def get(self, cartype):
        result_data = {
            'error_code': 0,
            'reason': 'ok',
            'data': [],
        }
        if cartype in self.CARTYPE:
            # 检查是否有缓存数据
            cache_list = car_question_cache_factory(cartype)
            if not cache_list:
                # cache 空:缓存数据
                question_data_cartype = car_question_factory(cartype)
                all_data = question_data_cartype.get_all()
                for each_data in all_data:
                    data_info = get_question_info(each_data)
                    cache_list.append(data_info)
            cache_list = car_question_cache_factory(cartype)


            for data in cache_list:
                data_info = {}
                data_info["id"] = data['id']
                data_info["ta"] = data['ta']
                data_info["Type"] = data['Type']
                result_data['data'].append(data_info)
        self.write_json(result_data)
Exemplo n.º 2
0
    def get(self, cartype):

        result_data = {
            'error_code': 0,
            'reason': 'ok',
            'data': [],
        }
        cache_list = car_question_cache_factory(cartype)
        if not cache_list:
            all_data = car_question_factory(cartype).get_all()
            if len(all_data) != len(cache_list):
                #缓存数据与后台数据不同一,则更新缓存
                for question in all_data:
                    temp = {}
                    temp["id"] = question.qid
                    temp["uuid"] = question.uuid
                    temp["question"] = question.question
                    temp["answer"] = question.answer
                    temp["explains"] = question.explains
                    temp["a"] = question.item1
                    temp["b"] = question.item2
                    temp["c"] = question.item3
                    temp["d"] = question.item4
                    temp["img"] = question.img_url
                    cache_list.append(temp)

        result_data['data'].extend(cache_list)
        self.write_json(result_data)
Exemplo n.º 3
0
    def get(self, cartype):
        # print(cartype)
        result_data = {
            'error_code': 0,
            'reason': 'ok',
            'data': [],
        }
        uuid = self.get_argument('uuid', None)

        for_del_data = car_question_factory(cartype).\
                                    get_by_uuid(uuid)
        # print(uuid, for_del_data.locked, for_del_data)
        if for_del_data:
            for_del_data.locked = True
            self.dbs.add(for_del_data)
            try:
                self.dbs.commit()  # 删除问题数据
            except Exception as e:
                print(e)
                self.dbs.rollback()
                result_data['error_code'] = 400
                result_data['reason'] = '删除失败'
            finally:
                self.dbs.close()
        self.write_json(result_data)
Exemplo n.º 4
0
    def get(self, cartype):
        result_data = {
            'error_code': 0,
            'reason': 'ok',
            'data': [],
        }
        #qid = self.get_argument('id', None)
        uuid = self.get_argument('uuid', None)
        question = self.get_argument('question', None)  # 问题叙述
        bestanswer = self.get_argument('bestanswer', None)  # 答案详解
        a = self.get_argument('a', None)
        b = self.get_argument('b', None)
        c = self.get_argument('c', None)
        d = self.get_argument('d', None)
        ta = self.get_argument('ta', None)  # 答案
        imageurl = self.get_argument('img', None)
        locked = bool(self.get_argument('locked', None))

        cartype_orm = car_question_factory(cartype)
        if uuid:
            '''传入uuid则为修改数据'''
            data_info = cartype_orm.get_by_uuid(uuid)
            #data_info.qid = qid
            data_info.question = question
            data_info.explains = bestanswer
            data_info.answer = ta
            data_info.img_url = imageurl
            data_info.item1 = a
            data_info.item2 = b
            data_info.item3 = c
            data_info.item4 = d
            data_info.locked = locked
        else:
            '''添加数据'''
            data_info = cartype_orm(
                #qid=qid,
                question=question,
                explains=bestanswer,
                item1=a,
                item2=b,
                item3=c,
                item4=d,
                answer=ta,
                img_url=imageurl,
                locked=locked)

        self.dbs.add(data_info)
        try:
            self.dbs.commit()
        except Exception as e:
            print(e)
            self.dbs.rollback()
            result_data['error_code'] = 400
            result_data['reason'] = '数据提交错误!'
        finally:
            self.dbs.close()
        self.write_json(result_data)
Exemplo n.º 5
0
    def get(self, cartype):
        uuid = self.get_argument('uuid', None)
        data = car_question_factory(cartype).get_by_uuid(uuid)

        # print(data, uuid)
        self.render(
            'control/edit-data.html',
            data=data,
        )
Exemplo n.º 6
0
    def get(self, cartype):
        # print(self.current_user)

        all_data = car_question_factory(cartype).get_all()
        result_data = []

        for data in all_data:
            if data.locked:
                continue
            result_data.append(data)
            # print(result_data)
        self.render('control/show.html',
                    data=result_data,
                    uri=self.request.uri)
Exemplo n.º 7
0
    def save_to_database(self):
        if self._cartype in self.questiontype:
            if not car_question_factory(self._cartype).get_all():
                if self._cartype == 'small':
                    for eachtype in self.alltype['small']:
                        small_car = car_question_factory(
                            self._cartype)  #小车类型(数据库)
                        remote_data = api_exam.exam(1,
                                                    model=eachtype,
                                                    testType='order')
                        self.load_data(remote_data, small_car)
                elif self._cartype == 'truck':
                    for eachtype in self.alltype['truck']:
                        truck_car = car_question_factory(self._cartype)  #货车类型
                        remote_data = api_exam.exam(1,
                                                    model=eachtype,
                                                    testType='order')
                        self.load_data(remote_data, truck_car)
                elif self._cartype == 'bus':
                    for eachtype in self.alltype['bus']:
                        bus_car = car_question_factory(self._cartype)  # 客车类型
                        remote_data = api_exam.exam(1,
                                                    model=eachtype,
                                                    testType='order')
                        self.load_data(remote_data, bus_car)

                elif self._cartype == '4':
                    for eachtype in self.alltype['4']:
                        car4 = car_question_factory(self._cartype)  # 科目四
                        remote_data = api_exam.exam(4,
                                                    model=4,
                                                    testType='order')
                        self.load_data(remote_data, car4)
            else:
                print('已初始化数据库!!!请清空数据库...')

            datebase_session.close()
Exemplo n.º 8
0
    def get(self, cartype):
        '''

        :param cartype: c参数:small, truck, bus, 4
        :return: json data
        '''
        result_data = {
            'error_code': 0,
            'reason': 'ok',
            'data': [],
        }
        try:
            # 捕获题库查询错误异常
            question_data_cartype = car_question_factory(cartype)
        except KeyError as e:
            result_data = {
                'error_code': 40001,
                'msg': '获取数据失败',
                'data': [],
            }
            # print(e)
            self.write_json(result_data)
        else:
            qid = self.get_argument('id', None)
            try:
                int(qid)
            except (ValueError,TypeError) as e:
                result_data = {
                    'error_code': 40001,
                    'msg': '获取数据失败',
                    'data': [],
                }
                # print(e)
                self.write_json(result_data)
            else:
                self.set_header('Content-Type', 'application/json;charset=utf-8')
                data = question_data_cartype.get_by_id(qid)
                if data:
                    data_info = get_question_info(data)
                    result_data['data'] = data_info
                    self.write_json(result_data)
                else:
                    result_data = {
                        'error_code': 40001,
                        'msg': '获取数据失败',
                        'data': [],
                    }
                    # print(e)
                    self.write_json(result_data)
Exemplo n.º 9
0
 def cache_write_to(self, cartype):
     '''
     写入数据至缓存
     :param cartype:
     :return:
     '''
     # 检查是否有缓存数据
     cache_list = self.cache_get_data_list(cartype)
     if not cache_list:
         # cache 空:缓存数据
         question_data_cartype = car_question_factory(cartype)
         all_data = question_data_cartype.get_all()
         for each_data in all_data:
             data_info = self.get_question_info(each_data)
             self.cache_list_lpush(cartype, data_info)  # 写入redis缓存
Exemplo n.º 10
0
 def get(self, cartype):
     result_data = {
         'error_code': 0,
         'reason': 'ok',
         'data': [],
     }
     if cartype in self.CARTYPE:
         #检查是否有缓存数据
         cache_list = car_question_cache_factory(cartype)
         if not cache_list:
             # cache 空:缓存数据
             question_data_cartype = car_question_factory(cartype)
             all_data = question_data_cartype.get_all()
             for each_data in all_data:
                 data_info = get_question_info(each_data)
                 cache_list.append(data_info)
         result_data['data'].extend(cache_list)
         self.set_header('Content-Type', 'application/json;charset=utf-8')
         self.write_json(result_data)