2013-08-02 04:35:26 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-09-24 10:52:06 +00:00
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
2013-08-02 04:35:26 +00:00
|
|
|
|
2013-08-06 09:31:15 +00:00
|
|
|
from openerp import SUPERUSER_ID
|
2013-08-02 04:35:26 +00:00
|
|
|
from openerp.addons.web import http
|
|
|
|
from openerp.addons.web.http import request
|
2013-08-02 10:11:22 +00:00
|
|
|
from openerp.tools.translate import _
|
2013-11-04 15:52:30 +00:00
|
|
|
from openerp.addons.website.controllers.main import Website as controllers
|
|
|
|
controllers = controllers()
|
|
|
|
|
2013-08-02 10:11:22 +00:00
|
|
|
|
2013-11-13 17:14:38 +00:00
|
|
|
from datetime import datetime, timedelta
|
2014-02-26 11:31:21 +00:00
|
|
|
import time
|
2013-08-02 10:11:22 +00:00
|
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
from openerp import tools
|
2014-01-29 12:13:16 +00:00
|
|
|
import werkzeug.urls
|
2014-02-28 09:41:48 +00:00
|
|
|
import GeoIP
|
2013-08-05 10:21:10 +00:00
|
|
|
|
2013-08-05 16:16:00 +00:00
|
|
|
class website_event(http.Controller):
|
2014-03-14 16:23:46 +00:00
|
|
|
@http.route(['/event', '/event/page/<int:page>'], type='http', auth="public", website=True, multilang=True)
|
2013-08-12 13:43:12 +00:00
|
|
|
def events(self, page=1, **searches):
|
2013-09-24 11:19:57 +00:00
|
|
|
cr, uid, context = request.cr, request.uid, request.context
|
2013-08-05 12:44:28 +00:00
|
|
|
event_obj = request.registry['event.event']
|
2013-09-24 11:19:57 +00:00
|
|
|
type_obj = request.registry['event.type']
|
|
|
|
country_obj = request.registry['res.country']
|
2013-08-05 12:44:28 +00:00
|
|
|
|
2013-08-05 10:21:10 +00:00
|
|
|
searches.setdefault('date', 'all')
|
2013-08-05 12:44:28 +00:00
|
|
|
searches.setdefault('type', 'all')
|
2013-08-05 13:04:20 +00:00
|
|
|
searches.setdefault('country', 'all')
|
2013-08-05 15:03:19 +00:00
|
|
|
|
2013-08-05 12:44:28 +00:00
|
|
|
domain_search = {}
|
2013-08-02 10:45:04 +00:00
|
|
|
|
2013-11-11 21:07:59 +00:00
|
|
|
def sdn(date):
|
|
|
|
return date.strftime('%Y-%m-%d 23:59:59')
|
2013-08-02 10:11:22 +00:00
|
|
|
def sd(date):
|
2013-08-02 10:45:04 +00:00
|
|
|
return date.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)
|
2013-08-02 10:11:22 +00:00
|
|
|
today = datetime.today()
|
|
|
|
dates = [
|
2013-11-11 21:07:59 +00:00
|
|
|
['all', _('Next Events'), [("date_end", ">", sd(today))], 0],
|
2013-08-02 10:11:22 +00:00
|
|
|
['today', _('Today'), [
|
2013-11-11 21:07:59 +00:00
|
|
|
("date_end", ">", sd(today)),
|
|
|
|
("date_begin", "<", sdn(today))],
|
2013-08-02 10:11:22 +00:00
|
|
|
0],
|
|
|
|
['week', _('This Week'), [
|
2013-11-11 21:07:59 +00:00
|
|
|
("date_end", ">=", sd(today + relativedelta(days=-today.weekday()))),
|
|
|
|
("date_begin", "<", sdn(today + relativedelta(days=6-today.weekday())))],
|
2013-08-02 10:11:22 +00:00
|
|
|
0],
|
|
|
|
['nextweek', _('Next Week'), [
|
2013-11-11 21:07:59 +00:00
|
|
|
("date_end", ">=", sd(today + relativedelta(days=7-today.weekday()))),
|
|
|
|
("date_begin", "<", sdn(today + relativedelta(days=13-today.weekday())))],
|
2013-08-02 10:11:22 +00:00
|
|
|
0],
|
|
|
|
['month', _('This month'), [
|
2013-11-11 21:07:59 +00:00
|
|
|
("date_end", ">=", sd(today.replace(day=1))),
|
|
|
|
("date_begin", "<", (today.replace(day=1) + relativedelta(months=1)).strftime('%Y-%m-%d 00:00:00'))],
|
|
|
|
0],
|
|
|
|
['nextmonth', _('Next month'), [
|
|
|
|
("date_end", ">=", sd(today.replace(day=1) + relativedelta(months=1))),
|
|
|
|
("date_begin", "<", (today.replace(day=1) + relativedelta(months=2)).strftime('%Y-%m-%d 00:00:00'))],
|
|
|
|
0],
|
|
|
|
['old', _('Old Events'), [
|
|
|
|
("date_end", "<", today.strftime('%Y-%m-%d 00:00:00'))],
|
2013-08-02 10:11:22 +00:00
|
|
|
0],
|
|
|
|
]
|
|
|
|
|
2013-08-02 10:45:04 +00:00
|
|
|
# search domains
|
2013-11-10 12:42:44 +00:00
|
|
|
current_date = None
|
2013-09-24 11:19:57 +00:00
|
|
|
current_type = None
|
|
|
|
current_country = None
|
2013-08-02 10:45:04 +00:00
|
|
|
for date in dates:
|
2013-09-24 11:19:57 +00:00
|
|
|
if searches["date"] == date[0]:
|
2013-08-05 10:21:10 +00:00
|
|
|
domain_search["date"] = date[2]
|
2013-11-10 12:42:44 +00:00
|
|
|
if date[0] != 'all':
|
|
|
|
current_date = date[1]
|
2013-09-24 11:19:57 +00:00
|
|
|
if searches["type"] != 'all':
|
|
|
|
current_type = type_obj.browse(cr, uid, int(searches['type']), context=context)
|
|
|
|
domain_search["type"] = [("type", "=", int(searches["type"]))]
|
|
|
|
if searches["country"] != 'all':
|
|
|
|
current_country = country_obj.browse(cr, uid, int(searches['country']), context=context)
|
|
|
|
domain_search["country"] = [("country_id", "=", int(searches["country"]))]
|
2013-08-05 13:04:20 +00:00
|
|
|
|
|
|
|
def dom_without(without):
|
2013-11-11 21:24:33 +00:00
|
|
|
domain = [('state', "in", ['draft','confirm','done'])]
|
2013-08-05 13:04:20 +00:00
|
|
|
for key, search in domain_search.items():
|
|
|
|
if key != without:
|
|
|
|
domain += search
|
|
|
|
return domain
|
2013-08-02 10:45:04 +00:00
|
|
|
|
|
|
|
# count by domains without self search
|
|
|
|
for date in dates:
|
2013-11-11 21:07:59 +00:00
|
|
|
if date[0] <> 'old':
|
|
|
|
date[3] = event_obj.search(
|
|
|
|
request.cr, request.uid, dom_without('date') + date[2],
|
|
|
|
count=True, context=request.context)
|
2013-08-02 10:45:04 +00:00
|
|
|
|
2013-08-05 13:04:20 +00:00
|
|
|
domain = dom_without('type')
|
2013-09-05 15:18:03 +00:00
|
|
|
types = event_obj.read_group(
|
|
|
|
request.cr, request.uid, domain, ["id", "type"], groupby="type",
|
|
|
|
orderby="type", context=request.context)
|
|
|
|
type_count = event_obj.search(request.cr, request.uid, domain,
|
|
|
|
count=True, context=request.context)
|
|
|
|
types.insert(0, {
|
|
|
|
'type_count': type_count,
|
|
|
|
'type': ("all", _("All Categories"))
|
|
|
|
})
|
2013-08-05 10:21:10 +00:00
|
|
|
|
2013-08-05 13:04:20 +00:00
|
|
|
domain = dom_without('country')
|
2013-09-05 15:18:03 +00:00
|
|
|
countries = event_obj.read_group(
|
|
|
|
request.cr, request.uid, domain, ["id", "country_id"],
|
|
|
|
groupby="country_id", orderby="country_id", context=request.context)
|
|
|
|
country_id_count = event_obj.search(request.cr, request.uid, domain,
|
|
|
|
count=True, context=request.context)
|
|
|
|
countries.insert(0, {
|
|
|
|
'country_id_count': country_id_count,
|
|
|
|
'country_id': ("all", _("All Countries"))
|
|
|
|
})
|
2013-08-05 10:21:10 +00:00
|
|
|
|
2013-08-12 13:43:12 +00:00
|
|
|
step = 5
|
2013-09-05 15:18:03 +00:00
|
|
|
event_count = event_obj.search(
|
|
|
|
request.cr, request.uid, dom_without("none"), count=True,
|
|
|
|
context=request.context)
|
2014-03-14 16:23:46 +00:00
|
|
|
pager = request.website.pager(url="/event", total=event_count, page=page, step=step, scope=5)
|
2013-11-11 21:07:59 +00:00
|
|
|
|
|
|
|
order = 'website_published desc, date_begin'
|
|
|
|
if searches.get('date','all') == 'old':
|
|
|
|
order = 'website_published desc, date_begin desc'
|
2013-09-05 15:18:03 +00:00
|
|
|
obj_ids = event_obj.search(
|
|
|
|
request.cr, request.uid, dom_without("none"), limit=step,
|
2013-11-11 21:07:59 +00:00
|
|
|
offset=pager['offset'], order=order, context=request.context)
|
2013-09-05 15:18:03 +00:00
|
|
|
events_ids = event_obj.browse(request.cr, request.uid, obj_ids,
|
|
|
|
context=request.context)
|
2013-08-02 10:45:04 +00:00
|
|
|
|
2013-09-04 15:32:36 +00:00
|
|
|
values = {
|
2013-09-24 11:19:57 +00:00
|
|
|
'current_date': current_date,
|
|
|
|
'current_country': current_country,
|
|
|
|
'current_type': current_type,
|
2013-09-05 15:18:03 +00:00
|
|
|
'event_ids': events_ids,
|
2013-08-02 10:11:22 +00:00
|
|
|
'dates': dates,
|
2013-08-05 12:44:28 +00:00
|
|
|
'types': types,
|
2013-08-05 13:04:20 +00:00
|
|
|
'countries': countries,
|
2013-08-12 13:43:12 +00:00
|
|
|
'pager': pager,
|
2013-08-05 10:21:10 +00:00
|
|
|
'searches': searches,
|
2014-01-29 12:13:16 +00:00
|
|
|
'search_path': "?%s" % werkzeug.url_encode(searches),
|
2013-09-04 15:32:36 +00:00
|
|
|
}
|
2013-08-02 04:35:26 +00:00
|
|
|
|
2013-09-10 13:41:33 +00:00
|
|
|
return request.website.render("website_event.index", values)
|
2013-08-02 04:35:26 +00:00
|
|
|
|
2014-01-20 15:37:33 +00:00
|
|
|
@http.route(['/event/<model("event.event"):event>/page/<page:page>'], type='http', auth="public", website=True, multilang=True)
|
2013-11-24 22:38:02 +00:00
|
|
|
def event_page(self, event, page, **post):
|
|
|
|
values = {
|
|
|
|
'event': event,
|
2013-12-16 10:54:03 +00:00
|
|
|
'main_object': event
|
2013-11-24 22:38:02 +00:00
|
|
|
}
|
|
|
|
return request.website.render(page, values)
|
|
|
|
|
2014-01-20 15:37:33 +00:00
|
|
|
@http.route(['/event/<model("event.event"):event>'], type='http', auth="public", website=True, multilang=True)
|
2013-12-05 15:54:50 +00:00
|
|
|
def event(self, event, **post):
|
2013-11-24 21:02:21 +00:00
|
|
|
if event.menu_id and event.menu_id.child_id:
|
2013-11-26 13:24:24 +00:00
|
|
|
target_url = event.menu_id.child_id[0].url
|
|
|
|
else:
|
|
|
|
target_url = '/event/%s/register' % str(event.id)
|
|
|
|
if post.get('enable_editor') == '1':
|
|
|
|
target_url += '?enable_editor=1'
|
|
|
|
return request.redirect(target_url);
|
2013-11-24 21:02:21 +00:00
|
|
|
|
2014-01-20 15:37:33 +00:00
|
|
|
@http.route(['/event/<model("event.event"):event>/register'], type='http', auth="public", website=True, multilang=True)
|
2013-12-05 15:54:50 +00:00
|
|
|
def event_register(self, event, **post):
|
2013-09-04 15:32:36 +00:00
|
|
|
values = {
|
2013-11-24 18:54:34 +00:00
|
|
|
'event': event,
|
2013-12-16 10:54:03 +00:00
|
|
|
'main_object': event,
|
2013-09-28 19:13:47 +00:00
|
|
|
'range': range,
|
2013-09-04 15:32:36 +00:00
|
|
|
}
|
2013-09-24 10:52:06 +00:00
|
|
|
return request.website.render("website_event.event_description_full", values)
|
2013-08-05 15:03:19 +00:00
|
|
|
|
2014-03-13 13:20:13 +00:00
|
|
|
@http.route('/event/add_event', type='http', auth="user", multilang=True, methods=['POST'], website=True)
|
2013-11-19 11:06:56 +00:00
|
|
|
def add_event(self, event_name="New Event", **kwargs):
|
2014-01-30 16:33:21 +00:00
|
|
|
return self._add_event(event_name, request.context, **kwargs)
|
2013-11-27 10:38:16 +00:00
|
|
|
|
2014-02-06 15:47:11 +00:00
|
|
|
def _add_event(self, event_name=None, context={}, **kwargs):
|
|
|
|
if not event_name:
|
|
|
|
event_name = _("New Event")
|
2014-01-30 16:33:21 +00:00
|
|
|
Event = request.registry.get('event.event')
|
|
|
|
date_begin = datetime.today() + timedelta(days=(14))
|
2013-11-27 10:38:16 +00:00
|
|
|
vals = {
|
2013-11-19 11:06:56 +00:00
|
|
|
'name': event_name,
|
2013-11-13 17:14:38 +00:00
|
|
|
'date_begin': date_begin.strftime('%Y-%m-%d'),
|
|
|
|
'date_end': (date_begin + timedelta(days=(1))).strftime('%Y-%m-%d'),
|
2013-11-27 10:38:16 +00:00
|
|
|
}
|
2014-01-30 16:33:21 +00:00
|
|
|
event_id = Event.create(request.cr, request.uid, vals, context=context)
|
2014-03-14 18:00:29 +00:00
|
|
|
return request.redirect("/event/%s?enable_editor=1" % event_id)
|
2014-02-26 11:31:21 +00:00
|
|
|
|
2014-02-28 09:41:48 +00:00
|
|
|
def get_visitors_country(self):
|
|
|
|
GI = GeoIP.open('/usr/share/GeoIP/GeoIP.dat', 0)
|
|
|
|
return {'country_code': GI.country_code_by_addr(request.httprequest.remote_addr), 'country_name': GI.country_name_by_addr(request.httprequest.remote_addr)}
|
|
|
|
|
|
|
|
def get_formated_date(self, event):
|
2014-03-21 12:05:50 +00:00
|
|
|
start_date = datetime.strptime(event.date_begin, tools.DEFAULT_SERVER_DATETIME_FORMAT).date()
|
|
|
|
end_date = datetime.strptime(event.date_end, tools.DEFAULT_SERVER_DATETIME_FORMAT).date()
|
|
|
|
return ('%s %s%s') % (start_date.strftime("%b"), start_date.strftime("%e"), (end_date != start_date and ("-"+end_date.strftime("%e")) or ""))
|
2014-02-28 09:41:48 +00:00
|
|
|
|
|
|
|
@http.route('/event/get_country_event_list', type='http', auth='public', website=True)
|
|
|
|
def get_country_events(self ,**post):
|
2014-02-26 11:31:21 +00:00
|
|
|
country_obj = request.registry['res.country']
|
2014-02-28 09:41:48 +00:00
|
|
|
event_obj = request.registry['event.event']
|
|
|
|
cr, uid, context,event_ids = request.cr, request.uid, request.context,[]
|
|
|
|
country_code = self.get_visitors_country()['country_code']
|
2014-03-04 07:01:04 +00:00
|
|
|
result = {'events':[],'country':False}
|
2014-02-28 09:41:48 +00:00
|
|
|
if country_code:
|
|
|
|
country_ids = country_obj.search(request.cr, request.uid, [('code', '=', country_code)], context=request.context)
|
2014-03-04 07:01:04 +00:00
|
|
|
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)
|
2014-02-28 09:41:48 +00:00
|
|
|
if not event_ids:
|
2014-03-04 07:01:04 +00:00
|
|
|
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)
|
|
|
|
for event in event_obj.browse(request.cr, request.uid, event_ids, context=request.context)[:6]:
|
|
|
|
if country_code and event.country_id.code == country_code:
|
|
|
|
result['country'] = country_obj.browse(request.cr, request.uid, country_ids[0], context=request.context)
|
2014-02-28 09:41:48 +00:00
|
|
|
result['events'].append({
|
|
|
|
"date": self.get_formated_date(event),
|
|
|
|
"event": event,
|
|
|
|
"url": event.website_url})
|
2014-02-26 11:31:21 +00:00
|
|
|
return request.website.render("website_event.country_events_list",result)
|