def perform_create(self, serializer): object_class = get_model_class_by_name(self.kwargs['object_type']) content_object = object_class.objects.get( id=self.request.data['object_id']) kwargs = {'user': self.request.user, 'content_object': content_object} serializer.save(**kwargs)
def validate(self, attrs): pk = self.context['view'].kwargs.get('pk') link = attrs.get('link') if attrs.get('link_type') == BANNER_LINK_TYPE_IN_APP_NATIVE: params = link.split(':') if len(params) != 2: raise ValidationError( {'message': ['链接格式错误: model_name:obj_id']}) model_name, obj_id = params if model_name not in BANNER_LINK_MODEL_TYPE: raise ValidationError({'message': ['未知的链接对象']}) model_class = get_model_class_by_name(model_name) if not model_class.objects.filter(pk=obj_id).exists(): raise ValidationError({ 'message': [f'{model_class._meta.verbose_name}:{obj_id} 不存在'] }) else: url_validator(link) if models.Banner.objects.filter( order_number=attrs.get('order_number')).exclude( id=pk).exists(): raise ValidationError({'message': ['序号已存在']}) return attrs
def content_object(self): object_class = get_model_class_by_name(self.kwargs['object_type']) query_set = object_class.objects.all() if hasattr(object_class, 'is_show'): query_set = query_set.filter(is_show=True) return get_object_or_404(query_set, pk=self.kwargs['object_id'])
def share_view(request, object_type, object_id): model_class = get_model_class_by_name(object_type) obj = get_object_or_404(model_class, pk=object_id) sub_title = picture = '' plural_prefix = 's' if object_type != 'game_news' else '' url = f'{h5_base_url}{object_type}{plural_prefix}/{object_id}' weibo = '' # 微博分享专用字段,解决因产品需求导致分享文案不同问题 if isinstance(obj, Message): user = obj.user title = obj.content if hasattr(user, 'user_profile'): profile = user.user_profile if not title: title = f'分享{profile.nick_name}动态' sub_title = f'来自拳城出击的{profile.nick_name}' picture = get_share_img_url(profile.avatar) title = _truncate_text(title, 14) sub_title = _truncate_text(sub_title, 20) forward_message(object_id) elif isinstance(obj, HotVideo): user_id = request.GET.get('user_id') if user_id: user = get_object_or_404(User, pk=user_id) else: user = obj.users.first() title = obj.name sub_title = f'来自拳城出击的热门视频' picture = get_share_img_url(obj.cover) or get_share_img_url( obj.try_url, is_video=True) url = f'{h5_base_url}hot_videos/{user.id}/{object_id}' forward_hotvideo(object_id) elif isinstance(obj, GameNews): title = obj.title sub_title = obj.sub_title picture = get_share_img_url(obj.picture) user = obj.operator url = f'{h5_base_url}game_news/{object_id}/0' # 0 不在app内打开 elif isinstance(obj, Player): title = f'分享{obj.user.user_profile.nick_name}的个人战绩' user = obj.user follower = follower_count(user.id) following = following_count(user.id) sub_title = f'已关注: {following},粉丝数: {follower}' picture = get_share_img_url(obj.avatar) url = f'{h5_base_url}players/{obj.id}' weibo = url + f' 分享来自拳城出击的{user.user_profile.nick_name}的精彩瞬间' else: title = '我在拳城出击发现了一个很棒的拳击教练,一起去约课吧~' sub_title = '上拳城,玩转拳击' picture = get_share_img_url( UserProfile.objects.filter( user_id=obj.id).only('avatar').first().avatar) user = obj data = { 'title': title, 'sub_title': sub_title, 'picture': picture, 'url': url, 'weibo': weibo } if user: incr_number_of_share(user.id) return Response(data)
def get_content_object(self, obj): object_class = get_model_class_by_name(self.context['object_type']) return object_class.objects.get(pk=obj['id'])