[IMP] website: set geoip in user session
This commit is contained in:
parent
3160eb74c4
commit
890a9ac652
|
@ -24,6 +24,7 @@ class ir_http(orm.AbstractModel):
|
|||
_inherit = 'ir.http'
|
||||
|
||||
rerouting_limit = 10
|
||||
geo_ip_resolver = None
|
||||
|
||||
def _get_converters(self):
|
||||
return dict(
|
||||
|
@ -53,6 +54,18 @@ class ir_http(orm.AbstractModel):
|
|||
|
||||
request.website_multilang = request.website_enabled and func and func.routing.get('multilang', True)
|
||||
|
||||
if not request.session.has_key('geoip'):
|
||||
record = {}
|
||||
if self.geo_ip_resolver is None:
|
||||
try:
|
||||
import GeoIP
|
||||
self.geo_ip_resolver = GeoIP.open('/usr/share/GeoIP/GeoIP.dat', GeoIP.GEOIP_STANDARD)
|
||||
except ImportError:
|
||||
self.geo_ip_resolver = False
|
||||
if self.geo_ip_resolver:
|
||||
record = self.geo_ip_resolver.record_by_addr(request.httprequest.remote_addr)
|
||||
request.session['geoip'] = record
|
||||
|
||||
if request.website_enabled:
|
||||
if func:
|
||||
self._authenticate(func.routing['auth'])
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import logging
|
||||
import re
|
||||
import werkzeug
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
try:
|
||||
import GeoIP
|
||||
except ImportError:
|
||||
GeoIP = None
|
||||
_logger.warn("Please install GeoIP python module to use events localisation.")
|
||||
|
||||
from openerp import SUPERUSER_ID
|
||||
from openerp.addons.web import http
|
||||
from openerp.addons.web.http import request
|
||||
|
@ -20,12 +11,6 @@ from openerp.tools.translate import _
|
|||
class WebsiteCrmPartnerAssign(http.Controller):
|
||||
_references_per_page = 40
|
||||
|
||||
def _get_current_country_code(self):
|
||||
if not GeoIP:
|
||||
return False
|
||||
GI = GeoIP.open('/usr/share/GeoIP/GeoIP.dat', 0)
|
||||
return GI.country_code_by_addr(request.httprequest.remote_addr)
|
||||
|
||||
@http.route([
|
||||
'/partners',
|
||||
'/partners/page/<int:page>',
|
||||
|
@ -52,7 +37,7 @@ class WebsiteCrmPartnerAssign(http.Controller):
|
|||
# group by grade
|
||||
grade_domain = list(base_partner_domain)
|
||||
if not country and not country_all:
|
||||
country_code = self._get_current_country_code()
|
||||
country_code = request.session['geoip'].get('country_code')
|
||||
if country_code:
|
||||
country_ids = country_obj.search(request.cr, request.uid, [('code', '=', country_code)], context=request.context)
|
||||
if country_ids:
|
||||
|
|
|
@ -19,28 +19,18 @@
|
|||
#
|
||||
##############################################################################
|
||||
|
||||
import logging
|
||||
import time
|
||||
from datetime import datetime, timedelta
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
import werkzeug.urls
|
||||
|
||||
from openerp import SUPERUSER_ID
|
||||
from openerp import http
|
||||
from openerp import tools
|
||||
from openerp.http import request
|
||||
from openerp.tools.translate import _
|
||||
from openerp.addons.website.models.website import slug
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
try:
|
||||
import GeoIP
|
||||
except ImportError:
|
||||
GeoIP = None
|
||||
_logger.warn("Please install GeoIP python module to use events localisation.")
|
||||
|
||||
class website_event(http.Controller):
|
||||
@http.route(['/event', '/event/page/<int:page>'], type='http', auth="public", website=True)
|
||||
def events(self, page=1, **searches):
|
||||
|
@ -214,10 +204,6 @@ class website_event(http.Controller):
|
|||
event = Event.browse(request.cr, request.uid, event_id, context=context)
|
||||
return request.redirect("/event/%s/register?enable_editor=1" % slug(event))
|
||||
|
||||
def get_visitors_country(self):
|
||||
GI = GeoIP.open('/usr/share/GeoIP/GeoIP.dat', 0)
|
||||
return {'country_code': GI.country_code_by_addr(request.httprequest.remote_addr), 'country_name': GI.country_name_by_addr(request.httprequest.remote_addr)}
|
||||
|
||||
def get_formated_date(self, event):
|
||||
start_date = datetime.strptime(event.date_begin, tools.DEFAULT_SERVER_DATETIME_FORMAT).date()
|
||||
end_date = datetime.strptime(event.date_end, tools.DEFAULT_SERVER_DATETIME_FORMAT).date()
|
||||
|
@ -225,21 +211,19 @@ class website_event(http.Controller):
|
|||
|
||||
@http.route('/event/get_country_event_list', type='http', auth='public', website=True)
|
||||
def get_country_events(self ,**post):
|
||||
if not GeoIP:
|
||||
return ""
|
||||
cr, uid, context, event_ids = request.cr, request.uid, request.context,[]
|
||||
country_obj = request.registry['res.country']
|
||||
event_obj = request.registry['event.event']
|
||||
cr, uid, context,event_ids = request.cr, request.uid, request.context,[]
|
||||
country_code = self.get_visitors_country()['country_code']
|
||||
country_code = request.session['geoip'].get('country_code')
|
||||
result = {'events':[],'country':False}
|
||||
if country_code:
|
||||
country_ids = country_obj.search(request.cr, request.uid, [('code', '=', country_code)], context=request.context)
|
||||
event_ids = event_obj.search(request.cr, request.uid, ['|', ('address_id', '=', None),('country_id.code', '=', country_code),('date_begin','>=', time.strftime('%Y-%m-%d 00:00:00')),('state', '=', 'confirm')], order="date_begin", context=request.context)
|
||||
country_ids = country_obj.search(cr, uid, [('code', '=', country_code)], context=context)
|
||||
event_ids = event_obj.search(cr, uid, ['|', ('address_id', '=', None),('country_id.code', '=', country_code),('date_begin','>=', time.strftime('%Y-%m-%d 00:00:00')),('state', '=', 'confirm')], order="date_begin", context=context)
|
||||
if not event_ids:
|
||||
event_ids = event_obj.search(request.cr, request.uid, [('date_begin','>=', time.strftime('%Y-%m-%d 00:00:00')),('state', '=', 'confirm')], order="date_begin", context=request.context)
|
||||
for event in event_obj.browse(request.cr, request.uid, event_ids, context=request.context)[:6]:
|
||||
event_ids = event_obj.search(cr, uid, [('date_begin','>=', time.strftime('%Y-%m-%d 00:00:00')),('state', '=', 'confirm')], order="date_begin", context=context)
|
||||
for event in event_obj.browse(cr, uid, event_ids, context=context)[:6]:
|
||||
if country_code and event.country_id.code == country_code:
|
||||
result['country'] = country_obj.browse(request.cr, request.uid, country_ids[0], context=request.context)
|
||||
result['country'] = country_obj.browse(cr, uid, country_ids[0], context=context)
|
||||
result['events'].append({
|
||||
"date": self.get_formated_date(event),
|
||||
"event": event,
|
||||
|
|
Loading…
Reference in New Issue