Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • tdp003-projekt/egna-datormiljoen
1 result
Show changes
Commits on Source (2)
#!/.venv/bin/python
# ---- IMPORTS ---- #
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) # Assigns the file contents to data as json
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)
file.close()
# Reload data
load()
# Get project count
def get_project_count(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: # 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 = [] # 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() # Sort it so it'll look nice.
return techniques
# Gets all unique techniques from all projects. ( NOT USED? )
def get_technique_stats(data):
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 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()}) # 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 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: # For each project in data
results.append(project) # Add all projects in data
# sort it
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 the sort order is set to desc, then reverse the list.
results.reverse()
# filter it (by techniques)
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)) # Otherwise remove it from our results.
# search for it
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() # Make case insensitive
if substring.find(search) != -1: # If found
search_results.append(project)
break
elif isinstance(substring, int):
if str(substring).find(search) != -1:
search_results.append(project)
break
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
elif isinstance(subsubstring, int):
if str(subsubstring).find(search) != -1:
search_results.append(project)
break
elif search_fields == "": # And if the search_fields is empty..
results.clear() # Clear all results
break
else:
for substring in list(project.values()):
# Check type before calling lower()
if isinstance(substring, str):
if substring.lower().find(search) != -1:
search_results.append(project)
break
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):
if subsubstring.lower().find(search) != -1:
search_results.append(project)
break
elif isinstance(subsubstring, int):
if str(subsubstring).find(search) != -1:
search_results.append(project)
break
results = search_results # Make results into search_results as they should be the same
return results # Returns our results.
# WIP
def cls():
os.system('cls' if os.name == 'nt' else 'clear')
pass
# WIP
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_name": project_title,
"project_id": project_id,
"used_techniques": techniques,
"long_description": 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.append(new_project)
save(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:
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: ")
# 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
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('MyPortfolio/data.json')
if __name__ == "__main__":
main()
from flask import Flask, render_template, request
from data import *
app = Flask(__name__, template_folder='./templates')
data = load('data.json')
@app.route("/")
def index():
return render_template('index.html')
@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')
filtered_projects = search(load('data.json'), sort_by=sort_by, sort_order=sort_order, search=search_query)
return render_template('list.html',
projects=filtered_projects,
search_query=search_query,
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)
if project:
return render_template('project_details.html', project=project)
else:
return "404: Project not found", 404
@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')
search_field = request.form.getlist('search_field', None)
techniques_search = request.form.getlist('technique')
print(f"Searching for: ", search_query)
print(search_field)
filtered_projects = search(load('data.json'), sort_by=sort_by, sort_order=sort_order, techniques=techniques_search, search=search_query, search_fields=search_field)
return render_template('techniques.html',
techniques = techniques,
techniques_search = techniques_search,
projects=filtered_projects,
search_query=search_query,
sort_by=sort_by,
sort_order=sort_order,
search_field=search_field)
if __name__ == "__main__":
app.run(debug=True)