1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>).
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.
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.
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/>.
20 ##############################################################################
22 from openerp import SUPERUSER_ID
23 from openerp.addons.web import http
24 from openerp.addons.web.http import request
25 from openerp.tools.translate import _
26 from openerp.addons.website.models import website
27 from openerp.addons.website.controllers.main import Website as controllers
28 controllers = controllers()
31 from datetime import datetime, timedelta
32 from dateutil.relativedelta import relativedelta
33 from openerp import tools
36 class website_event(http.Controller):
37 @website.route(['/event/', '/event/page/<int:page>'], type='http', auth="public", multilang=True)
38 def events(self, page=1, **searches):
39 cr, uid, context = request.cr, request.uid, request.context
40 event_obj = request.registry['event.event']
41 type_obj = request.registry['event.type']
42 country_obj = request.registry['res.country']
44 searches.setdefault('date', 'all')
45 searches.setdefault('type', 'all')
46 searches.setdefault('country', 'all')
51 return date.strftime('%Y-%m-%d 23:59:59')
53 return date.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)
54 today = datetime.today()
56 ['all', _('Next Events'), [("date_end", ">", sd(today))], 0],
57 ['today', _('Today'), [
58 ("date_end", ">", sd(today)),
59 ("date_begin", "<", sdn(today))],
61 ['week', _('This Week'), [
62 ("date_end", ">=", sd(today + relativedelta(days=-today.weekday()))),
63 ("date_begin", "<", sdn(today + relativedelta(days=6-today.weekday())))],
65 ['nextweek', _('Next Week'), [
66 ("date_end", ">=", sd(today + relativedelta(days=7-today.weekday()))),
67 ("date_begin", "<", sdn(today + relativedelta(days=13-today.weekday())))],
69 ['month', _('This month'), [
70 ("date_end", ">=", sd(today.replace(day=1))),
71 ("date_begin", "<", (today.replace(day=1) + relativedelta(months=1)).strftime('%Y-%m-%d 00:00:00'))],
73 ['nextmonth', _('Next month'), [
74 ("date_end", ">=", sd(today.replace(day=1) + relativedelta(months=1))),
75 ("date_begin", "<", (today.replace(day=1) + relativedelta(months=2)).strftime('%Y-%m-%d 00:00:00'))],
77 ['old', _('Old Events'), [
78 ("date_end", "<", today.strftime('%Y-%m-%d 00:00:00'))],
85 current_country = None
87 if searches["date"] == date[0]:
88 domain_search["date"] = date[2]
90 current_date = date[1]
91 if searches["type"] != 'all':
92 current_type = type_obj.browse(cr, uid, int(searches['type']), context=context)
93 domain_search["type"] = [("type", "=", int(searches["type"]))]
94 if searches["country"] != 'all':
95 current_country = country_obj.browse(cr, uid, int(searches['country']), context=context)
96 domain_search["country"] = [("country_id", "=", int(searches["country"]))]
98 def dom_without(without):
99 domain = [('state', "in", ['draft','confirm','done'])]
100 for key, search in domain_search.items():
105 # count by domains without self search
108 date[3] = event_obj.search(
109 request.cr, request.uid, dom_without('date') + date[2],
110 count=True, context=request.context)
112 domain = dom_without('type')
113 types = event_obj.read_group(
114 request.cr, request.uid, domain, ["id", "type"], groupby="type",
115 orderby="type", context=request.context)
116 type_count = event_obj.search(request.cr, request.uid, domain,
117 count=True, context=request.context)
119 'type_count': type_count,
120 'type': ("all", _("All Categories"))
123 domain = dom_without('country')
124 countries = event_obj.read_group(
125 request.cr, request.uid, domain, ["id", "country_id"],
126 groupby="country_id", orderby="country_id", context=request.context)
127 country_id_count = event_obj.search(request.cr, request.uid, domain,
128 count=True, context=request.context)
129 countries.insert(0, {
130 'country_id_count': country_id_count,
131 'country_id': ("all", _("All Countries"))
135 event_count = event_obj.search(
136 request.cr, request.uid, dom_without("none"), count=True,
137 context=request.context)
138 pager = request.website.pager(url="/event/", total=event_count, page=page, step=step, scope=5)
140 order = 'website_published desc, date_begin'
141 if searches.get('date','all') == 'old':
142 order = 'website_published desc, date_begin desc'
143 obj_ids = event_obj.search(
144 request.cr, request.uid, dom_without("none"), limit=step,
145 offset=pager['offset'], order=order, context=request.context)
146 events_ids = event_obj.browse(request.cr, request.uid, obj_ids,
147 context=request.context)
150 'current_date': current_date,
151 'current_country': current_country,
152 'current_type': current_type,
153 'event_ids': events_ids,
156 'countries': countries,
158 'searches': searches,
159 'search_path': "?%s" % urllib.urlencode(searches),
162 return request.website.render("website_event.index", values)
164 @website.route(['/event/<model("event.event"):event>/page/<page:page>'], type='http', auth="public", multilang=True)
165 def event_page(self, event, page, **post):
169 return request.website.render(page, values)
171 @website.route(['/event/<model("event.event"):event>'], type='http', auth="public", multilang=True)
172 def event(self, event=None, **post):
173 if event.menu_id and event.menu_id.child_id:
174 return request.redirect(event.menu_id.child_id[0].url)
175 return request.redirect('/event/%s/register' % str(event.id))
177 @website.route(['/event/<model("event.event"):event>/register'], type='http', auth="public", multilang=True)
178 def event_register(self, event=None, **post):
183 return request.website.render("website_event.event_description_full", values)
185 @website.route(['/event/add_cart'], type='http', auth="public", multilang=True)
186 def add_cart(self, event_id, **post):
187 user_obj = request.registry['res.users']
188 order_line_obj = request.registry.get('sale.order.line')
189 ticket_obj = request.registry.get('event.event.ticket')
191 order = request.context['website_sale_order']
193 order = request.registry['website']._get_order(request.cr, request.uid, context=request.context)
195 partner_id = user_obj.browse(request.cr, SUPERUSER_ID, request.uid,
196 context=request.context).partner_id.id
198 fields = [k for k, v in order_line_obj._columns.items()]
199 values = order_line_obj.default_get(request.cr, SUPERUSER_ID, fields,
200 context=request.context)
203 for key, value in post.items():
205 quantity = int(value)
208 ticket_id = key.split("-")[0] == 'ticket' and int(key.split("-")[1]) or None
209 if not ticket_id or not quantity:
211 ticket = ticket_obj.browse(request.cr, request.uid, ticket_id,
212 context=request.context)
214 values['product_id'] = ticket.product_id.id
215 values['event_id'] = ticket.event_id.id
216 values['event_ticket_id'] = ticket.id
217 values['product_uom_qty'] = quantity
218 values['price_unit'] = ticket.price
219 values['order_id'] = order.id
220 values['name'] = "%s: %s" % (ticket.event_id.name, ticket.name)
222 ticket.check_registration_limits_before(quantity)
224 # change and record value
225 pricelist_id = order.pricelist_id and order.pricelist_id.id or False
226 _values = order_line_obj.product_id_change(
227 request.cr, SUPERUSER_ID, [], pricelist_id, ticket.product_id.id,
228 partner_id=partner_id, context=request.context)['value']
229 _values.update(values)
231 order_line_id = order_line_obj.create(request.cr, SUPERUSER_ID,
232 _values, context=request.context)
233 order.write({'order_line': [(4, order_line_id)]}, context=request.context)
236 return request.redirect("/event/%s/" % event_id)
237 return request.redirect("/shop/checkout")
239 @website.route(['/event/publish'], type='json', auth="public")
240 def publish(self, id, object):
241 # if a user publish an event, he publish all linked res.partner
242 event = request.registry[object].browse(request.cr, request.uid, int(id))
243 if not event.website_published:
244 if event.organizer_id and not event.organizer_id.website_published:
245 event.organizer_id.write({'website_published': True})
246 if event.address_id and not event.address_id.website_published:
247 event.address_id.write({'website_published': True})
249 return controllers.publish(id, object)
251 @website.route('/event/add_event/', type='http', auth="user", multilang=True, methods=['POST'])
252 def add_event(self, event_name="New Event", **kwargs):
253 Event = request.registry.get('event.event')
254 date_begin = datetime.today() + timedelta(days=(15)) # FIXME: better defaults
255 event_id = Event.create(request.cr, request.uid, {
257 'date_begin': date_begin.strftime('%Y-%m-%d'),
258 'date_end': (date_begin + timedelta(days=(1))).strftime('%Y-%m-%d'),
259 }, context=request.context)
261 return request.redirect("/event/%s/?enable_editor=1" % event_id)