Skip to content
Snippets Groups Projects
data_layer.py 4.46 KiB
Newer Older
  • Learn to ignore specific revisions
  • #!/.venv/bin/python
    
    
    # ---- IMPORTS ---- #
    import os
    
    import json
    import pprint
    
    import re
    # ---- IMPORTS ---- #
    
        
        with open('data.json', 'r', encoding='utf-8') as file:
    
            data = json.load(file)
    
        file.close()
        
        return data
    
    
    
    
    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]
    
    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']
    
    # 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
    
    
    
    def search():
        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)
        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 ----
    
        new_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: ")
                
                
    
        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:
    
                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()
    
    
    
    
    
    if __name__ == "__main__":
        main()