Exemplo n.º 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
    })
Exemplo n.º 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
    })
Exemplo n.º 3
0
def forum_create(request):
    response = {}
    if request.method == "POST":
        try:
            data = json.loads(request.body)

            name = data.get("name", None)
            short_name = data.get("short_name", None)
            user = data.get("user", None)

            if not (name and user and short_name):
                response.update({"code": 3, "response": "Missing field"})
                return JsonResponse(response)

            cursor = connection.cursor()

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

            sql = "SELECT id, owner_id FROM forum WHERE name = %s"
            cursor.execute(sql, (name,))

            sql_response = cursor.fetchone()
            if sql_response:
                response["code"] = 0
                response["response"] = {
                    "id": sql_response[0],
                    "name": name,
                    "short_name": short_name,
                    "user": get_user_by_id(sql_response[1])[2],
                }
                cursor.close()
                return JsonResponse(response)

            sql = "INSERT INTO forum (name, short_name, owner_id) VALUES (%s, %s, %s);"

            cursor.execute(sql, (name, short_name, user_data[0]))

            response["code"] = 0
            response["response"] = {"id": cursor.lastrowid, "name": name, "short_name": short_name, "user": user}

            cursor.close()

        except ValueError:
            response = {"code": 3, "response": "No JSON object could be decoded"}
        except Exception:
            response = {"code": 4, "response": "Unknown error occured"}

    else:
        response.update({"code": 2, "response": "Method is unsupported"})
    return JsonResponse(response)
Exemplo n.º 4
0
def forum_details(request):
    response = {}
    if not request.method == "GET":
        return JsonResponse({"code": 2, "response": "Method in not supported"})

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

    forum = request.GET.get("forum")

    cursor = connection.cursor()
    forum_data = get_forum_by_shortname(cursor, forum)
    cursor.close()
    if not forum_data:
        return JsonResponse({"code": 1, "response": "Forum does not exist"})

    user_data = get_user_by_id(forum_data[3])

    if "related" in request.GET:
        if request.GET["related"] != "user":
            return JsonResponse({"code": 3, "response": "Wrong related parameter"})

        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 = {"id": forum_data[0], "name": forum_data[1], "short_name": forum_data[4], "user": user_info}

    return JsonResponse({"code": 0, "response": response})
Exemplo n.º 5
0
def thread_create(request):
    response = {}
    if not request.method == 'POST':
        return JsonResponse({
            'code': 2,
            'response': 'Method in not supported'
        })

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

        forum = request_params.get('forum', None)
        title = request_params.get('title', None)
        user = request_params.get('user', None)
        message = request_params.get('message', None)
        slug = request_params.get('slug', None)
        date = request_params.get('date', None)

        is_deleted = request_params.get('isDeleted', False)
        if type(is_deleted) is not bool:
            return JsonResponse({
                'code': 3,
                'response': 'Wrong isDeleted parameter type'
            })

        if not 'isClosed' in request_params or not (forum and title and user and message and slug and date):
            return JsonResponse({
                'code': 3,
                'response': 'Missing field'
            })

        is_closed = request_params.get('isClosed')
        if type(is_closed) is not bool:
            return JsonResponse({
                'code': 3,
                'response': 'Wrong isClosed parameter type'
            })

        try:
            datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
        except ValueError:
            return JsonResponse({
                'code': 3,
                'response': 'Wrong date format'
            })

        cursor = connection.cursor()

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

        forum_data = get_forum_by_shortname(cursor, forum)
        if not forum_data:
            return JsonResponse({
                'code': 1,
                'response': 'Forum not found'
            })

        sql_select = "SELECT * FROM thread WHERE title = %s AND forum_id = %s"

        cursor.execute(sql_select, (title, forum_data[0]))
        sql_response = cursor.fetchone()

        if sql_response:
            response = {
                'title': title,
                'forum': forum_data[4],
                'message': sql_response[6],
                'slug': sql_response[7],
                'isClosed': bool(sql_response[3]),
                'isDeleted': bool(sql_response[8]),
                'id': sql_response[0],
                'date': sql_response[5].strftime('%Y-%m-%d %H:%M:%S'),
                'user': get_user_by_id(sql_response[4])[2]

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

        sql_insert = "INSERT INTO thread VALUES (null, %s, %s, %s, %s, %s, %s, %s, %s, 0, 0, 0, 0)"
        sql_insert_data = (forum_data[0], title, is_closed, user_data[0], date, message, slug, is_deleted)

        cursor.execute(sql_insert, sql_insert_data)

        response.update({
            'title': title,
            'forum': forum_data[4],
            'message': message,
            'slug': slug,
            'isClosed': is_closed,
            'isDeleted': is_deleted,
            'id': cursor.lastrowid,
            'date': date,
            'user': user_data[2]
        })

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

    return JsonResponse({
        'code': 0,
        'response': response
    })
Exemplo n.º 6
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
    })
Exemplo n.º 7
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
    })
Exemplo n.º 8
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
    })
Exemplo n.º 9
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,
    })
Exemplo n.º 10
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
    })
Exemplo n.º 11
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
    })
Exemplo n.º 12
0
def forum_list_posts(request):
    response = []
    if not request.method == "GET":
        return JsonResponse({"code": 2, "response": "Method in not supported"})

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

    forum = request.GET.get("forum")
    cursor = connection.cursor()

    forum_data = get_forum_by_shortname(cursor, forum)
    if not forum_data:
        cursor.close()
        return JsonResponse({"code": 1, "response": "Forum 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:
            cursor.close()
            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:
            cursor.close()
            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":
            cursor.close()
            return JsonResponse({"code": 3, "response": "Order param is wrong"})
    else:
        order = "desc"

    user_related = False
    forum_related = False
    thread_related = False

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

        for r in related:
            if r != "forum" and r != "user" and r != "thread":
                cursor.close()
                return JsonResponse({"code": 3, "response": "Wrong related params"})
        if "forum" in related:
            forum_related = True
        if "thread" in related:
            thread_related = True
        if "user" in related:
            user_related = True

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

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

    posts = cursor.fetchall()

    for p in posts:
        if forum_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]

        thread_data = get_thread_by_id(p[2])
        if thread_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]

        user_data = get_user_by_id(p[3])
        if user_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]

        response.append(
            {
                "id": p[0],
                "forum": forum_info,
                "thread": thread_info,
                "user": user_info,
                "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],
            }
        )

    cursor.close()
    return JsonResponse({"code": 0, "response": response})