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.controllers.main import Website as controllers
27 controllers = controllers()
30 from datetime import datetime, timedelta
31 from dateutil.relativedelta import relativedelta
32 from openerp import tools
35 class website_event(http.Controller):
36 @http.route(['/event/', '/event/page/<int:page>'], type='http', auth="public", website=True, multilang=True)
37 def events(self, page=1, **searches):
38 cr, uid, context = request.cr, request.uid, request.context
39 event_obj = request.registry['event.event']
40 type_obj = request.registry['event.type']
41 country_obj = request.registry['res.country']
43 searches.setdefault('date', 'all')
44 searches.setdefault('type', 'all')
45 searches.setdefault('country', 'all')
50 return date.strftime('%Y-%m-%d 23:59:59')
52 return date.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)
53 today = datetime.today()
55 ['all', _('Next Events'), [("date_end", ">", sd(today))], 0],
56 ['today', _('Today'), [
57 ("date_end", ">", sd(today)),
58 ("date_begin", "<", sdn(today))],
60 ['week', _('This Week'), [
61 ("date_end", ">=", sd(today + relativedelta(days=-today.weekday()))),
62 ("date_begin", "<", sdn(today + relativedelta(days=6-today.weekday())))],
64 ['nextweek', _('Next Week'), [
65 ("date_end", ">=", sd(today + relativedelta(days=7-today.weekday()))),
66 ("date_begin", "<", sdn(today + relativedelta(days=13-today.weekday())))],
68 ['month', _('This month'), [
69 ("date_end", ">=", sd(today.replace(day=1))),
70 ("date_begin", "<", (today.replace(day=1) + relativedelta(months=1)).strftime('%Y-%m-%d 00:00:00'))],
72 ['nextmonth', _('Next month'), [
73 ("date_end", ">=", sd(today.replace(day=1) + relativedelta(months=1))),
74 ("date_begin", "<", (today.replace(day=1) + relativedelta(months=2)).strftime('%Y-%m-%d 00:00:00'))],
76 ['old', _('Old Events'), [
77 ("date_end", "<", today.strftime('%Y-%m-%d 00:00:00'))],
84 current_country = None
86 if searches["date"] == date[0]:
87 domain_search["date"] = date[2]
89 current_date = date[1]
90 if searches["type"] != 'all':
91 current_type = type_obj.browse(cr, uid, int(searches['type']), context=context)
92 domain_search["type"] = [("type", "=", int(searches["type"]))]
93 if searches["country"] != 'all':
94 current_country = country_obj.browse(cr, uid, int(searches['country']), context=context)
95 domain_search["country"] = [("country_id", "=", int(searches["country"]))]
97 def dom_without(without):
98 domain = [('state', "in", ['draft','confirm','done'])]
99 for key, search in domain_search.items():
104 # count by domains without self search
107 date[3] = event_obj.search(
108 request.cr, request.uid, dom_without('date') + date[2],
109 count=True, context=request.context)
111 domain = dom_without('type')
112 types = event_obj.read_group(
113 request.cr, request.uid, domain, ["id", "type"], groupby="type",
114 orderby="type", context=request.context)
115 type_count = event_obj.search(request.cr, request.uid, domain,
116 count=True, context=request.context)
118 'type_count': type_count,
119 'type': ("all", _("All Categories"))
122 domain = dom_without('country')
123 countries = event_obj.read_group(
124 request.cr, request.uid, domain, ["id", "country_id"],
125 groupby="country_id", orderby="country_id", context=request.context)
126 country_id_count = event_obj.search(request.cr, request.uid, domain,
127 count=True, context=request.context)
128 countries.insert(0, {
129 'country_id_count': country_id_count,
130 'country_id': ("all", _("All Countries"))
134 event_count = event_obj.search(
135 request.cr, request.uid, dom_without("none"), count=True,
136 context=request.context)
137 pager = request.website.pager(url="/event/", total=event_count, page=page, step=step, scope=5)
139 order = 'website_published desc, date_begin'
140 if searches.get('date','all') == 'old':
141 order = 'website_published desc, date_begin desc'
142 obj_ids = event_obj.search(
143 request.cr, request.uid, dom_without("none"), limit=step,
144 offset=pager['offset'], order=order, context=request.context)
145 events_ids = event_obj.browse(request.cr, request.uid, obj_ids,
146 context=request.context)
149 'current_date': current_date,
150 'current_country': current_country,
151 'current_type': current_type,
152 'event_ids': events_ids,
155 'countries': countries,
157 'searches': searches,
158 'search_path': "?%s" % werkzeug.url_encode(searches),
161 return request.website.render("website_event.index", values)
163 @http.route(['/event/<model("event.event"):event>/page/<page:page>'], type='http', auth="public", website=True, multilang=True)
164 def event_page(self, event, page, **post):
169 return request.website.render(page, values)
171 @http.route(['/event/<model("event.event"):event>'], type='http', auth="public", website=True, multilang=True)
172 def event(self, event, **post):
173 if event.menu_id and event.menu_id.child_id:
174 target_url = event.menu_id.child_id[0].url
176 target_url = '/event/%s/register' % str(event.id)
177 if post.get('enable_editor') == '1':
178 target_url += '?enable_editor=1'
179 return request.redirect(target_url);
181 @http.route(['/event/<model("event.event"):event>/register'], type='http', auth="public", website=True, multilang=True)
182 def event_register(self, event, **post):
185 'main_object': event,
188 return request.website.render("website_event.event_description_full", values)
190 @http.route('/event/add_event/', type='http', auth="user", multilang=True, methods=['POST'], website=True)
191 def add_event(self, event_name="New Event", **kwargs):
192 return self._add_event(event_name, request.context, **kwargs)
194 def _add_event(self, event_name=None, context={}, **kwargs):
196 event_name = _("New Event")
197 Event = request.registry.get('event.event')
198 date_begin = datetime.today() + timedelta(days=(14))
201 'date_begin': date_begin.strftime('%Y-%m-%d'),
202 'date_end': (date_begin + timedelta(days=(1))).strftime('%Y-%m-%d'),
204 event_id = Event.create(request.cr, request.uid, vals, context=context)
205 return request.redirect("/event/%s/?enable_editor=1" % event_id)