diff --git a/MyPortfolio/data_layer.py b/MyPortfolio/data_layer.py deleted file mode 100644 index 9f3ebfad69dc490017345c04db03d985bcd6fffc..0000000000000000000000000000000000000000 --- a/MyPortfolio/data_layer.py +++ /dev/null @@ -1,240 +0,0 @@ -#!/.venv/bin/python - -# TODO - -# Gör project_id dynamisk så att den uppdateras efter borttagning/addering av projekt. - - - -# ---- IMPORTS ---- # -import os -import json -import pprint -import re -from operator import itemgetter -# ---- IMPORTS ---- # - - -def load(): - - try: - with open('data.json', 'r', encoding='utf-8') as file: - data = json.load(file) - - file.close() - return data - except: - return None - - - -def save(data): - - with open('data.json', 'w', encoding='utf-8') as file: - json.dump(data, file, ensure_ascii=False, indent=4) - - file.close() - - # Update unique techniques if user added new ones to the file. - get_techniques_stats(data) - - # Reload data - load() - - - -# Get project by ID -def get_project(data, id): - for n in range(0, get_project_count(data)): - if data["projects"][n]["project_id"] == id: - return data["projects"][n] - - -# Get project count -def get_project_count(data): - return len(data["projects"]) - - -# Get techniques from project by ID -def get_techniques(data, id): - return get_project(data,id)['techniques'].sort() - - -# Gets all unique techniques from all projects ! COULD USE SOME FILTERING ! -def get_techniques_stats(data): - - techniques = [] - for n in range(0, get_project_count(data)): - tech = get_techniques(data, n) - for t in tech: - if t not in techniques: - techniques.append(t) - - - return techniques - - -# Fetches and sorts projects matching criteria from the specified list. -def search(data, sort_by='project_id', sort_order='desc', techniques=None, search=None, search_field=None): - - results = [] - # get it - for project in data['projects']: - results.append(project) - - # sort it - sorted_list = sorted(results, key=itemgetter(sort_by)) - - # order it - if sort_order == 'asc': results.reverse() - - # filter it (by techniques) - for project in sorted_list: - pass - - pprint.pp(sorted_list) - pass - - - -def cls(): - os.system('cls' if os.name == 'nt' else 'clear') - pass - - - -def new_project(data): - - cls() - - # ---- COLLECT INFO ---- - project_title = input("Project title: ") - project_id = get_project_count(data)+1 - techniques = input("\nWhat techniques does your project use? Write them out in the following format: python, java, html, css\n\nTechniques: ").replace(" ", "").lower().split(",") - description = input("Provide a description of your project: ") - url = input("Provide a link to the source code/demo of your project: ") - img_url = input("Image source (ex: logo.jpg): ") - # ---- COLLECT INFO ---- - - # lexicographical order sort aka alphabetical - techniques.sort() - - new_project = { - "project_title": project_title, - "project_id": project_id, - "techniques": techniques, - "desc": description, - "img_url": img_url, - "url": url - } - - cls() - - print("\n\nProject preview:\n") - pprint.pp(new_project) - - option = int(input("\n1: Create\n2: Cancel\n> ")) - - if option == 1: - - data['projects'].append(new_project) - - save(data) - - pass - - - -def list_projects(data): - cls() - for project in data['projects']: - pprint.pp(project) - print("\n") - - - -def edit_project(data, id): - - while True: - if id > get_project_count(data) or id < 0: - print("Project ID doesn't exist.\n") - id = int(input("Project_ID to edit: ")) - else: - - cls() - - project = get_project(data, id) - project.pop('project_id') # Project ID shouldn't be changed - - print(f"Editing project: {project['title']}\n") - - pprint.pp(project) - print("") - - for field in enumerate(project): - print(f"{field[0]}: {field[1]}") - - input("\nField to edit: ") - - - - -def delete_project(data): - pass - - - -def menu(data): - - menu_items = ["Add new project", "List projects", "Edit existing project", "Delete project", "Quit"] - menu_index = 0 - - while True: - - cls() - - titular = r""" - ____ _ __ _ _ - | _ \ ___ _ __ | |_ / _| ___ | | (_) ___ - | |_) | / _ \ | '__| | __| | |_ / _ \ | | | | / _ \ - | __/ | (_) | | | | |_ | _| | (_) | | | _ | | | (_) | - |_| \___/ |_| \__| |_| \___/ |_| (_) |_| \___/ - """ - - print(titular) - - for i in menu_items: - print(f"{menu_items.index(i)+1}: {i}") - - try: - option = int(input(f"> ")) - - if option == 1: - new_project(data) - elif option == 2: - list_projects(data) - input() - elif option == 3: - list_projects(data) - edit_project(data, int(input("Project_ID to edit: "))) - elif option == 4: - delete_project(data, int(input("Project_ID to delete: "))) - elif option == 5: - cls() - break - except: - print("") - - - - - -def main(): - - data = load() - menu(data) - - search(data, 'project_id', 'desc') - -if __name__ == "__main__": - main()