Skip to content
Snippets Groups Projects
Commit fcc354c4 authored by Oliwer Mattsson's avatar Oliwer Mattsson :headphones:
Browse files

Kommenterade filen för att underlätta presentation.

parent 8b1a05de
No related branches found
No related tags found
No related merge requests found
#!/.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
import unicodedata
from operator import itemgetter
import os # Import os module for cls and executing commands through python
import json # Import json so we can handle our data.json file
import pprint # Pretty print for debugging
import re # Not used atm
import unicodedata # Not used atm
from operator import itemgetter # Used for sorting our results after a specific option.
# ---- IMPORTS ---- #
# Opens a file and reads it so it's contents can be used as data for the program
def load(filename):
try:
# Opens file with R (read) with UTF 8 encoding as file
with open(filename, 'r', encoding='utf-8') as file:
data = json.load(file)
data = json.load(file) # Assigns the file contents to data as json
file.close()
return data
file.close() # Close the file
return data
except:
return None
# WIP
def save(data):
with open('data.json', 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4)
......@@ -37,93 +35,95 @@ def save(data):
# Get project count
def get_project_count(data):
return len(data)
return len(data) # Returns the len of the data list which is equivalent to the amount of projects
# Get project by ID
def get_project(data, id):
# Loops over a range with the length of the amount of projects present and gets a project if it matches the passed ID.
for n in range(0, get_project_count(data)):
if data[n]['project_id'] == id:
return data[n]
if data[n]['project_id'] == id: # If the current projects ID corresponds to the ID we're looking for then...
return data[n] # Return the project at that index
# Get all unique techniques from project
def get_techniques(data):
techniques = []
for project in data:
for tech in project['techniques_used']:
if tech not in techniques:
techniques.append(tech)
techniques = [] # A list which will store the techniques found
for project in data: # For every project in our data
for tech in project['techniques_used']: # For every technique in that project
if tech not in techniques: # If it's not in the previously techniques, it's unique.
techniques.append(tech) # Append it to the list.
techniques.sort()
techniques.sort() # Sort it so it'll look nice.
return techniques
# Gets all unique techniques from all projects ! COULD USE SOME FILTERING !
# Gets all unique techniques from all projects. ( NOT USED? )
def get_technique_stats(data):
technique_list = get_techniques(data)
technique_stats = {}
current_techniques = []
technique_list = get_techniques(data) # Get all the unique techniques so we know what techniques we can expect.
technique_stats = {} # Creates a dictionary which will contain the id as a key and the name as a value.
current_techniques = [] # A list which will contain the found techniques.
for technique in technique_list:
for project in data:
if technique in project['techniques_used']:
current_techniques.append({'id': project['project_id'], 'name': project['project_name']})
for technique in technique_list: # For each unique technique we found with get_techniques()...
for project in data: # For every project in data, as we need to look over them all for each technique.
if technique in project['techniques_used']: # If the technique we're currently looking for is in the used techniques of the project we're iterating over.
current_techniques.append({'id': project['project_id'], 'name': project['project_name']}) # Create a new dict INSIDE the list with the project ID and project name.
technique_stats.update({technique: current_techniques.copy()})
current_techniques.clear()
technique_stats.update({technique: current_techniques.copy()}) # Updates the dictionary with the list WITH a dict inside.
current_techniques.clear() # Clears the buffer so a new key value pair can be created.
return technique_stats
return technique_stats # Return a dict with all techniques and the projects which uses them.
# Fetches and sorts projects matching criteria from the specified list.
def search(data, sort_by='start_date', sort_order='desc', techniques=None, search=None, search_fields=None):
# If search str belongs to search and if so, make search lower case.
if isinstance(search, str):
search = search.lower()
results = []
# get it
for project in data:
results.append(project)
for project in data: # For each project in data
results.append(project) # Add all projects in data
# sort it
results = sorted(results, key=itemgetter(sort_by))
results = sorted(results, key=itemgetter(sort_by)) # Sorts our results based on a specific variable by using a lambda function with itemgetter.
# order it
if sort_order == 'desc':
if sort_order == 'desc': # If the sort order is set to desc, then reverse the list.
results.reverse()
# filter it (by techniques)
if techniques is not None:
for technique in get_techniques(data):
for project in results:
if all(n in project['techniques_used'] for n in techniques):
pass
if techniques is not None: # If there are techniques which are going to be filtered by..
for technique in get_techniques(data): # For each unique technique..
for project in results: # And for each project we've found so far..
if all(n in project['techniques_used'] for n in techniques): # If all the techniques we're looking for are inside the projects technique_used field..
pass # Then do nothing..
else:
results.pop(results.index(project))
results.pop(results.index(project)) # Otherwise remove it from our results.
# search for it
if search is not None:
search_results = []
for project in results:
if search_fields is not None and search_fields != "" and search != "" and search_fields != ['None']:
for field in search_fields:
substring = project[field]
# Check type before calling lower()
if search is not None: # If the search field isn't set to None..
search_results = [] # Create a list for our search results..
for project in results: # For every project we've found so far..
if search_fields is not None and search_fields != "" and search != "" and search_fields != ['None']: # If the searchf ield is not set to None AND search_fields is not empty AND search_fields is not an empty list..
for field in search_fields: # For each field in search fields..
substring = project[field] # Save the current field in substring so we can do operations on it..
# Check types before calling lower()
if isinstance(substring, str):
substring = substring.lower()
if substring.find(search) != -1:
substring = substring.lower() # Make case insensitive
if substring.find(search) != -1: # If found
search_results.append(project)
break
elif isinstance(substring, int):
elif isinstance(substring, int):
if str(substring).find(search) != -1:
search_results.append(project)
break
elif isinstance(substring, list):
for subsubstring in substring:
if isinstance(subsubstring, str):
elif isinstance(substring, list): # If it's a list..
for subsubstring in substring: # We'll need to make another substring so we can iterate over each index of the list instead of just for multiple lists.
if isinstance(subsubstring, str):
if subsubstring.lower().find(search) != -1:
search_results.append(project)
break
......@@ -132,8 +132,8 @@ def search(data, sort_by='start_date', sort_order='desc', techniques=None, searc
search_results.append(project)
break
elif search_fields == "":
results.clear()
elif search_fields == "": # And if the search_fields is empty..
results.clear() # Clear all results
break
else:
......@@ -158,16 +158,19 @@ def search(data, sort_by='start_date', sort_order='desc', techniques=None, searc
search_results.append(project)
break
results = search_results
results = search_results # Make results into search_results as they should be the same
return results # Returns our results.
return results
# WIP
def cls():
os.system('cls' if os.name == 'nt' else 'clear')
pass
# WIP
def new_project(data):
cls()
......@@ -205,14 +208,14 @@ def new_project(data):
pass
# WIP
def list_projects(data):
cls()
for project in data:
pprint.pp(project)
print("\n")
# WIP
def edit_project(data, id):
while True:
if id > get_project_count(data) or id < 0:
......@@ -232,11 +235,11 @@ def edit_project(data, id):
input("\nField to edit: ")
# WIP
def delete_project(data):
pass
# WIP
def menu(data):
menu_items = ["Add new project", "List projects", "Edit existing project", "Delete project", "Quit"]
menu_index = 0
......
......@@ -11,6 +11,8 @@ def index():
@app.route("/list", methods=['GET', 'POST'])
def list():
# Gets POST data from forms
search_query = request.form.get('search', '')
sort_by = request.form.get('sort_by', 'start_date')
sort_order = request.form.get('sort_order', 'desc')
......@@ -22,6 +24,7 @@ def list():
sort_by=sort_by,
sort_order=sort_order)
# Converts ID to int incase it's passed as string
@app.route("/project/<int:project_id>")
def project_details(project_id):
project = get_project(load('data.json'), project_id)
......@@ -33,8 +36,10 @@ def project_details(project_id):
@app.route("/techniques", methods=['GET', 'POST'])
def techniques():
# Gets all unique techniques in our projects using get_technique_stats function.
techniques = get_technique_stats(load('data.json'))
# Gets POST data from forms
search_query = request.form.get('search', '')
sort_by = request.form.get('sort_by', 'start_date')
sort_order = request.form.get('sort_order', 'desc')
......
......@@ -27,7 +27,7 @@
<div class="content-container">
<div class="info-container">
<div class="img-container">
<img class="profile-img"src="../static/images/placeholder.jpg">
<img class="profile-img" src="../static/images/placeholder.jpg" alt="Profile Picture">
<div class="short-desc">
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.
</div>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment