From e5ac65de59f45f2d15ae6f6b840b02cac78afd03 Mon Sep 17 00:00:00 2001 From: Jeremy Kersten Date: Wed, 7 Jan 2015 18:11:24 +0100 Subject: [PATCH] [IMP] website_customer: add tags on partners. That allows to sort/filter the customers on website. Old categories [res.partner.category] (named Tags in UI) was confusing with tags and have been renamed into Categories. --- addons/website_customer/__init__.py | 1 + addons/website_customer/__openerp__.py | 3 + addons/website_customer/controllers/main.py | 26 +++++++- addons/website_customer/models/__init__.py | 1 + .../models/website_customer.py | 44 +++++++++++++ .../security/ir.model.access.csv | 3 + addons/website_customer/security/ir_rule.xml | 13 ++++ .../views/website_customer.xml | 16 ++++- .../views/website_customer_view.xml | 62 +++++++++++++++++++ openerp/addons/base/res/res_partner.py | 4 +- openerp/addons/base/res/res_partner_view.xml | 22 +++---- 11 files changed, 180 insertions(+), 15 deletions(-) create mode 100644 addons/website_customer/models/__init__.py create mode 100644 addons/website_customer/models/website_customer.py create mode 100644 addons/website_customer/security/ir.model.access.csv create mode 100644 addons/website_customer/security/ir_rule.xml create mode 100644 addons/website_customer/views/website_customer_view.xml diff --git a/addons/website_customer/__init__.py b/addons/website_customer/__init__.py index 3a37b9285ec..afa6323de2c 100644 --- a/addons/website_customer/__init__.py +++ b/addons/website_customer/__init__.py @@ -20,3 +20,4 @@ ############################################################################## import controllers +import models diff --git a/addons/website_customer/__openerp__.py b/addons/website_customer/__openerp__.py index bea006fb3dc..8f57a096116 100644 --- a/addons/website_customer/__openerp__.py +++ b/addons/website_customer/__openerp__.py @@ -40,6 +40,9 @@ OpenERP Customer References ], 'data': [ 'views/website_customer.xml', + 'views/website_customer_view.xml', + 'security/ir.model.access.csv', + 'security/ir_rule.xml', ], 'qweb': [], 'installable': True, diff --git a/addons/website_customer/controllers/main.py b/addons/website_customer/controllers/main.py index 793d6067fb7..2b874f722e3 100644 --- a/addons/website_customer/controllers/main.py +++ b/addons/website_customer/controllers/main.py @@ -7,6 +7,7 @@ from openerp.tools.translate import _ from openerp.addons.web.http import request import werkzeug.urls + class WebsiteCustomer(http.Controller): _references_per_page = 20 @@ -17,10 +18,17 @@ class WebsiteCustomer(http.Controller): '/customers/country/-', '/customers/country//page/', '/customers/country/-/page/', + '/customers/tag/', + '/customers/tag//page/', + '/customers/tag//country/', + '/customers/tag//country/-', + '/customers/tag//country//page/', + '/customers/tag//country/-/page/', ], type='http', auth="public", website=True) - def customers(self, country_id=0, page=0, country_name='', **post): + def customers(self, country_id=0, page=0, country_name='', tag_id=0, **post): cr, uid, context = request.cr, request.uid, request.context country_obj = request.registry['res.country'] + tag_obj = request.registry['res.partner.tag'] partner_obj = request.registry['res.partner'] partner_name = post.get('search', '') @@ -32,6 +40,13 @@ class WebsiteCustomer(http.Controller): ('website_description', 'ilike', post.get("search")) ] + if tag_id: + try: + tag_id = int(tag_id.split("-")[-1]) + except: + tag_id = 0 + domain += [('tag_ids', 'in', tag_id)] + # group by country, based on customers found with the search(domain) countries = partner_obj.read_group( cr, openerp.SUPERUSER_ID, domain, ["id", "country_id"], @@ -49,6 +64,7 @@ class WebsiteCustomer(http.Controller): 'country_id': (country_id, country['name']) }) countries.sort(key=lambda d: d['country_id'][1]) + curr_country = country_obj.browse(cr, uid, country_id, context) countries.insert(0, { 'country_id_count': country_count, @@ -73,14 +89,22 @@ class WebsiteCustomer(http.Controller): google_map_partner_ids = ','.join(map(str, partner_ids)) partners = partner_obj.browse(request.cr, openerp.SUPERUSER_ID, partner_ids, request.context) + tag_obj = request.registry['res.partner.tag'] + tag_ids = tag_obj.get_tags_for_partners(cr, uid, partner_ids, context=context) + tags = tag_obj.browse(cr, uid, tag_ids, context=context) + tag = tag_id and tag_obj.browse(cr, uid, tag_id, context=context) or False + values = { 'countries': countries, 'current_country_id': country_id or 0, + 'current_country': country_id and curr_country or False, 'partners': partners, 'google_map_partner_ids': google_map_partner_ids, 'pager': pager, 'post': post, 'search_path': "?%s" % werkzeug.url_encode(post), + 'tag': tag, + 'tags': tags, } return request.website.render("website_customer.index", values) diff --git a/addons/website_customer/models/__init__.py b/addons/website_customer/models/__init__.py new file mode 100644 index 00000000000..e130830196d --- /dev/null +++ b/addons/website_customer/models/__init__.py @@ -0,0 +1 @@ +import website_customer diff --git a/addons/website_customer/models/website_customer.py b/addons/website_customer/models/website_customer.py new file mode 100644 index 00000000000..2bca630d49a --- /dev/null +++ b/addons/website_customer/models/website_customer.py @@ -0,0 +1,44 @@ +from openerp.osv import osv, fields + + +class res_partner(osv.Model): + _inherit = 'res.partner' + + _columns = { + 'tag_ids': fields.many2many('res.partner.tag', id1='partner_id', id2='tag_id', string='Tags'), + } + + +class res_partner_tags(osv.Model): + _description = 'Partner Tags - These tags can be used on website to find customers by sector, or ... ' + _name = 'res.partner.tag' + + def getSelectionClass(self, cr, uid, context=None): + classname = ['default', 'primary', 'success', 'warning', 'danger'] + return [(x, str.title(x)) for x in classname] + + _columns = { + 'name': fields.char('Category Name', required=True, translate=True), + 'partner_ids': fields.many2many('res.partner', 'res_partner_res_partner_tag_rel', id1='tag_id', id2='partner_id', string='Partners'), + 'classname': fields.selection(getSelectionClass, 'Class', help="Bootstrap class to customize the color of the tag", required=True), + 'website_published': fields.boolean('Publish', help="The publish field allows you to show the tag on website."), + 'active': fields.boolean('Active'), + } + _defaults = { + 'active': 1, + 'website_published': 1, + 'classname': 'default', + } + + def get_tags_for_partners(self, cr, uid, partner_ids=[], context=None): + cr.execute(""" + SELECT distinct rel.tag_id, t.name + FROM res_partner_res_partner_tag_rel as rel + JOIN res_partner_tag as t on rel.tag_id = t.id + WHERE partner_id = ANY(%s) + GROUP BY t.name, rel.tag_id + ORDER BY t.name ASC + + """, (partner_ids,)) + tags = cr.dictfetchall() + return [x['tag_id'] for x in tags] diff --git a/addons/website_customer/security/ir.model.access.csv b/addons/website_customer/security/ir.model.access.csv new file mode 100644 index 00000000000..ceb1599dbf7 --- /dev/null +++ b/addons/website_customer/security/ir.model.access.csv @@ -0,0 +1,3 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +res_partner_tag,res.partner.tag,model_res_partner_tag,,1,0,0,0 +res_partner_tag_edition,res.partner.tag,model_res_partner_tag,base.group_sale_manager,1,1,1,1 diff --git a/addons/website_customer/security/ir_rule.xml b/addons/website_customer/security/ir_rule.xml new file mode 100644 index 00000000000..304409dbdf6 --- /dev/null +++ b/addons/website_customer/security/ir_rule.xml @@ -0,0 +1,13 @@ + + + + + + Partner Tag: published only + + [('website_published', '=', True)] + + + + + diff --git a/addons/website_customer/views/website_customer.xml b/addons/website_customer/views/website_customer.xml index faeb256130c..63361d70484 100644 --- a/addons/website_customer/views/website_customer.xml +++ b/addons/website_customer/views/website_customer.xml @@ -97,7 +97,7 @@
  • - + @@ -109,6 +109,20 @@ + +