Repository: fossasia/open-event-server

Issue:
Preselect timezone when creating new event
When I create a new event, the Timezone dropdown should have a default value based on my location. I am in Vietnam, for example, the dropdown should be "UTC+7" by default.

![selection_051](https://cloud.githubusercontent.com/assets/314607/19408938/e4703c7e-92f2-11e6-9426-cf54c787ab90.png)



Discussion:

Resolving patch:
diff --git a/app/helpers/flask_helpers.py b/app/helpers/flask_helpers.py
--- a/app/helpers/flask_helpers.py
+++ b/app/helpers/flask_helpers.py
@@ -1,19 +1,25 @@
 import re
+from urllib2 import urlopen
+
 from flask import request
 from flask.json import JSONEncoder
 from jinja2 import Undefined
 from slugify import slugify as unicode_slugify
 from slugify import SLUG_OK
 
-def get_real_ip():
+def get_real_ip(local_correct=False):
     try:
         if 'X-Forwarded-For' in request.headers:
-            return request.headers.getlist("X-Forwarded-For")[0].rpartition(' ')[-1]
+            ip = request.headers.getlist("X-Forwarded-For")[0].rpartition(' ')[-1]
         else:
-            return request.remote_addr or 'untrackable'
+            ip = request.remote_addr or None
+
+        if local_correct and (ip == '127.0.0.1' or ip == '0.0.0.0'):
+            ip = urlopen('http://ip.42.pl/raw').read()  # On local test environments
     except:
-        return 'untrackable'
+        ip = None
 
+    return ip
 
 class MiniJSONEncoder(JSONEncoder):
     """Minify JSON output."""
diff --git a/app/views/admin/models_views/events.py b/app/views/admin/models_views/events.py
--- a/app/views/admin/models_views/events.py
+++ b/app/views/admin/models_views/events.py
@@ -10,8 +10,10 @@
 from flask.ext.admin import BaseView
 from flask.ext.restplus import abort
 from flask_admin import expose
+from geoip import geolite2
 
 from app import db
+from app.helpers.flask_helpers import get_real_ip
 from app.helpers.invoicing import InvoicingManager
 from app.helpers.storage import upload, upload_local, UPLOAD_PATHS
 from app.helpers.helpers import uploaded_file
@@ -139,6 +141,12 @@ def create_view(self, ):
         if CallForPaper.query.filter_by(hash=hash).all():
             hash = get_random_hash()
 
+        match = geolite2.lookup(get_real_ip(True) or '127.0.0.1')
+        if match is not None:
+            current_timezone = match.timezone
+        else:
+            current_timezone = 'UTC'
+
         return self.render(
             '/gentelella/admin/event/new/new.html',
             current_date=datetime.datetime.now(),
@@ -149,6 +157,7 @@ def create_view(self, ):
             event_sub_topics=DataGetter.get_event_subtopics(),
             timezones=DataGetter.get_all_timezones(),
             cfs_hash=hash,
+            current_timezone=current_timezone,
             payment_countries=DataGetter.get_payment_countries(),
             payment_currencies=DataGetter.get_payment_currencies(),
             included_settings=self.get_module_settings())
@@ -329,6 +338,12 @@ def edit_view_stepped(self, event_id, step):
             if CallForPaper.query.filter_by(hash=hash).all():
                 hash = get_random_hash()
 
+            match = geolite2.lookup(get_real_ip(True) or '127.0.0.1')
+            if match is not None:
+                current_timezone = match.timezone
+            else:
+                current_timezone = 'UTC'
+
             return self.render('/gentelella/admin/event/edit/edit.html',
                                event=event,
                                session_types=session_types,
@@ -342,6 +357,7 @@ def edit_view_stepped(self, event_id, step):
                                event_topics=DataGetter.get_event_topics(),
                                event_sub_topics=DataGetter.get_event_subtopics(),
                                preselect=preselect,
+                               current_timezone=current_timezone,
                                timezones=DataGetter.get_all_timezones(),
                                cfs_hash=hash,
                                step=step,
diff --git a/app/views/api_v1_views.py b/app/views/api_v1_views.py
--- a/app/views/api_v1_views.py
+++ b/app/views/api_v1_views.py
@@ -536,9 +536,8 @@ def documentation():
 
 @app.route('/api/location/', methods=('GET', 'POST'))
 def location():
-    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
+    ip = get_real_ip(True)
+
     try:
         reader = geoip2.database.Reader(os.path.realpath('.') + '/static/data/GeoLite2-Country.mmdb')
         response = reader.country(ip)


Test patch: