Skip to content

Commit

Permalink
Merge branch 'venue-search' into develop
Browse files Browse the repository at this point in the history
This closes #61.
  • Loading branch information
dirn committed Nov 12, 2012
2 parents e93da05 + f0043f4 commit 552a6e4
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 31 deletions.
23 changes: 22 additions & 1 deletion meetups/forms.py
Expand Up @@ -69,6 +69,28 @@ class VenueClaimForm(VenueEditForm):
[validators.Required()])


class VenueSearchForm(Form):
"""Search for a venue."""
name = TextField('Venue Name', [validators.Required()])

use_current_location = BooleanField('Find venues near me')

longitude = HiddenField()
latitude = HiddenField()

# TODO: Move this to a custom OptionalIfNot validator after OptionalIf
# has been merged.
def validate_use_current_location(form, field):
"""Validate that the geolocation has been included for a form
specifying to use the current location.
"""
if field.data:
if not (form.longitude.data or form.latitude.data):
raise validators.ValidationError('It appears you have blocked '
'this site from accessing '
'your location.')


class RequestForSpaceForm(Form):
name = TextField('Your Name', [validators.Required()])
email = TextField('Your Email',
Expand Down Expand Up @@ -116,4 +138,3 @@ def RequestForSpaceInitial(user, event, group):
}

return _RequestForSpaceInitial(**initial)

15 changes: 13 additions & 2 deletions meetups/logic.py
Expand Up @@ -23,6 +23,7 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import re
from urllib import urlencode

from . import meetup
Expand Down Expand Up @@ -50,12 +51,22 @@ def get_users_venues(user_id):
return _get_list(Venue, {'user_id': user_id}, 'name')


def get_unclaimed_venues():
def get_unclaimed_venues(name=None, location=None):
"""Fetch a list of all venues that have yet to be claimed.
Providing a value for `name` will result in a regular expression
query being performed.
Values for `location` should be in the form of `[longitude, latitude]`.
Returns a list of :class:`~meetups.models.Venue` objects.
"""
return _get_list(Venue, {'claimed': False}, 'name')
query = {'claimed': False}
if name is not None:
query['name'] = {'$regex': re.compile(name, re.IGNORECASE)}
if location is not None:
query['loc'] = {'$near': location}
return _get_list(Venue, query, 'name')


def get_groups(query, sort=None):
Expand Down
101 changes: 78 additions & 23 deletions meetups/templates/have.html
@@ -1,5 +1,7 @@
{% extends 'base.html' %}

{% from 'helpers/forms.html' import render_field -%}

{%- block title -%}
I have a space - {{ super() }}
{%- endblock -%}
Expand All @@ -12,28 +14,81 @@ <h1>I <em>have</em> a space</h1>
</div>

<div class="container">
<ul class="venues">
{% for venue in venues %}
<li>
<h3>{{ venue }}</h3>
{% for tag in venue.taglist %}
<span class="label">{{ tag }}</span>
{% endfor %}

<address>
{{ venue.address_1 }}<br>
{%- if venue.address_2 -%}
{{ venue.address_2 }}<br>
{%- endif %}
{% if venue.city and venue.state -%}
{{ venue.city }}, {{ venue.state }}
{%- endif %}
{{ venue.zip }}
</address>

<a href="{{ url_for('venue_claim', _id=venue._id) }}" class="btn btn-primary btn-mini">Claim this space</a>
</li>
{% endfor %}
</ul>
<form action="{{ url_for('have') }}" method="post" class="well">
{{ render_field(form.name) }}
{{ render_field(form.use_current_location) }}

{{ render_field(form.longitude) }}
{{ render_field(form.latitude) }}

<input type="submit" value="Search" class="btn btn-primary">

<script>
window.onload = function() {
var $form = $('div.container form.well');

var addGeoLocation = function(e) {
if ($('input[name=use_current_location]', $form).is(':checked')) {
e.preventDefault();

navigator.geolocation.getCurrentPosition(function(position) {
$('input[name=longitude]', $form).val(position.coords.longitude);
$('input[name=latitude]', $form).val(position.coords.latitude);


$form.unbind('submit', addGeoLocation).submit();
}, function() {
$form.unbind('submit', addGeoLocation).submit();
});
} else {
$('input[name=longitude]', $form).val('');
$('input[name=latitude]', $form).val('');
}
}

if (!navigator.geolocation) {
$('input[name=use_current_location]', $form).hide();
} else {
$form.bind('submit', addGeoLocation)
}
}
</script>
</form>
</div>

<div class="container">
{% if venues %}
<em>
Showing results matching &quot;{{ form.name.data }}&quot;
{%- if form.longitude.data and form.latitude.data %}
near you
{%- endif -%}
.
</em>

<ul class="venues">
{% for venue in venues %}
<li>
<h3>{{ venue }}</h3>
{% for tag in venue.taglist %}
<span class="label">{{ tag }}</span>
{% endfor %}

<address>
{{ venue.address_1 }}<br>
{%- if venue.address_2 -%}
{{ venue.address_2 }}<br>
{%- endif %}
{% if venue.city and venue.state -%}
{{ venue.city }}, {{ venue.state }}
{%- endif %}
{{ venue.zip }}
</address>

<a href="{{ url_for('venue_claim', _id=venue._id) }}" class="btn btn-primary btn-mini">Claim this space</a>
</li>
{% endfor %}
</ul>
{% endif %}
</div>
{%- endblock %}
4 changes: 3 additions & 1 deletion meetups/templates/helpers/forms.html
Expand Up @@ -6,7 +6,9 @@
{{ field.label.text }}
</label>
{% else %}
{{ field.label }}
{% if field.type != 'HiddenField' %}
{{ field.label }}
{% endif %}
{{ field(**kwargs)|safe }}
{% endif %}
{% if field.errors %}
Expand Down
18 changes: 14 additions & 4 deletions meetups/views.py
Expand Up @@ -33,7 +33,7 @@
from flask.ext.login import current_user, login_required, login_user, logout_user

from .forms import (VenueEditForm, VenueClaimForm, RequestForSpaceForm,
UserProfileForm, RequestForSpaceInitial)
UserProfileForm, RequestForSpaceInitial, VenueSearchForm)
from .logic import sync_user, get_unclaimed_venues, get_users_venues, get_groups, get_events, get_venues, event_cmp
from .models import User, Group, Venue, Event, login_manager

Expand Down Expand Up @@ -61,10 +61,20 @@ def index():
return render_template('index.html')


@app.route('/have/')
@app.route('/have/', methods=('GET', 'POST'))
def have():
venues = get_unclaimed_venues()
return render_template('have.html', venues=venues)
form = VenueSearchForm(request.form)

if request.method == 'POST' and form.validate():
name = form.name.data or None
location = None
if form.longitude.data and form.latitude.data:
location = [float(form.longitude.data), float(form.latitude.data)]
venues = get_unclaimed_venues(name=name, location=location)
else:
venues = ()

return render_template('have.html', form=form, venues=venues)


@app.route('/login/')
Expand Down

0 comments on commit 552a6e4

Please sign in to comment.