Преглед изворни кода

adding basic search functionality

joel
Simon Bowie пре 1 година
родитељ
комит
d44ce7042d
6 измењених фајлова са 52 додато и 3 уклоњено
  1. +4
    -0
      web/app/__init__.py
  2. +0
    -1
      web/app/book.py
  3. +35
    -0
      web/app/search.py
  4. +4
    -1
      web/app/templates/base.html
  5. +9
    -0
      web/app/templates/search.html
  6. +0
    -1
      web/app/tool.py

+ 4
- 0
web/app/__init__.py Прегледај датотеку

@@ -85,4 +85,8 @@ def create_app():
from .api import api as api_blueprint
app.register_blueprint(api_blueprint)

# blueprint for search parts of app
from .search import search as search_blueprint
app.register_blueprint(search_blueprint)

return app

+ 0
- 1
web/app/book.py Прегледај датотеку

@@ -12,7 +12,6 @@ from .models import Resource
from .resources import *
from .relationships import *
from . import db
from sqlalchemy import text
import os

book = Blueprint('book', __name__)

+ 35
- 0
web/app/search.py Прегледај датотеку

@@ -0,0 +1,35 @@
# @name: search.py
# @creation_date: 2023-04-04
# @license: The MIT License <https://opensource.org/licenses/MIT>
# @author: Simon Bowie <ad7588@coventry.ac.uk>
# @purpose: Search route for search results
# @acknowledgements:
# https://medium.com/@joseortizcosta/search-utility-with-flask-and-mysql-60bb8ee83dad

from flask import Blueprint, render_template, request, flash, redirect, url_for
from .models import Resource
from sqlalchemy import or_

search = Blueprint('search', __name__)

#endpoint for search
@search.route('/search', methods=['GET'])
def basic_search():
if request.args.get('query') is not None:
query = request.args.get('query')
results = Resource.query.filter(
or_(
Resource.name.ilike('%' + query + '%'),
Resource.description.ilike('%' + query + '%'),
Resource.developer.ilike('%' + query + '%'),
Resource.longDescription.ilike('%' + query + '%'),
Resource.experimental.ilike('%' + query + '%'),
Resource.considerations.ilike('%' + query + '%'),
Resource.references.ilike('%' + query + '%'),
Resource.author.ilike('%' + query + '%'),
Resource.isbn.ilike('%' + query + '%'),
Resource.typology.ilike('%' + query + '%'),
)).all()
return render_template('search.html', results=results)
else:
return redirect(url_for('main.index'))

+ 4
- 1
web/app/templates/base.html Прегледај датотеку

@@ -306,7 +306,10 @@
{% endif %}
</div> -->

<input class=" text-center text-base border-l-2 border-black pl-1 " type="text" placeholder="Search">
<form action="{{ url_for('search.basic_search') }}">
<input type="text" name="query" class="text-center text-base border-l-2 border-black pl-1" placeholder="Search">
<input type="submit" hidden />
</form>
</nav>
</header>

+ 9
- 0
web/app/templates/search.html Прегледај датотеку

@@ -0,0 +1,9 @@
{% extends 'base.html' %}

{% block content %}

{% for result in results %}
{{ result.name }}
{% endfor %}

{% endblock %}

+ 0
- 1
web/app/tool.py Прегледај датотеку

@@ -12,7 +12,6 @@ from .models import Resource
from .resources import *
from .relationships import *
from . import db
from sqlalchemy import text
import os

tool = Blueprint('tool', __name__)

Loading…
Откажи
Сачувај