[IMP] moved snippet to content, Get country with help of GeoIP module

bzr revid: pga@tinyerp.com-20140228094148-e2nr13o9e9u0vkor
This commit is contained in:
Parth Gajjar (Open ERP) 2014-02-28 15:11:48 +05:30
parent c40b4fe69b
commit 6a3a62d94c
3 changed files with 79 additions and 76 deletions

View File

@ -32,6 +32,7 @@ import time
from dateutil.relativedelta import relativedelta
from openerp import tools
import werkzeug.urls
import GeoIP
class website_event(http.Controller):
@http.route(['/event/', '/event/page/<int:page>'], type='http', auth="public", website=True, multilang=True)
@ -205,23 +206,32 @@ class website_event(http.Controller):
event_id = Event.create(request.cr, request.uid, vals, context=context)
return request.redirect("/event/%s/?enable_editor=1" % event_id)
@http.route('/event/get_country_event_list/<path:country_code>', type='http', auth='public', website=True)
def get_country_events(self,country_code ,**post):
cr, uid, context = request.cr, request.uid, request.context
event_obj = request.registry['event.event']
event_ids = event_obj.search(request.cr, request.uid, [('country_id.code', '=', country_code),('state', '=', 'confirm'),('date_begin','>=', time.strftime('%Y-%m-%d 00:00:00'))], order="date_begin", context=request.context)
events = event_obj.browse(request.cr, request.uid, event_ids, context=request.context)
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):
start_date = datetime.strptime(event.date_begin, "%Y-%m-%d %H:%M:%S").date()
end_date = datetime.strptime(event.date_end, "%Y-%m-%d %H:%M:%S").date()
return start_date.strftime("%b")+" "+start_date.strftime("%e")+ (end_date != start_date and ("-"+end_date.strftime("%e")) or "")
@http.route('/event/get_country_event_list', type='http', auth='public', website=True)
def get_country_events(self ,**post):
country_obj = request.registry['res.country']
country_ids = country_obj.search(request.cr, request.uid, [('code', '=', country_code)], context=request.context)
country = country_obj.browse(request.cr, request.uid, country_ids[0], context=request.context)
result = {'events':[], 'country': country}
for event in events:
start_date = datetime.strptime(event.date_begin.split(' ')[0], "%Y-%m-%d")
end_date = datetime.strptime(event.date_end.split(' ')[0], "%Y-%m-%d")
if end_date == start_date:
result['events'].append({"date": start_date.strftime("%b")+" "+start_date.strftime("%e"),
"event": event, 'url': event.website_url})
else:
result['events'].append({"date":start_date.strftime("%b")+" "+start_date.strftime("%e")+"-"+end_date.strftime("%e"),
"event": event, 'url': event.website_url})
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']
result = {'events':[]}
if country_code:
country_ids = country_obj.search(request.cr, request.uid, [('code', '=', country_code)], context=request.context)
result['country'] = country_obj.browse(request.cr, request.uid, country_ids[0], context=request.context)
event_ids = event_obj.search(request.cr, request.uid, [('country_id.code', '=', country_code),('date_begin','>=', time.strftime('%Y-%m-%d 00:00:00'))], order="date_begin", context=request.context)
if not event_ids:
event_ids = event_obj.search(request.cr, request.uid, [('date_begin','>=', time.strftime('%Y-%m-%d 00:00:00'))], order="date_begin", context=request.context)
result['country'] = False
for event in event_obj.browse(request.cr, request.uid, event_ids, context=request.context):
result['events'].append({
"date": self.get_formated_date(event),
"event": event,
"url": event.website_url})
return request.website.render("website_event.country_events_list",result)

View File

@ -1,33 +1,9 @@
(function() {
"use strict";
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{alert("Geolocation is not supported by this browser.");}
}
function showPosition(position)
{
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
$.ajax({
url: "https://maps.googleapis.com/maps/api/geocode/json?latlng="+latitude+","+longitude+"&sensor=false",
type: 'GET',
dataType: 'json',
success: function(response, status, xhr, wfe){
if(response.status == 'OK'){
var last_element = response.results[response.results.length - 1]
if( last_element.types.indexOf( "country" ) != -1){
var country_obj = last_element.address_components[0];
$('img.event_country_flag').attr('src','base/static/img/country_flags/'+country_obj.short_name.toLowerCase()+'.png');
$.post( "/event/get_country_event_list/"+country_obj.short_name, function( data ) {
$( ".country_events_list" ).replaceWith( data );
});
}
}
function getLocation(){
$.post( "/event/get_country_event_list/", function( data ) {
if(data){
$( ".country_events_list" ).replaceWith( data );
}
});
}

View File

@ -338,54 +338,71 @@
</template>
<template id="country_events" name="Country Events" inherit_id="website.snippets">
<xpath expr="//div[@id='snippet_feature']" position="inside">
<div data-snippet-id="country_events" data-selector-children=".oe_structure, [data-oe-type=html]" class="country_events">
<xpath expr="//div[@id='snippet_content']" position="inside">
<div data-snippet-id="country_events" data-selector-siblings="p, h2, h3, blockquote" data-selector-children=".content">
<div class="oe_snippet_thumbnail">
<img class="oe_snippet_thumbnail_img" src="/website/static/src/img/blocks/block-country_events.png" />
<span class="oe_snippet_thumbnail_title">Country Events</span>
</div>
<section class="oe_snippet_body country_events">
<div class="container">
<div class="row">
<div class="col-md-3">
<div class="country_events_list">
<div class="text-muted text-center">
<h1><i class="fa fa-map-marker fa-5x"></i></h1>
<h3>Allow browser to use your Location.</h3>
</div>
</div>
</div>
<div class="col-md-5">
</div>
<div class="col-md-4">
</div>
<div class="oe_snippet_body country_events">
<div class="country_events_list">
<div class="text-muted text-center">
<h1>
<i class="fa fa-flag fa-5x"></i>
</h1>
<h1>Events from Your Country</h1>
</div>
</div>
</section>
</div>
</div>
</xpath>
</template>
<template id="event_right_country_event" inherit_option_id="website_event.index" name="Country Events">
<xpath expr="//div[@id='right_column']" position="inside">
<div class="row">
<div class="col-md-12 mb16 country_events">
<div class="country_events_list">
<div class="text-muted text-center">
<h1>
<i class="fa fa-flag fa-5x"></i>
</h1>
<h1>Events from Your Country</h1>
</div>
</div>
</div>
</div>
</xpath>
</template>
<template id="country_events_list" name="Country">
<t t-ignore="true">
<div class="mb16 country_events_list">
<img class="img-rounded img-responsive" t-att-src="'/website/image?model=res.country&amp;field=image&amp;id='+str(country.id)"></img>
<t t-if="events">
<h4><b>Events In <span t-esc="country.name"></span></b></h4>
<t t-if="events">
<div class="mb16 country_events_list">
<t t-if="country">
<img class="img-rounded img-responsive" t-att-src="'/website/image?model=res.country&amp;field=image&amp;id='+str(country.id)"></img>
<h4><b>Events In <span t-esc="country.name"></span></b></h4>
</t>
<t t-if="not country">
<h4><b>Upcoming Events</b></h4>
</t>
<div t-foreach="events[:5]" t-as="country_event" class="oe_website_overflow_ellipsis mb8">
<t t-if="not country_event['event'].website_published">
<span class="label label-danger"><i class="fa fa-ban"></i></span>
</t>
<i class="fa fa-calendar"></i> <b><span t-esc="country_event['date']"/>: <span><a t-att-href="country_event['url']"><t t-esc="country_event['event'].name"/></a></span></b>
</div>
</t>
<t t-if="len(events) > 5">
<b class="text-center"><a t-att-href="'/event/?country='+str(country.id)">All events for <span t-esc="country.name"></span></a></b>
</t>
<t t-if="events == []">
<h4 ><b>No events available for <span t-esc="country.name"></span></b></h4>
</t>
</div>
<t t-if="len(events) > 5">
<div t-if="country" class="text-center">
<b><a t-att-href="'/event/?country='+str(country.id)">See All events from <span t-esc="country.name"></span></a></b>
</div>
<div t-if="not country" class="text-center">
<b><a t-att-href="'/event/"> See All Upcoming events </a></b>
</div>
</t>
</div>
</t>
</t>
</template>
</data>