Browse Source

Implement search feature

marches
Josh Gordon 3 years ago
parent
commit
14904ae07f
  1. 32
      app/routes.py
  2. 31
      app/templates/index.html

32
app/routes.py

@ -24,6 +24,38 @@ def index():
return render_template('index.html',featureType=featureType,featureText=item)
@app.route('/search', methods=['POST'])
def search():
typ = request.form['type']
query = request.form['inputText']
print(typ + ": " + query)
results = []
for direct in ["../Items/*","../Places/*","../Spells/*"]:
objects = get_items(direct)
for (name,heads,body) in objects:
if typ == "name":
if query.lower() in name.lower():
results.append((name,heads,body))
elif typ == "rarity":
if "rarity" in [h[0].lower() for h in heads]:
if query.lower() in [h[1].lower() for h in heads if h[0].lower() == "rarity"]:
results.append((name,heads,body))
elif typ == "aura":
if "aura" in [h[0].lower() for h in heads]:
if query.lower() in [h[1].lower() for h in heads if h[0].lower() == "aura"]:
results.append((name,heads,body))
elif typ == "text":
if query.lower() in body.lower():
results.append((name,heads,body))
print("Number of results: " + str(len(results)))
featureType = choice(["Item","Place","Spell"])
item = choice(get_items("../"+featureType+"s/*"))
return render_template('index.html',featureType=featureType,featureText=item, searchResults=results)
@app.route('/items')
def items():
return render_template('listing.html',title="Items",items=get_items("../Items/*"))

31
app/templates/index.html

@ -15,6 +15,37 @@
<li><a href="/spells">Spells</a></li>
</ul>
</nav>
<form action="/search" id="searchForm" method="post">
Search Type:
<select name="type">
<option value="name">Name</option>
<option value="rarity">Rarity</option>
<option value="aura">Aura</option>
<option value="text">Text</option>
</select>
<input type="text" name="inputText" placeholder="Enter your search here">
<input type="submit" value="search">
</form>
{% if searchResults is defined %}
<table>
{% for (name,heads,text) in searchResults %}
<tr>
<th class="topalign" id="{{name}}">
{{name}}
<table class="props">
{% for (key, value) in heads %}
<tr><th> {{key}} </th><td> {{value}} </td></tr>
{% endfor %}
</table>
</th>
<td class="freeform monospace"> {{text}} </td>
</tr>
{% endfor %}
</table>
{% endif %}
<section>
<h2>Featured {{featureType}}:</h2>
<table>

Loading…
Cancel
Save