class EventSerializer(serializers.ModelSerializer): owner = UserSimpleSerializer() participant = UserSimpleSerializer(many=True) class Meta: model = Event fields = '__all__'
class BlogDetailSerializer(serializers.ModelSerializer): user = UserSimpleSerializer() category = CategoryCreateSerializer() class Meta: model = Blog fields = '__all__'
class OrganizationSerializer(serializers.ModelSerializer): users = UserSimpleSerializer(many=True) class Meta: model = Organization fields = ('id', 'name', 'description', 'created_at', 'updated_at', 'users') read_only_fields = ('id', )
class BlogSimpleSerializer(serializers.ModelSerializer): """ blog 概要信息 适用于展示blog列表的时候 """ user = UserSimpleSerializer() category = CategoryCreateSerializer() class Meta: model = Blog fields = ('user', 'category', 'id', 'add_time', 'title', 'num', 'excerpt', 'sumary_img')
class ProjectMemberSerializer(serializers.ModelSerializer): role = serializers.CharField() user = UserSimpleSerializer() class Meta: model = ProjectMember fields = ( 'role', 'user', )
class ChamberSerializer(serializers.ModelSerializer): """ 会议室序列化--查看或删除 """ host = UserSimpleSerializer(many=False) equipment = EquipmentSerializer(many=True) add_time = serializers.DateTimeField(read_only=True, format="%Y-%m-%d %H:%M") class Meta: model = Chamber fields = "__all__"
class AllMeetingSimpleSerializer(serializers.ModelSerializer): """ 所有会议的简要信息 """ start = serializers.DateTimeField(read_only=True, format='%Y-%m-%d') end = serializers.DateTimeField(read_only=True, format='%Y-%m-%d') add_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M') remark = serializers.CharField(write_only=True) chamber = serializers.CharField(write_only=True) status = serializers.SerializerMethodField() color = serializers.SerializerMethodField() speaker = UserSimpleSerializer(many=False) pubuser = UserSimpleSerializer(many=False) joins = UserSimpleSerializer(many=True) class Meta: model = ApplyMeeting fields = "__all__" def get_color(self, obj): start = obj.start end = obj.end now = datetime.datetime.now() if now < start: return "#66CCFF" elif start <= now <= end: return "#67C23A" return "#909399" def get_status(self, obj): start = obj.start end = obj.end now = datetime.datetime.now() if now < start: return "未开始" elif start <= now <= end: return "进行中" return "已结束"
def remove_ability(self, request, *args, **kwargs): me = request.user my_profile = me.profile ability = get_object_or_404(my_profile.abilities, id=kwargs.get('ability_id')) new_count = ability.count - 1 ability.count = new_count ability.save() my_profile.abilities.remove(ability) serializer = UserSimpleSerializer(me) return Response(serializer.data, status=status.HTTP_200_OK)
def remove_keyword(self, request, *args, **kwargs): me = request.user my_profile = me.profile keyword = get_object_or_404(my_profile.keywords, id=kwargs.get('keyword_id')) new_count = keyword.count - 1 keyword.count = new_count keyword.save() my_profile.keywords.remove(keyword) serializer = UserSimpleSerializer(me) return Response(serializer.data, status=status.HTTP_200_OK)
def get_pubuser(self, obj): pubuser = obj.pubuser us = UserSimpleSerializer(pubuser, many=False, context={ 'request': self.context['request'] }).data is_signed = Signed.objects.filter(meeting=obj.id, user=pubuser).count() if is_signed: us["signed"] = "已签到" else: us["signed"] = "未签到" return us
def add_ability(self, request, *args, **kwargs): me = request.user my_profile = me.profile ability_string = request.data.get('ability') try: my_profile.abilities.get(name=ability_string) return Response({}, status=status.HTTP_409_CONFLICT) except ObjectDoesNotExist: ability, created = Ability.objects.get_or_create( name=ability_string) new_count = ability.count + 1 ability.count = new_count ability.save() my_profile.abilities.add(ability) serializer = UserSimpleSerializer(me) return Response(serializer.data, status=status.HTTP_200_OK)
def add_keyword(self, request, *args, **kwargs): me = request.user my_profile = me.profile keyword_string = request.data.get('keyword') try: my_profile.keywords.get(name=keyword_string) return Response({}, status=status.HTTP_409_CONFLICT) except ObjectDoesNotExist: keyword, created = Keyword.objects.get_or_create( name=keyword_string) new_count = keyword.count + 1 keyword.count = new_count keyword.save() my_profile.keywords.add(keyword) serializer = UserSimpleSerializer(me) return Response(serializer.data, status=status.HTTP_200_OK)
def get_joins(self, obj): joins_list = [] pre_joins = obj.joins.all() for v in pre_joins: join = UserProfile.objects.get(id=v.id) us = UserSimpleSerializer(join, many=False, context={ 'request': self.context['request'] }).data is_signed = Signed.objects.filter(meeting=obj.id, user=v).count() if is_signed: us["signed"] = "已签到" else: us["signed"] = "未签到" department = join.department.all().first().name us["department"] = department joins_list.append(us) json_data = json.dumps(joins_list, ensure_ascii=False) # return eval(json_data) # 不能用eval不属于python存在安全问题,且会可能会报错NameError: name 'null' is not defined return json.loads(json_data)
def retrieve(self, request, *args, **kwargs): serializer = UserSimpleSerializer(request.user) return Response(serializer.data, status=status.HTTP_200_OK)
def retrieve(self, request, *args, **kwargs): user = get_object_or_404(User, pk=kwargs.get('user_id')) serializer = UserSimpleSerializer(user) return Response(serializer.data, status=status.HTTP_200_OK)
class NotificationSerializer(serializers.ModelSerializer): """ 目前通知共有 3 种: 1. 帖子被评论,帖子作者收到通知 2. 帖子的回复被其他人回复,即回复别人的回复,被回复者收到通知 3. 回复被点赞 4. 管理员收到所有关于评论通知 """ # reply_comment replied_comment sender = UserSimpleSerializer() post = serializers.SerializerMethodField() reply_comment = serializers.SerializerMethodField() # 评论 replied_comment = serializers.SerializerMethodField() # 被回复的评论 receiver = UserSimpleSerializer() class Meta: model = Notification read_only_fields = ('sender', 'receiver', 'verb', 'action_content_type', 'action_object_id', 'target_content_type', 'target_object_id') # fields = '__all__' exclude = ['message'] def get_reply_comment(self, obj): if obj.verb == 'like': # 点赞目标回复的内容 comment = obj.target return {'id': comment.id, 'content': comment.content} elif obj.verb == 'comment': # 评论内容 面向post comment = obj.action if obj.action else None if not comment: return None return {'id': comment.id, 'content': comment.content} elif obj.verb == 'respond': # 回复的内容 面向comment comment = obj.action return {'id': comment.id, 'content': comment.content} def get_replied_comment(self, obj): if obj.verb == 'respond': replied_comment = obj.target if replied_comment: return { 'id': replied_comment.id, 'content': replied_comment.content } return None def get_post(self, obj): if obj.verb == 'like': # 被赞的回复所在的帖子 post = obj.action return {'post_id': post.id, 'post_title': post.title} else: # 评论或者回复所属的帖子 post = find_post(obj.target) return {'post_id': post.id, 'post_title': post.title}