Esempio n. 1
0
def get_list_of_threads(data, related):
    if related is None:
        related = []
    cursor = connection.cursor()
    code = Code
    if "since" in data:
        data["since"] = validate_date(data["since"])
    try:
        if "forum" in related:
            has_forum = True
        else:
            has_forum = False
        query = get_query_list_threads(data, has_forum)
        cursor.execute(query.get())
        if not cursor.rowcount:
            cursor.close()
            return {'code': code.OK, 'response': []}
    except Exception as e:
        print str(e)
        cursor.close()
        return {
            'code': code.UNKNOWN_ERROR,
            'response': 'failed select threads'
        }
    response = []
    for thread in cursor.fetchall():
        if has_forum:
            forum = {
                "id": thread[13],
                "name": thread[14],
                "short_name": thread[15],
                "user": thread[16]
            }
        else:
            forum = thread[11]

        if "user" in related:
            user = get_detail_user(thread[12])
        else:
            user = thread[12]

        response.append({
            "id": thread[0],
            "title": thread[1],
            "slug": thread[2],
            "message": thread[3],
            "date": str(thread[4]),
            "posts": thread[5],
            "likes": thread[6],
            "dislikes": thread[7],
            "points": thread[8],
            "isClosed": bool(thread[9]),
            "isDeleted": bool(thread[10]),
            "forum": forum,
            "user": user
        })

    return {'code': code.OK, 'response': response}
Esempio n. 2
0
def create(request):
    code = Code
    try:

        request_data = loads(request.body)

        data = {
            "date": str(request_data["date"]),
            "thread": request_data["thread"],
            "message": request_data["message"],
            "user": request_data["user"],
            "forum": request_data["forum"]
        }
    except Exception as e:
        print str(e)
        return HttpResponse(
            dumps({
                'code': code.NOT_VALID,
                "response": "failed loads json"
            }))

    data["date"] = validate_date(data["date"])
    if not data["date"]:
        return HttpResponse(
            dumps({
                'code': code.NOT_CORRECT,
                'response': 'incorrect date format'
            }))
    if not data["message"]:
        return HttpResponse(
            dumps({
                'code': code.NOT_CORRECT,
                'response': 'incorrect message format'
            }))

    ##################### optional arguments           #####################
    optional_args = [
        'isApproved', 'isDeleted', 'isEdited', 'isHighlighted', 'isSpam'
    ]
    for optional_arg_name in optional_args:
        optional_arg_value = request_data.get(optional_arg_name)
        if optional_arg_value is not None:
            # print optional_arg_name, optional_arg_value
            if not isinstance(optional_arg_value, bool):
                return HttpResponse(
                    dumps({
                        'code': code.NOT_CORRECT,
                        'response': 'optional flag should be bool'
                    }))
            data[optional_arg_name] = optional_arg_value

    parent_id = request_data.get("parent")
    if parent_id is not None:
        if isinstance(parent_id, int):
            data["parent"] = parent_id

    response = create_post(data)
    return HttpResponse(dumps(response))
Esempio n. 3
0
def get_list_of_posts(data):
    cursor = None
    code = Code
    if "since" in data:
        data["since"] = validate_date(data["since"])
    try:
        if data["sort"] == "parent_tree":
            response = get_list_posts_pt(data)
            return response
        elif data["sort"] == "tree":
            response = get_list_posts_t(data)
            return response
        else:
            cursor = connection.cursor()
            query = get_query_list_posts_by_forum(data, False, False)
            cursor.execute(query.get())
            if not cursor.rowcount:
                cursor.close()
                return {'code': code.OK,
                        'response': []}
    except Exception as e:
        print str(e)
        if cursor:
            cursor.close()
        return {'code': code.UNKNOWN_ERROR,
                'response': 'failed select posts'}
    response = []
    # return {'code': code.OK,
    #       'response': response}
    for post in cursor.fetchall():

        response.append({
            "id": post[0],
            "message": post[1],
            "date": str(post[2]),
            "isApproved": post[3],
            "isHighlighted": bool(post[4]),
            "isEdited": bool(post[5]),
            "isSpam": bool(post[6]),
            "isDeleted": bool(post[7]),
            "forum": post[8],
            "thread": post[9],
            "user": post[10],
            "dislikes": post[11],
            "likes": post[12],
            "points": post[13],
            "parent": post[14]
        })
    return {'code': code.OK,
            'response': response}
Esempio n. 4
0
def get_list(data):
    cursor = connection.cursor()
    code = Code
    if "since" in data:
        data["since"] = validate_date(data["since"])
    try:
        if data.get("forum"):
            query = get_query_list_threads(data, False)
        else:
            query = get_query_list_threads(data, False)
        cursor.execute(query.get())
        if not cursor.rowcount:

            cursor.close()
            return {'code': code.OK, 'response': []}
    except Exception as e:
        print str(e)
        cursor.close()
        return {
            'code': code.UNKNOWN_ERROR,
            'response': 'failed select threads'
        }
    response = []
    # return {'code': code.OK,
    #       'response': response}
    for thread in cursor.fetchall():
        response.append({
            "id": thread[0],
            "title": thread[1],
            "slug": thread[2],
            "message": thread[3],
            "date": str(thread[4]),
            "posts": thread[5],
            "likes": thread[6],
            "dislikes": thread[7],
            "points": thread[8],
            "isClosed": bool(thread[9]),
            "isDeleted": bool(thread[10]),
            "forum": thread[11],
            "user": thread[12]
        })
    return {'code': code.OK, 'response': response}
Esempio n. 5
0
def get_list_of_posts(data, related):
    if related is None:
        related = []
    cursor = connection.cursor()
    code = Code
    if "since" in data:
        data["since"] = validate_date(data["since"])
    try:
        if "forum" in related:
            has_forum = True
        else:
            has_forum = False
        if "thread" in related:
            has_thread = True
        else:
            has_thread = False
        query = get_query_list_posts_by_forum(data, has_forum, has_thread)
        cursor.execute(query.get())
        if not cursor.rowcount:
            cursor.close()
            return {'code': code.OK, 'response': []}
    except Exception as e:
        print str(e)
        cursor.close()
        return {'code': code.UNKNOWN_ERROR, 'response': 'failed select posts'}
    response = []
    for post in cursor.fetchall():
        if has_forum:
            number_of_columns_of_forum = 0
            forum = {
                "id": post[15],
                "name": post[16],
                "short_name": post[17],
                "user": post[18]
            }
        else:
            number_of_columns_of_forum = 4
            forum = post[8]

        if has_thread:
            i = number_of_columns_of_forum
            thread = {
                "id": post[19 - i],
                "title": post[20 - i],
                "slug": post[21 - i],
                "message": post[22 - i],
                "date": str(post[23 - i]),
                "posts": post[24 - i],
                "likes": post[25 - i],
                "dislikes": post[26 - i],
                "points": post[27 - i],
                "isClosed": bool(post[28 - i]),
                "isDeleted": bool(post[29 - i]),
                "forum": post[30 - i],
                "user": post[31 - i]
            }
        else:
            thread = post[9]

        if "user" in related:
            user = get_detail_user(post[10])
        else:
            user = post[10]

        response.append({
            "id": post[0],
            "message": post[1],
            "date": str(post[2]),
            "isApproved": post[3],
            "isHighlighted": bool(post[4]),
            "isEdited": bool(post[5]),
            "isSpam": bool(post[6]),
            "isDeleted": bool(post[7]),
            "forum": forum,
            "thread": thread,
            "user": user,
            "dislikes": post[11],
            "likes": post[12],
            "points": post[13],
            "parent": post[14]
        })
    return {'code': code.OK, 'response': response}
Esempio n. 6
0
def create_thread(data):
    cursor = connection.cursor()
    code = Code

    ########### user verification ##############

    try:
        query = get_query_id_user_by_email(data["user"])
        cursor.execute(query.get())
    except Exception as e:
        print str(e)
        cursor.close()
        return {'code': code.UNKNOWN_ERROR, "response": "select user failed"}

    if not cursor.rowcount:
        cursor.close()
        return {'code': code.NOT_FOUND, 'response': 'user not found'}

    user_id = cursor.fetchone()[0]

    ###########  forum verification ##############

    try:
        query = get_query_id_forum_by_short_name(data["forum"])
        cursor.execute(query.get())

    except Exception as e:
        print str(e)
        cursor.close()
        return {'code': code.UNKNOWN_ERROR, "response": "select forum failed"}

    if not cursor.rowcount:
        cursor.close()
        return {'code': code.NOT_FOUND, 'response': 'forum not found'}
    forum_id = cursor.fetchone()[0]

    ##################### validate arguments  #####################

    data["date"] = validate_date(data["date"])
    if not data["date"]:
        cursor.close()
        return {'code': code.NOT_CORRECT, 'response': 'incorrect date format'}

    if not data["message"]:
        cursor.close()
        return {
            'code': code.NOT_CORRECT,
            'response': 'incorrect message format'
        }

    if not data["slug"]:
        cursor.close()
        return {
            'code': code.NOT_CORRECT,
            'response': 'slug should not be empty'
        }

    if not data["title"]:
        cursor.close()
        return {
            'code': code.NOT_CORRECT,
            'response': 'title should not be empty'
        }

    ######################## insert without optional arguments ###################

    try:
        query.clear()
        query.add_insert("thread", data.items())
        cursor.execute(query.get())

    except Exception as e:
        print str(e)
        cursor.close()
        return {'code': code.UNKNOWN_ERROR, "response": "insert failed"}
    try:
        query.clear()
        query.select_last_insert_id()
        cursor.execute(query.get())

        thread_id = cursor.fetchone()[0]
    except Exception as e:
        print str(e)
        cursor.close()
        return {
            'code': code.UNKNOWN_ERROR,
            "response": "select last id failed"
        }

        ##################### optional arguments / remove thread  #####################
    try:
        if isinstance(data["is_deleted"], bool):
            try:
                query = get_query_for_remove_thread(forum_id,
                                                    data["is_deleted"])
                cursor.execute(query.get())

            except:
                cursor.close()
                return {
                    'code': code.UNKNOWN_ERROR,
                    "response": "remove thread failed"
                }
    except Exception as e:
        print str(e)
        data["is_deleted"] = 0

    ##################### response #####################
    response = {
        "date": data["date"],
        "forum": data["forum"],
        "id": thread_id,
        "isClosed": data["isClosed"],
        "isDeleted": data["is_deleted"],
        "message": data["message"],
        "slug": data["slug"],
        "title": data["title"],
        "user": data["user"]
    }

    cursor.close()
    return {'code': code.OK, "response": response}