class DishesDiscountConfig(models.Model): """ 菜品优惠配置 """ dishes_id = models.IntegerField(u'菜品ID', db_index=True) # dishes_name = models.CharField(u'菜品名称', max_length=40) # business_id = models.IntegerField(u'商户ID') # business_name = models.CharField(u'商品名称', max_length=128) # food_court_id = models.IntegerField(u'美食城ID') # food_court_name = models.CharField(u'美食城名称', max_length=200) service_ratio = models.IntegerField(u'平台商承担(优惠)比例') business_ratio = models.IntegerField(u'商户承担(优惠)比例') # expires = models.DateTimeField(u'优惠券失效日期', default=main.days_7_plus) # 数据状态:1:正常 其它值:已删除 status = models.IntegerField(u'数据状态', default=1) created = models.DateTimeField(u'创建时间', default=now) updated = models.DateTimeField(u'最后更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'ys_dishes_discount_config' unique_together = ('dishes_id', 'status') index_together = (['dishes_id', 'status']) app_label = 'Admin_App.ad_coupons.models.DishesDiscountConfig' @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e
class City(models.Model): """ 城市信息 """ city = models.CharField('城市名称', max_length=40, db_index=True) # 市区数据结构: # [{'id': 1, 'name': u'大兴区'}, ... # ] district = models.CharField('市区信息', max_length=40) user_id = models.IntegerField('创建者') # 状态:1:有效 2:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField(default=now) updated = models.DateTimeField(auto_now=True) objects = BaseManager() class Meta: db_table = 'ys_city' unique_together = ('city', 'district', 'status') ordering = ['city', 'district'] def __unicode__(self): return self.city @classmethod def get_object(cls, **kwargs): try: return cls.objects.get(**kwargs) except Exception as e: return e
class DishesClassify(models.Model): """ 菜品分类信息表 """ name = models.CharField('类别名称', max_length=64, db_index=True) description = models.CharField('类别描述', max_length=256, null=True, blank=True) user_id = models.IntegerField('用户ID') # 状态:1:有效 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'ys_dishes_classify' unique_together = ('user_id', 'name', 'status') ordering = ('name', ) @classmethod def get_object(cls, **kwargs): try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def filter_objects(cls, **kwargs): try: return cls.objects.filter(**kwargs) except Exception as e: return e
class Dimension(models.Model): """ 维度 """ name = models.CharField('维度名称', max_length=32, db_index=True) subtitle = models.CharField('维度副标题', max_length=32, null=True, blank=True) description = models.CharField('维度描述', max_length=256, null=True, blank=True) sort_order = models.IntegerField('排序顺序', default=0) picture = models.ImageField('维度矢量图片', max_length=200, upload_to=IMAGE_PICTURE_PATH, default=os.path.join(IMAGE_PICTURE_PATH, 'noImage.png')) # 资源状态:1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_dimension' unique_together = ('name', 'status') ordering = ['-updated'] def __unicode__(self): return self.name @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def filter_objects(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: instances = cls.objects.filter(**kwargs) except Exception as e: return e # 排序 sort_instances = sorted(instances, key=lambda x: x.sort_order) key = -1 for index, item in enumerate(sort_instances): if item.sort_order != 0: break key = index if key == -1: return sort_instances else: new_sort_list = sort_instances[key + 1:] new_sort_list.extend(sort_instances[:key + 1]) return new_sort_list
class ReplyComment(models.Model): """ 管理员回复点评 """ comment_id = models.IntegerField(u'被回复点评的记录ID', db_index=True) user_id = models.IntegerField('管理员用户ID') message = models.TextField('点评回复', null=True, blank=True) # 数据状态 1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_reply_comment' unique_together = ['comment_id', 'status'] app_label = 'Web_App.web_comment.models.ReplyComment' def __unicode__(self): return str(self.comment_id) @classmethod def get_object(cls, **kwargs): try: return cls.objects.get(**kwargs) except Exception as e: return e
class Role(models.Model): """ 用户角色 """ name = models.CharField('角色名称', max_length=32, db_index=True) # 数据状态:1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_user_role' unique_together = ['name', 'status'] ordering = ['-updated'] def __unicode__(self): return self.name @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def filter_objects(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.filter(**kwargs) except Exception as e: return e
class MediaType(models.Model): """ 资源类型 """ name = models.CharField('资源类型名称', max_length=64, db_index=True) sort_order = models.IntegerField('排序顺序', default=0) # 数据状态 1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_media_type' unique_together = ['name', 'status'] ordering = ['-updated'] app_label = 'Web_App.web_media.models.MediaType' class AdminMeta: fuzzy_fields = ['name'] def __unicode__(self): return '%s' % self.name @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def filter_objects(cls, fuzzy=True, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) if fuzzy: for key in kwargs: if key in cls.AdminMeta.fuzzy_fields: kwargs['%s__contains' % key] = kwargs.pop(key) try: instances = cls.objects.filter(**kwargs) except Exception as e: return e # 排序 sort_instances = sorted(instances, key=lambda x: x.sort_order) key = -1 for index, item in enumerate(sort_instances): if item.sort_order != 0: break key = index if key == -1: return sort_instances else: new_sort_list = sort_instances[key + 1:] new_sort_list.extend(sort_instances[:key + 1]) return new_sort_list
class Tag(models.Model): """ 标签 """ name = models.CharField('标签名称', max_length=64, db_index=True) description = models.CharField('描述', max_length=256, null=True, blank=True) dimension_id = models.IntegerField('所属维度ID', null=True) picture = models.ImageField('简介图片', max_length=200, upload_to=IMAGE_PICTURE_PATH, default=os.path.join(IMAGE_PICTURE_PATH, 'noImage.png')) # picture_detail = models.ImageField('详情图片', max_length=200, # upload_to=IMAGE_PICTURE_PATH, # default=os.path.join(IMAGE_PICTURE_PATH, 'noImage.png')) # 数据状态:1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_tag' unique_together = ['name', 'status'] ordering = ['-updated'] def __unicode__(self): return self.name @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def filter_objects(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.filter(**kwargs) except Exception as e: return e @classmethod def filter_objects_by_dimension_id(cls, dimension_id): attribute_instances = Attribute.filter_objects( dimension_id=dimension_id) attribute_ids = [ins.id for ins in attribute_instances] tag_config_instances = TagConfigure.filter_objects( attribute_id__in=attribute_ids) tag_ids = [ins.tag_id for ins in tag_config_instances] tag_instances = cls.filter_objects(id__in=list(set(tag_ids))) return tag_instances
class AdvertResource(models.Model): """ 广告资源 """ title = models.CharField('广告标题', max_length=128) subtitle = models.CharField('广告副标题', max_length=128, null=True, blank=True) # 点评资源类型: 1:资源 2:案例 3:资讯 source_type = models.IntegerField('媒体资源类型') link_url = models.TextField('链接地址', null=True, blank=True) picture = models.ImageField(max_length=200, upload_to=ADVERT_PICTURE_PATH, default=os.path.join(ADVERT_PICTURE_PATH, 'noImage.png')) # 数据状态:1:有效 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_advert_resource' index_together = ('title', ) app_label = 'Web_App.web_media.models.AdvertResource' class AdminMeta: fuzzy_fields = ['title'] origin_picture = 'picture' perfect_picture = { 'max_disk_size': 1 * 1024 * 1024, 'goal_picture': { 'picture': { 'size': None, 'save_path': ADVERT_PICTURE_PATH, }, }, } def __unicode__(self): return self.title @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def filter_objects(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.filter(**kwargs) except Exception as e: return e
class FoodCourt(models.Model): """ 美食城数据表 """ name = models.CharField('美食城名字', max_length=200, db_index=True) # 美食城类别 10: 公元铭 20:食代铭 type = models.IntegerField('美食城类别', default=10) city_id = models.IntegerField('所属城市ID') city = models.CharField('所属城市', max_length=100, null=False) district = models.CharField('所属市区', max_length=100, null=False) mall = models.CharField('所属购物中心', max_length=200, default='') address = models.CharField('购物中心地址', max_length=256, null=True, blank=True) image = models.ImageField( '美食城平面图', upload_to=FOOD_COURT_DIR, default=os.path.join(FOOD_COURT_DIR, 'noImage.png'), ) # 经度 longitude = models.CharField('经度', max_length=32, null=True) # 维度 latitude = models.CharField('维度', max_length=32, null=True) # 状态:1:有效 2:已删除 status = models.IntegerField('数据状态', default=1) extend = models.TextField('扩展信息', default='', blank=True, null=True) objects = BaseManager() class Meta: db_table = 'ys_food_court' unique_together = ('name', 'mall', 'city_id', 'status') ordering = ['city', 'district'] def __unicode__(self): return self.name @classmethod def get_object(cls, **kwargs): try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def get_object_list(cls, **kwargs): if 'page_size' in kwargs: kwargs.pop('page_size') if 'page_index' in kwargs: kwargs.pop('page_index') return cls.objects.filter(**kwargs)
class AppVersion(models.Model): """ App版本信息 """ version_name = models.CharField('版本号名称', max_length=16) version_code = models.IntegerField('版本号', default=0) message = models.CharField('版本描述信息', max_length=256) is_force_update = models.BooleanField('是否强制更新', default=False) package_path = models.FileField('App包存放目录', upload_to=PACKAGE_DIR) # 数据状态:1:正常,其他:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('最后修改时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'ys_app_version' unique_together = ['version_name', 'version_code', 'status'] ordering = ['-updated'] def __unicode__(self): return '%s.%s' % (self.version_name, self.version_code) @classmethod def get_object(cls, **kwargs): try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def get_last_version(cls): instances = cls.filter_objects() if isinstance(instances, Exception): return instances if instances.count() == 0: return Exception('Version data does not exist.') return instances[0] @classmethod def filter_objects(cls, **kwargs): try: return cls.objects.filter(**kwargs) except Exception as e: return e
class ResourceTags(models.Model): """ 资源标签 """ name = models.CharField('标签名字', max_length=64, db_index=True) description = models.CharField('描述', max_length=256, null=True, blank=True) # 数据状态:1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_resource_tag' ordering = ['-updated'] unique_together = ['name', 'status'] app_label = 'Web_App.web_media.models.ResourceTags' class AdminMeta: fuzzy_fields = ['name'] def __unicode__(self): return self.name @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def filter_objects(cls, fuzzy=True, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) if fuzzy: for key in kwargs: if key in cls.AdminMeta.fuzzy_fields: kwargs['%s__contains' % key] = kwargs.pop(key) try: return cls.objects.filter(**kwargs) except Exception as e: return e
class Role(models.Model): """ 用户角色 """ name = models.CharField('角色名称', max_length=32, db_index=True) # 数据状态:1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_user_role' unique_together = ['name', 'status'] ordering = ['-updated'] app_label = 'Web_App.web_users.models.Role' class AdminMeta: fuzzy_fields = ['name'] def __unicode__(self): return self.name @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def filter_objects(cls, fuzzy=True, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) if fuzzy: if cls.AdminMeta.fuzzy_fields: for key in kwargs: if key in cls.AdminMeta.fuzzy_fields: kwargs['%s__contains' % key] = kwargs.pop(key) try: return cls.objects.filter(**kwargs) except Exception as e: return e
class ClientDetail(models.Model): """ 客户显示屏信息 """ user_id = models.IntegerField(u'用户ID', db_index=True) ip = models.CharField(u'IP地址', max_length=40) port = models.IntegerField(u'端口') # 数据状态:1:有效 2:已删除 status = models.IntegerField(u'数据状态', default=1) created = models.DateTimeField(u'创建时间', default=now) updated = models.DateTimeField(u'更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'ys_client_detail' ordering = ['-created'] def __unicode__(self): return unicode(self.user_id) @classmethod def get_object(cls, **kwargs): try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def filter_objects(cls, **kwargs): try: return cls.objects.filter(**kwargs) except Exception as e: return e @classmethod def get_object_by_user(cls, user_id): instances = cls.filter_objects(user_id=user_id) if isinstance(instances, Exception): return instances if len(instances) == 0: return None return instances[0]
class Article(BaseModelMixin, models.Model): """ 文章 """ url = models.CharField(u'文章url', max_length=128, unique=True) title = models.CharField(u'文章标题', max_length=256, null=True, blank=True) # 数据状态:status 1:正常 非1:已删除 status = models.IntegerField(u'数据状态', default=1) created = models.DateTimeField(u'创建时间', default=now) updated = models.DateTimeField(u'更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'tt_article' def __unicode__(self): return self.title
class Attribute(models.Model): """ 属性 """ name = models.CharField('属性名称', max_length=64, db_index=True) description = models.CharField('描述', max_length=256, null=True, blank=True) dimension_id = models.IntegerField('所属维度ID') # picture = models.ImageField('属性矢量图片', max_length=200, # upload_to=IMAGE_PICTURE_PATH, # default=os.path.join(IMAGE_PICTURE_PATH, 'noImage.png')) # 资源状态:1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_attribute' unique_together = ['name', 'status'] ordering = ['dimension_id', '-updated'] def __unicode__(self): return self.name @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def filter_objects(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.filter(**kwargs) except Exception as e: return e
class TagConfigure(models.Model): """ 标签配置 """ tag_id = models.IntegerField('标签ID', db_index=True) attribute_id = models.IntegerField('匹配属性ID') match_value = models.FloatField('与属性匹配值', default=1.0) # 数据状态:1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_tag_configure' unique_together = ['tag_id', 'attribute_id', 'status'] ordering = ['tag_id', 'attribute_id', '-updated'] def __unicode__(self): return '%s:%s' % (self.tag_id, self.attribute_id) @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def filter_objects(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.filter(**kwargs) except Exception as e: return e
class AdjustCoefficient(models.Model): """ 调整系数 """ name = models.CharField('调整系数名称', max_length=64) description = models.CharField('字段描述', max_length=128) value = models.FloatField('调整值', default=0) # 数据状态:1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_adjust_coefficient' app_label = 'Web_App.web_dimensions.models.AdjustCoefficient' def __unicode__(self): return self.name @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def filter_objects(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.filter(**kwargs) except Exception as e: return e
class Tag(models.Model): """ 标签 """ name = models.CharField('标签名称', max_length=64, db_index=True) description = models.CharField('描述', max_length=256, null=True, blank=True) dimension_id = models.IntegerField('所属维度ID', null=True) picture = models.ImageField('简介图片', max_length=200, upload_to=IMAGE_PICTURE_PATH, default=os.path.join(IMAGE_PICTURE_PATH, 'noImage.png')) # picture_detail = models.ImageField('详情图片', max_length=200, # upload_to=IMAGE_PICTURE_PATH, # default=os.path.join(IMAGE_PICTURE_PATH, 'noImage.png')) # 数据状态:1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_tag' unique_together = ['name', 'status'] ordering = ['-updated'] app_label = 'Web_App.web_dimensions.models.Tag' class AdminMeta: fuzzy_fields = ['name'] origin_picture = 'picture' perfect_picture = { 'max_disk_size': 1 * 1024 * 1024, 'goal_picture': { 'picture': { 'size': (75, 75), 'save_path': TAG_PICTURE_PATH, }, }, } def __unicode__(self): return self.name @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def filter_objects(cls, fuzzy=True, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) if fuzzy: for key in kwargs.keys(): if key in cls.AdminMeta.fuzzy_fields: kwargs['%s__contains' % key] = kwargs.pop(key) try: return cls.objects.filter(**kwargs) except Exception as e: return e @classmethod def filter_objects_by_dimension_id(cls, dimension_id): attribute_instances = Attribute.filter_objects( dimension_id=dimension_id) attribute_ids = [ins.id for ins in attribute_instances] tag_config_instances = TagConfigure.filter_objects( attribute_id__in=attribute_ids) tag_ids = [ins.tag_id for ins in tag_config_instances] tag_instances = cls.filter_objects(id__in=list(set(tag_ids))) return tag_instances
class Case(models.Model): """ 案例 """ title = models.CharField('标题', max_length=128, db_index=True) subtitle = models.CharField('副标题', max_length=128, null=True, blank=True) description = models.TextField('描述/介绍', null=True, blank=True) content = models.TextField('正文') picture = models.ImageField('封面图', max_length=200, upload_to=MEDIA_PICTURE_PATH, default=os.path.join(MEDIA_PICTURE_PATH, 'noImage.png')) # 标签:数据格式为JSON字符串,如:['综艺', '植入', '片头'] tags = models.CharField('标签', max_length=256) # 浏览数 read_count = models.IntegerField('浏览数', default=0) # 点赞数量 like = models.IntegerField('点赞数量', default=0) # 收藏数量 collection_count = models.IntegerField('收藏数量', default=0) # 评论数量 comment_count = models.IntegerField('评论数量', default=0) # 运营标记:0:无标记 1:重磅发布 mark = models.IntegerField('运营标记', default=0) # 栏目 0:无标记 1: 最新发布 2:电影大事件 3:娱乐营销观察 4:影片资讯 column = models.IntegerField('栏目', default=0) # 数据状态:1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_case' ordering = ['-updated'] app_label = 'Web_App.web_media.models.Case' class AdminMeta: fuzzy_fields = ['title'] origin_picture = 'picture' perfect_picture = { 'max_disk_size': 1 * 1024 * 1024, 'goal_picture': { 'picture': { 'size': None, 'save_path': MEDIA_PICTURE_PATH, }, }, } def __unicode__(self): return self.title @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def get_detail(cls, **kwargs): instance = cls.get_object(**kwargs) if isinstance(instance, Exception): return instance return instance.perfect_detail @property def perfect_detail(self): detail = model_to_dict(self) tag_ids = json.loads(detail['tags']) detail['tags'] = self.get_perfect_tags(tag_ids) return detail def get_perfect_tags(self, tag_ids): tag_details = [] for tag_id in tag_ids: tag = ResourceTags.get_object(pk=tag_id) if isinstance(tag, Exception): continue tag_details.append(tag.name) return tag_details @classmethod def filter_objects(cls, fuzzy=True, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) if fuzzy: for key in kwargs: if key in cls.AdminMeta.fuzzy_fields: kwargs['%s__contains' % key] = kwargs.pop(key) try: return cls.objects.filter(**kwargs) except Exception as e: return e @classmethod def filter_details(cls, **kwargs): instances = cls.filter_objects(**kwargs) if isinstance(instances, Exception): return instances details = [] for ins in instances: details.append(ins.perfect_detail) return details
class ThemeType(models.Model): """ 题材类别 """ name = models.CharField('题材类别名称', max_length=64) media_type_id = models.IntegerField('所属资源类型ID', db_index=True) sort_order = models.IntegerField('排序顺序', default=0) # 数据状态 1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_theme_type' unique_together = ['name', 'media_type_id', 'status'] ordering = ['media_type_id', '-updated'] app_label = 'Web_App.web_media.models.ThemeType' class AdminMeta: fuzzy_fields = ['name'] def __unicode__(self): return '%s' % self.name @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def get_detail(cls, **kwargs): instance = cls.get_object(**kwargs) if isinstance(instance, Exception): return instance media_type_ins = MediaType.get_object(pk=instance.media_type_id) if isinstance(media_type_ins, Exception): return media_type_ins detail = model_to_dict(instance) detail['media_type_name'] = media_type_ins.name return detail @classmethod def filter_objects(cls, fuzzy=True, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) if fuzzy: for key in kwargs: if key in cls.AdminMeta.fuzzy_fields: kwargs['%s__contains' % key] = kwargs.pop(key) try: instances = cls.objects.filter(**kwargs) except Exception as e: return e # 排序 sort_instances = sorted(instances, key=lambda x: x.sort_order) key = -1 for index, item in enumerate(sort_instances): if item.sort_order != 0: break key = index if key == -1: return sort_instances else: new_sort_list = sort_instances[key + 1:] new_sort_list.extend(sort_instances[:key + 1]) return new_sort_list @classmethod def filter_details(cls, **kwargs): instances = cls.filter_objects(**kwargs) if isinstance(instances, Exception): return instances details = [] media_type_ins_dict = {} for ins in instances: media_type_ins = media_type_ins_dict.get(ins.media_type_id) if not media_type_ins: media_type_ins = MediaType.get_object(pk=ins.media_type_id) if isinstance(media_type_ins, Exception): continue media_type_ins_dict[ins.media_type_id] = media_type_ins ins_detail = model_to_dict(ins) ins_detail['media_type_name'] = media_type_ins.name details.append(ins_detail) return details
class TagConfigure(models.Model): """ 标签配置 """ tag_id = models.IntegerField('标签ID', db_index=True) attribute_id = models.IntegerField('匹配属性ID') match_value = models.FloatField('与属性匹配值', default=1.0) # 数据状态:1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_tag_configure' unique_together = ['tag_id', 'attribute_id', 'status'] ordering = ['tag_id', 'attribute_id', '-updated'] app_label = 'Web_App.web_dimensions.models.TagConfigure' def __unicode__(self): return '%s:%s' % (self.tag_id, self.attribute_id) @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def get_detail(cls, **kwargs): instance = cls.get_object(**kwargs) if isinstance(instance, Exception): return instance tag_ins = Tag.get_object(pk=instance.tag_id) if isinstance(tag_ins, Exception): return tag_ins attr_ins = Attribute.get_object(pk=instance.attribute_id) if isinstance(attr_ins, Exception): return attr_ins detail = model_to_dict(instance) detail['tag_name'] = tag_ins.name detail['attribute_name'] = attr_ins.name return detail @classmethod def filter_objects(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.filter(**kwargs) except Exception as e: return e @classmethod def filter_details(cls, **kwargs): instances = cls.filter_objects(**kwargs) if isinstance(instances, Exception): return instances tag_ins_dict = {} attr_ins_dict = {} details = [] for ins in instances: tag_ins = tag_ins_dict.get(ins.tag_id) if not tag_ins: tag_ins = Tag.get_object(pk=ins.tag_id) if isinstance(tag_ins, Exception): continue attr_ins = attr_ins_dict.get(ins.attribute_id) if not attr_ins: attr_ins = Attribute.get_object(pk=ins.attribute_id) if isinstance(attr_ins, Exception): continue item_detail = model_to_dict(ins) item_detail['tag_name'] = tag_ins.name item_detail['attribute_name'] = attr_ins.name details.append(item_detail) return details
class MediaConfigure(models.Model): """ 媒体资源属性配置 """ media_id = models.IntegerField('媒体资源ID') dimension_id = models.IntegerField('所属维度ID', db_index=True) attribute_id = models.IntegerField('所属属性ID') # 数据状态:1:正常 非1:已删除 status = models.IntegerField('状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_media_configure' index_together = ['dimension_id', 'attribute_id'] unique_together = ['media_id', 'attribute_id', 'status'] app_label = 'Web_App.web_media.models.MediaConfigure' def __unicode__(self): return '%s:%s:%s' % (self.media_id, self.dimension_id, self.attribute_id) @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def get_detail(cls, **kwargs): instance = cls.get_object(**kwargs) if isinstance(instance, Exception): return instance return instance.perfect_detail @property def perfect_detail(self): media_instance_dict = getattr(self, '_media_instance_dict', {}) dime_instance_dict = getattr(self, '_dime_instance_dict', {}) attr_instance_dict = getattr(self, '_attr_instance_dict', {}) media_instance = media_instance_dict.get(self.media_id) if not media_instance: media_instance = Media.get_object(pk=self.media_id) media_instance_dict[self.media_id] = media_instance setattr(self, '_media_instance_dict', media_instance_dict) dime_instance = dime_instance_dict.get(self.dimension_id) if not dime_instance: dime_instance = Dimension.get_object(pk=self.dimension_id) dime_instance_dict[self.dimension_id] = dime_instance setattr(self, '_dime_instance_dict', dime_instance_dict) attr_instance = attr_instance_dict.get(self.attribute_id) if not attr_instance: attr_instance = Attribute.get_object(pk=self.attribute_id) attr_instance_dict[self.attribute_id] = attr_instance setattr(self, '_attr_instance_dict', attr_instance_dict) if isinstance(media_instance, Exception): return media_instance detail = { 'media_name': getattr(media_instance, 'title', None), 'dimension_id': self.dimension_id, 'dimension_name': getattr(dime_instance, 'name', None), 'attribute_id': self.attribute_id, 'attribute_name': getattr(attr_instance, 'name', None) } detail.update(**model_to_dict(self)) return detail @classmethod def filter_objects(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.filter(**kwargs) except Exception as e: return e @classmethod def filter_details(cls, **kwargs): instances = cls.filter_objects(**kwargs) if isinstance(instances, Exception): return instances details = [] for ins in instances: item_detail = ins.perfect_detail if isinstance(item_detail, Exception): continue details.append(item_detail) return details
class Report(models.Model): """ 报告 """ title = models.CharField('报告标题', max_length=32) subtitle = models.CharField('报告副标题', max_length=128, null=True, blank=True) description = models.TextField('报告描述', null=True, blank=True) media_id = models.IntegerField('所属资源ID', db_index=True) # 标签:数据格式为JSON字符串,如:['行业月报', '综艺'] tags = models.CharField('标签', max_length=256) report_file = models.FileField('报告文件', max_length=200, upload_to=REPORT_PICTURE_PATH, default=os.path.join( REPORT_PICTURE_PATH, 'noImage.png')) # 数据状态:1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField(default=now) updated = models.DateTimeField(auto_now=True) objects = BaseManager() class Meta: db_table = 'by_report' unique_together = ('media_id', 'status') ordering = ['-updated'] app_label = 'Web_App.web_reports.models.Report' class AdminMeta: json_fields = ['tags'] fuzzy_fields = ['title'] def __unicode__(self): return self.title @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def get_detail(cls, **kwargs): instance = cls.get_object(**kwargs) if isinstance(instance, Exception): return instance return instance.perfect_detail @property def perfect_detail(self): media_ins = Media.get_object(pk=self.media_id) if isinstance(media_ins, Exception): return media_ins detail = model_to_dict(self) detail['media_name'] = media_ins.title if self.AdminMeta.json_fields: for json_key in self.AdminMeta.json_fields: detail[json_key] = json.loads(detail[json_key]) return detail @classmethod def filter_objects(cls, fuzzy=True, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) if fuzzy: if cls.AdminMeta.fuzzy_fields: for key in kwargs: if key in cls.AdminMeta.fuzzy_fields: kwargs['%s__contains' % key] = kwargs.pop(key) try: return cls.objects.filter(**kwargs) except Exception as e: return e @classmethod def filter_details(cls, **kwargs): instances = cls.filter_objects(**kwargs) if isinstance(instances, Exception): return instances details = [] for ins in instances: perfect_detail = ins.perfect_detail if isinstance(perfect_detail, Exception): continue details.append(perfect_detail) return details
class Attribute(models.Model): """ 属性 """ name = models.CharField('属性名称', max_length=64, db_index=True) description = models.CharField('描述', max_length=256, null=True, blank=True) dimension_id = models.IntegerField('所属维度ID') # picture = models.ImageField('属性矢量图片', max_length=200, # upload_to=IMAGE_PICTURE_PATH, # default=os.path.join(IMAGE_PICTURE_PATH, 'noImage.png')) # 资源状态:1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_attribute' unique_together = ['name', 'status'] ordering = ['dimension_id', '-updated'] app_label = 'Web_App.web_dimensions.models.Attribute' class AdminMeta: fuzzy_fields = ['name'] def __unicode__(self): return self.name @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def get_detail(cls, **kwargs): instance = cls.get_object(**kwargs) if isinstance(instance, Exception): return instance dime_instance = Dimension.get_object(pk=instance.dimension_id) if isinstance(dime_instance, Exception): return dime_instance detail = model_to_dict(instance) detail['dimension_name'] = dime_instance.name return detail @classmethod def filter_objects(cls, fuzzy=True, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) if fuzzy: for key in kwargs.keys(): if key in cls.AdminMeta.fuzzy_fields: kwargs['%s__contains' % key] = kwargs.pop(key) try: return cls.objects.filter(**kwargs) except Exception as e: return e @classmethod def filter_details(cls, **kwargs): instances = cls.filter_objects(**kwargs) if isinstance(instances, Exception): return instances details = [] dime_dict = {} for ins in instances: dime_ins = dime_dict.get(ins.dimension_id) if not dime_ins: dime_ins = Dimension.get_object(pk=ins.dimension_id) if isinstance(dime_ins, Exception): continue dime_dict[ins.dimension_id] = dime_ins item_detail = model_to_dict(ins) item_detail['dimension_name'] = dime_ins.name details.append(item_detail) return details
class Comment(models.Model): """ 用户点评 """ user_id = models.IntegerField('用户ID', db_index=True) # 点评资源类型: 1:资源 2:案例 3:资讯 source_type = models.IntegerField('点评资源类型', default=1) # 资源数据ID source_id = models.IntegerField('点评资源ID') content = models.CharField('点评内容', max_length=512) # 是否被管理员推荐该评论: 0: 否 1:是 is_recommend = models.IntegerField('是否被管理员推荐', default=0) like = models.IntegerField('喜欢数量', default=0) dislike = models.IntegerField('不喜欢数量', default=0) # 数据状态:1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_comment' unique_together = ['user_id', 'source_type', 'source_id'] index_together = ['source_type', 'source_id'] ordering = ['-created'] app_label = 'Web_App.web_comment.models.Comment' def __unicode__(self): return '%s:%s:%s' % (self.user_id, self.source_type, self.source_id) @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def get_detail(cls, **kwargs): instance = cls.get_object(**kwargs) if isinstance(instance, Exception): return instance return instance.perfect_detail @property def perfect_detail(self): is_recommend = self.is_recommend reply_message = '' created_for_admin = None if is_recommend: reply = ReplyComment.get_object(comment_id=self.pk) reply_message = reply.message created_for_admin = reply.updated source_ins = self.get_source_object(source_type=self.source_type, source_id=self.source_id) if isinstance(source_ins, Exception): source_title = '' else: source_title = source_ins.title item_dict = model_to_dict(self) item_dict['is_recommend'] = is_recommend item_dict['reply_message'] = reply_message item_dict['source_title'] = source_title item_dict['created_for_user'] = item_dict['created'] item_dict['created_for_admin'] = created_for_admin return item_dict @classmethod def filter_objects(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.filter(**kwargs) except Exception as e: return e @classmethod def filter_details(cls, **kwargs): instances = cls.filter_objects(**kwargs) details = [] for ins in instances: details.append(ins.perfect_detail) return details @classmethod def get_source_object(cls, source_type, source_id): source_class = SOURCE_TYPE_DB.get(source_type) if not source_class: return Exception('Params is incorrect') return source_class.get_object(pk=source_id)
class Media(models.Model): """ 媒体资源 """ title = models.CharField('资源标题', max_length=128, db_index=True) subtitle = models.CharField('资源副标题', max_length=128, null=True, blank=True) description = models.TextField('资源描述/介绍', null=True, blank=True) # 资源类型:10:电影 20:电视剧 30:综艺节目 media_type = models.IntegerField('资源类型', default=10) # 题材类别 1:爱情 2:战争 3:校园 4:真人秀 theme_type = models.IntegerField('题材类别', default=1) # 项目进度 1:筹备期 2:策划期 3:xxx progress = models.IntegerField('项目进度', default=1) # 模板类型 1:模板1 2:模板2 template_type = models.IntegerField('展示页面模板类型', default=1) # # 资源概要展示类型:1:电影、剧集 2:综艺、活动 # outline_type = models.IntegerField('资源概要展示类型', default=1) # 标签:数据格式为JSON字符串,如:['综艺', '植入', '片头'] tags = models.CharField('资源标签', max_length=256) # 资源热度 temperature = models.FloatField('热度') # 票房预测 box_office_forecast = models.FloatField('票房预测') # 口碑预测 public_praise_forecast = models.FloatField('口碑预测') # ROI 投资回报比 例如:1:5 (1比5) roi = models.CharField('投入回报比', max_length=10) # 资源属性:数据格式为JSON字符串,如:[1, 3, 5] (数字为属性ID) # attributes = models.CharField('资源属性', max_length=256, ) # # 导演:数据格式为JSON字符串,如:['斯皮尔伯格', '冯小刚'] # director = models.CharField('导演', max_length=256) # # 主演:数据格式为JSON字符串,如:['汤姆克鲁斯', '威尔史密斯', '皮尔斯布鲁斯南'] # stars = models.CharField('主演', max_length=256) # # 演员:数据格式为JSON字符串,如:['王晓霞', '詹姆斯', '韦德'] # actors = models.CharField('演员', max_length=256) # # 监制:数据格式为JSON字符串,如:['欧文'] # producer = models.CharField('监制', max_length=256) # # 出品公司:数据格式为JSON字符串,如:['华文映像', '福星传媒'] # production_company = models.CharField('出品公司', max_length=256) # # # 预计开机/录制时间 # recorded_time = models.DateTimeField('开机时间') # 资源概述 数据格式为字典形式的JSON字符串,如:{"导演": ["冯小刚", "吴宇森"], # "主演": ["成龙", "李连杰"], # "出演": ["巩俐", "章子怡"], ......} media_outline = models.TextField('资源概述', null=True, blank=True) # 预计上映/播出时间 air_time = models.DateTimeField('播出时间') # # 预计播出平台:数据格式为JSON字符串,如:['一线卫视', '视频网络渠道'] # play_platform = models.CharField('播出平台', max_length=256) # 浏览数 read_count = models.IntegerField('浏览数', default=0) # 点赞数量 like = models.IntegerField('点赞数量', default=0) # 收藏数量 collection_count = models.IntegerField('收藏数量', default=0) # 评论数量 comment_count = models.IntegerField('评论数量', default=0) # 运营标记 0: 未设定 1:热门 mark = models.IntegerField('运营标记', default=0) # 电影表现大数据分析 数据格式为字典形式的JSON字符串,如:{"导演号召力": 3.5, # "男主角号召力": 4.0, # "女主角号召力": 4.2, # "类型关注度": 3.8, # "片方指数": 3.7} film_performance = models.CharField('电影表现大数据分析', max_length=512, null=True, blank=True) picture = models.ImageField('媒体资源原始图片', max_length=200, upload_to=MEDIA_PICTURE_PATH, default=os.path.join(MEDIA_PICTURE_PATH, 'noImage.png')) picture_profile = models.ImageField('简介图片', max_length=200, upload_to=MEDIA_PICTURE_PATH, default=os.path.join( MEDIA_PICTURE_PATH, 'noImage.png')) picture_detail = models.ImageField('详情图片', max_length=200, upload_to=MEDIA_PICTURE_PATH, default=os.path.join( MEDIA_PICTURE_PATH, 'noImage.png')) # picture_profile = models.CharField('简介图片', # max_length=200, # default=os.path.join(MEDIA_PICTURE_PATH, 'noImage.png')) # picture_detail = models.CharField('详情图片', # max_length=200, # default=os.path.join(MEDIA_PICTURE_PATH, 'noImage.png')) # 资源状态:1:正常 非1:已删除 status = models.IntegerField('资源状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_media' ordering = ['-updated'] unique_together = ['title', 'subtitle', 'status'] app_label = 'Web_App.web_media.models.Media' class AdminMeta: fuzzy_fields = ['title'] json_fields = ['tags', 'media_outline', 'film_performance'] origin_picture = 'picture' perfect_picture = { 'max_disk_size': 1 * 1024 * 1024, 'goal_picture': { 'picture_profile': { 'size': (320, 200), 'save_path': MEDIA_PICTURE_PATH, }, 'picture_detail': { 'size': None, 'save_path': MEDIA_PICTURE_PATH, }, 'picture': { 'size': None, 'save_path': MEDIA_PICTURE_PATH, }, }, } def __unicode__(self): return self.title @classmethod def get_object(cls, fuzzy=True, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) if fuzzy: for key in kwargs: if key in cls.AdminMeta.fuzzy_fields: kwargs['%s__contains' % key] = kwargs.pop(key) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def get_detail(cls, **kwargs): instance = cls.get_object(**kwargs) if isinstance(instance, Exception): return instance return instance.perfect_detail @property def perfect_detail(self): detail = model_to_dict(self) for key in detail.keys(): if key in self.AdminMeta.json_fields: if detail[key]: if key == 'tags': tag_ids = json.loads(detail[key]) detail[key] = self.get_perfect_tags(tag_ids) else: detail[key] = json.loads(detail[key]) media_type_dict = getattr(self, '_media_type_dict', {}) theme_type_dict = getattr(self, '_theme_type_dict', {}) progress_dict = getattr(self, '_progress_dict', {}) media_type_ins = media_type_dict.get(self.media_type) if not media_type_ins: media_type_ins = MediaType.get_object(pk=self.media_type) media_type_dict[self.media_type] = media_type_ins setattr(self, '_media_type_dict', media_type_dict) theme_type_ins = theme_type_dict.get(self.theme_type) if not theme_type_ins: theme_type_ins = ThemeType.get_object(pk=self.theme_type) theme_type_dict[self.theme_type] = theme_type_ins setattr(self, '_theme_type_dict', theme_type_dict) progress_ins = progress_dict.get(self.progress) if not progress_ins: progress_ins = ProjectProgress.get_object(pk=self.progress) progress_dict[self.progress] = progress_ins setattr(self, '_progress_dict', progress_dict) detail['media_type_name'] = getattr(media_type_ins, 'name', None) detail['theme_type_name'] = getattr(theme_type_ins, 'name', None) detail['progress_name'] = getattr(progress_ins, 'name', None) return detail def get_perfect_tags(self, tag_ids): tag_details = [] for tag_id in tag_ids: tag = ResourceTags.get_object(pk=tag_id) if isinstance(tag, Exception): continue tag_details.append(tag.name) return tag_details @classmethod def filter_objects(cls, fuzzy=True, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) if fuzzy: for key in kwargs: if key in cls.AdminMeta.fuzzy_fields: kwargs['%s__contains' % key] = kwargs.pop(key) try: return cls.objects.filter(**kwargs) except Exception as e: return e @classmethod def filter_details(cls, **kwargs): instances = cls.filter_objects(**kwargs) if isinstance(instances, Exception): return instances details = [] for ins in instances: details.append(ins.perfect_detail) return details
class Collect(models.Model): """ 用户收藏 """ user_id = models.IntegerField('用户ID', db_index=True) # 媒体资源类型: 1:资源 2:案例 3:资讯 source_type = models.IntegerField('收藏类型', default=1) # 资源ID source_id = models.IntegerField('资源ID') # 收藏品状态:1:有效 非1:已取消收藏 status = models.IntegerField('收藏品状态', default=1) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('更新时间', auto_now=True) objects = BaseManager() class Meta: db_table = 'by_collect' unique_together = ('user_id', 'source_type', 'source_id', 'status') ordering = ['-updated'] def __unicode__(self): return '%s:%s:%s' % (self.user_id, self.source_type, self.source_id) @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def filter_objects(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.filter(**kwargs) except Exception as e: return e @classmethod def get_source_object(cls, source_type, source_id): source_class = SOURCE_TYPE_DB.get(source_type) if not source_class: return Exception('Params is incorrect') return source_class.get_detail(pk=source_id) @classmethod def filter_details(cls, **kwargs): instances = cls.filter_objects(**kwargs) details = [] for ins in instances: source_detail = cls.get_source_object(source_type=ins.source_type, source_id=ins.source_id) if isinstance(source_detail, Exception): continue item_dict = model_to_dict(ins) item_dict['source_title'] = source_detail['title'] item_dict['source_description'] = source_detail['description'] item_dict['updated'] = source_detail['created'] item_dict['tags'] = source_detail['tags'] item_dict['picture'] = source_detail['picture'] details.append(item_dict) return details @classmethod def get_collection_count(cls, source_type, source_id): """ 获取收藏数 """ kwargs = {'source_type': source_type, 'source_id': source_id} instances = cls.filter_objects(**kwargs) if isinstance(instances, Exception): return 0 return len(instances)
class Dishes(models.Model): """ 菜品信息表 """ title = models.CharField('菜品名称', null=False, max_length=40) subtitle = models.CharField('菜品副标题', max_length=100, default='') description = models.TextField('菜品描述', default='') # 默认:10,小份:11,中份:12,大份:13,自定义:20 size = models.IntegerField('菜品规格', default=10) size_detail = models.CharField('菜品规格详情', max_length=30, null=True, blank=True) price = models.CharField('价格', max_length=16, null=False, blank=False) image = models.ImageField( '菜品图片(封面)', upload_to=DISHES_PICTURE_DIR, default=os.path.join(DISHES_PICTURE_DIR, 'noImage.png'), ) image_detail = models.ImageField( '菜品图片(详情)', upload_to=DISHES_PICTURE_DIR, default=os.path.join(DISHES_PICTURE_DIR, 'noImage.png'), ) user_id = models.IntegerField('创建者ID', db_index=True) food_court_id = models.IntegerField('商城ID', db_index=True) created = models.DateTimeField('创建时间', default=now) updated = models.DateTimeField('最后修改时间', auto_now=True) status = models.IntegerField('数据状态', default=1) # 1 有效 2 已删除 3 其他(比如暂时不用) is_recommend = models.BooleanField('是否推荐该菜品', default=False) # 0: 不推荐 1:推荐 # 运营标记: 0:无标记 10:新品 20:特惠 30:招牌 40: 新商户专区 50: 晚市特惠 mark = models.IntegerField('运营标记', default=0) # 优惠金额 discount = models.CharField('优惠金额', max_length=16, default='0') # 优惠时间段-开始 (用来标记菜品在某个时段是否有优惠),以24小时制数字为标准, 如:8:00(代表早晨8点) discount_time_slot_start = models.CharField('优惠时间段-开始', max_length=16, null=True) # 优惠时间段-结束 (用来标记菜品在某个时段是否有优惠),以24小时制数字为标准, 如:19:30(代表晚上7点30分) discount_time_slot_end = models.CharField('优惠时间段-结束', max_length=16, null=True) # 菜品标记和排序顺序 tag = models.CharField('标记', max_length=64, default='', null=True, blank=True) sort_orders = models.IntegerField('排序标记', default=None, null=True) # 菜品类别: 0: 默认 classify = models.IntegerField('菜品类别', default=0) extend = models.TextField('扩展信息', default='', null=True, blank=True) objects = BaseManager() class Meta: db_table = 'ys_dishes' unique_together = ('user_id', 'title', 'size', 'size_detail', 'status') ordering = ['-updated'] def __unicode__(self): return self.title @property def perfect_data(self): detail = model_to_dict(self) dishes_classify = DishesClassify.get_object(pk=self.classify) if isinstance(dishes_classify, Exception): detail['classify_name'] = '' else: detail['classify_name'] = dishes_classify.name return detail @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def get_detail(cls, **kwargs): instance = cls.get_object(**kwargs) if isinstance(instance, Exception): return instance return instance.perfect_data @classmethod def get_object_list(cls, request, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) kwargs.update(**{'user_id': request.user.id}) try: return cls.objects.filter(**kwargs) except Exception as e: return e @classmethod def filter_details(cls, request, **kwargs): instances = cls.get_object_list(request, **kwargs) if isinstance(instances, Exception): return instances details = [] for ins in instances: details.append(ins.perfect_data) return details
class Report(models.Model): """ 报告 """ title = models.CharField('报告标题', max_length=32) subtitle = models.CharField('报告副标题', max_length=128, null=True, blank=True) description = models.TextField('报告描述', null=True, blank=True) media_id = models.IntegerField('所属资源ID', db_index=True) # 标签:数据格式为JSON字符串,如:['行业月报', '综艺'] tags = models.CharField('标签', max_length=256) report_file = models.FileField('报告文件', max_length=200, upload_to=REPORT_PICTURE_PATH, default=os.path.join( REPORT_PICTURE_PATH, 'noImage.png')) # 数据状态:1:正常 非1:已删除 status = models.IntegerField('数据状态', default=1) created = models.DateTimeField(default=now) updated = models.DateTimeField(auto_now=True) objects = BaseManager() class Meta: db_table = 'by_report' unique_together = ('media_id', 'status') ordering = ['-updated'] class AdminMeta: json_fields = ['tags'] def __unicode__(self): return self.title @classmethod def get_object(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.get(**kwargs) except Exception as e: return e @classmethod def get_detail(cls, **kwargs): instance = cls.get_object(**kwargs) if isinstance(instance, Exception): return instance return instance.perfect_detail @classmethod def filter_objects(cls, **kwargs): kwargs = get_perfect_filter_params(cls, **kwargs) try: return cls.objects.filter(**kwargs) except Exception as e: return e @classmethod def filter_details(cls, **kwargs): instances = cls.filter_objects(**kwargs) if isinstance(instances, Exception): return instances details = [] for ins in instances: details.append(ins.perfect_detail) return details @property def perfect_detail(self): detail = model_to_dict(self) if hasattr(self, 'AdminMeta'): if hasattr(self.AdminMeta, 'json_fields'): for field in self.AdminMeta.json_fields: if field == 'tags': tag_ids = json.loads(detail[field]) detail[field] = self.get_perfect_tags(tag_ids) else: detail[field] = json.loads(detail[field]) media = MediaCache().get_media_by_id(self.media_id) if isinstance(media, Exception): return media detail['media_picture'] = media.picture return detail def get_perfect_tags(self, tag_ids): tag_names = [] for tag_id in tag_ids: tag = ResourceTags.get_object(pk=tag_id) if isinstance(tag, Exception): continue tag_names.append(tag.name) return tag_names