Пример #1
0
def thread_update(request):
    response = {}
    if not request.method == 'POST':
        return JsonResponse({
            'code': 2,
            'response': 'Method in not supported'
        })
    try:
        request_params = json.loads(request.body)

        if not ('thread' in request_params and 'message' in request_params and 'slug' in request_params):
            return JsonResponse({
                'code': 3,
                'response': 'Missing field'
            })

        thread_id = request_params.get('thread')
        message = request_params.get('message')
        slug = request_params.get('slug')

        thread_data = get_thread_by_id(thread_id)
        if not thread_data:
            return JsonResponse({
                'code': 1,
                'response': 'Thread not found'
            })

        sql = "UPDATE thread SET message = %s, slug = %s WHERE id = %s"

        cursor = connection.cursor()

        cursor.execute(sql, (message, slug, thread_id))

        response = {
            'user': get_user_by_id(thread_data[4])[2],
            'forum': get_forum_by_id(thread_data[1])[4],
            'id': thread_id,
            'title': thread_data[2],
            'isClosed': bool(thread_data[3]),
            'date': thread_data[5].strftime('%Y-%m-%d %H:%M:%S'),
            'message': message,
            'slug': slug,
            'isDeleted': bool(thread_data[8]),
            'posts': thread_data[12] if not thread_data[8] else 0,
            'likes': thread_data[10],
            'dislikes': thread_data[9],
            'points': thread_data[11],
        }

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

    return JsonResponse({
        'code': 0,
        'response': response
    })
Пример #2
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
    })
Пример #3
0
def user_list_posts(request):
    response = []
    if not request.method == 'GET':
        return JsonResponse({
            'code': 2,
            'response': 'Method in not supported'
        })


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

    user_email = request.GET.get('user')
    cursor = connection.cursor()

    user_data = get_user_by_email(cursor, user_email)
    if not user_data:
        cursor.close()
        return JsonResponse({
            'code': 1,
            'response': 'User not found'
        })

    if 'since' in request.GET:
        since = request.GET.get('since')
        try:
            datetime.datetime.strptime(since, '%Y-%m-%d %H:%M:%S')
        except ValueError:
            return JsonResponse({
                'code': 3,
                'response': 'Wrong since date param'
            })
    else:
        since = 0

    if "limit" in request.GET:
        limit = request.GET.get('limit')
        try:
            limit = int(limit)
        except ValueError:
            return JsonResponse({
                'code': 3,
                'response': 'Wrong limit param'
            })
    else:
        limit = None

    if "order" in request.GET:
        order = request.GET.get('order')
        if order != 'asc' and order != 'desc':
            return JsonResponse({
                'code': 3,
                'response': 'Wrong order param'
            })
    else:
        order = 'desc'

    cursor = connection.cursor()

    sql = "SELECT * FROM post WHERE user_id = %s AND date>=%s ORDER BY date "
    sql += order

    if limit:
        sql += " LIMIT %s"
        cursor.execute(sql, (user_data[0], since, limit))
    else:
        cursor.execute(sql, (user_data[0], since))

    posts = cursor.fetchall()

    for p in posts:
        response.append({
            'id': p[0],
            'user': user_email,
            'forum': get_forum_by_id(p[1])[4],
            'thread': p[2],
            'message': p[4],
            'date': p[5].strftime('%Y-%m-%d %H:%M:%S'),
            'parent': p[6],
            'isApproved': bool(p[8]),
            'isHighlighted': bool(p[9]),
            'isSpam': bool(p[10]),
            'isEdited': bool(p[11]),
            'isDeleted': bool(p[12]),
            'likes': p[13],
            'dislikes': p[14],
            'points': p[15],
        })


    return JsonResponse({
        'code': 0,
        'response': response,
    })
Пример #4
0
def thread_list_posts(request):
    t1 = datetime.datetime.now()
    response = []
    if not request.method == 'GET':
        return JsonResponse({
            'code': 2,
            'response': 'Method in not supported'
        })

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

    thread_id = request.GET.get('thread')
    thread_data = get_thread_by_id(thread_id)

    if not thread_data:
        return JsonResponse({
                'code': 1,
                'response': 'Thread not found'
            })

    if "since" in request.GET:
        since = request.GET['since']
        try:
            since = datetime.datetime.strptime(since, '%Y-%m-%d %H:%M:%S')
        except ValueError:
            return JsonResponse({
                'code': 3,
                'response': 'Since id param is wrong'
            })
    else:
        since = 0

    if "limit" in request.GET:
        limit = request.GET['limit']
        try:
            limit = int(limit)
        except ValueError:
            return JsonResponse({
                'code': 3,
                'response': 'Limit param is wrong'
            })
    else:
        limit = None

    if "order" in request.GET:
        order = request.GET['order']
        if order != 'asc' and order != 'desc':
            return JsonResponse({
                'code': 3,
                'response': 'Order param is wrong'
            })
    else:
        order = 'desc'

    if "sort" in request.GET:
        sort = request.GET['sort']
        if sort != 'flat' and sort != 'tree' and sort != 'parent_tree':
            return JsonResponse({
                'code': 3,
                'response': 'Sort param is wrong'
            })
    else:
        sort = 'flat'

    cursor = connection.cursor()

    if sort == 'flat':
        sql = "SELECT * FROM post WHERE thread_id = %s AND date>=%s ORDER BY date "
        sql += order

        if limit:
            sql += " LIMIT %s"
            cursor.execute(sql, (thread_id, since, limit))
        else:
            cursor.execute(sql, (thread_id, since))

        data = cursor.fetchall()

    elif sort == 'tree':
        if order == 'asc':
            sql = "SELECT * FROM post WHERE thread_id = %s AND date>=%s ORDER BY path ASC"

            if limit:
                sql += " LIMIT %s"
                cursor.execute(sql, (thread_id, since, limit))
            else:
                cursor.execute(sql, (thread_id, since))

            data = cursor.fetchall()

        else:
            # tree -> desc
            query = "SELECT * FROM post " \
                    "WHERE date>=%s AND thread_id=%s AND parent IS NULL ORDER BY path DESC"
            query += " LIMIT %s" if limit is not None else ""
            sql_data = (since, thread_id, limit) if limit is not None else (since, thread_id)

            cursor.execute(query, sql_data)
            roots = cursor.fetchall()
            data = []
            if limit:
                # tree -> desc -> limit
                posts = 0
                for root in roots:
                    if posts < limit:
                        data.append(root)
                        posts += 1
                        if posts < limit:
                            parent_path = root[7] + '.%'
                            query = "SELECT * FROM post " \
                                    "WHERE path LIKE %s ORDER BY path ASC LIMIT %s"
                            sql_data = (parent_path, limit)
                            cursor.execute(query, sql_data)
                            children = cursor.fetchall()
                            for child in children:
                                if posts < limit:
                                    data.append(child)
                                    posts += 1
            else:
                # tree -> desc -> no limit
                for p in roots:
                    data.append(p)
                    parent_path = p[7] + '.%'
                    query = "SELECT * FROM post " \
                            "WHERE path LIKE %s ORDER BY path ASC"
                    sql_data = (parent_path,)
                    cursor.execute(query, sql_data)
                    data2 = cursor.fetchall()
                    for p1 in data2:
                        data.append(p1)
    else:
        # parent_tree
        query = "SELECT * FROM post " \
                "WHERE date>=%s AND thread_id=%s AND parent IS NULL ORDER BY date "
        query += order
        query += " LIMIT %s" if limit is not None else ""
        sql_data = (since, thread_id, limit) if limit is not None else (since, thread_id)
        cursor.execute(query, sql_data)
        data1 = cursor.fetchall()
        data = []
        for p in data1:
            data.append(p)
            parent_path = p[7] + '.%'
            query = "SELECT * FROM post " \
                    "WHERE path LIKE %s ORDER BY path ASC"
            sql_data = (parent_path,)
            cursor.execute(query, sql_data)
            data2 = cursor.fetchall()
            for p1 in data2:
                data.append(p1)

    forum_cache = {}
    user_cache = {}
    for p in data:
        response.append({
            'id': p[0],
            'forum': forum_cache.setdefault(p[1], get_forum_by_id(p[1])[4]),
            'thread': int(thread_id),
            'user': user_cache.setdefault(p[3], get_user_by_id(p[3])[2]),
            'message': p[4],
            'date': p[5].strftime('%Y-%m-%d %H:%M:%S'),
            'parent': p[6],
            'isApproved': bool(p[8]),
            'isHighlighted': bool(p[9]),
            'isSpam': bool(p[10]),
            'isEdited': bool(p[11]),
            'isDeleted': bool(p[12]),
            'likes': p[13],
            'dislikes': p[14],
            'points': p[15],
        })

    t2 = datetime.datetime.now()
    print "ELAPSED: ", t2-t1

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

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

    if 'forum' in request.GET and 'user' in request.GET:
        return JsonResponse({
            'code': 3,
            'response': 'Provide only forum or user'
        })

    if "since" in request.GET:
        since = request.GET['since']
        try:
            since = datetime.datetime.strptime(since, '%Y-%m-%d %H:%M:%S')
        except ValueError:
            return JsonResponse({
                'code': 3,
                'response': 'Since id param is wrong'
            })
    else:
        since = 0

    if "limit" in request.GET:
        limit = request.GET['limit']
        try:
            limit = int(limit)
        except ValueError:
            return JsonResponse({
                'code': 3,
                'response': 'Limit param is wrong'
            })
    else:
        limit = None

    if "order" in request.GET:
        order = request.GET['order']
        if order != 'asc' and order != 'desc':
            return JsonResponse({
                'code': 3,
                'response': 'Order param is wrong'
            })
    else:
        order = 'desc'

    by_forum = 'forum' in request.GET

    cursor = connection.cursor()
    if by_forum:
        forum = request.GET.get('forum')
        forum_data = get_forum_by_shortname(cursor, forum)
        if not forum_data:
            cursor.close()
            return JsonResponse({
                'code': 1,
                'response': 'Forum not found'
            })
        search_by = forum_data[0]
    else:
        user_email = request.GET.get('user')
        user_data = get_user_by_email(cursor, user_email)
        if not user_data:
            cursor.close()
            return JsonResponse({
                'code': 1,
                'response': 'Thread not found'
            })
        search_by = user_data[0]

    sql = "SELECT * FROM thread WHERE date>=%s AND "

    sql += " forum_id = %s" if by_forum else " user_id = %s"
    sql += " ORDER BY date "
    sql += order

    if limit:
        sql += " LIMIT %s"
        cursor.execute(sql, (since, search_by, limit))
    else:
        cursor.execute(sql, (since, search_by))

    data = cursor.fetchall()

    for t in data:
        response.append({
            'user': get_user_by_id(t[4])[2] if by_forum else user_data[2],
            'forum': forum_data[4] if by_forum else get_forum_by_id(t[1])[4],
            'id': t[0],
            'title': t[2],
            'isClosed': bool(t[3]),
            'date': t[5].strftime('%Y-%m-%d %H:%M:%S'),
            'message': t[6],
            'slug': t[7],
            'isDeleted': bool(t[8]),
            'posts': t[12],
            'likes': t[10],
            'dislikes': t[9],
            'points': t[11],
        })

    return JsonResponse({
        'code': 0,
        'response': response
    })
Пример #6
0
def thread_vote(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 ('thread' in request_params and 'vote' in request_params):
        return JsonResponse({
                'code': 3,
                'response': 'Missing field'
            })

    thread_id = request_params.get('thread')

    thread_data = get_thread_by_id(thread_id)
    if not thread_data:
        return JsonResponse({
                'code': 1,
                'response': 'Thread not found'
            })

    vote = request_params.get('vote')

    try:
        vote = int(vote)
    except ValueError:
        return JsonResponse({
                'code': 3,
                'response': 'Wrong vote param'
            })
    if vote != 1 and vote != -1:
        return JsonResponse({
                'code': 3,
                'response': 'Wrong vote param'
            })

    cursor = connection.cursor()

    if vote == 1:
        cursor.execute("UPDATE thread SET likes=likes+1 WHERE id=%s", (thread_id,))
    else:
        cursor.execute("UPDATE thread SET dislikes=dislikes+1 WHERE id=%s", (thread_id,))

    cursor.execute("UPDATE thread SET points=points+%s WHERE id=%s", (vote, thread_id))

    thread_data = get_thread_by_id(thread_id)

    response.update({
        'user': get_user_by_id(thread_data[4])[2],
        'forum': get_forum_by_id(thread_data[1])[4],
        'id': thread_id,
        'title': thread_data[2],
        'isClosed': bool(thread_data[3]),
        'date': thread_data[5].strftime('%Y-%m-%d %H:%M:%S'),
        'message': thread_data[6],
        'slug': thread_data[7],
        'isDeleted': bool(thread_data[8]),
        'posts': thread_data[12],
        'likes': thread_data[10],
        'dislikes': thread_data[9],
        'points': thread_data[11],
    })

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

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

    thread_id = request.GET.get('thread')

    thread_data = get_thread_by_id(thread_id)
    if not thread_data:
        return JsonResponse({
            'code': 1,
            'response': 'Thread not found'
        })

    related = request.GET.getlist('related')
    forum_data = get_forum_by_id(thread_data[1])
    user_data = get_user_by_id(thread_data[4])

    for case in related:
        if case not in ['user', 'forum']:
            return JsonResponse({
                'code': 3,
                'response': 'Wrong related param'
            })

    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 'user' in related:
        followers, following = get_follow_data(user_data[0])
        subs = get_subscriptions(user_data[0])
        user_info = {
            'username': user_data[1],
            'email': user_data[2],
            'name': user_data[3],
            'about': user_data[4],
            'isAnonymous': user_data[5],
            'id': user_data[0],
            '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]

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

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

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

    if 'forum' in request.GET and 'thread' in request.GET:
        return JsonResponse({
            'code': 3,
            'response': 'Provide only forum or thread'
        })

    if "since" in request.GET:
        since = request.GET['since']
        try:
            since = datetime.datetime.strptime(since, '%Y-%m-%d %H:%M:%S')
        except ValueError:
            return JsonResponse({
                'code': 3,
                'response': 'Since id param is wrong'
            })
    else:
        since = 0

    if "limit" in request.GET:
        limit = request.GET['limit']
        try:
            limit = int(limit)
        except ValueError:
            return JsonResponse({
                'code': 3,
                'response': 'Limit param is wrong'
            })
    else:
        limit = None

    if "order" in request.GET:
        order = request.GET['order']
        if order != 'asc' and order != 'desc':
            return JsonResponse({
                'code': 3,
                'response': 'Order param is wrong'
            })
    else:
        order = 'desc'

    by_forum = 'forum' in request.GET

    search_by = None
    cursor = connection.cursor()

    if by_forum:
        forum = request.GET.get('forum')
        forum_data = get_forum_by_shortname(cursor, forum)
        if not forum_data:
            cursor.close()
            return JsonResponse({
                'code': 1,
                'response': 'Forum not found'
            })
        search_by = forum_data[0]
    else:
        thread_id = request.GET.get('thread')
        thread_data = get_thread_by_id(thread_id)
        if not thread_data:
            cursor.close()
            return JsonResponse({
                'code': 1,
                'response': 'Thread not found'
            })
        search_by = thread_id

    sql = "SELECT * FROM post WHERE date>=%s AND "

    sql += " forum_id = %s" if by_forum else " thread_id = %s"
    sql += " ORDER BY date "
    sql += order

    if limit:
        sql += " LIMIT %s"
        cursor.execute(sql, (since, search_by, limit))
    else:
        cursor.execute(sql, (since, search_by))

    data = cursor.fetchall()

    forum_cache = {}
    user_cache = {}
    for p in data:
        response.append({
            'id': int(p[0]),
            'forum': forum_data[4] if by_forum else forum_cache.setdefault(p[1], get_forum_by_id(p[1])[4]),
            'thread': p[2],
            'user': user_cache.setdefault(p[3], get_user_by_id(p[3])[2]),
            'message': p[4],
            'date': p[5].strftime('%Y-%m-%d %H:%M:%S'),
            'parent': p[6],
            'isApproved': bool(p[8]),
            'isHighlighted': bool(p[9]),
            'isSpam': bool(p[10]),
            'isEdited': bool(p[11]),
            'isDeleted': bool(p[12]),
            'likes': p[13],
            'dislikes': p[14],
            'points': p[15],
        })

    return JsonResponse({
        'code': 0,
        'response': response
    })
Пример #9
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
    })