[IMP] website_crm_partner_assign: use geoip to display partners in the visitor's country.

Also added a breadcrumb in partners navigation; when going on the partner view
a link to come back to the list view has been added and takes into account
selected grade and country.
This commit is contained in:
Thibault Delavallée
2014-05-20 13:24:18 +02:00
parent c1a0d23422
commit 96128a678e
2 changed files with 51 additions and 9 deletions
@@ -1,8 +1,15 @@
# -*- 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
@@ -13,6 +20,12 @@ 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>',
@@ -27,7 +40,9 @@ class WebsiteCrmPartnerAssign(http.Controller):
'/partners/grade/<model("res.partner.grade"):grade>/country/<model("res.country"):country>/page/<int:page>',
], type='http', auth="public", website=True, multilang=True)
def partners(self, country=None, grade=None, page=0, **post):
country_all = post.pop('country_all', False)
partner_obj = request.registry['res.partner']
country_obj = request.registry['res.country']
search = post.get('search', '')
base_partner_domain = [('is_company', '=', True), ('grade_id.website_published', '=', True), ('website_published', '=', True)]
@@ -36,6 +51,12 @@ 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()
if country_code:
country_ids = country_obj.search(request.cr, request.uid, [('code', '=', country_code)], context=request.context)
if country_ids:
country = country_obj.browse(request.cr, request.uid, country_ids[0], context=request.context)
if country:
grade_domain += [('country_id', '=', country.id)]
grades = partner_obj.read_group(
@@ -118,7 +139,7 @@ class WebsiteCrmPartnerAssign(http.Controller):
'google_map_partner_ids': google_map_partner_ids,
'pager': pager,
'searches': post,
'search_path': "?%s" % werkzeug.url_encode(post),
'search_path': "%s" % werkzeug.url_encode(post),
}
return request.website.render("website_crm_partner_assign.index", values)
@@ -126,11 +147,26 @@ class WebsiteCrmPartnerAssign(http.Controller):
@http.route(['/partners/<partner_id>'], type='http', auth="public", website=True, multilang=True)
def partners_detail(self, partner_id, partner_name='', **post):
mo = re.search('-([-0-9]+)$', str(partner_id))
current_grade, current_country = None, None
grade_id = post.get('grade_id')
country_id = post.get('country_id')
if grade_id:
grade_ids = request.registry['res.partner.grade'].exists(request.cr, request.uid, int(grade_id), context=request.context)
if grade_ids:
current_grade = request.registry['res.partner.grade'].browse(request.cr, request.uid, grade_ids[0], context=request.context)
if country_id:
country_ids = request.registry['res.country'].exists(request.cr, request.uid, int(country_id), context=request.context)
if country_ids:
current_country = request.registry['res.country'].browse(request.cr, request.uid, country_ids[0], context=request.context)
if mo:
partner_id = int(mo.group(1))
partner = request.registry['res.partner'].browse(request.cr, SUPERUSER_ID, partner_id, context=request.context)
if partner.exists() and partner.website_published:
values = {}
values['main_object'] = values['partner'] = partner
values = {
'main_object': partner,
'partner': partner,
'current_grade': current_grade,
'current_country': current_country
}
return request.website.render("website_crm_partner_assign.partner", values)
return self.partners(**post)
@@ -15,7 +15,7 @@
<t t-set="additional_title">Resellers</t>
<div id="wrap">
<div class="oe_structure"/>
<div class="container">
<div class="container mt16">
<div class="row">
<t t-raw="ref_content" />
</div>
@@ -42,7 +42,7 @@
<li class="nav-header"><h3>Filter by Grade</h3></li>
<t t-foreach="grades" t-as="grade">
<li t-att-class="grade['active'] and 'active' or ''">
<a t-attf-href="/partners/#{ grade['grade_id'][0] and 'grade/%s/' % grade['grade_id'][0] or '' }#{ current_country and 'country/%s/' % slug(current_country) or '' }#{ search_path }">
<a t-attf-href="/partners#{ grade['grade_id'][0] and '/grade/%s' % grade['grade_id'][0] or '' }#{ current_country and '/country/%s' % slug(current_country) or '' }#{ '?' + (search_path or '') }">
<span class="badge pull-right" t-esc="grade['grade_id_count'] or ''"/>
<t t-esc="grade['grade_id'][1]"/>
</a>
@@ -54,7 +54,7 @@
<li class="nav-header"><h3>Filter by Country</h3></li>
<t t-foreach="countries" t-as="country">
<li t-if="country['country_id']" t-att-class="country['active'] and 'active' or ''">
<a t-attf-href="/partners/#{ current_grade and 'grade/%s/' % slug(current_grade) or ''}#{country['country_id'][0] and 'country/%s/' % country['country_id'][0] or '' }#{ search_path }">
<a t-attf-href="/partners#{ current_grade and '/grade/%s' % slug(current_grade) or ''}#{country['country_id'][0] and '/country/%s' % country['country_id'][0] or '' }#{ '?' + (search_path or '') + (country['country_id'][0] == 0 and 'country_all=True' or '')}">
<span class="badge pull-right" t-esc="country['country_id_count'] or ''"/>
<t t-esc="country['country_id'][1]"/>
</a>
@@ -85,12 +85,12 @@
<t t-set="last_grade" t-value="partner.grade_id.id"/>
</t>
<div class="media">
<a class="pull-left" t-attf-href="/partners/#{slug(partner)}"
<a class="pull-left" t-attf-href="/partners/#{slug(partner)}?#{current_grade and 'grade_id=%s&amp;' % current_grade.id}#{current_country and 'country_id=%s' % current_country.id}"
t-field="partner.image_small"
t-field-options='{"widget": "image", "class": "media-object"}'
></a>
<div class="media-body o_partner_body" style="min-height: 64px;">
<a class="media-heading" t-attf-href="/partners/#{slug(partner)}">
<a class="media-heading" t-attf-href="/partners/#{slug(partner)}?#{current_grade and 'grade_id=%s&amp;' % current_grade.id}#{current_country and 'country_id=%s' % current_country.id}">
<span t-field="partner.display_name"/>
</a>
<div t-field="partner.website_short_description"/>
@@ -126,6 +126,12 @@
<template id="partner" name="Partner Detail">
<t t-call="website_crm_partner_assign.layout">
<t t-set="ref_content">
<div class="col-md-5">
<ol class="breadcrumb">
<li><a t-attf-href="/partners#{current_grade and '/grade/%s' % slug(current_grade)}#{current_country and '/country/%s' % slug(current_country)}">Our Partners</a></li>
<li class="active"><span t-field="partner.display_name"/></li>
</ol>
</div>
<t t-call="website_partner.partner_detail">
<t t-set="right_column">
<div id="right_column" class="mb16"><t t-call="website_crm_partner_assign.references_block"/></div>