Пример #1
0
def post_update(request):
    response = {}
    if not request.method == 'POST':
        return JsonResponse({
            'code': 2,
            'response': 'Method in not supported'
        })
    try:
        request_params = json.loads(request.body)
    except ValueError:
        return JsonResponse({
            'code': 3,
            'response': 'No JSON object could be decoded'
        })

    if not ('post' in request_params and 'message' in request_params):
        return JsonResponse({
                'code': 3,
                'response': 'Missing field'
            })
    post_id = request_params.get('post')

    post_data = get_post_by_id(post_id)
    if not post_data:
        return JsonResponse({
                'code': 1,
                'response': 'Post not found'
            })

    message = request_params.get('message')

    cursor = connection.cursor()
    sql = "UPDATE post SET message=%s WHERE id = %s"

    cursor.execute(sql, (message, post_id))

    response.update({
        'id': int(post_id),
        'forum': get_forum_by_id(post_data[1])[4],
        'thread': post_data[2],
        'user': get_user_by_id(post_data[3])[2],
        'message': message,
        'date': post_data[5].strftime('%Y-%m-%d %H:%M:%S'),
        'parent': post_data[6],
        'isApproved': bool(post_data[8]),
        'isHighlighted': bool(post_data[9]),
        'isSpam': bool(post_data[10]),
        'isEdited': bool(post_data[11]),
        'isDeleted': bool(post_data[12]),
        'likes': post_data[13],
        'dislikes': post_data[14],
        'points': post_data[15],
    })

    return JsonResponse({
        'code': 0,
        'response': response
    })
Пример #2
0
def post_restore(request):
    response = {}
    if not request.method == 'POST':
        return JsonResponse({
            'code': 2,
            'response': 'Method in not supported'
        })

    try:
        request_params = json.loads(request.body)

        if 'post' not in request_params:
            return JsonResponse({
                'code': 3,
                'response': 'Missing field'
            })

        post_id = request_params.get('post')
        post_data = get_post_by_id(post_id)

        if not post_data:
            return JsonResponse({
                'code': 1,
                'response': 'Post not found'
            })

        cursor = connection.cursor()
        sql = "UPDATE post SET is_deleted = 0 WHERE id = %s"
        cursor.execute(sql, (post_id,))

        cursor.execute("UPDATE thread SET posts = posts+1 WHERE id = %s", (post_data[2],))

        response.update({
            'post': post_id,
        })

    except ValueError:
        return JsonResponse({
            'code': 3,
            'response': 'No JSON object could be decoded'
        })

    return JsonResponse({
        'code': 0,
        'response': response,
    })
Пример #3
0
def post_details(request):
    response = {}
    if not request.method == 'GET':
        return JsonResponse({
            'code': 2,
            'response': 'Method in not supported'
        })

    if 'post' not in request.GET:
        return JsonResponse({
                'code': 3,
                'response': 'Missing field'
            })

    post_id = request.GET.get('post')
    post_data = get_post_by_id(post_id)

    if not post_data:
        return JsonResponse({
                'code': 1,
                'response': 'Post not found'
            })
    user_data = get_user_by_id(post_data[3])
    thread_data = get_thread_by_id(post_data[2])
    forum_data = get_forum_by_id(post_data[1])

    if 'related' in request.GET:
        related = request.GET.getlist('related')

        for r in related:
            if r != 'forum' and r != 'user' and r != 'thread':
                return JsonResponse({
                    'code': 3,
                    'response': 'Wrong related params'
                })

        if 'user' in related:
            followers, following = get_follow_data(user_data[0])
            subs = get_subscriptions(user_data[0])
            user_info = {
                'id': user_data[0],
                'username': user_data[1],
                'email': user_data[2],
                'name': user_data[3],
                'about': user_data[4],
                'isAnonymous': bool(user_data[5]),
                'followers': [
                    f[0] for f in followers
                ],
                'following': [
                    f[0] for f in following
                ],
                'subscriptions': [
                    s[0] for s in subs
                ]
            }
        else:
            user_info = user_data[2]

        if 'forum' in related:
            forum_info = {
                'id': forum_data[0],
                'name': forum_data[1],
                'short_name': forum_data[4],
                'user': get_user_by_id(forum_data[3])[2],
                'isDeleted': bool(forum_data[2])
            }
        else:
            forum_info = forum_data[4]

        if 'thread' in related:
            thread_info = {
                'id': thread_data[0],
                'forum': forum_data[4],
                'title': thread_data[2],
                'isClosed': bool(thread_data[3]),
                'user': get_user_by_id(thread_data[4])[2],
                'date': thread_data[5].strftime('%Y-%m-%d %H:%M:%S'),
                'message': thread_data[6],
                'slug': thread_data[7],
                'isDeleted': bool(thread_data[8]),
                'dislikes': thread_data[9],
                'likes': thread_data[10],
                'points': thread_data[11],
                'posts': thread_data[12]
            }
        else:
            thread_info = thread_data[0]
    else:
        user_info = user_data[2]
        thread_info = thread_data[0]
        forum_info = forum_data[4]


    response.update({
        'id': int(post_id),
        'forum': forum_info,
        'thread': thread_info,
        'user': user_info,
        'message': post_data[4],
        'date': post_data[5].strftime('%Y-%m-%d %H:%M:%S'),
        'parent': post_data[6],
        'isApproved': bool(post_data[8]),
        'isHighlighted': bool(post_data[9]),
        'isSpam': bool(post_data[10]),
        'isEdited': bool(post_data[11]),
        'isDeleted': bool(post_data[12]),
        'likes': post_data[13],
        'dislikes': post_data[14],
        'points': post_data[15],
    })

    return JsonResponse({
        'code': 0,
        'response': response
    })