linuxgaming

This is my own Linux gaming aggregate web app I built for personal use.
git clone git://git.beardyjay.co.uk/linuxgaming
Log | Files | Refs | README

commit 7e46f7e6b817e217aa990e08adf09f8edeb42e5c
parent 50adff9e315c07da7be76ffb14fca22dbd6ddc9e
Author: Jay Scott <jay@beardyjay.co.uk>
Date:   Wed, 18 Jul 2018 12:02:46 +0100

refactor of search page

Diffstat:
MTODO.md | 7++++---
Mlinuxgaming/__init__.py | 8+++-----
Alinuxgaming/database.py | 15+++++++++++++++
Mlinuxgaming/search.py | 87+++++++++++++++++--------------------------------------------------------------
Mlinuxgaming/templates/pages/search.html | 17+----------------
5 files changed, 41 insertions(+), 93 deletions(-)

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 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): 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 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')) 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">