Repository: fossasia/open-event-server

Issue:
Make /browse page show the events nearby to the user



Discussion:

Resolving patch:
diff --git a/open_event/helpers/flask_helpers.py b/open_event/helpers/flask_helpers.py
--- a/open_event/helpers/flask_helpers.py
+++ b/open_event/helpers/flask_helpers.py
@@ -1,5 +1,8 @@
+import re
 from flask import request
 from jinja2 import Undefined
+from unidecode import unidecode
+
 
 def get_real_ip():
     try:
@@ -24,3 +27,14 @@ def _fail_with_undefined_error(self, *args, **kwargs):
         __float__ = __complex__ = __pow__ = __rpow__ = \
         _fail_with_undefined_error
 
+_punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
+
+def slugify(text, delim=u'-'):
+    """Generates an ASCII-only slug."""
+    result = []
+    for word in _punct_re.split(text.lower()):
+        result.extend(unidecode(word).split())
+    return unicode(delim.join(result))
+
+def deslugify(text, delim='-'):
+    return text.replace(delim, " ")
diff --git a/open_event/views/admin/home.py b/open_event/views/admin/home.py
--- a/open_event/views/admin/home.py
+++ b/open_event/views/admin/home.py
@@ -1,5 +1,7 @@
 """Copyright 2015 Rafal Kowalski"""
 import logging
+import os
+from urllib2 import urlopen
 
 from flask import url_for, redirect, request, session, flash
 from flask.ext import login
@@ -8,6 +10,7 @@
 from flask.ext.scrypt import generate_password_hash
 from wtforms import ValidationError
 
+from open_event.helpers.flask_helpers import get_real_ip, slugify
 from ...helpers.data import DataManager, save_to_db, get_google_auth, get_facebook_auth, create_user_password, \
     user_logged_in, record_activity
 from ...helpers.data_getter import DataGetter
@@ -15,7 +18,7 @@
     get_serializer, get_request_stats
 from open_event.helpers.oauth import OAuth, FbOAuth
 from open_event.models.user import User
-
+import geoip2.database
 
 def intended_url():
     return request.args.get('next') or url_for('.index')
@@ -180,3 +183,16 @@ def view_user_sessions(self):
     @expose('/forbidden/', methods=('GET',))
     def forbidden_view(self):
         return self.render('/gentelella/admin/forbidden.html')
+
+    @expose('/browse/', methods=('GET',))
+    def browse_view(self):
+        reader = geoip2.database.Reader(os.path.realpath('.') + '/static/data/GeoLite2-Country.mmdb')
+        ip = get_real_ip()
+        if ip == '127.0.0.1' or ip == '0.0.0.0':
+            ip = urlopen('http://ip.42.pl/raw').read()  # On local test environments
+        try:
+            response = reader.country(ip)
+            country = response.country.name
+        except:
+            country = "United States"
+        return redirect(url_for('search_results.browses', location=slugify(country)))
diff --git a/open_event/views/public/browse.py b/open_event/views/public/browse.py
--- a/open_event/views/public/browse.py
+++ b/open_event/views/public/browse.py
@@ -2,6 +2,7 @@
 from flask_admin import BaseView, expose
 
 from open_event.api.helpers.helpers import get_paginated_list
+from open_event.helpers.flask_helpers import deslugify
 from open_event.helpers.helpers import get_date_range
 from open_event.models.event import Event
 from flask import request
@@ -53,6 +54,7 @@ def browse(self, location):
 
     @expose('/s', methods=('GET', 'POST'))
     def browses(self, location):
+        location = deslugify(location)
         current_page = request.args.get('page')
         if not current_page:
             current_page = 1


Test patch: