Example #1
0
def post_comments_as_json(request, teddy_id, post_id):
    post = get_object_or_404(Post, pk=post_id)
    comments = []
    for comment in post.comment_set.all().order_by("-comment_time"):
        comments.append([comment.comment,
                         utils.get_username_or_fullname(comment.user),
                         comment.user.id,
                         utils.get_friendly_time(comment.comment_time)])
    comments = post.comment_set.all().order_by("-comment_time")
    comment_dictionary = [{
                          'comment': c.comment,
                          'comment_time': utils.get_friendly_time(c.comment_time),
                          'user_id': c.user.id,
                          'user_name': c.user.username,

                          } for c in comments]
    data = json.dumps(comment_dictionary)
    return HttpResponse(data, mimetype='application/json')
Example #2
0
def get_posts_as_json(request):
    posts = Post.objects.all()
    post_dictionary = [{
                       'id': p.id,
                       'title': p.title,
                       'small_picture': p.small_picture.url,
                       'picture': p.picture.url,
                       'user_id': p.user.id,
                       'user_name': p.user.username,
                       'teddy_id': p.teddy.id,
                       'comments': [{
                                    'comment': c.comment,
                                    'teddy_id': p.teddy.id,
                                    'post_id': p.id,
                                    'comment_time': utils.get_friendly_time(c.comment_time),
                                    'user_id': c.user.id,
                                    'user_name': c.user.username, } for c in p.comment_set.all()], } for p in posts]
    data = json.dumps(post_dictionary)
    return HttpResponse(data, mimetype='application/json')
Example #3
0
def post_comment(request, teddy_id, post_id):
    request_comment = json.loads(request.body)
    comment = Comment()
    if request.user.is_authenticated():
        post = get_object_or_404(Post, pk=post_id)
        comment.comment = request_comment['comment']
        comment.comment_time = datetime.datetime.now()
        comment.post = post
        comment.user = request.user
        comment.save()

    comment_time_tz_aware = timezone.make_aware(comment.comment_time, timezone.get_default_timezone())
    json_comment = {
        'comment': comment.comment,
        'comment_time': utils.get_friendly_time(comment_time_tz_aware),
        'user_id': comment.user.id,
        'user_name': comment.user.username
    }

    return HttpResponse(json.dumps(json_comment), mimetype='application/json')