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 _logger = logging.getLogger(__name__)
32 from datetime import datetime, timedelta
34 from dateutil.relativedelta import relativedelta
35 from openerp import tools
37 from openerp.addons.website.models.website import slug
43 _logger.warn("Please install GeoIP python module to use events localisation.")
45 class website_event(http.Controller):
46 @http.route(['/event', '/event/page/<int:page>'], type='http', auth="public", website=True, multilang=True)
47 def events(self, page=1, **searches):
48 cr, uid, context = request.cr, request.uid, request.context
49 event_obj = request.registry['event.event']
50 type_obj = request.registry['event.type']
51 country_obj = request.registry['res.country']
53 searches.setdefault('date', 'all')
54 searches.setdefault('type', 'all')
55 searches.setdefault('country', 'all')
60 return date.strftime('%Y-%m-%d 23:59:59')
62 return date.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)
63 today = datetime.today()
65 ['all', _('Next Events'), [("date_end", ">", sd(today))], 0],
66 ['today', _('Today'), [
67 ("date_end", ">", sd(today)),
68 ("date_begin", "<", sdn(today))],
70 ['week', _('This Week'), [
71 ("date_end", ">=", sd(today + relativedelta(days=-today.weekday()))),
72 ("date_begin", "<", sdn(today + relativedelta(days=6-today.weekday())))],
74 ['nextweek', _('Next Week'), [
75 ("date_end", ">=", sd(today + relativedelta(days=7-today.weekday()))),
76 ("date_begin", "<", sdn(today + relativedelta(days=13-today.weekday())))],
78 ['month', _('This month'), [
79 ("date_end", ">=", sd(today.replace(day=1))),
80 ("date_begin", "<", (today.replace(day=1) + relativedelta(months=1)).strftime('%Y-%m-%d 00:00:00'))],
82 ['nextmonth', _('Next month'), [
83 ("date_end", ">=", sd(today.replace(day=1) + relativedelta(months=1))),
84 ("date_begin", "<", (today.replace(day=1) + relativedelta(months=2)).strftime('%Y-%m-%d 00:00:00'))],
86 ['old', _('Old Events'), [
87 ("date_end", "<", today.strftime('%Y-%m-%d 00:00:00'))],
94 current_country = None
96 if searches["date"] == date[0]:
97 domain_search["date"] = date[2]
99 current_date = date[1]
100 if searches["type"] != 'all':
101 current_type = type_obj.browse(cr, uid, int(searches['type']), context=context)
102 domain_search["type"] = [("type", "=", int(searches["type"]))]
103 if searches["country"] != 'all':
104 current_country = country_obj.browse(cr, uid, int(searches['country']), context=context)
105 domain_search["country"] = [("country_id", "=", int(searches["country"]))]
107 def dom_without(without):
108 domain = [('state', "in", ['draft','confirm','done'])]
109 for key, search in domain_search.items():
114 # count by domains without self search
117 date[3] = event_obj.search(
118 request.cr, request.uid, dom_without('date') + date[2],
119 count=True, context=request.context)
121 domain = dom_without('type')
122 types = event_obj.read_group(
123 request.cr, request.uid, domain, ["id", "type"], groupby="type",
124 orderby="type", context=request.context)
125 type_count = event_obj.search(request.cr, request.uid, domain,
126 count=True, context=request.context)
128 'type_count': type_count,
129 'type': ("all", _("All Categories"))
132 domain = dom_without('country')
133 countries = event_obj.read_group(
134 request.cr, request.uid, domain, ["id", "country_id"],
135 groupby="country_id", orderby="country_id", context=request.context)
136 country_id_count = event_obj.search(request.cr, request.uid, domain,
137 count=True, context=request.context)
138 countries.insert(0, {
139 'country_id_count': country_id_count,
140 'country_id': ("all", _("All Countries"))
144 event_count = event_obj.search(
145 request.cr, request.uid, dom_without("none"), count=True,
146 context=request.context)
147 pager = request.website.pager(url="/event", total=event_count, page=page, step=step, scope=5)
149 order = 'website_published desc, date_begin'
150 if searches.get('date','all') == 'old':
151 order = 'website_published desc, date_begin desc'
152 obj_ids = event_obj.search(
153 request.cr, request.uid, dom_without("none"), limit=step,
154 offset=pager['offset'], order=order, context=request.context)
155 events_ids = event_obj.browse(request.cr, request.uid, obj_ids,
156 context=request.context)
159 'current_date': current_date,
160 'current_country': current_country,
161 'current_type': current_type,
162 'event_ids': events_ids,
165 'countries': countries,
167 'searches': searches,
168 'search_path': "?%s" % werkzeug.url_encode(searches),
171 return request.website.render("website_event.index", values)
173 @http.route(['/event/<model("event.event"):event>/page/<page:path>'], type='http', auth="public", website=True, multilang=True)
174 def event_page(self, event, page, **post):
179 return request.website.render(page, values)
181 @http.route(['/event/<model("event.event"):event>'], type='http', auth="public", website=True, multilang=True)
182 def event(self, event, **post):
183 if event.menu_id and event.menu_id.child_id:
184 target_url = event.menu_id.child_id[0].url
186 target_url = '/event/%s/register' % str(event.id)
187 if post.get('enable_editor') == '1':
188 target_url += '?enable_editor=1'
189 return request.redirect(target_url);
191 @http.route(['/event/<model("event.event"):event>/register'], type='http', auth="public", website=True, multilang=True)
192 def event_register(self, event, **post):
195 'main_object': event,
198 return request.website.render("website_event.event_description_full", values)
200 @http.route('/event/add_event', type='http', auth="user", multilang=True, methods=['POST'], website=True)
201 def add_event(self, event_name="New Event", **kwargs):
202 return self._add_event(event_name, request.context, **kwargs)
204 def _add_event(self, event_name=None, context={}, **kwargs):
206 event_name = _("New Event")
207 Event = request.registry.get('event.event')
208 date_begin = datetime.today() + timedelta(days=(14))
211 'date_begin': date_begin.strftime('%Y-%m-%d'),
212 'date_end': (date_begin + timedelta(days=(1))).strftime('%Y-%m-%d'),
214 event_id = Event.create(request.cr, request.uid, vals, context=context)
215 event = Event.browse(request.cr, request.uid, event_id, context=context)
216 return request.redirect("/event/%s/register?enable_editor=1" % slug(event))
218 def get_visitors_country(self):
219 GI = GeoIP.open('/usr/share/GeoIP/GeoIP.dat', 0)
220 return {'country_code': GI.country_code_by_addr(request.httprequest.remote_addr), 'country_name': GI.country_name_by_addr(request.httprequest.remote_addr)}
222 def get_formated_date(self, event):
223 start_date = datetime.strptime(event.date_begin, tools.DEFAULT_SERVER_DATETIME_FORMAT).date()
224 end_date = datetime.strptime(event.date_end, tools.DEFAULT_SERVER_DATETIME_FORMAT).date()
225 return ('%s %s%s') % (start_date.strftime("%b"), start_date.strftime("%e"), (end_date != start_date and ("-"+end_date.strftime("%e")) or ""))
227 @http.route('/event/get_country_event_list', type='http', auth='public', website=True)
228 def get_country_events(self ,**post):
231 country_obj = request.registry['res.country']
232 event_obj = request.registry['event.event']
233 cr, uid, context,event_ids = request.cr, request.uid, request.context,[]
234 country_code = self.get_visitors_country()['country_code']
235 result = {'events':[],'country':False}
237 country_ids = country_obj.search(request.cr, request.uid, [('code', '=', country_code)], context=request.context)
238 event_ids = event_obj.search(request.cr, request.uid, ['|', ('address_id', '=', None),('country_id.code', '=', country_code),('date_begin','>=', time.strftime('%Y-%m-%d 00:00:00')),('state', '=', 'confirm')], order="date_begin", context=request.context)
240 event_ids = event_obj.search(request.cr, request.uid, [('date_begin','>=', time.strftime('%Y-%m-%d 00:00:00')),('state', '=', 'confirm')], order="date_begin", context=request.context)
241 for event in event_obj.browse(request.cr, request.uid, event_ids, context=request.context)[:6]:
242 if country_code and event.country_id.code == country_code:
243 result['country'] = country_obj.browse(request.cr, request.uid, country_ids[0], context=request.context)
244 result['events'].append({
245 "date": self.get_formated_date(event),
247 "url": event.website_url})
248 return request.website.render("website_event.country_events_list",result)