Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

allow for multiple commands

  • Loading branch information...
commit e9a0973355bb75cfd4e8c00d3bb8ef74afdf2c36 1 parent 07d1790
@dmalmer authored
Showing with 75 additions and 72 deletions.
  1. +64 −62 musclistings.py
  2. +11 −10 templates/show_listings.html
View
126 musclistings.py
@@ -9,6 +9,7 @@
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash
from werkzeug import iri_to_uri, url_unquote, url_quote
+from urllib import unquote
# configuration
DATABASE = '/tmp/flaskr.db'
@@ -45,46 +46,50 @@ def teardown_request(exception):
# views
@app.route('/')
-@app.route('/<commands>')
-def show_listings(commands=None):
+@app.route('/<urlcommand>')
+def show_listings(urlcommand=None):
sqlexec = 'select * from listings'
- #flash('commands = ' + str(commands))
-
#= in event search will break
#figure out how to send commands with # and ?
- if commands:
- for command in commands.strip().split('&'):
- csplit = command.split('=')
- if csplit[0] == '_search':
- if csplit[1] == '':
- session['eventsearch'] = False
- return redirect(url_for('show_listings'))
- else:
- #add check for 'search terms' vs '"search terms"'
- sqlexec += '\nwhere title like "%' + \
- csplit[1] + '%"'
- elif csplit[0] == '_sort':
- sqlexec += '\norder by '
- if csplit[1] == 'date_asc':
- sqlexec += 'eventdate asc'
- elif csplit[1] == 'date_desc':
- sqlexec += 'eventdate desc'
- elif csplit[1] == 'event_asc':
- sqlexec += 'title asc'
- elif csplit[1] == 'event_desc':
- sqlexec += 'title desc'
- elif csplit[1] == 'venue_asc':
- sqlexec += 'venue asc'
- elif csplit[1] == 'venue_desc':
- sqlexec += 'venue desc'
- elif csplit[1] == 'buy_asc':
- sqlexec += 'avail asc'
- elif csplit[1] == 'buy_desc':
- sqlexec += 'avail desc'
- elif csplit[0] == '_clause':
- #add check for 'search terms' vs '"search terms"'
- sqlexec += '\n' + csplit[1]
+
+ if urlcommand:
+ urldict = dict([(command.split('=')[0], command.split('=')[1]) for command in urlcommand.strip().split('&')])
+ urlkeys = urldict.keys()
+ flash('urldict = ' + str(urldict))
+
+
+ #add break
+ if '_clause' in urlkeys:
+ #add check for 'search terms' vs '"search terms"'
+ sqlexec += '\n' + urldict['_clause']
+
+ if '_search' in urlkeys:
+ #add check for 'search terms' vs '"search terms"'
+ sqlexec += '\nwhere title like "%' + \
+ urldict['_search'] + '%"'
+ session['commanddict']['search'] = urldict['_search']
+
+ if '_sort' in urlkeys:
+ sqlexec += '\norder by '
+ #change this to a mapping
+ if urldict['_sort'] == 'date_asc':
+ sqlexec += 'eventdate asc'
+ elif urldict['_sort'] == 'date_desc':
+ sqlexec += 'eventdate desc'
+ elif urldict['_sort'] == 'event_asc':
+ sqlexec += 'title asc'
+ elif urldict['_sort'] == 'event_desc':
+ sqlexec += 'title desc'
+ elif urldict['_sort'] == 'venue_asc':
+ sqlexec += 'venue asc'
+ elif urldict['_sort'] == 'venue_desc':
+ sqlexec += 'venue desc'
+ elif urldict['_sort'] == 'buy_asc':
+ sqlexec += 'avail asc'
+ elif urldict['_sort'] == 'buy_desc':
+ sqlexec += 'avail desc'
+ session['commanddict']['sort'] = urldict['_sort']
else:
#default date asc sort
sqlexec += '\norder by eventdate asc'
@@ -126,8 +131,7 @@ def refresh():
line = listings.readline().rstrip()
g.db.commit()
- session['currentsort'] = ''
- session['currentsearch'] = ''
+ session['commanddict'] = {}
session['eventsearch'] = False
return redirect(url_for('show_listings'))
@@ -135,35 +139,33 @@ def refresh():
@app.route('/enablesearch')
def enable_search():
session['eventsearch'] = True
- if session['currentsearch'] != '':
- return redirect(url_for('show_listings', commands=session['currentsearch']))
return redirect(url_for('show_listings'))
-@app.route('/search', methods=['POST'])
-def search():
- session['currentsearch'] = request.form['eventsearch']
- return redirect(url_for('show_listings', commands='_search=' + request.form['eventsearch']))
-
+@app.route('/callcommand', methods=['GET', 'POST'])
+@app.route('/callcommand/<command>', methods=['GET', 'POST'])
+def callcommand(command=None):
+ #this function/route is necessary for now to handle multiple commands
+ #still need to cut it down/get rid of it as commanddict is set redundantly in show_listings
+ if command:
+ csplit = command.split('=')
+ func = csplit[0]
+ term = csplit[1]
+ session['commanddict'][func] = term
+ elif request.form:
+ #flash('request form = ' + str(request.form))
+ for key in request.form.keys():
+ session['commanddict'][key] = request.form[key]
+ flash("session['commanddict'] = " + str(session['commanddict']))
+
+ url = ''
+ for key in session['commanddict'].keys():
+ url += '_' + key + '=' + session['commanddict'][key] + '&'
+ url = url.rstrip('&')
+
+ return redirect(url_for('show_listings', urlcommand=url if url != '' else None))
-#helper functions
-#def addcommand(term, function):
-# if function == 'search':
-
-# sort functions
-'''
-@app.route('/sort_date/<sort>')
-def sort_date(sort):
- if str(sort) == 'desc':
- datesort = 1
- else:
- datesort = -1
- venuesort = 0
- buysort = 0
- flash('Sort date = ' + str(datesort))
- return redirect(url_for('show_listings'))
-'''
# test
@app.route('/test/')
View
21 templates/show_listings.html
@@ -3,6 +3,7 @@
<form action="{{ url_for('refresh') }}">
<input type="submit" value="Refresh listings">
</form>
+search = {{ session.commanddict.search }}
<center>
<table id="table-main">
<thead>
@@ -12,14 +13,14 @@
<tr>
<td rowspan="2" id="td-header">Date&nbsp;</td>
<td id="td-header">
- <a href="_sort=date_asc">
+ <a href="callcommand/sort=date_asc">
<img src="http://www.freeiconsweb.com/Icons/16x16_arrow_icons/arrow_17.gif"/>
</a>
</td>
</tr>
<tr>
<td id="td-header">
- <a href="_sort=date_desc">
+ <a href="callcommand/sort=date_desc">
<img src="http://www.freeiconsweb.com/Icons/16x16_arrow_icons/arrow_18.gif">
</a>
</td>
@@ -31,8 +32,8 @@
<tr>
{% if session.eventsearch %}
<td rowspan="2" id="td-header">
- <form action="search" method=post class=eventsearch>
- <input type="text" name="eventsearch" value="{{ session.currentsearch }}"/>
+ <form action="callcommand" method=post class=eventsearch>
+ <input type="text" name="search" value="{{ session.commanddict.search }}"/>
</form>
</td>
{% else %}
@@ -41,14 +42,14 @@
</td>
{% endif %}
<td id="td-header">
- <a href="_sort=event_asc">
+ <a href="callcommand/sort=event_asc">
<img src="http://www.freeiconsweb.com/Icons/16x16_arrow_icons/arrow_17.gif">
</a>
</td>
</tr>
<tr>
<td id="td-header">
- <a href="_sort=event_desc">
+ <a href="callcommand/sort=event_desc">
<img src="http://www.freeiconsweb.com/Icons/16x16_arrow_icons/arrow_18.gif">
</a>
</td>
@@ -60,14 +61,14 @@
<tr>
<td rowspan="2" id="td-header">Venue&nbsp;</td>
<td id="td-header">
- <a href="_sort=venue_asc">
+ <a href="callcommand/sort=venue_asc">
<img src="http://www.freeiconsweb.com/Icons/16x16_arrow_icons/arrow_17.gif">
</a>
</td>
</tr>
<tr>
<td id="td-header">
- <a href="_sort=venue_desc">
+ <a href="callcommand/sort=venue_desc">
<img src="http://www.freeiconsweb.com/Icons/16x16_arrow_icons/arrow_18.gif">
</a>
</td>
@@ -79,14 +80,14 @@
<tr>
<td rowspan="2" id="td-header">Buy Tickets&nbsp;</td>
<td id="td-header">
- <a href="_sort=buy_asc">
+ <a href="callcommand/sort=buy_asc">
<img src="http://www.freeiconsweb.com/Icons/16x16_arrow_icons/arrow_17.gif">
</a>
</td>
</tr>
<tr>
<td id="td-header">
- <a href="_sort=buy_desc">
+ <a href="callcommand/sort=buy_desc">
<img src="http://www.freeiconsweb.com/Icons/16x16_arrow_icons/arrow_18.gif">
</a>
</td>
Please sign in to comment.
Something went wrong with that request. Please try again.