[MERGE] forward port of branch saas-3 up to fdc6271
[odoo/odoo.git] / addons / website_event_sale / controllers / main.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 import SUPERUSER_ID
23 from openerp.addons.web import http
24 from openerp.addons.web.http import request
25 from openerp.addons.website_event.controllers.main import website_event
26 from openerp.tools.translate import _
27
28
29 class website_event(website_event):
30
31     @http.route(['/event/cart/update'], type='http', auth="public", methods=['POST'], website=True)
32     def cart_update(self, event_id, **post):
33         cr, uid, context = request.cr, request.uid, request.context
34         ticket_obj = request.registry.get('event.event.ticket')
35
36         sale = False
37         for key, value in post.items():
38             quantity = int(value or "0")
39             if not quantity:
40                 continue
41             sale = True
42             ticket_id = key.split("-")[0] == 'ticket' and int(key.split("-")[1]) or None
43             ticket = ticket_obj.browse(cr, SUPERUSER_ID, ticket_id, context=context)
44             request.website.sale_get_order(force_create=1)._cart_update(
45                 product_id=ticket.product_id.id, add_qty=quantity, context=dict(context, event_ticket_id=ticket.id))
46             if 'tax_id' in _values:
47                 _values['tax_id'] = [(6, 0, _values['tax_id'])]
48
49         if not sale:
50             return request.redirect("/event/%s" % event_id)
51         return request.redirect("/shop/checkout")
52
53     def _add_event(self, event_name="New Event", context={}, **kwargs):
54         try:
55             dummy, res_id = request.registry.get('ir.model.data').get_object_reference(request.cr, request.uid, 'event_sale', 'product_product_event')
56             context['default_event_ticket_ids'] = [[0,0,{
57                 'name': _('Subscription'),
58                 'product_id': res_id,
59                 'deadline' : False,
60                 'seats_max': 1000,
61                 'price': 0,
62             }]]
63         except ValueError:
64             pass
65         return super(website_event, self)._add_event(event_name, context, **kwargs)
66
67
68