From 4ccfad1767b67ef1312c8df890f3d6b9994b7ea2 Mon Sep 17 00:00:00 2001 From: Savan Patel Date: Tue, 3 Oct 2023 17:57:59 +0530 Subject: [PATCH 1/4] Added code for session0 --- server.py | 77 ++++++++++++++++++++++++++++++++++++++++ users/ENROLLMENT_ID.json | 8 +++++ 2 files changed, 85 insertions(+) create mode 100644 server.py create mode 100644 users/ENROLLMENT_ID.json diff --git a/server.py b/server.py new file mode 100644 index 0000000..62b085c --- /dev/null +++ b/server.py @@ -0,0 +1,77 @@ +from flask import Flask +import json, os +from typing import Dict, Any + +directory_path = "./users" + +app = Flask(__name__) + +styles = """ + +""" + +def read_json_files_from_directory(directory_path: str): + """ Read JSON files from provided path and return dict in format of {user_id:user_info}""" + json_data: Dict[str, Any] = {} + + try: + # Iterate through the files in the directory + for filename in os.listdir(directory_path): + if filename.endswith('.json'): + file_path = os.path.join(directory_path, filename) + with open(file_path, 'r') as file: + # Read and parse the JSON data + data = json.load(file) + for user_info in data: + json_data[user_info["user_id"]] = user_info + except FileNotFoundError: + print(f"Directory not found: {directory_path}") + return None + except json.JSONDecodeError as e: + print(f"Error decoding JSON: {e}") + return None + + return json_data + + +@app.route('/') +def get_users(): + """Renders a table of all users.""" + user_objs = read_json_files_from_directory(directory_path) + response = "" + for person_id in user_objs: + response += f"""{person_id}""" + response = f"""{styles}{response}
""" + return response, 200, {'Content-Type': 'text/html'} + + +@app.route('/get_user/', methods=['GET']) +def get_user(person_id): + """Renders fields of a person with specified person_id.""" + response = "" + user_objs = read_json_files_from_directory(directory_path) + + if person_id in user_objs: + person_obj = user_objs[person_id] + response += f"User ID{person_obj['user_id']}" + response += f"First Name{person_obj['first_name']}" + response += f"Last Name{person_obj['last_name']}" + response += f"""Friends{",".join([f"{p}" for p in person_obj['friends']])}""" + response = f"""{styles}{response}
""" + return response, 200, {'Content-Type': 'text/html'} + else: + return f"User {person_id} not found." +if __name__ == '__main__': + app.run() \ No newline at end of file diff --git a/users/ENROLLMENT_ID.json b/users/ENROLLMENT_ID.json new file mode 100644 index 0000000..7ddd476 --- /dev/null +++ b/users/ENROLLMENT_ID.json @@ -0,0 +1,8 @@ +[ + { + "user_id": "ENROLLMENT_ID", + "first_name": "User_First_Name", + "last_name": "User_Last_Name", + "friends": ["FRIEND_1_ENROLLMENT_ID", "FRIEND_2_ENROLLMENT_ID"] + } + ] \ No newline at end of file From 1754d4e6dd385f31ef0de0e94ebaef08bc90d879 Mon Sep 17 00:00:00 2001 From: Savan Patel Date: Wed, 4 Oct 2023 09:47:16 +0530 Subject: [PATCH 2/4] updated readme --- README.md | 7 ++++++- requirements.txt | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 requirements.txt diff --git a/README.md b/README.md index 8cc3419..e70e382 100644 --- a/README.md +++ b/README.md @@ -1 +1,6 @@ -# software_engineering_essentials \ No newline at end of file +# Software Engineering Principles +## Session #0 +1. Launch codespaces +2. Install libraries using requirements.txt - Dependency Management +3. Add json in “users” dir and serve locally +and create a new PR (checkout, add, commit, push) - Git \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1750fb2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +Flask==2.3.3 +snakeviz==2.2.0 +pytest==7.4.2 +pytest-cov==4.1.0 +yapf==0.40.2 +names==0.3.0 \ No newline at end of file From a70aca60049775aeaa9aab09d4bb30e025bdaa28 Mon Sep 17 00:00:00 2001 From: Savan Patel Date: Wed, 4 Oct 2023 09:51:05 +0530 Subject: [PATCH 3/4] update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e70e382..cc50add 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Software Engineering Principles ## Session #0 1. Launch codespaces -2. Install libraries using requirements.txt - Dependency Management +2. Install libraries using requirements.txt - **Dependency Management** 3. Add json in “users” dir and serve locally -and create a new PR (checkout, add, commit, push) - Git \ No newline at end of file +and create a new PR (checkout, add, commit, push) - **Git** \ No newline at end of file From eb4bf39485a49ab4d04dc2b89ce4a54571d69da6 Mon Sep 17 00:00:00 2001 From: Sagar-2401 <118077434+Sagar-2401@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:43:22 +0530 Subject: [PATCH 4/4] 21bec102 --- users/21bec102.json | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 users/21bec102.json diff --git a/users/21bec102.json b/users/21bec102.json new file mode 100644 index 0000000..f9f280c --- /dev/null +++ b/users/21bec102.json @@ -0,0 +1,8 @@ +[ + { + "user_id": "21bec102", + "first_name": "sagar", + "last_name": "ramani", + "friends": ["21bec085", "21bec100"] + } +] \ No newline at end of file