示例#1
0
文件: content.py 项目: JonasRSV/delta
def annotate():
    session_token    = None
    annotate_request = None

    TARGET_POST      = None
    TARGET_COMMENT   = None
    BEGINING         = None
    ENDING           = None
    COLOR            = None
    try:
        request_data = flask.request.get_data().decode("utf-8")
        request_data = json.loads(request_data)

        session_token     = request_data["token"]
        annotate_request  = request_data["annotation"]

        TARGET_POST       = request_data["annotation"]["target_post"]
        TARGET_COMMENT    = request_data["annotation"]["target_comment"]
        BEGINING          = request_data["annotation"]["begining"]
        ENDING            = request_data["annotation"]["ending"]
        COLOR             = request_data["annotation"]["color"]

    except Exception as e:
        config.write_server_log("Unable to parse annotation request: {}".format(str(e)))
        response = { "success": False
                   , "expired": False
                   , "annotation": None
                   , "id": None}
        response = json.dumps(response)
        return flask.Response(response, status=500, mimetype="application/json")

    try:
        OWNER, expired = config.decode_token(session_token)

        if expired:
            config.write_server_log("Unable to insert annotation, expired token")

            response = { "success": False
                       , "expired": True
                       , "annotation": annotate_request
                       , "id": None}
            response = json.dumps(response)
            return flask.Response(response, status=500, mimetype="application/json")

    except Exception as e:
        config.write_server_log("Unable to decode token: {}".format(str(e)))

        response = { "success": False
                   , "expired": None
                   , "annotation": annotate_request
                   , "id": None}
        response = json.dumps(response)
        return flask.Response(response, status=500, mimetype="application/json")

    query = connection.Request( "insert_annotation"
                              , INSERT_ANNO
                              , ( TARGET_POST
                                , TARGET_COMMENT
                                , BEGINING
                                , ENDING
                                , COLOR)
                              , fetcher=lambda c: c.fetchone())

    anno_id = None
    try:
        anno_id = DB.request(query).data[0]
    except Exception as e:
        config.write_server_log("Unable to insert annotation: {}".format(str(e)))

        response = { "success": False
                   , "expired": False
                   , "annotate_request": annotate_request
                   , "id": None}
        response = json.dumps(response)
        return flask.Response(response, status=500, mimetype="application/json")


    response = { "success": True
               , "expired": False
               , "annotation": annotate_request
               , "id": anno_id}

    response = json.dumps(response)
    return flask.Response(response, status=200, mimetype="application/json")
示例#2
0
文件: content.py 项目: JonasRSV/delta
def comment():
    session_token   = None
    comment_request = None

    TARGET_POST     = None
    PARENT_COMMENT  = None
    OWNER           = None
    CONTENT         = None
    TIME            = str(datetime.datetime.now())

    try:
        request_data = flask.request.get_data().decode("utf-8")
        request_data = json.loads(request_data)

        session_token    = request_data["token"]
        comment_request  = request_data["comment"]

        TARGET_POST      = request_data["comment"]["target_id"]
        PARENT_COMMENT   = request_data["comment"]["parent"]
        CONTENT          = request_data["comment"]["content"]

    except Exception as e:
        config.write_server_log("Unable to parse comment request: {}".format(str(e)))

        response = { "success": False
                   , "expired": False
                   , "comment": None
                   , "id": None}
        response = json.dumps(response)
        return flask.Response(response, status=500, mimetype="application/json")

    try:
        OWNER, expired = config.decode_token(session_token)

        if expired:
            config.write_server_log("Unable to insert comment, expired token")

            response = { "success": False
                       , "expired": True
                       , "comment": comment_request
                       , "id": None}
            response = json.dumps(response)
            return flask.Response(response, status=500, mimetype="application/json")

    except Exception as e:
        config.write_server_log("Unable to decode token: {}".format(str(e)))

        response = { "success": False
                   , "expired": None
                   , "comment": comment_request
                   , "id": None}
        response = json.dumps(response)
        return flask.Response(response, status=500, mimetype="application/json")

    query = connection.Request("insert_comment"
                              , INSERT_COMMENT
                              , ( TARGET_POST
                                , PARENT_COMMENT
                                , OWNER
                                , psycopg2Json(CONTENT)
                                , TIME)
                              , fetcher=lambda c: c.fetchone())

    comment_id = None
    try:
        comment_id = DB.request(query).data[0]
    except Exception as e:
        config.write_server_log("Unable to insert comment: {}".format(str(e)))

        response = { "success": False
                   , "expired": False
                   , "comment": comment_request
                   , "id": None}
        response = json.dumps(response)
        return flask.Response(response, status=500, mimetype="application/json")

    response = { "success": True
               , "expired": False
               , "comment": comment_request
               , "id": comment_id }


    response = json.dumps(response)
    return flask.Response(response, status=200, mimetype="application/json")
示例#3
0
文件: content.py 项目: JonasRSV/delta
def create_post():
    session_token    = None
    post_information = None

    TIME             = str(datetime.datetime.now())
    TITLE            = None
    CONTENT          = None
    OWNER            = None
    TAGS             = None

    try:
        request_data = flask.request.get_data().decode("utf-8")
        request_data = json.loads(request_data)

        session_token     = request_data["token"]
        post_information  = request_data["post"]

        TITLE             = request_data["post"]["title"]
        CONTENT           = request_data["post"]["content"]
        TAGS              = request_data["post"]["tags"]
    except Exception as e:
        config.write_server_log("Unable to parse post request: {}".format(str(e)))

        response = { "success": False
                   , "expired": None
                   , "post": None
                   , "id": None }

        response = json.dumps(response)
        return flask.Response(response, status=500, mimetype="application/json")

    try: 
        OWNER, expired = config.decode_token(session_token)


        if expired:
            response = { "success": False
                       , "expired": True
                       , "post": post_information
                       , "id": None }

            response = json.dumps(response)
            return flask.Response(response, status=500, mimetype="application/json")
    except Exception as e:
        config.write_server_log("Unable to decode token {}".format(str(e)))

        response = { "success": False
                   , "expired": None
                   , "post": post_information
                   , "id": None }

        response = json.dumps(response)
        return flask.Response(response, status=500, mimetype="application/json")



    query = connection.Request( "create_post"
                              , INSERT_POST
                              , (TIME, TITLE, psycopg2Json(CONTENT), OWNER)
                              , lambda c: c.fetchone())
    post_id = None
    try:
        post_id = DB.request(query).data[0]

        for tag in TAGS:
            query = connection.Request( "create_tag"
                                      , INSERT_TAG
                                      , (tag, post_id))
            DB.request(query)
    except Exception as e:
        config.write_server_log("Unable to create post, error: {}".format(str(e)))

        response = { "success": False
                   , "expired": False
                   , "post": post_information
                   , "id": None }

        response = json.dumps(response)
        return flask.Response(response, status=500, mimetype="application/json")
        
    response = { "success": True
               , "expired": False
               , "post": post_information 
               , "id": post_id}

    response = json.dumps(response)
    return flask.Response(response, status=200, mimetype="application/json")
示例#4
0
文件: content.py 项目: JonasRSV/delta
def like():
    session_token   = None
    like_request    = None

    TARGET_ID       = None
    TYPE            = None
    OWNER           = None
    STATE           = None
    
    try:
        request_data = flask.request.get_data().decode("utf-8")
        request_data = json.loads(request_data)

        session_token = request_data["token"]
        like_request  = request_data["like"]

        TARGET_ID     = request_data["like"]["target_id"]
        TYPE          = request_data["like"]["type"]
        STATE         = request_data["like"]["state"]

    except Exception as e:
        config.write_server_log("Unable to parse like request: {}".format(str(e)))

        response = {"success": False, "expired": False, "like": None}
        response = json.dumps(response)
        return flask.Response(response, status=500, mimetype="application/json")

    try:
        OWNER, expired = config.decode_token(session_token)

        if expired:
            config.write_server_log("Unable to like because session token expired")
            
            response = { "success": False, "expired": True, "like": like_request }
            response = json.dumps(response)

            return flask.Response(response, status=500, mimetype="application/json")
    except Exception as e:
        config.write_server_log("Unable to decode token: {}".format(str(e)))

        response = { "success": False, "expired": None, "like": like_request }
        response = json.dumps(response)

        return flask.Response(response, status=500, mimetype="application/json")


    likeables = { "comment": connection.Request( "comment_like"
                                               , LIKE_COMMENT 
                                               , (TARGET_ID, OWNER, STATE))
                , "post": connection.Request( "post_like"
                                            , LIKE_POST
                                            , (TARGET_ID, OWNER, STATE))
                }

    if TYPE not in likeables:
        config.write_server_log("{} is not likeable".format(TYPE))

        response = { "success": False, "expired": False, "like": like_request }
        response = json.dumps(response)

        return flask.Response(response, status=500, mimetype="application/json")

    query = likeables[TYPE]
    try:
        DB.request(query)
    except Exception as e:
        config.write_server_log("Unable to like, error: {}".format(str(e)))

        response = { "success": False , "expired": False , "like": like_request }
        response = json.dumps(response)
        return flask.Response(response, status=500, mimetype="application/json")

    response = { "success": True, "expired": False, "like": like_request }
    response = json.dumps(response)
    return flask.Response(response, status=200, mimetype="application/json")
示例#5
0
def logout():
    session_token = None
    try:
        session_token = flask.request.get_data().decode("utf-8")
        session_token = json.loads(session_token)
        session_token = session_token["token"]
    except Exception as e:
        config.write_server_log("Unable to parse logout request: {}".format(
            str(e)))

        response = {
            "success": False,
            "token": None,
            "message": "Malformed request"
        }
        response = json.dumps(response)
        return flask.Response(response,
                              status=500,
                              mimetype="application/json")

    user_id = None
    try:
        user_id, expired = config.decode_token(session_token)
    except Exception as e:
        config.write_server_log("Invalid Token, Cannot Log Out: {}".format(
            str(e)))

        response = {
            "success": False,
            "token": token,
            "message": "Logout Failed"
        }
        response = json.dumps(response)
        return flask.Response(response,
                              status=500,
                              mimetype="application/json")

    if not expired:
        query = connection.Request("update_token", INSERT_TOKEN,
                                   ("None", user_id))

        try:
            DB.request(query)
        except Exception as e:
            config.write_server_log("Failed to Logout, Session Update Failed: {}"\
                    .format(str(e)))

            response = {
                "success": False,
                "token": token,
                "message": "Logout Failed"
            }
            response = json.dumps(response)
            return flask.Response(response,
                                  status=500,
                                  mimetype="application/json")

    response = {
        "success": True,
        "token": None,
        "message": "Thank you! Come again!"
    }
    response = json.dumps(response)
    return flask.Response(response, status=200, mimetype="application/json")