refactor of search page - linuxgaming - Linux gaming aggregate tool, built to test out NodeJS.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) commit 79554a6eb94acc3fcf37bec7ee6c8fffa97a2b22
 (DIR) parent e6c2cbf8aeee89296942f9e7974dba10a6921f9e
 (HTM) Author: Jay Scott <me@jay.scot>
       Date:   Wed, 18 Jul 2018 12:02:46 +0100
       
       refactor of search page
       
       Diffstat:
         M TODO.md                             |       7 ++++---
         M linuxgaming/__init__.py             |       8 +++-----
         A linuxgaming/database.py             |      15 +++++++++++++++
         M linuxgaming/search.py               |      87 +++++++------------------------
         M linuxgaming/templates/pages/search… |      17 +----------------
       
       5 files changed, 41 insertions(+), 93 deletions(-)
       ---
 (DIR) diff --git a/TODO.md b/TODO.md
       @@ -3,7 +3,7 @@
        
        ## pre 1.0
        
       -  - Add better searching
       +
          - Refactor code
          
        
       @@ -13,4 +13,5 @@
          - Move updates to AWS Lambda function
          - Add API
          - Add Itch.io games
       -  - Add Steam games
       -\ No newline at end of file
       +  - Add Steam games
       +  - Add better searching
       +\ No newline at end of file
 (DIR) diff --git a/linuxgaming/__init__.py b/linuxgaming/__init__.py
       @@ -8,6 +8,7 @@ import dateutil.parser
        from . import update
        from . import details
        from . import search
       +from . import database
        
        compress = Compress()
        
       @@ -34,11 +35,8 @@ def create_app():
        
            @app.route("/")
            def home():
       -
       -        today = datetime.now()
       -        all_data = mongo.db.items.find(
       -            {"date": {'$gte': today - timedelta(hours=24)}}).sort('date', -1)
       -        return render_template('pages/home.html', entries=all_data)
       +        result = database.db_search({"date": {'$gte': datetime.now() - timedelta(hours=24)}})
       +        return render_template('pages/home.html', entries=result)
        
            @app.errorhandler(500)
            def internal_error(error):
 (DIR) diff --git a/linuxgaming/database.py b/linuxgaming/database.py
       @@ -0,0 +1,15 @@
       +from flask import current_app
       +from pprint import pprint
       +
       +
       +def db_search(query={}):
       +
       +    try:
       +        d = current_app.mongo.db.items.find(
       +            query
       +        ).sort('date', -1)
       +    except pymongo.errors.OperationFailure:
       +        print("DB Error")
       +        return False
       +
       +    return d
 (DIR) diff --git a/linuxgaming/search.py b/linuxgaming/search.py
       @@ -1,77 +1,26 @@
        from flask import (
            Blueprint,
       +    flash,
       +    redirect,
       +    url_for,
            render_template,
            current_app)
        
        bp = Blueprint('search', __name__, url_prefix='/search')
        
        
       -@bp.route("/twitch", methods=('GET', 'POST'))
       -def twitch():
       -
       -    all_data = current_app.mongo.db.items.find(
       -        {"type": "twitch"}).sort('date', -1)
       -    return render_template(
       -        'pages/search.html',
       -        entries=all_data,
       -        count=all_data.count(),
       -        source="twitch")
       -
       -
       -@bp.route("/youtube", methods=('GET', 'POST'))
       -def youtube():
       -
       -    all_data = current_app.mongo.db.items.find(
       -        {"type": "youtube"}).sort('date', -1)
       -    return render_template(
       -        'pages/search.html',
       -        entries=all_data,
       -        count=all_data.count(),
       -        source="youtube")
       -
       -
       -@bp.route("/article", methods=('GET', 'POST'))
       -def article():
       -
       -    all_data = current_app.mongo.db.items.find(
       -        {"type": "article"}).sort('date', -1)
       -    return render_template(
       -        'pages/search.html',
       -        entries=all_data,
       -        count=all_data.count(),
       -        source="articles")
       -
       -
       -@bp.route("/podcast", methods=('GET', 'POST'))
       -def podcast():
       -
       -    all_data = current_app.mongo.db.items.find(
       -        {"type": "podcast"}).sort('date', -1)
       -    return render_template(
       -        'pages/search.html',
       -        entries=all_data,
       -        count=all_data.count(),
       -        source="podcasts")
       -
       -
       -@bp.route("/gog", methods=('GET', 'POST'))
       -def gog():
       -
       -    all_data = current_app.mongo.db.items.find(
       -        {"name": "gog"}).sort('date', -1)
       -    return render_template(
       -        'pages/search.html',
       -        entries=all_data,
       -        count=all_data.count(),
       -        source="gog")
       -
       -
       -@bp.route("/allthethings", methods=('GET', 'POST'))
       -def allthethings():
       -
       -    all_data = current_app.mongo.db.items.find().sort('date', -1)
       -    return render_template(
       -        'pages/search.html',
       -        entries=all_data,
       -        count=all_data.count(),
       -        source="of all the things")
       +@bp.route("/<path:path>", methods=('GET', 'POST'))
       +def test(path):
       +
       +    pages = ['gog', 'twitch', 'youtube', 'article', 'podcast', 'allthethings']
       +    if any(x in path for x in pages):
       +        result = current_app.mongo.db.items.find(
       +            {"type": path}).sort('date', -1)
       +        return render_template(
       +            'pages/search.html',
       +            entries=result,
       +            count=result.count())
       +    else:
       +        flash('1337 Hacks in progress...')
       +        current_app.logger.info('Manual search probe %s', path)
       +        return redirect(url_for('home'))
 (DIR) diff --git a/linuxgaming/templates/pages/search.html b/linuxgaming/templates/pages/search.html
       @@ -3,23 +3,8 @@
        
        {% block content %}
        
       -{% with messages = get_flashed_messages() %}
       -   {% if messages %}
       -      {% for message in messages %}
       -        <div class="ui massive red icon message">
       -          <i class="spinner loading icon"></i>
       -          <div class="content">
       -            <div class="header">
       -              {{ message }}
       -            </div>
       -          </div>
       -        </div>
       -      {% endfor %}
       -   {% endif %}
       -{% endwith %}
       -
        <div class="ui horizontal divider">
       -  {{ count }} {{ source }} Results
       +  {{ count }} Results
        </div>
        
        <table class="ui striped very compact small olive table">