class DialogListSerializer(ModelSerializer): owner = UserDetailSerializer(read_only=True) opponent = UserDetailSerializer(read_only=True) class Meta: model = Dialog fields = ['owner', 'opponent', 'id']
class ApprovalDetailSerializer(ModelSerializer): url = patient_detail_url user = UserDetailSerializer(read_only=True) approvedby = UserDetailSerializer(read_only=True) # store_logo = SerializerMethodField() class Meta: model = Approval fields = [ 'url', 'id', 'user', 'status', 'approvedby', 'updated', 'timestamp' ]
class ApprovalListSerializer(ModelSerializer): url = patient_detail_url user = UserDetailSerializer(read_only=True) approvedby = UserDetailSerializer(read_only=True) delete_url = HyperlinkedIdentityField( view_name='approvals-api:delete', lookup_field='id' #or primary key <pk> ) class Meta: model = Approval fields = [ 'url', 'user', 'id', 'delete_url', 'status', 'approvedby', 'updated', 'timestamp' ]
class FeedbackListSerializer(ModelSerializer): user = UserDetailSerializer(read_only=True) timestamp = DateTimeField(read_only=True) class Meta: model = Feedback fields = ['user', 'content', 'timestamp']
class CommentDetailSerializer(ModelSerializer): user = UserDetailSerializer(read_only = True) reply_count = SerializerMethodField() replies = SerializerMethodField() class Meta: model = AddComment fields = [ 'user', 'comment', 'reply_count', 'replies', ] read_only_fields = [ 'reply_count', 'replies', ] def get_replies(self, obj): if obj.is_parent: return CommentChildSerializer(obj.children(), many=True).data return None def get_reply_count(self, obj): if obj.is_parent: return obj.children().count() return 0
class CommentDetailSerializer(ModelSerializer): user = UserDetailSerializer(read_only=True) replies = SerializerMethodField() reply_count = SerializerMethodField() content_object_url = SerializerMethodField() class Meta: model = Comment fields = ['user', 'id', 'content', 'replies', 'reply_count', 'timestamp', 'content_object_url'] read_only_fields = ['replies', 'reply_count'] def get_content_object_url(self, obj): try: return obj.content_object.get_api_url() except: return None def get_replies(self, obj): if obj.is_parent: return CommentChildSerializer(obj.children(), many=True).data return None def get_reply_count(self, obj): if obj.is_parent: return obj.children().count() return 0
class CommentListSerializer(ModelSerializer): reply_count = SerializerMethodField() reply_total = SerializerMethodField() replies = SerializerMethodField() user = UserDetailSerializer(read_only=True) url = HyperlinkedIdentityField( view_name='api-comments:detail' ) class Meta: model = Comment fields = [ 'id', 'user', 'content', 'reply_count', 'reply_total', 'replies', 'timestamp', 'url' ] def get_reply_count(self, obj): return get_children_count(obj) def get_reply_total(self, obj): return get_children_count_total(obj) def get_replies(self, obj): try: return CommentChildSerializer(obj.children(), many=True).data except: return None
class PostListSerializer(ModelSerializer): url = detail_url user = UserDetailSerializer(read_only=True) image = SerializerMethodField() html = SerializerMethodField() class Meta: model = Post fields = [ 'id', 'url', 'user', 'title', 'image', 'content', 'html', 'published' ] def get_image(self,obj): try: image = obj.image.url except: image = None return image def get_html(self,obj): return obj.get_markdown()
class PostDetailSerializer(ModelSerializer): user = UserDetailSerializer(read_only=True) image = SerializerMethodField() comments = SerializerMethodField() class Meta: model = Post fields = [ 'user', 'title', 'slug', 'content', 'timestamp', 'image', 'comments', ] # def get_user(self, obj): # return str(obj.user.username) def get_image(self, obj): try: image = obj.image.url except: image = None return image def get_comments(self, obj): c_qs = Comment.objects.filter_by_instance(obj) comments = CommentSerializer(c_qs, many=True).data return comments
class PostDetailSerializer(ModelSerializer): user = UserDetailSerializer(read_only=True) image = ImageField(max_length=None, use_url=True) html = SerializerMethodField() comments = SerializerMethodField() class Meta: model = Post fields = [ 'id', 'user', 'title', 'slug', 'content', 'html', 'image', 'comments', 'publish', ] read_only_fields = [ "slug", # SerializerMethodFields() are automatically read_only ] def get_html(self, obj): return obj.get_markdown() def get_comments(self, obj): comments_queryset = Comment.objects.filter_by_instance(obj) serializer_context = {'request': self.context['request']} comments = CommentListSerializer(comments_queryset, many=True, context=serializer_context).data return comments
class CareerListSerializer(ModelSerializer): url = career_detail_url user = UserDetailSerializer(read_only=True) image = SerializerMethodField() class Meta: model = Career fields = [ "url", "user", "title", "category", "image", "description", "entry", "experience", "education", ] def get_image(self, obj): try: image = obj.image.url except: image = None return image
class CareerDetailSerializer(ModelSerializer): url = career_detail_url user = UserDetailSerializer(read_only=True) image = SerializerMethodField() comments = SerializerMethodField() class Meta: model = Career fields = [ "url", "user", "title", "category", "description", "entry", "experience", "education", "publish", "image", "comments", ] def get_image(self, obj): try: image = obj.image.url except: image = None return image def get_comments(self, obj): c_qs = Comment.objects.filter_by_instance(obj) comments = CommentSerializer(c_qs, many=True).data return comments
class CommentDetailSerializer(ModelSerializer): user = UserDetailSerializer(read_only=True) replies = SerializerMethodField() reply_count = SerializerMethodField() class Meta: model = Comment fields = [ "id", "user", "content_type", "object_id", "created_at", "content", "reply_count", "replies", ] def get_replies(self, obj): if obj.is_parent: return CommentChildSerializer(obj.children(), many=True).data return None def get_reply_count(self, obj): if obj.is_parent: return obj.children().count() return 0
class PostDetailSerializer(ModelSerializer): url = post_detail_url user = UserDetailSerializer(read_only=True) comment = SerializerMethodField() class Meta: model = Post fields = [ 'url', 'user', 'id', 'id', 'title', 'content', 'content_html', 'publish', 'image', 'read_time', 'draft', 'private', 'published', 'content_display', 'comment', ] def get_comment(self, obj): # content_type = obj.get_content_type # object_id = obj.id c_qs = Comment.objects.filter_by_instance(obj) comments = CommentSerializer(c_qs, many=True).data return comments
class PostDetailSerializer(ModelSerializer): url = post_detail_url user = UserDetailSerializer(read_only=True) image = SerializerMethodField() class Meta: model = Post fields = [ 'url', 'id', 'user', 'title', 'slug', 'content', 'publish', 'image' ] def get_user(self, obj): return str(obj.user.username) def get_image(self, obj): try: image = obj.image.path except: image = None return image
class CommentDetailSerializer(ModelSerializer): user = UserDetailSerializer() content_object_url = SerializerMethodField() replies = SerializerMethodField() class Meta: model = Comment fields = [ "id", "user", "content_type", "object_id", "content", "replies", "content_object_url", ] read_only_fields = [ # 'content_type', 'reply_count', 'replies', # 'object_id', ] def get_replies(self, obj): if obj.is_parent: return CommentChildSerializer(obj.children(), many=True).data return None def get_content_object_url(self, obj): try: return obj.content_object.get_api_url() except: return None
class PostListSerializer(ModelSerializer): # url = HyperlinkedIdentityField( # # view_name é similar ao get_absolute_url # view_name='posts-api:detail', # # o default é a pk (primary key) # lookup_field='slug', # ) url = hyperlinked('posts-api:detail') # delete_url = hyperlinked('posts-api:delete') # update_url = hyperlinked('posts-api:update') user = UserDetailSerializer(read_only=True) # image = SerializerMethodField() class Meta: model = Post fields = [ 'url', # 'update_url', 'user', 'title', 'slug', 'content', 'publish', 'image', # 'delete_url' ]
class CommentListSerializer(ModelSerializer): """ Comment serializer for listing use case. Extends ModelSerializer """ author = UserDetailSerializer(read_only = True) reply_count = SerializerMethodField() class Meta: model = Comment fields = [ 'id', 'author', 'parent_id', 'content', 'timestamp', 'reply_count', ] def get_reply_count(self, obj): """ Returns reply count for reply_count SerializerMethodField """ if obj.is_parent: return obj.children().count() return 0
class PostDetailSerializer(ModelSerializer): url = post_detail_url image = SerializerMethodField() html = SerializerMethodField() user = UserDetailSerializer(read_only=True) class Meta: model = Post fields = [ 'id', 'url', 'user', 'title', 'slug', 'content', 'html', 'publish', 'image', ] def get_image(self, obj): try: return obj.image.url except: return None def get_html(self, obj): return obj.get_markdown()
class PostListSerializer(ModelSerializer): url = post_detail_url user = UserDetailSerializer(read_only=True) class Meta: model = Post fields = ['user', 'url', 'title', 'content', 'publish']
class CommentChildSerializer(ModelSerializer): reply_count = SerializerMethodField() reply_total = SerializerMethodField() replies = SerializerMethodField() user = UserDetailSerializer(read_only=True) class Meta: model = Comment fields = [ 'id', 'user', 'content', 'timestamp', 'reply_count', 'reply_total', 'replies' ] def get_reply_count(self, obj): return get_children_count(obj) def get_reply_total(self, obj): return get_children_count_total(obj) def get_replies(self, obj): try: return CommentChildSerializer(obj.children(), many=True).data except: return None
class PostDetailSerializer(serializers.ModelSerializer): url = post_detail_url # user = SerializerMethodField() user = UserDetailSerializer(read_only=True) image = SerializerMethodField() html = SerializerMethodField() comments = SerializerMethodField() class Meta: model = Post fields = [ "url", "title", "slug", "publish", "user", "image", "content", "html", "comments" ] def get_user(self, obj): return str(obj.user.username) def get_image(self, obj): if obj.image and obj.image.url: return obj.image.url return None def get_html(self, obj): return obj.get_markdown() def get_comments(self, obj): # content_type = obj.get_content_type # object_id = obj.id c_qs = Comment.objects.filter_by_instance(obj) comments = CommentSerializer(c_qs, many=True).data return comments
class PostListSerializer(ModelSerializer): user = UserDetailSerializer(read_only=True) type = SerializerMethodField() # url = post_url delete_url = post_delete_url edit_url = post_edit_url class Meta: model = Post fields = [ # 'url', 'id', 'user', 'type', 'image', 'file', 'content', 'timestamp', 'delete_url', 'edit_url', ##### ] def get_type(self, obj): return obj.get_type_display()
class PostDetailSerializer(ModelSerializer): # url = post_detail_url user = UserDetailSerializer(read_only=True) image = SerializerMethodField() html = SerializerMethodField() comments = SerializerMethodField() class Meta: model = Post fields = [ 'id', 'title', 'user', 'slug', 'content', 'html', 'image', 'publish', 'comments' ] def get_image(self, obj): try: image = obj.image.url except: image = None return image def get_html(self, obj): return obj.get_markdown() # # def get_user(self, obj): # return str(obj.user.username) def get_comments(self, obj): content_type = obj.get_content_type object_id = obj.id c_qs = Comment.objects.filter_by_instance(obj) # comments_queryset comments = CommentSerializer(c_qs, many=True).data return comments
class PostDetailSerializer(ModelSerializer): url = post_detail_url user = UserDetailSerializer(read_only=True) image = SerializerMethodField() comments = SerializerMethodField() class Meta: model = Post fields = [ 'url', 'id', 'title', 'slug', 'user', 'content', 'publish', 'image', 'comments', ] def get_image(self, obj): try: image = obj.image.url except: image = None return image def get_comments(self, obj): c_qs = Comment.objects.filter_by_instance(obj) comments = CommentSerializer(c_qs, many=True).data return comments
class PostDetailSerializer(ModelSerializer): url = DETAIL_URL user = UserDetailSerializer(read_only=True) image = SerializerMethodField() html = SerializerMethodField() comments = SerializerMethodField() class Meta: model = Post fields = [ 'url', 'id', 'title', 'slug', 'content', 'html', 'image', 'user', 'draft', 'publish', 'comments' ] def get_html(self, obj): return obj.get_markdown() def get_image(self, obj): try: image = obj.image.url except: image = None return image def get_comments(self, obj): qs = Comment.objects.filter_by_instance(obj) comments = CommentSerializer(qs, many=True).data return comments
class PostDetailSerializer(ModelSerializer): createcmnts = HyperlinkedIdentityField( view_name = 'blogs-api:c-create', lookup_field = 'pk' ) edit_url = HyperlinkedIdentityField( view_name = 'blogs-api:edit', lookup_field = 'pk' ) user = UserDetailSerializer(read_only=True) comments = SerializerMethodField() class Meta: model = Base fields = [ 'createcmnts', 'title', 'body', 'user', 'edit_url', 'comments'] def get_comments(self, obj): post_id = obj.id c_qs = AddComment.objects.filter(post_id=post_id).filter(parent=None) content = CommentSerializer(c_qs, many = True, context=self.context).data return content
class PostListSerializer(ModelSerializer): # view_name 來自於blog/urls.py 的 namespace='posts-api' # 再加上 posts/urls.py 的 name='detail' # url = HyperlinkedIdentityField( # view_name='posts-api:detail', # lookup_field='slug' # ) # delete_url = HyperlinkedIdentityField( # view_name='posts-api:delete', # lookup_field='slug' # ) url = post_detail_url # user = SerializerMethodField() user = UserDetailSerializer(read_only=True) # image = SerializerMethodField() # html = SerializerMethodField() class Meta: model = Post fields = [ 'url', 'user', 'title', 'content', 'publish', # 'image', # 'html', # 'delete_url', ]
class FeedbackSerializer(ModelSerializer): user = UserDetailSerializer(read_only=True) timestamp = DateTimeField(read_only=True) shopSlug = SlugField(write_only=True) def validate_shopSlug(self, slug): shop_qs = Shop.objects.filter(slug=slug) if shop_qs.exists() and shop_qs.count() == 1: return shop_qs.first() raise ValidationError( 'Sorry cannot find the shop you are commentig on') def create(self, validate_data): shopSlug = validate_data['shopSlug'] shop = validate_data['shopSlug'] content = validate_data["content"] user = self.context['request'].user # print ("hello") # print (self.context['request'].COOKIES['token']) # print(self.context['request'].META['REMOTE_ADDR']) return Feedback.objects.create(user=user, shop=shop, content=content) class Meta: model = Feedback fields = ['shopSlug', 'user', 'content', 'timestamp']
class CommentListSerializer(ModelSerializer): url = HyperlinkedIdentityField(view_name='comments-api:thread', ) user = UserDetailSerializer(read_only=True) reply_count = SerializerMethodField() class Meta: model = Comment fields = [ 'id', 'url', 'user', # 'content_type', # 'object_id', # 'parent', 'timestamp', 'content', 'reply_count', ] def get_reply_count(self, obj): if obj.is_parent: return obj.children().count() return 0