c13ba88a17f142df66327212d000c64c6ad39046
[odoo/odoo.git] / addons / website_event / models / event.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from openerp.osv import osv, fields
23 from openerp import SUPERUSER_ID
24
25 from openerp.tools.translate import _
26 import re
27
28 from openerp.addons.website.models.website import slug
29
30
31 class event(osv.osv):
32     _name = 'event.event'
33     _inherit = ['event.event','website.seo.metadata']
34
35     def _get_new_menu_pages(self, cr, uid, event, context=None):
36         context = context or {}
37         todo = [
38             (_('Introduction'), 'website_event.template_intro'),
39             (_('Location'), 'website_event.template_location')
40         ]
41         web = self.pool.get('website')
42         result = []
43         for name,path in todo:
44             name2 = name+' '+event.name
45             newpath = web.new_page(cr, uid, name2, path, ispage=False, context=context)
46             url = "/event/"+slug(event)+"/page/" + newpath
47             result.append((name, url))
48         return result
49
50     def _set_show_menu(self, cr, uid, ids, name, value, arg, context=None):
51         menuobj = self.pool.get('website.menu')
52         eventobj = self.pool.get('event.event')
53         for event in self.browse(cr, uid, [ids], context=context):
54             if event.menu_id and not value:
55                 menuobj.unlink(cr, uid, [event.menu_id.id], context=context)
56             elif value and not event.menu_id:
57                 root = menuobj.create(cr, uid, {
58                     'name': event.name
59                 }, context=context)
60                 tocreate = self._get_new_menu_pages(cr, uid, event, context)
61                 tocreate.append((_('Register'), '/event/%s/register' % slug(event)))
62                 sequence = 0
63                 for name,url in tocreate:
64                     menuobj.create(cr, uid, {
65                         'name': name,
66                         'url': url,
67                         'parent_id': root,
68                         'sequence': sequence
69                     }, context=context)
70                     sequence += 1
71                 eventobj.write(cr, uid, [event.id], {'menu_id': root}, context=context)
72         return True
73
74     def _get_show_menu(self, cr, uid, ids, field_name, arg, context=None):
75         res = dict.fromkeys(ids, '')
76         for event in self.browse(cr, uid, ids, context=context):
77             res[event.id] = bool(event.menu_id)
78         return res
79
80     def _website_url(self, cr, uid, ids, field_name, arg, context=None):
81         res = dict.fromkeys(ids, '')
82         for event in self.browse(cr, uid, ids, context=context):
83             res[event.id] = "/event/" + slug(event)
84         return res
85
86     def _default_hashtag(self, cr, uid, context={}):
87         name = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.name
88         return re.sub("[- \\.\\(\\)\\@\\#\\&]+", "", name).lower()
89
90     _columns = {
91         'twitter_hashtag': fields.char('Twitter Hashtag'),
92         'website_published': fields.boolean('Visible in Website', copy=False),
93         # TDE TODO FIXME: when website_mail/mail_thread.py inheritance work -> this field won't be necessary
94         'website_message_ids': fields.one2many(
95             'mail.message', 'res_id',
96             domain=lambda self: [
97                 '&', ('model', '=', self._name), ('type', '=', 'comment')
98             ],
99             string='Website Messages',
100             help="Website communication history",
101         ),
102         'website_url': fields.function(_website_url, string="Website url", type="char"),
103         'show_menu': fields.function(_get_show_menu, fnct_inv=_set_show_menu, type='boolean', string='Dedicated Menu'),
104         'menu_id': fields.many2one('website.menu', 'Event Menu'),
105     }
106     _defaults = {
107         'show_menu': False,
108         'twitter_hashtag': _default_hashtag
109     }
110
111     def google_map_img(self, cr, uid, ids, zoom=8, width=298, height=298, context=None):
112         event = self.browse(cr, uid, ids[0], context=context)
113         if event.address_id:
114             return self.browse(cr, SUPERUSER_ID, ids[0], context=context).address_id.google_map_img()
115         return None
116
117     def google_map_link(self, cr, uid, ids, zoom=8, context=None):
118         event = self.browse(cr, uid, ids[0], context=context)
119         if event.address_id:
120             return self.browse(cr, SUPERUSER_ID, ids[0], context=context).address_id.google_map_link()
121         return None
122