def perform_update(self, serializer): title_str = serializer.validated_data.get('title') text_str = serializer.validated_data.get('text') category_str = serializer.validated_data.get('categories') blog_id = self.get_object().id blog = BlogService.update_blog_from_string(blog_id, title_str, text_str, category_str)
def get_comments_received_content(request): if request.is_ajax(): user_id = request.GET.get("user_id") user = get_object_or_404(User, id=user_id) notification_list = NotificationService.get_comment_reply_notifications( user) comment_list = list() # get comment list for notification in notification_list: comment = notification.content_object # add related notification field comment.related_notification_id = notification.id comment.unread = notification.unread comment_list.append(comment) comment_list = sorted(comment_list, key=lambda x: x.publish_time, reverse=True) page = request.GET.get('page') comments, page_list = BlogService.get_paginated_items( comment_list, page) context = {} context['comments'] = comments context['page_list'] = page_list context['origin_ajax_url'] = request.get_full_path() return render(request, "accounts/comment_received.html", context) else: raise Http404("Page not found!")
def get_queryset(self): paras = dict(self.request.GET) try: blogs = BlogService.search_blogs(**paras) except Exception as e: raise ParseError(str(e)) return blogs
def get_subscribe_blogs_content(request): if request.is_ajax(): user_id = request.GET.get("user_id") user = get_object_or_404(User, id=user_id) notification_list = NotificationService.get_subscribe_blog_notifications( user) blog_list = list() # get blog list for notification in notification_list: blog = notification.content_object # add related notification field blog.related_notification_id = notification.id blog.unread = notification.unread blog_list.append(blog) blog_list = sorted(blog_list, key=lambda x: x.publish_time, reverse=True) page = request.GET.get('page') blogs, page_list = BlogService.get_paginated_items(blog_list, page) context = {} context['blogs'] = blogs context['page_list'] = page_list context['origin_ajax_url'] = request.get_full_path() return render(request, "accounts/blog_posts.html", context) else: raise Http404("Page not found!")
def perform_create(self, serializer): title_str = serializer.validated_data.get('title') text_str = serializer.validated_data.get('text') category_str = serializer.validated_data.get('categories') user = self.request.user # the cleaned data will swallow space which would break markdown format # blog.text = form.cleaned_data.get('text') blog = BlogService.create_blog_from_string(user, title_str, text_str, category_str) serializer.validated_data['id'] = blog.id
def perform_create(self, serializer): blog_id = self.kwargs.get('blog_id') try: blog = Blog.objects.get(id=blog_id) except Blog.DoesNotExist: blog = None user = self.request.user if blog: text = serializer.validated_data.get("text") comment = BlogService.create_comment_from_string( user, blog, text, None) if comment: serializer.validated_data['id'] = comment.id else: raise ParseError("Comment level is too high!") else: raise NotFound(detail="Blog does not exist", code=404)
def get_blog_posts_content(request): if request.is_ajax(): user_id = request.GET.get("user_id") user = get_object_or_404(User, id=user_id) blog_list = Blog.objects.filter(author=user).order_by('-publish_time') page = request.GET.get('page') blogs, page_list = BlogService.get_paginated_items(blog_list, page) context = {} context['blogs'] = blogs context['page_list'] = page_list context['origin_ajax_url'] = request.get_full_path() return render(request, "accounts/blog_posts.html", context) else: raise Http404("Page not found!")
def perform_create(self, serializer): comment_id = self.kwargs.get('comment_id') try: parent_comment = Comment.objects.get(id=comment_id) except Comment.DoesNotExist: parent_comment = None user = self.request.user if parent_comment: text = serializer.validated_data.get("text") blog = parent_comment.blog comment = BlogService.create_comment_from_string( user, blog, text, parent_comment) if comment: serializer.validated_data['id'] = comment.id else: raise ParseError("Comment level is too high!") else: raise NotFound(detail="Comment does not exist", code=404)
def get_at_content(request): if request.is_ajax(): user_id = request.GET.get("user_id") user = get_object_or_404(User, id=user_id) at_blog_notification_list = NotificationService.get_at_blog_notifications( user) at_comment_notification_list = NotificationService.get_at_comment_notifications( user) at_notifications = [] for ind in range(len(at_blog_notification_list)): at_blog_notification_list[ind].src = "blog" at_notifications.append(at_blog_notification_list[ind]) for ind in range(len(at_comment_notification_list)): at_comment_notification_list[ind].src = "comment" at_notifications.append(at_comment_notification_list[ind]) at_list = list() # get obj list for notification in at_notifications: obj = notification.content_object # add related notification field obj.related_notification_id = notification.id obj.unread = notification.unread obj.src = notification.src at_list.append(obj) at_list = sorted(at_list, key=lambda x: x.publish_time, reverse=True) page = request.GET.get('page') items, page_list = BlogService.get_paginated_items(at_list, page) context = {} context['items'] = items context['page_list'] = page_list context['origin_ajax_url'] = request.get_full_path() return render(request, "accounts/at_received.html", context) else: raise Http404("Page not found!")
def perform_update(self, serializer): text_str = serializer.validated_data.get('text') comment_id = self.get_object().id comment = BlogService.update_comment_from_string(comment_id, text_str)