Exemplo n.º 1
0
def show(request, id, slug='a'):
    recipe = get_object_or_404(Recipe, id=id)
    categories = Category.objects.all()
    ingredients = recipe.ingredients.splitlines()
    comments = Comment.objects.filter(recipe=recipe).order_by('-date')

    if request.method == 'POST':
        form = AddCommentForm(request.POST)

        if form.is_valid():
            comment = Comment()
            comment.text = form.cleaned_data['text']
            comment.author = request.user
            comment.recipe = recipe
            comment.date = datetime.now()
            comment.save()
    else:
        form = AddCommentForm()

    return render(request, 'recipe/show.html', {'form': form,
                                                'recipe': recipe,
                                                'categories': categories,
                                                'currentCat': recipe.category,
                                                'ingredients': ingredients,
                                                'comments': comments
                                                })
    def create(self, validated_data):
        """
        Create and return a new 'Comment' instance, given the validated data
        This is like the form class's save method.
        
        Serializers have a save method, which will in turn invoke these 
        functions
        """
        print "CommentSerializer: In Create"
        comment = Comment()
        
        comment.author = validated_data.get('author', None)

        if comment.author is None:
            comment.author_name = validated_data.get('author_name', None)
            comment.author_email = validated_data.get('author_email', None)
            comment.author_url = validated_data.get('author_url', None)
            
            if comment.author_name is None and comment.author_email is None:
                return None 
        
        comment.body = validated_data.get('body')
        comment.post = validated_data.get('post')
        comment.published = False
        if comment.author is not None and settings.COMMENT_MODERATION_ENABLED is not True:
            comment.published = True
        elif comment.author is not None and comment.author.is_staff:
            comment.published = True 

        comment.save()
        return comment
Exemplo n.º 3
0
    def handle(self, *args, **options):
        users = list(User.objects.all())

        for i in range(10):
            t = Topic()
            t.name = u'Topic Name {}'.format(i)
            t.description = u'Topic Description {}'.format(i)
            t.save()
        topics = list(Topic.objects.all())

        for j in range(100):
            q = Question()
            q.author = random.choice(users)
            q.title = u'title {}'.format(j)
            q.text = u'text {}'.format(j)
            q.pub_date = datetime.datetime.now()
            q.is_published = True
            q.save()
            q.topics = random.sample(topics, random.randint(1, 6))
        questions = list(Question.objects.all())

        for k in range(100):
            c = Comment()
            c.author = random.choice(users)
            c.question = random.choice(questions)
            c.text = u'text {}'.format(k)
            c.pub_date = datetime.datetime.now()
            c.save()
Exemplo n.º 4
0
    def post(self, request):
        try:
            user = request.user
        except Account.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        serializer = CreateCommentSerializer(data=request.data)
        if serializer.is_valid():
            try:
                post = Post.objects.get(slug=request.data.get('post'))
            except Post.DoesNotExist:
                return Response(status=status.HTTP_404_NOT_FOUND)
            parent_comment = serializer.data.get('parent_comment', None)
            if parent_comment:
                try:
                    parent_comment = Comment.objects.get(id=parent_comment_id)
                except Comment.DoesNotExist:
                    parent_comment = None
            comment = Comment()
            comment.body = serializer.data.get('body')
            comment.post = post
            comment.author = user
            comment.parent_comment = parent_comment
            comment.save()
            return Response(data=DetailCommentSerializer(comment).data,
                            status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 5
0
 def post_handler(request):
     # JSON post body of what you post to a posts' comemnts
     # POST to http://service/posts/{POST_ID}/comments
     output = {
         "query": "addComment",
     }
     # change body = request.POST to body = request.body.decode('utf-8'),
     # because request.POST only accepts form, but here is json format.
     # change new_comment.comment to new_comment.content,
     # because that's how it defined in comment model.
     
     try:
         body = request.body.decode('utf-8')
         comment_info = loads(body)
         comment_info = comment_info['comment']
         new_comment = Comment()
         new_comment.contentType = comment_info['contentType']
         new_comment.content = comment_info['comment']
         new_comment.published = comment_info['published']
         new_comment.author = Author.objects.filter(id=comment_info['author']['id']).first()
         new_comment.parentPost = Post.objects.filter(id=post_id).first()
         new_comment.save()
         output['type'] = True
         output['message'] = "Comment added"
     except Exception as e:
         output['type'] = False
         output['message'] = "Comment not allowed"
         output['error'] = str(e)
     finally:
         return JsonResponse(output)
Exemplo n.º 6
0
    def handle(self, *args, **options):
        users = list(User.objects.all())

        for i in range(10):
            t = Topic()
            t.name = u'Topic Name {}'.format(i)
            t.description = u'Topic Description {}'.format(i)
            t.save()
        topics = list(Topic.objects.all())

        for j in range(100):
            q = Question()
            q.author = random.choice(users)
            q.title = u'title {}'.format(j)
            q.text = u'text {}'.format(j)
            q.pub_date = datetime.datetime.now()
            q.is_published = True
            q.save()
            q.topics = random.sample(topics, random.randint(1, 6))
        questions = list(Question.objects.all())

        for k in range(100):
            c = Comment()
            c.author = random.choice(users)
            c.question = random.choice(questions)
            c.text = u'text {}'.format(k)
            c.pub_date = datetime.datetime.now()
            c.save()
Exemplo n.º 7
0
def add_comment(request, article_id):
    if request.user.is_authenticated and request.POST:
        print(request.POST)
        comment = Comment()
        comment.author = request.user
        comment.article_id = article_id
        comment.content = request.POST["comment_body"]
        comment.save()
        return redirect(reverse("main:single_art", args=[article_id]))
    else:
        return HttpResponseNotFound()
Exemplo n.º 8
0
def leave_comment_to_blog(request, blog_id):
    if request.method == 'POST':
        blog = Blog.objects.get(id=str(blog_id))
        profile = Profile.objects.get(user=request.user)
        comment = Comment()
        comment.author = profile
        comment.text = request.POST.get('text', '---')
        comment.save()
        blog.comments.add(comment)
        blog.save()
        return redirect('single_blog', blog_id=blog_id)
Exemplo n.º 9
0
    def form_valid(seld, form):
        post = self.get_object()
        comment = Comment(**form.cleaned_data)
        comment.comment = post

        if self.request.user.is_authenticated:
            comment.author = self.request.user

        comment.save()
        messages.success(self.request, 'Comment sent successfully.')
        return redirect('post_single', id=post.id)
Exemplo n.º 10
0
def leave_comment_to_blog(request, blog_id):
    if request.method == 'POST':
        blog = Blog.objects.get(id=str(blog_id))
        profile = Profile.objects.get(user=request.user)
        comment = Comment()
        comment.author = profile
        text = request.POST.get('text')
        comment.text = text
        comment.save() # нам нужно чтобы у коммента появился id
        blog.comments.add(comment)
        blog.save()
        return redirect('single_blog', blog_id=blog_id)
Exemplo n.º 11
0
 def handle(self, *args, **options):
     posts = list(Post.objects.all())
     users = list(get_user_model().objects.all())
     for i in range(1000):
         c = Comment()
         c.text = u"Комментарий {}".format(i)
         parent_post = random.choice(posts)
         c.parent_post = parent_post
         parent_comments = list(parent_post.comments.all())
         if parent_comments:
             c.parent_comment = random.choice(parent_comments)
         c.author = random.choice(users)
         c.save()
Exemplo n.º 12
0
 def post(self, request, *args, **kwargs):
     self.object = self.get_object()
     form = self.comment_form(request.POST)
     if request.user.is_anonymous():
         return redirect_to_login(next=reverse('photos:photo', args=[str(self.object.pk)]), login_url=LOGIN_URL)
     if form.is_valid():
         comment = Comment()
         comment.author = request.user
         comment.text = form.cleaned_data['comment']
         comment.content_type = ContentType.objects.get_for_model(self.model)
         comment.object_id = self.object.pk
         comment.save()
     return HttpResponseRedirect(self.success_url)
Exemplo n.º 13
0
    def post_handler(request):
        # JSON post body of what you post to a posts' comemnts
        # POST to http://service/posts/{POST_ID}/comments
        output = {
            "query": "addComment",
        }

        # checks if local host
        if Post.objects.filter(id=post_id).exists():
            # checks visibility of the post
            if not check_get_perm(
                    request,
                    Post.objects.get(id=post_id).to_api_object()):
                return JsonResponse(
                    {
                        "query": "addComment",
                        "success": False,
                        "message": "Comment not allowed"
                    },
                    status=403)

        # change body = request.POST to body = request.body.decode('utf-8'),
        # because request.POST only accepts form, but here is json format.
        # change new_comment.comment to new_comment.content,
        # because that's how it defined in comment model.
        try:
            body = request.body.decode('utf-8')
            comment_info = loads(body)
            comment_info = comment_info['comment']
            new_comment = Comment()
            new_comment.contentType = comment_info['contentType']
            new_comment.content = comment_info['comment']
            new_comment.published = comment_info['published']
            new_comment.author = url_regex.sub(
                '', comment_info['author']['id']).rstrip("/")
            new_comment.parentPost = Post.objects.filter(id=post_id).first()
            new_comment.save()
            output['success'] = True
            output['message'] = "Comment added"
        except Exception as e:
            output['success'] = False
            output['message'] = "Comment not allowed"
            output['error'] = str(e)
        finally:
            if output["success"]:
                return JsonResponse(output, status=200)
            else:
                return JsonResponse(output, status=403)
Exemplo n.º 14
0
def submit_comment(request):

    if request.method == 'POST':

        comment_title = request.POST.get('comment_title')
        comment_content = request.POST.get('comment_content')
        post_id = request.POST.get('post_id')
        current_user = request.user
        profile = UserProfileInfo.objects.get(user=current_user)

        c = Comment()
        c.post = Post.objects.get(id=post_id)
        c.content = comment_content
        c.author = UserProfileInfo.objects.get(id=profile.id)
        c.published_date = datetime.now()
        c.save()

    return HttpResponseRedirect(reverse('blog:post_details', args=(post_id, )))
Exemplo n.º 15
0
def create_comment(request):
    if request.method == 'POST':
        
        profile = Profile.objects.get(user = request.user)
        blog_id = request.POST['blog_id']
        text = request.POST['comment']

        
        blog = Blog.objects.get(id = str(blog_id))
        
        comment = Comment()
        comment.text = text
        comment.author = profile
        comment.save()

        blog.comments.add(comment)
        blog.save


        return redirect('blogs:blog',blog_id=blog_id)
        
    def create(self, validated_data):
        """
        Create and return a new 'Comment' instance, given the validated data
        This is like the form class's save method.
        
        Serializers have a save method, which will in turn invoke these 
        functions
        """

        print('In create')

        comment = Comment()

        comment.author = validated_data.get('author')

        if comment.author is None:
            comment.author_name = validated_data.get('author_name', None)
            comment.author_email = validated_data.get('author_email', None)
            comment.author_url = validated_data.get('author_url', None)

            if comment.author_name is None and comment.author_email is None:
                return None

        comment.body = validated_data.get('body')

        comment.post = validated_data.get('post')
        comment.published = False
        if comment.author is not None and settings.COMMENT_MODERATION_ENABLED is not True:
            comment.published = True
        elif comment.author is not None and comment.author.is_staff:
            comment.published = True

        comment.parent_comment = validated_data.get('parent_comment', None)
        print(comment.parent_comment)

        comment.save()
        return comment
Exemplo n.º 17
0
def task_detail(request, pk=None):

    task = get_object_or_404(Task, id=pk)

    if request.method == 'GET':
        task.usertask.add(request.user)
        task.save()
    if request.method == 'POST':
        if request.POST['type'] == 'comment':
            comment = Comment()
            comment.text = request.POST['comment']
            comment.author = request.user
            comment.task_id = pk
            comment.save()
            task.task_comments.add(comment)
            task.save()
        elif request.POST['type'] == 'close':
            task.is_finished = True
            task.save()
    context = {
        'task': task,
        'comments': Comment.objects.all().filter(task=task),
    }
    return render(request, 'tasks/tasks_detail.html', context)