class ListHandler(tornado.web.RequestHandler): def initialize(self, mysql_handler, LOG): self.mysql_handler = mysql_handler self.LOG = LOG self.user = User(mysql_handler, LOG) self.order = Order(mysql_handler, LOG) self.evaluation = Evaluation(mysql_handler, LOG) self.device = Device(mysql_handler, LOG) self.picture = Picture(mysql_handler, LOG) self.device_attribute = DeviceAttribute(mysql_handler, LOG) self.rsp_handler = CommResponse() return def post(self): self.LOG.info('this is %s' % self.__class__.__name__) self.LOG.debug(self.request.headers) self.LOG.debug(self.request.body) try: request_json = json.loads(self.request.body) user_phone = request_json['user_phone'] user_login_mobile_uuid = request_json['user_login_mobile_uuid'] user_role = request_json['order_info']['role'] except Exception as e: self.LOG.error('parameters error.') self.rsp_msg = self.rsp_handler.generate_rsp_msg('21001', None) self.write(self.rsp_msg) return if not self.user.check_login_status(user_phone, user_login_mobile_uuid): self.LOG.error('user [%s] not login.' % user_phone) self.mysql_handler.rollback_db() self.rsp_msg = self.rsp_handler.generate_rsp_msg('21006', None) self.write(self.rsp_msg) return if user_role == 'buyer': condition = {'order_buyer': user_phone} elif user_role == 'seller': condition = {'order_seller': user_phone} else: self.LOG.debug('user role [%s] invalid' % user_role) self.rsp_msg = self.rsp_handler.generate_rsp_msg('21011', None) self.mysql_handler.rollback_db() self.write(self.rsp_msg) return orders = self.order.get_order(condition) if not orders: self.LOG.error('user [%s] as role [%s] has no orders.' % (user_phone, user_role)) self.rsp_msg = self.rsp_handler.generate_rsp_msg('200', {'rsp_data': {'orders':[]}}) self.mysql_handler.commit_db() self.write(self.rsp_msg) return for order in orders: device_info = {} device_info = self.device.get_device_by_number(order['order_device']) if not device_info: device_info = {} else: device_attributes = self.device_attribute.get_attribute_by_number(device_info['device_number']) device_info['device_attributes'] = device_attributes if device_attributes else [] device_pictures = self.picture.get_image_by_number(device_info['device_number'], 'device') if device_pictures: for picture in device_pictures: del picture['pic_owner_mark'] del picture['create_time'] del picture['pic_path'] del picture['pic_owner_type'] device_info['device_pictures'] = device_pictures if device_pictures else [] order['device_info'] = dict(device_info.items()) evaluation_info = self.evaluation.get_evaluation(order['order_number']) order['evaluation_text'] = evaluation_info['evaluation_text'] if evaluation_info else '' order['evaluation_time'] = evaluation_info['evaluation_time'] if evaluation_info else '' order['evaluation_level'] = evaluation_info['evaluation_level'] if evaluation_info else 0 order['feedback_text'] = evaluation_info['feedback_text'] if evaluation_info else '' order['feedback_time'] = evaluation_info['feedback_time'] if evaluation_info else '' rsp_data = { 'rsp_data': {'orders': orders} } self.rsp_msg = self.rsp_handler.generate_rsp_msg('200', rsp_data) self.mysql_handler.commit_db() self.write(self.rsp_msg) return
class ShowHandler(tornado.web.RequestHandler): def initialize(self, mysql_handler, LOG): self.mysql_handler = mysql_handler self.LOG = LOG self.user = User(mysql_handler, LOG) self.device = Device(mysql_handler, LOG) self.favorite = Favorite(mysql_handler, LOG) self.picture = Picture(mysql_handler, LOG) self.device_attribute = DeviceAttribute(mysql_handler, LOG) self.rsp_handler = CommResponse() return def post(self): self.LOG.debug('this is %s' % self.__class__.__name__) try: request_json = json.loads(self.request.body) user_phone = request_json['user_phone'] user_login_mobile_uuid = request_json['user_login_mobile_uuid'] collection_type = request_json['collection_type'] except Exception as e: self.LOG.error('parameters error.') self.rsp_msg = self.rsp_handler.generate_rsp_msg('21001', None) self.write(self.rsp_msg) return if not self.user.check_login_status(user_phone, user_login_mobile_uuid): self.LOG.error('user [%s] not login.' % user_phone) self.rsp_msg = self.rsp_handler.generate_rsp_msg('21006', None) self.mysql_handler.rollback_db() self.write(self.rsp_msg) return collections = self.favorite.get_collection(user_phone, collection_type) if not collections: self.LOG.error('user [%s] has no collections.' % user_phone) self.rsp_msg = self.rsp_handler.generate_rsp_msg('200', {'rsp_data': {'collections':[]}}) self.mysql_handler.rollback_db() self.write(self.rsp_msg) return result = [] targets = [collection['collection_mark'] for collection in collections] if collection_type == 'user': users = self.user.get_user_by_phones(targets) for user in users: image_info = self.picture.get_image_by_phone(user['user_phone'], 'user') try: image_big_mame = image_info['pic_big_name'] image_sml_mame = image_info['pic_sml_name'] except: image_big_mame = None image_sml_mame = None user['pic_big_name'] = image_big_mame user['pic_sml_name'] = image_sml_mame del user['user_login_mobile_uuid'] del user['user_phone_mno'] result = users elif collection_type == 'device': devices = self.device.get_device_by_numbers(targets) for device in devices: attributes = self.device_attribute.get_attribute_by_number(device['device_number']) device['device_attributes'] = attributes if attributes else [] pictures = self.picture.get_image_by_number(device['device_number'], 'device') if pictures: for picture in pictures: del picture['pic_owner_mark'] del picture['create_time'] del picture['pic_path'] del picture['pic_owner_type'] device['device_pictures'] = pictures if pictures else [] device['user_phone'] result = devices else: self.LOG.error('collection_type [%s] is error.' % collection_type) self.rsp_msg = self.rsp_handler.generate_rsp_msg('21999', None) self.mysql_handler.rollback_db() self.write(self.rsp_msg) return self.rsp_msg = self.rsp_handler.generate_rsp_msg('200', {'rsp_data': {'collections':result}}) self.mysql_handler.commit_db() self.write(self.rsp_msg) return