~openerp-dev/openobject-addons/saas-4-fixes-tde

« back to all changes in this revision

Viewing changes to website_crm_partner_assign/controllers/main.py

  • Committer: Thibault Delavallée
  • Date: 2014-05-20 10:59:23 UTC
  • Revision ID: tde@openerp.com-20140520105923-jboj4owfzljp9a90
[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.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- coding: utf-8 -*-
 
2
import logging
2
3
import re
3
 
 
4
4
import werkzeug
5
5
 
 
6
_logger = logging.getLogger(__name__)
 
7
try:
 
8
    import GeoIP
 
9
except ImportError:
 
10
    GeoIP = None
 
11
    _logger.warn("Please install GeoIP python module to use events localisation.")
 
12
 
6
13
from openerp import SUPERUSER_ID
7
14
from openerp.addons.web import http
8
15
from openerp.addons.web.http import request
13
20
class WebsiteCrmPartnerAssign(http.Controller):
14
21
    _references_per_page = 40
15
22
 
 
23
    def _get_current_country_code(self):
 
24
        if not GeoIP:
 
25
            return False
 
26
        GI = GeoIP.open('/usr/share/GeoIP/GeoIP.dat', 0)
 
27
        return GI.country_code_by_addr(request.httprequest.remote_addr)
 
28
 
16
29
    @http.route([
17
30
        '/partners',
18
31
        '/partners/page/<int:page>',
27
40
        '/partners/grade/<model("res.partner.grade"):grade>/country/<model("res.country"):country>/page/<int:page>',
28
41
    ], type='http', auth="public", website=True, multilang=True)
29
42
    def partners(self, country=None, grade=None, page=0, **post):
 
43
        country_all = post.pop('country_all', False)
30
44
        partner_obj = request.registry['res.partner']
 
45
        country_obj = request.registry['res.country']
31
46
        search = post.get('search', '')
32
47
 
33
48
        base_partner_domain = [('is_company', '=', True), ('grade_id.website_published', '=', True), ('website_published', '=', True)]
36
51
 
37
52
        # group by grade
38
53
        grade_domain = list(base_partner_domain)
 
54
        if not country and not country_all:
 
55
            country_code = self._get_current_country_code()
 
56
            if country_code:
 
57
                country_ids = country_obj.search(request.cr, request.uid, [('code', '=', country_code)], context=request.context)
 
58
                if country_ids:
 
59
                    country = country_obj.browse(request.cr, request.uid, country_ids[0], context=request.context)
39
60
        if country:
40
61
            grade_domain += [('country_id', '=', country.id)]
41
62
        grades = partner_obj.read_group(
118
139
            'google_map_partner_ids': google_map_partner_ids,
119
140
            'pager': pager,
120
141
            'searches': post,
121
 
            'search_path': "?%s" % werkzeug.url_encode(post),
 
142
            'search_path': "%s" % werkzeug.url_encode(post),
122
143
        }
123
144
        return request.website.render("website_crm_partner_assign.index", values)
124
145
 
126
147
    @http.route(['/partners/<partner_id>'], type='http', auth="public", website=True, multilang=True)
127
148
    def partners_detail(self, partner_id, partner_name='', **post):
128
149
        mo = re.search('-([-0-9]+)$', str(partner_id))
 
150
        current_grade, current_country = None, None
 
151
        grade_id = post.get('grade_id')
 
152
        country_id = post.get('country_id')
 
153
        if grade_id:
 
154
            grade_ids = request.registry['res.partner.grade'].exists(request.cr, request.uid, int(grade_id), context=request.context)
 
155
            if grade_ids:
 
156
                current_grade = request.registry['res.partner.grade'].browse(request.cr, request.uid, grade_ids[0], context=request.context)
 
157
        if country_id:
 
158
            country_ids = request.registry['res.country'].exists(request.cr, request.uid, int(country_id), context=request.context)
 
159
            if country_ids:
 
160
                current_country = request.registry['res.country'].browse(request.cr, request.uid, country_ids[0], context=request.context)
129
161
        if mo:
130
162
            partner_id = int(mo.group(1))
131
163
            partner = request.registry['res.partner'].browse(request.cr, SUPERUSER_ID, partner_id, context=request.context)
132
164
            if partner.exists() and partner.website_published:
133
 
                values = {}
134
 
                values['main_object'] = values['partner'] = partner
 
165
                values = {
 
166
                    'main_object': partner,
 
167
                    'partner': partner,
 
168
                    'current_grade': current_grade,
 
169
                    'current_country': current_country
 
170
                }
135
171
                return request.website.render("website_crm_partner_assign.partner", values)
136
172
        return self.partners(**post)