Skip to content
Snippets Groups Projects
Commit 9ff032ef authored by Martin Mareš's avatar Martin Mareš
Browse files

Místa: Vyhledávání uvažuje každé slovo dotazu zvlášť

Podobně jako to děláme u hledání uživatelů podle jména.

Také upozorňujeme, když se hledá jenom v podstromu.

A pokud jsme našli jednoznačný výsledek, flashneme zprávu upozorňující,
že je to výsledek hledání.
parent 128451f8
No related branches found
No related tags found
1 merge request!128Místa: hledání a další drobná vylepšení
This commit is part of merge request !128. Comments created here will be created in the context of that merge request.
...@@ -19,7 +19,7 @@ import wtforms.validators as validators ...@@ -19,7 +19,7 @@ import wtforms.validators as validators
class PlaceSearchForm(FlaskForm): class PlaceSearchForm(FlaskForm):
query = mo_fields.String(render_kw={'autofocus': True, 'placeholder': 'Kód nebo začátek názvu'}) query = mo_fields.String(render_kw={'autofocus': True, 'placeholder': 'Kód nebo části názvu'})
submit = wtforms.SubmitField('Hledat') submit = wtforms.SubmitField('Hledat')
...@@ -35,22 +35,26 @@ def org_place(id: int): ...@@ -35,22 +35,26 @@ def org_place(id: int):
found_places = None found_places = None
search_failed = False search_failed = False
search_limited = False search_limited = False
if search_form.validate_on_submit() and search_form.query.data: if search_form.validate_on_submit():
query = search_form.query.data query = search_form.query.data
found = db.get_place_by_code(query) query_words = query.split()
if len(query_words) == 1:
found = db.get_place_by_code(query_words[0])
if found is not None: if found is not None:
flash('Nalezeno toto místo', 'info')
return redirect(url_for('org_place', id=found.place_id)) return redirect(url_for('org_place', id=found.place_id))
if len(query_words) > 0 and '%' not in query:
max_places = 100 max_places = 100
if '%' in query: place_q = (sess.query(db.Place)
found_places = []
else:
found_places_q = (sess.query(db.Place)
.filter(func.lower(db.f_unaccent(db.Place.name)).like(func.lower(db.f_unaccent(query + '%'))))
.filter(db.Place.place_id != place.place_id)) .filter(db.Place.place_id != place.place_id))
for qw in query_words:
place_q = place_q.filter(func.lower(db.f_unaccent(db.Place.name)).like(func.lower(db.f_unaccent(f'%{qw}%'))))
if place.level > 0: if place.level > 0:
found_places_q = found_places_q.join(db.RegionDescendant, and_(db.RegionDescendant.region == place.place_id, place_q = place_q.join(db.RegionDescendant, and_(db.RegionDescendant.region == place.place_id,
db.RegionDescendant.descendant == db.Place.place_id)) db.RegionDescendant.descendant == db.Place.place_id))
found_places = (found_places_q found_places = (place_q
.options(joinedload(db.Place.parent_place)) .options(joinedload(db.Place.parent_place))
.order_by(db.Place.level, db.Place.name, db.Place.place_id) .order_by(db.Place.level, db.Place.name, db.Place.place_id)
.limit(max_places) .limit(max_places)
...@@ -59,6 +63,7 @@ def org_place(id: int): ...@@ -59,6 +63,7 @@ def org_place(id: int):
if not found_places: if not found_places:
search_failed = True search_failed = True
if len(found_places) == 1: if len(found_places) == 1:
flash('Nalezeno toto místo', 'info')
return redirect(url_for('org_place', id=found_places[0].place_id)) return redirect(url_for('org_place', id=found_places[0].place_id))
else: else:
search_limited = len(found_places) >= max_places search_limited = len(found_places) >= max_places
......
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
{% endif %} {% endif %}
</div> </div>
<h3>Vyhledávání</h3> <h3>Vyhledávání míst{% if place.level > 0 %} v této oblasti{% endif %}</h3>
{% if search_failed %} {% if search_failed %}
<div class='alert alert-danger' role='alert'> <div class='alert alert-danger' role='alert'>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment