Ejemplo n.º 1
0
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