Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Name + Stevens Login
Jason Rossi, jrossi3@stevens.edu
Nouman Syed, nsyed1@stevens.edu
Atishay Jain, ajain70@stevens.edu

# The URL of your public GitHub repo
https://github.com/Jrossi3/WebForum
Expand Down Expand Up @@ -40,3 +41,14 @@ We tested persistence in a very simple way. We ran the command "curl http://127.
3) Persistence

We tested persistence through 2 different ways. Our first way was restarting the server through running two commands to end and start the server. There is a "kill $PID" command and a "flask run &" command to end and start the server resepctively. The second way of testing was giving it a bad request. The exact command is "curl http://127.0.0.1:5000/post/fulltext" and here this is a bad request because it is an unfinished request and therefore will return an error. Then there is a test to see if the post is still existing after the command is run. There are many comments in the testing to show the exact specifics of when these tests occur.

4) Threaded replies

We tested threaded replies for which we have send a POST request to the URL with an 'id' parameter and a JSON object containing a 'msg' field. For testing we first created a command just to generate a new post, using the command: "curl http://127.0.0.1:5000/post" and gave the msg as "First". Now that we have the first post available, our job is to now add threaded replies inside of this 1st post. The exact command is "curl http://127.0.0.1:5000/post/1" in which we gave a msg "First Reply".When then check if the threaded reply gets inserted inside of the thread array or not.it should finally generate a reply id, a msg, and timestamp when returning.

We tested the threaded replies


5) Date based range queries

We tested date based range queries by running: curl "http://127.0.0.1:5000/post/2023-04-30T21:13:52Z/2023-04-30T21:13:44Z" for example. Here the two parameters after /post are start and end timestamps. They result with posts in between the two timestamps. We have added three more cases for this. First case if the user wants to give the start timestamp as none, second in which the end time is none and the third one in which both are none. The program will handle the first two out of three but for the third one in which both are entered as none, then it would just give a 404 and throw an error.
98 changes: 59 additions & 39 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def post_request():
post_id = max_id + 1

# Get the current time

timestamp = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')

# Generate a random key
Expand Down Expand Up @@ -68,69 +67,90 @@ def post_request():

@app.route("/post/<int:id>", methods=["POST"])
def threaded_replies(id):

body = request.get_json(force=True)
msg = body['msg']
# key = body['key']
if not isinstance(msg, str) or msg is None:
return "Post content should be of type string", 400

# Get the parent post object
parent_post = db["posts_collection"].find_one({"id": id})
if parent_post is None:
return "Parent post not found", 404

key = secrets.token_hex(16)
# parent_post = db["posts_collection"].find_one({"id": id})
# if parent_post is None:
# return "Parent post not found", 404



# if parent_post["key"] != key:
# return "Key is invalid"
# # Generate a new reply id
# max_reply_id = parent_post.get("max_reply_id", 0)
# reply_id = max_reply_id + 1
timestamp = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
# Generate a new UUID for the post
max_id_doc = db["posts_collection"].find_one(sort=[("id", -1)])
if max_id_doc is None:
max_id = 0
else:
max_id = max_id_doc["id"]

# Generate a new UUID for the reply
max_id = 0
for thread in parent_post["thread"]:
if thread["id"] > max_id:
max_id = thread["id"]
# Generate a new post_id by incrementing the maximum post_id
reply_id = max_id + 1
timestamp = datetime.now()


reply = {
"id": reply_id,
"msg": msg,
"key": key,
"timestamp": timestamp
"timestamp": timestamp,
# "parent_id": parent_post["id"],
"thread": []
}

# Insert the new post object into the database
with lock:
posts_collection = db["posts_collection"]
posts_collection.update_one({"_id": ObjectId(parent_post["_id"])},
{"$push": {"thread": reply}})
posts_collection.insert_one(reply)

inserted_reply = posts_collection.find_one(
{"id": id, "thread.id": reply_id},
{"_id": 0, "thread.$": 1}
)
with lock:
posts_collection = db["posts_collection"]
posts_collection.update_one(
{"id": id},
{"$push": {"thread": reply_id}}
)

return jsonify(inserted_reply["thread"][0]), 200
inserted_post = posts_collection.find_one({"id": reply_id})

post_dict = dict(inserted_post)
post_dict.pop("_id", None)
post_dict.pop("key", None)
post_dict.pop("thread", None)
return jsonify(post_dict), 200

# return jsonify(reply), 200

@app.route("/post/<int:id>/thread", methods=['GET'])
def get_thread_queries(id):
# Get the threads of a post from the database by ID
@app.route("/post/<string:start>/<string:end>", methods=['GET'])
def date_time_queries(start, end):
if start.lower() == "none":
start = start.lower()
if end.lower() == "none":
end = end.lower()
# print(end)
timestamp = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
with lock:
posts_collection = db["posts_collection"]
post = posts_collection.find_one({"id": id})
threads_in_post = post["thread"]

if post is None:
return f"Post with ID: {id} not found", 404

threads_list = list(threads_in_post)
for thread in threads_list:
thread.pop("_id", None)

return jsonify(threads_list), 200
if start == "none" and end == "none":
return "Both Start and End cannot be None", 404
elif start == "none":
posts = posts_collection.find({"timestamp": {"$lte": end}})
elif end == "none":
posts = posts_collection.find({"timestamp": {"$gte": start}})
else:
posts = posts_collection.find({"timestamp": {"$gte": start, "$lte": end}})

result = []
for post in posts:
post_dict = dict(post)
post_dict.pop("_id", None)
post_dict.pop("key", None)
result.append(post_dict)

return jsonify(result), 200

@app.route("/post/<int:id>", methods=['GET'])
def get_post(id):
Expand Down
Loading