diff --git a/README.md b/README.md index 8cc3419..cc50add 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 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/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 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