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
Branches
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
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')
......@@ -35,22 +35,26 @@ def org_place(id: int):
found_places = None
search_failed = 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
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:
flash('Nalezeno toto místo', 'info')
return redirect(url_for('org_place', id=found.place_id))
if len(query_words) > 0 and '%' not in query:
max_places = 100
if '%' in query:
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 + '%'))))
place_q = (sess.query(db.Place)
.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:
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))
found_places = (found_places_q
found_places = (place_q
.options(joinedload(db.Place.parent_place))
.order_by(db.Place.level, db.Place.name, db.Place.place_id)
.limit(max_places)
......@@ -59,6 +63,7 @@ def org_place(id: int):
if not found_places:
search_failed = True
if len(found_places) == 1:
flash('Nalezeno toto místo', 'info')
return redirect(url_for('org_place', id=found_places[0].place_id))
else:
search_limited = len(found_places) >= max_places
......
......
......@@ -36,7 +36,7 @@
{% endif %}
</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 %}
<div class='alert alert-danger' role='alert'>
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment