diff --git a/forms/event.py b/forms/event.py index 382150f..7670bde 100644 --- a/forms/event.py +++ b/forms/event.py @@ -1,3 +1,4 @@ +from flask import request from flask_babelex import lazy_gettext, gettext from flask_wtf import FlaskForm from flask_wtf.file import FileField, FileAllowed @@ -150,4 +151,7 @@ class FindEventForm(FlaskForm): organizer_id = SelectField(lazy_gettext('Organizer'), validators=[Optional()], coerce=int) - submit = SubmitField(lazy_gettext("Find events")) \ No newline at end of file + submit = SubmitField(lazy_gettext("Find events")) + + def is_submitted(self): + return 'submit' in request.args diff --git a/forms/event_date.py b/forms/event_date.py index 766dccb..a235c8a 100644 --- a/forms/event_date.py +++ b/forms/event_date.py @@ -1,7 +1,8 @@ +from flask import request from flask_babelex import lazy_gettext, gettext from flask_wtf import FlaskForm from flask_wtf.file import FileField, FileAllowed -from wtforms import SelectMultipleField, FieldList, RadioField, DateTimeField, StringField, SubmitField, TextAreaField, SelectField, BooleanField, IntegerField, FormField +from wtforms import HiddenField, SelectMultipleField, FieldList, RadioField, DateTimeField, StringField, SubmitField, TextAreaField, SelectField, BooleanField, IntegerField, FormField from wtforms.fields.html5 import DateTimeLocalField, EmailField from wtforms.validators import DataRequired, Optional from wtforms.widgets import html_params, HTMLString @@ -16,7 +17,12 @@ class FindEventDateForm(FlaskForm): date_from = CustomDateField(lazy_gettext('From'), validators=[Optional()]) date_to = CustomDateField(lazy_gettext('to'), validators=[Optional()]) keyword = StringField(lazy_gettext('Keyword'), validators=[Optional()]) - category_id = SelectField(lazy_gettext('Category'), validators=[Optional()], coerce=int) + coordinate = HiddenField(validators=[Optional()]) + location = StringField(lazy_gettext('Location'), validators=[Optional()]) + distance = IntegerField(lazy_gettext('Distance'), validators=[Optional()]) - submit = SubmitField(lazy_gettext("Find")) \ No newline at end of file + submit = SubmitField(lazy_gettext("Find")) + + def is_submitted(self): + return 'submit' in request.args \ No newline at end of file diff --git a/services/event_search.py b/services/event_search.py index 88380e3..97ece21 100644 --- a/services/event_search.py +++ b/services/event_search.py @@ -9,6 +9,7 @@ class EventSearchParams(object): self._date_to = None self._date_from_str = None self._date_to_str = None + self._coordinate = None self.admin_unit_id = None self.keyword = None self.latitude = None @@ -53,6 +54,19 @@ class EventSearchParams(object): self._date_to_str = value self._date_to = form_input_to_date(value) + @property + def coordinate(self): + return self._coordinate + + @coordinate.setter + def coordinate(self, value): + self._coordinate = value + if value is not None and len(value) > 0: + (self.latitude, self.longitude) = value.split(",") + else: + self.latitude = None + self.longitude = None + def set_default_date_range(self): self.date_from = today self.date_to = date_set_end_of_day(today + relativedelta(months=12)) @@ -68,9 +82,7 @@ class EventSearchParams(object): self.keyword = request.args['keyword'] if "coordinate" in request.args: - coordinate = request.args['coordinate'] - if coordinate is not None and len(coordinate) > 0: - (self.latitude, self.longitude) = coordinate.split(",") + self.coordinate = request.args['coordinate'] if "distance" in request.args: self.distance = request.args['distance'] diff --git a/static/site.js b/static/site.js index 11756e0..e689b1a 100644 --- a/static/site.js +++ b/static/site.js @@ -166,4 +166,45 @@ $( function() { $('.datepicker').each(function (index, element){ start_datepicker($(element)); }); + + $("#clear_location_btn").click(function () { + $("#coordinate").val(""); + $("#location").val(""); + $("#distance").val(""); + }); + + $("#geolocation_btn").click(function () { + if ("geolocation" in navigator){ + navigator.geolocation.getCurrentPosition(function(position){ + $("#coordinate").val(position.coords.latitude+","+position.coords.longitude); + $("#location").val("Aktuelle Position"); + $("#postcode").val(""); + $("#location").removeClass("is-invalid"); + + if ($("#distance").val() == "") { + $("#distance").val("500"); + } + }, handleError); + + function handleError(error){ + //Handle Errors + switch(error.code) { + case error.PERMISSION_DENIED: + alert("User denied the request for Geolocation."); + break; + case error.POSITION_UNAVAILABLE: + alert("Location information is unavailable."); + break; + case error.TIMEOUT: + alert("The request to get user location timed out."); + break; + case error.UNKNOWN_ERROR: + alert("An unknown error occurred."); + break; + } + } + }else{ + alert("Browser doesn't support geolocation!"); + } + }); }); \ No newline at end of file diff --git a/templates/_macros.html b/templates/_macros.html index 9e50ee0..c73bfb9 100644 --- a/templates/_macros.html +++ b/templates/_macros.html @@ -567,49 +567,63 @@ $( function() { {% endmacro %} {% macro render_event_dates_filter_form(form) %} -