Example #1
0
def insert_thread(id, forum_id, title, creator, timestamp):
    db = myDb.get_db(DATABASE)
    #initializing threads into the shards
    db.row_factory = myDb.dict_factory
    con = db.cursor()
    tmp = datetime.datetime.strptime(timestamp, "%a, %d %b %Y %H:%M:%S %Z")
    con.execute('INSERT INTO threads VALUES(?,?,?,?,?)',
                (id, forum_id, title, creator, tmp))
    db.commit()
    myDb.teardown_db(DATABASE)
Example #2
0
def create_threads(forum_id):

    req_data = request.get_json()

    b_auth = myAuthorizor()

    #gets input from user
    username = request.authorization['username']
    password = request.authorization['password']

    #gets json input
    threadTitle = req_data['title']
    text = req_data['text']

    # Create the timestamp
    ts = time.time()
    time_stamp = datetime.datetime.fromtimestamp(ts).strftime(
        '%a, %d %b %Y %H:%M:%S %Z')
    tmp = time_stamp + "GMT"
    time_stamp = datetime.datetime.strptime(tmp, '%a, %d %b %Y %H:%M:%S %Z')
    #If authorized user, insert
    if (b_auth.check_credentials(username, password, DATABASE)):
        if forum_id_found(int(forum_id)):
            db = myDb.get_db(DATABASE)
            db.row_factory = myDb.dict_factory
            con = db.cursor()
            # gets the top id
            threadsList = con.execute(
                'Select * FROM threads WHERE forum_id = ? ORDER BY id DESC LIMIT 0,1',
                (forum_id)).fetchall()
            for thread in threadsList:
                currentId = int(thread['id'])

        # inserting new items
            con.execute(
                'INSERT INTO threads(id,forum_id,title,creator,time_created) VALUES(?,?,?,?,?)',
                (currentId + 1, forum_id, threadTitle, username, time_stamp))
            db.commit()
            db.close()

            insert_post(forum_id, currentId + 1, text, username, tmp)
            response = Response("HTTP 201 Created\n" +
                                "Location header field set to /forums/" +
                                forum_id + "/" + str(currentId + 1) +
                                " for new thread.",
                                201,
                                mimetype='application/json')
            response.headers['Location'] = "/forums/" + forum_id + "/" + str(1)
        else:
            invalMsg = "HTTP 404 Not Found"
            response = Response(invalMsg, 404, mimetype='application/json')
    else:
        invalMsg = "HTTP 401 Not Authorized"
        response = Response(invalMsg, 401, mimetype='application/json')
    return response
Example #3
0
def change_password(username):
    db = myDb.get_db(DATABASE)
    db.row_factory = myDb.dict_factory
    con = db.cursor()

    check_user = con.execute('SELECT * FROM auth_users WHERE username= "******"').fetchall()

    if len(check_user) == 0:
        ## RETURN 404 - USER NOT FOUND
        message = {
            'status': 404,
            'message': 'User Not Found: ' + username,
        }
        resp = jsonify(message)
        resp.status_code = 404

        return resp

    elif request.authorization['username'] != username:
        message = {
            'status':
            409,
            'message':
            'Username: '******' does not match authorized user: '******'username'],
        }
        resp = jsonify(message)
        resp.status_code = 409

        return resp

    user_update = request.get_json()

    con.execute('UPDATE auth_users SET password="******" WHERE username="******"')
    updated_user = con.execute('SELECT * FROM auth_users WHERE username="******"').fetchall()

    print("*****Checking Credentials*****")
    print(check_user)
    print(updated_user)
    print("*****Checking Credentials*****")

    db.commit()

    return jsonify(updated_user)
Example #4
0
def post_forums():

    b_auth = myAuthorizor()
    db = myDb.get_db(DATABASE)
    db.row_factory = myDb.dict_factory
    conn = db.cursor()

    #pulls all forums and makes it to a json obj
    #all_forums = conn.execute('SELECT * FROM forums').fetchall()
    req_data = request.get_json()

    #checks for valid forum entry
    if (check_validForum(req_data, DATABASE)):

        #gets input from user
        username = request.authorization['username']
        password = request.authorization['password']

        #checks to see if user has proper auth
        if b_auth.check_credentials(username, password, DATABASE):
            #inserts into the database
            forumName = req_data['name']
            conn.execute('INSERT INTO forums(name,creator) VALUES(?,?)',
                         (forumName, username))
            db.commit()

            #returns a success response
            response = Response("HTTP 201 Created\n" +
                                "Location header field set to /forums/" +
                                forumName + " for new forum.",
                                201,
                                mimetype='application/json')
            response.headers['Location'] = "/forums/" + forumName
        else:
            invalMsg = "User not authenticated"
            response = Response(invalMsg, 409, mimetype='application/json')
    #if th conflict exisits return an error message
    else:
        invalMsg = "HTTP 409 Conflict if forum already exists"
        response = Response(invalMsg, 409, mimetype='application/json')

    db.close()
    return response
Example #5
0
def users():
    db = myDb.get_db(DATABASE)
    db.row_factory = myDb.dict_factory
    conn = db.cursor()

    req_data = request.get_json()
    newUser = req_data['username']
    newPass = req_data['password']

    if valid_username(newUser):
        conn.execute('INSERT INTO auth_users(username,password) VALUES(?,?)',
                     (newUser, newPass))
        db.commit()
        #returns a success response
        response = Response("HTTP 201 Created",
                            201,
                            mimetype='application/json')
    else:
        #returns a success response
        response = Response("HTTP 409 Conflict if username already exists\n",
                            409,
                            mimetype='application/json')
    return response
Example #6
0
def create_post(forum_id, thread_id):

    #Check to see if forum and thread exists
    forumCheck = check_forum(forum_id)
    threadCheck = check_thread(thread_id, forum_id)

    req_data = request.get_json()
    b_auth = myAuthorizor()
    check_user = request.authorization['username']
    check_pw = request.authorization['password']
    authed = b_auth.check_credentials(check_user, check_pw, DATABASE)

    shard_num = int(thread_id) % 3
    if shard_num == 0:
        print("Shard number: " + str(shard_num) + " selected.")
        db = myDb.get_db_s0()
    elif shard_num == 1:
        print("Shard number: " + str(shard_num) + " selected.")
        db = myDb.get_db_s1()
    elif shard_num == 2:
        print("Shard number: " + str(shard_num) + " selected.")
        db = myDb.get_db_s2()

    db.row_factory = myDb.dict_factory
    con = db.cursor()

    dataUUID = str(uuid.uuid4())

    if forumCheck == True and threadCheck == True:
        check_posts = con.execute('SELECT * FROM posts').fetchall()
        # Get the post text
        post_text = req_data['text']

        # Create the timestamp
        ts = time.time()
        time_stamp = st = datetime.datetime.fromtimestamp(ts).strftime(
            '%a, %d %b %Y %H:%M:%S %Z')
        tmp = time_stamp + "GMT"
        time_stamp = datetime.datetime.strptime(tmp,
                                                '%a, %d %b %Y %H:%M:%S %Z')

        #If authorized user, insert
        if (authed):
            con.execute('INSERT INTO posts VALUES(?,?,?,?,?,?)',
                        (dataUUID, forum_id, thread_id, post_text, check_user,
                         time_stamp))
            db.commit()
            check_posts = con.execute('SELECT * FROM posts').fetchall()
            response = Response("HTTP 201 Created\n" +
                                "Location Header field set to /forums/" +
                                forum_id + "/" + thread_id + " for new forum.",
                                201,
                                mimetype='application/json')
            response.headers[
                'Location'] = "/forums/" + forum_id + "/" + thread_id + str(1)
            myDb.teardown_db("shard" + str(shard_num))

            db = myDb.get_db(DATABASE)
            db.row_factory = myDb.dict_factory
            con = db.cursor()
            con.execute(
                'UPDATE threads SET time_created = ? WHERE forum_id = ? AND id = ?',
                (time_stamp, forum_id, thread_id))
            db.commit()
            myDb.teardown_db(DATABASE)

        else:
            invalMsg = "HTTP 401 Not Authorized"
            response = Response(invalMsg, 404, mimetype='application/json')
    else:
        invalMsg = "HTTP 404 Not Found"
        response = Response(invalMsg, 404, mimetype='application/json')
    return response