diff --git a/MyPortfolio/data.py b/MyPortfolio/data.py index 45792719c9a22e3095d73d8cb8d582f5d745fe4a..a2f2f0e70de1e53c906188249fd4ba37cae3b584 100644 --- a/MyPortfolio/data.py +++ b/MyPortfolio/data.py @@ -11,10 +11,12 @@ import os import json import pprint import re +import unicodedata from operator import itemgetter # ---- IMPORTS ---- # + def load(filename): try: @@ -35,14 +37,18 @@ def save(data): file.close() - # Update unique techniques if user added new ones to the file. - get_techniques_stats(data) # Reload data load() +# Get project count +def get_project_count(data): + return len(data) + + + # Get project by ID def get_project(data, id): @@ -50,12 +56,6 @@ def get_project(data, id): if data[n]['project_id'] == id: return data[n] - - -# Get project count -def get_project_count(data): - return len(data) - # Get all unique techniques from project @@ -94,7 +94,11 @@ def get_technique_stats(data): # 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): +def search(data, sort_by='start_date', sort_order='desc', techniques=None, search=None, search_fields=None): + + if type(search) == str: + + search = search.lower() results = [] # get it @@ -102,16 +106,88 @@ def search(data, sort_by='project_id', sort_order='desc', techniques=None, searc results.append(project) # sort it - sorted_list = sorted(results, key=itemgetter(sort_by)) + results = sorted(results, key=itemgetter(sort_by)) # order it - if sort_order == 'asc': results.reverse() + if sort_order == 'desc': results.reverse() + # filter it (by techniques) - for project in sorted_list: - pass + if techniques != None: + for technique in get_techniques(data): + for project in results: + + + if all(n in project['techniques_used'] for n in techniques): + pass + else: + results.pop(results.index(project)) + + # search for it + if search != None: - pass + search_results = [] + + for project in results: + + if search_fields != None and search_fields != "" and search != "": + + for field in search_fields: + substring = project[field] + if type(substring) == str: + substring = substring.lower() + if substring.find(search) != -1: + search_results.append(project) + break + if type(substring) == int: + if str(substring).find(search) != -1: + search_results.append(project) + break + if type(substring) == list: + for subsubstring in substring: + if type(subsubstring) == str: + if subsubstring.find(search) != -1: + search_results.append(project) + break + if type(subsubstring) == int: + if str(subsubstring).find(search) != -1: + search_results.append(project) + break + + + + + elif search_fields == "": + results.clear() + break + + else: + for substring in list(project.values()): + substring = substring.lower() + print(type(substring)) + if type(substring) == str: + if substring.find(search) != -1: + search_results.append(project) + break + if type(substring) == int: + if str(substring).find(search) != -1: + search_results.append(project) + break + if type(substring) == list: + for subsubstring in substring: + if type(subsubstring) == str: + if subsubstring.find(search) != -1: + search_results.append(project) + break + if type(subsubstring) == int: + if str(subsubstring).find(search) != -1: + search_results.append(project) + break + + results = search_results + + #pprint.pp(results) + return results @@ -196,7 +272,6 @@ def edit_project(data, id): - def delete_project(data): pass @@ -245,15 +320,14 @@ def menu(data): + def main(): - data = load('data.json') - menu(data) + data = load('MyPortfolio/data.json') + #menu(data) - get_technique_stats(data) - #search(data, 'project_id', 'desc') if __name__ == "__main__": main()