Esempio n. 1
0
def action(request):
    try:
        post_id = request.POST.get("post", None)
        post = Post.objects.get(id=post_id)
    except Post.DoesNotExist:
        return JsonResponse({"error": True, "caption": "Error", "message": "Post not found."})

    action = request.POST.get("action", None)
    state = request.POST.get("state", None)
    try:
        state = bool(int(state))
    except ValueError:
        state = None
    if action not in ("starred", "read") or state is None:
        return JsonResponse({"error": True, "caption": "Error", "message": "Invalid parameters."})

    try:
        user_post = UserPost.objects.get(user=request.user, post=post)
    except UserPost.DoesNotExist:
        user_post = UserPost(user=request.user, post=post)

    changed = False
    if user_post.pk is None:
        changed = True
    else:
        current_value = getattr(user_post, action)
        if current_value != state:
            changed = True

    if changed:
        setattr(user_post, action, state)
        user_post.save()
        if action == "read":
            _update_unread_count(user_post, -1 if state else +1)

        result_message = "Post {} marked as {}".format(post_id, action if state else "not " + action)
        return JsonResponse({"error": False, "caption": "Result", "message": result_message})

    return JsonResponse(
        {"error": False, "caption": "Result", "message": "Post[{}].{} not changed".format(post.id, action)}
    )
Esempio n. 2
0
def outline_mark_as_read( request ):
    try:
        outline = __get_outline(request)
    except Exception as e:
        return HttpJsonResponse(error=e.message)

    if outline.feed:
        posts = Post.objects.filter( feed = outline.feed )
    else:
        feed_ids = Outline.objects.filter( parent = outline ).values_list( 'feed', flat = True )
        feeds = Feed.objects.filter( pk__in = feed_ids )
        posts = Post.objects.filter( feed__in = feeds )
    post_count = change_count = 0
    for post in posts:
        post_count += 1
        try:
            u = UserPost.objects.get( user = request.user, post = post )
        except UserPost.DoesNotExist:
            u = UserPost( user = request.user, post = post )
        if not u.id or not u.read:
            u.read = True
            u.save()
            change_count += 1
    return HttpJsonResponse(post_count = post_count, change_count = change_count)