예제 #1
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)
예제 #2
0
def user_details(request):
    if not request.method == 'GET':
        return JsonResponse({
            'code': 2,
            'response': 'Method in not supported'
        })

    user_email = request.GET.get('user', None)

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

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

    followers, following = get_follow_data(user_data[0])
    subs = get_subscriptions(user_data[0])

    response = {
        'id': user_data[0],
        'username': user_data[1],
        'email': user_data[2],
        'name': user_data[3],
        'about': user_data[4],
        'isAnonymous': user_data[5],
        'followers': [
            f[0] for f in followers
        ],
        'following': [
            f[0] for f in following
        ],
        'subscriptions': [
            s[0] for s in subs
        ]
    }

    return JsonResponse({'code': 0, 'response': response})
예제 #3
0
def user_follow(request):
    response = {}
    if not request.method == 'POST':
        return JsonResponse({
            'code': 2,
            'response': 'Method in not supported'
        })

    try:
        request_params = json.loads(request.body)
        follower = request_params.get('follower', None)
        followee = request_params.get('followee', None)

        if not (followee and follower):
            return JsonResponse({
                'code': 3,
                'response': 'Missing field'
            })

        cursor = connection.cursor()

        follower_user = get_user_by_email(cursor, follower)
        followee_user = get_user_by_email(cursor, followee)

        if not (followee_user and follower_user):
            return JsonResponse({
                'code': 1,
                'response': 'User does not exist'
            })

        sql = "INSERT IGNORE INTO user_user_follow VALUES (null, %s, %s);"
        cursor.execute(sql, (follower_user[0], followee_user[0]))

        followers, following = get_follow_data(follower_user[0])
        subs = get_subscriptions(follower_user[0])

        response = {
            'id': follower_user[0],
            'username': follower_user[1],
            'email': follower_user[2],
            'name': follower_user[3],
            'about': follower_user[4],
            'isAnonymous': follower_user[5],
            'followers': [
                f[0] for f in followers
            ],
            'following': [
                f[0] for f in following
            ],
            'subscriptions': [
                s[0] for s in subs
            ]

        }


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

    return JsonResponse({'code': 0, 'response':response})
예제 #4
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,
    })
예제 #5
0
def user_update_profile(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 ('name' in request_params and 'about' in request_params and 'user' in request_params):
            return JsonResponse({
                'code': 3,
                'response': 'Missing field'
            })

        user_email = request_params.get('user')
        about = request_params.get('about')
        name = request_params.get('name')

        cursor = connection.cursor()

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

        sql = "UPDATE user SET name = %s, about = %s WHERE email = %s"
        cursor.execute(sql, (name, about, user_email))

        followers, following = get_follow_data(user_data[0])
        subs = get_subscriptions(user_data[0])

        response.update({
            'name': name,
            'about': about,
            'email': user_email,
            'isAnonymous': user_data[5],
            'username': user_data[1],
            'id': user_data[0],
            'followers': [
                f for f in followers
            ],
            'following': [
                f for f in following
            ],
            'subscriptions': [
                s for s in subs
            ]
        })

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

    return JsonResponse({
        'code': 0,
        'response': response,
    })
예제 #6
0
def user_list_following(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'
        })

    if "since_id" in request.GET:
        since_id = request.GET['since_id']
        try:
            since_id = int(since_id)
        except ValueError:
            return JsonResponse({
                'code': 3,
                'response': 'Since id param is wrong'
            })
    else:
        since_id = 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'

    cursor = connection.cursor()

    user_data = get_user_by_email(cursor, request.GET.get('user'))
    if not user_data:
        return JsonResponse({
            'code': 1,
            'response': 'User does not exist'
        })


    sql = """SELECT u.id, u.username, u.email, u.name, u.about, u.is_anonymous
             FROM user_user_follow
             LEFT JOIN user u ON to_user_id = u.id
             WHERE from_user_id = %s AND to_user_id>=%s
             ORDER BY u.name """

    sql += order

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

    followers = cursor.fetchall()

    for f in followers:
        followers, following = get_follow_data(f[0])
        subs = get_subscriptions(f[0])
        response.append({
            'username': f[1],
            'email': f[2],
            'name': f[3],
            'about': f[4],
            'isAnonymous': f[5],
            'id': f[0],
            'followers': [
                follower[0] for follower in followers
            ],
            'following': [
                follower[0] for follower in following
            ],
            'subscriptions': [
                s[0] for s in subs
            ]
        })

    return JsonResponse({
        'code': 0,
        'response': response
    })
예제 #7
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
    })
예제 #8
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
    })
예제 #9
0
def thread_unsubscribe(request):
    response = {}
    if not request.method == 'POST':
        return JsonResponse({
            'code': 2,
            'response': 'Method in not supported'
        })

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

        user_email = request_params.get('user', None)
        thread_id = request_params.get('thread', None)

        if not (user_email and thread_id):
            return JsonResponse({
                'code': 3,
                'response': 'Missing field'
            })

        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'
            })

        try:
            thread_id = int(thread_id)
        except ValueError:
            cursor.close()
            return JsonResponse({
                'code': 3,
                'response': 'Wrong thread id param'
            })

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

        sql_raw = "DELETE FROM subscriptions WHERE thread_id = '{0}' AND user_id = '{1}';"
        sql = sql_raw.format(thread_id, user_data[0])
        cursor.execute(sql)

        response.update({
            'user': user_data[2],
            'thread': thread_id,
        })

        cursor.close()

    except ValueError:
        return JsonResponse({
            'code': 3,
            'response': 'No JSON object could be decoded'
        })
    return JsonResponse({
        'code': 0,
        'response': response
    })
예제 #10
0
def post_create(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 ('date' in request_params and 'thread' in request_params and 'message' in request_params
        and 'user' in request_params and 'forum' in request_params):
        return JsonResponse({
            'code': 3,
            'response': 'Missing field'
        })

    date = request_params.get('date')
    thread_id = request_params.get('thread')
    message = request_params.get('message')
    user_email = request_params.get('user')
    forum_name = request_params.get('forum')

    cursor = connection.cursor()

    # Optional params:
    if 'isApproved' in request_params:
        is_approved = request_params.get('isApproved')

        if type(is_approved) is not bool:
            cursor.close()
            return JsonResponse({
                'code': 3,
                'response': 'Wrong isApproved parameter type'
            })
    else:
        is_approved = False

    if 'isHighlighted' in request_params:
        is_highlited = request_params.get('isHighlighted')

        if type(is_highlited) is not bool:
            cursor.close()
            return JsonResponse({
                'code': 3,
                'response': 'Wrong isHighlited parameter type'
            })
    else:
        is_highlited = False

    if 'isEdited' in request_params:
        is_edited = request_params.get('isEdited')

        if type(is_edited) is not bool:
            cursor.close()
            return JsonResponse({
                'code': 3,
                'response': 'Wrong isEdited parameter type'
            })
    else:
        is_edited = False

    if 'isSpam' in request_params:
        is_spam = request_params.get('isSpam')

        if type(is_spam) is not bool:
            cursor.close()
            return JsonResponse({
                'code': 3,
                'response': 'Wrong isSpam parameter type'
            })
    else:
        is_spam = False

    if 'isDeleted' in request_params:
        is_deleted = request_params.get('isDeleted')

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

    if 'parent' in request_params:
        parent = request_params.get('parent')
    else:
        parent = None

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

    cursor.execute("SELECT id, posts FROM thread WHERE id=%s AND forum_id=%s", (thread_id, forum_data[0]))
    thread_data = cursor.fetchone()
    if not thread_data:
        cursor.close()
        return JsonResponse({
            'code': 1,
            'response': 'Thread does not exist or no such thread in provided forum'
        })

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

    if not parent:
        posts_count = int(thread_data[1])
        post_path = '{0:011d}'.format(posts_count + 1)

    else:
        try:
            parent = int(parent)
        except ValueError:
            cursor.close()
            return JsonResponse({
                'code': 3,
                'response': 'Wrong parent parameter type'
            })

        cursor.execute("SELECT path FROM post WHERE id = %s AND thread_id = %s", (parent,thread_id))

        parent_post_data = cursor.fetchone()
        if not parent_post_data:
            cursor.close()
            return JsonResponse({
                'code': 1,
                'response': 'Post not found or post required is in another thread'
            })

        cursor.execute("SELECT COUNT(*) FROM post WHERE parent = %s", (parent,))

        parent_childs = int(cursor.fetchone()[0])
        post_path = parent_post_data[0] + '.' + '{0:011d}'.format(parent_childs + 1)


    sql = "INSERT INTO post VALUES (null, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 0, 0, 0)"

    insert_data = (forum_data[0], thread_data[0], user_data[0], message, date, parent, post_path, is_approved,
                   is_highlited, is_spam, is_edited, is_deleted)

    cursor.execute(sql, insert_data)

    response.update({
        'id': cursor.lastrowid,
        'forum': forum_name,
        'thread': thread_id,
        'user': user_email,
        'message': message,
        'parent': parent,
        'isSpam': is_spam,
        'isHighlighted': is_highlited,
        'isEdited': is_edited,
        'isApproved': is_approved,
        'date': date,
    })

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

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