Skip to content
Snippets Groups Projects
myFlaskProject.py 2.35 KiB
Newer Older
  • Learn to ignore specific revisions
  • from flask import Flask, render_template, request
    
    app = Flask(__name__, template_folder='./templates')
    
    @app.route("/")
    def index():
    
    @app.route("/list", methods=['GET', 'POST'])
    
    
        # 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)