Added geo calendar support.

This commit is contained in:
Martin Edenhofer 2015-09-22 16:48:43 +02:00
parent d692690da5
commit de7039b2b5
6 changed files with 192 additions and 1 deletions

View file

@ -278,6 +278,31 @@ returns
=begin =begin
generate uniq name (will check name of model and generates _1 sequenze)
Used as before_update callback, no own use needed
name = Model.genrate_uniq_name('some name')
returns
result = 'some name_X'
=end
def self.genrate_uniq_name(name)
return name if !find_by(name: name)
(1..100).each {|counter|
name = "#{name}_#{counter}"
exists = find_by(name: name)
next if exists
break
}
name
end
=begin
lookup model from cache (if exists) or retrieve it from db, id, name or login possible lookup model from cache (if exists) or retrieve it from db, id, name or login possible
result = Model.lookup( :id => 123 ) result = Model.lookup( :id => 123 )

View file

@ -10,6 +10,36 @@ class Calendar < ApplicationModel
=begin =begin
set inital default calendar
calendar = Calendar.init_setup
returns calendar object
=end
def self.init_setup(ip = nil)
# call for calendar suggestion
calendar_details = Service::GeoCalendar.location(ip)
return if !calendar_details
calendar_details['name'] = Calendar.genrate_uniq_name(calendar_details['name'])
calendar_details['default'] = true
calendar_details['created_by_id'] = 1
calendar_details['updated_by_id'] = 1
# find if auto generated calendar exists
calendar = Calendar.find_by(default: true, updated_by_id: 1, created_by_id: 1)
if calendar
calendar.update_attributes(calendar_details)
return calendar
end
create(calendar_details)
end
=begin
get default calendar get default calendar
calendar = Calendar.default calendar = Calendar.default

View file

@ -0,0 +1,28 @@
class UpdateSettings5 < ActiveRecord::Migration
def up
Setting.create_or_update(
title: 'Geo Calendar Service',
name: 'geo_calendar_backend',
area: 'System::Services',
description: 'Defines the backend for geo calendar lookups. Used for inital calendar succession.',
options: {
form: [
{
display: '',
null: true,
name: 'geo_calendar_backend',
tag: 'select',
options: {
'' => '-',
'Service::GeoCalendar::Zammad' => 'Zammad GeoCalendar Service',
},
},
],
},
state: 'Service::GeoCalendar::Zammad',
preferences: { prio: 2 },
frontend: false
)
Calendar.init_setup
end
end

View file

@ -276,6 +276,30 @@ Setting.create_if_not_exists(
frontend: false frontend: false
) )
Setting.create_if_not_exists(
title: 'Geo Calendar Service',
name: 'geo_calendar_backend',
area: 'System::Services',
description: 'Defines the backend for geo calendar lookups. Used for inital calendar succession.',
options: {
form: [
{
display: '',
null: true,
name: 'geo_calendar_backend',
tag: 'select',
options: {
'' => '-',
'Service::GeoCalendar::Zammad' => 'Zammad GeoCalendar Service',
},
},
],
},
state: 'Service::GeoCalendar::Zammad',
preferences: { prio: 2 },
frontend: false
)
Setting.create_if_not_exists( Setting.create_if_not_exists(
title: 'Send client stats', title: 'Send client stats',
name: 'ui_send_client_stats', name: 'ui_send_client_stats',
@ -1920,6 +1944,7 @@ Locale.create_if_not_exists(
) )
Locale.load Locale.load
Translation.load Translation.load
Calendar.init_setup
# install all packages in auto_install # install all packages in auto_install
Package.auto_install() Package.auto_install

View file

@ -0,0 +1,65 @@
# encoding: utf-8
require 'integration_test_helper'
class GeoIpCalendar < ActiveSupport::TestCase
# check
test 'check some results' do
result = Service::GeoCalendar.location( '127.0.0.0.1' )
assert(result)
assert_equal('My Calendar', result['name'])
assert_equal('America/Los_Angeles', result['timezone'])
result = Service::GeoCalendar.location( '127.0.0.1' )
assert(result)
assert_equal('My Calendar', result['name'])
assert_equal('America/Los_Angeles', result['timezone'])
result = Service::GeoCalendar.location( '195.65.29.254' )
assert(result)
assert_equal('Switzerland', result['name'])
assert_equal('Europe/Zurich', result['timezone'])
result = Service::GeoCalendar.location( '195.191.132.18' )
assert(result)
assert_equal('Switzerland', result['name'])
assert_equal('Europe/Zurich', result['timezone'])
result = Service::GeoCalendar.location( '134.109.140.74' )
assert(result)
assert_equal('Germany', result['name'])
assert_equal('Europe/Berlin', result['timezone'])
result = Service::GeoCalendar.location( '46.253.55.170' )
assert(result)
assert_equal('Germany', result['name'])
assert_equal('Europe/Berlin', result['timezone'])
result = Service::GeoCalendar.location( '169.229.216.200' )
assert(result)
assert_equal('United States/California', result['name'])
assert_equal('America/Los_Angeles', result['timezone'])
result = Service::GeoCalendar.location( '17.171.2.25' )
assert(result)
assert_equal('United States/California', result['name'])
assert_equal('America/Los_Angeles', result['timezone'])
result = Service::GeoCalendar.location( '184.168.47.225' )
assert(result)
assert_equal('United States/Arizona', result['name'])
assert_equal('America/Phoenix', result['timezone'])
result = Service::GeoCalendar.location( '69.172.201.245' )
assert(result)
assert_equal('United States/New York', result['name'])
assert_equal('America/New_York', result['timezone'])
result = Service::GeoCalendar.location( '132.247.70.37' )
assert(result)
assert_equal('Mexico/Sonora', result['name'])
assert_equal('America/Hermosillo', result['timezone'])
end
end

View file

@ -15,6 +15,15 @@ class GeoIpTest < ActiveSupport::TestCase
assert_equal(nil, result['latitude']) assert_equal(nil, result['latitude'])
assert_equal(nil, result['longitude']) assert_equal(nil, result['longitude'])
result = Service::GeoIp.location( '127.0.0.1' )
assert(result)
assert_equal(nil, result['country_name'])
assert_equal(nil, result['city_name'])
assert_equal(nil, result['country_code'])
assert_equal(nil, result['continent_code'])
assert_equal(nil, result['latitude'])
assert_equal(nil, result['longitude'])
result = Service::GeoIp.location( '195.65.29.254' ) result = Service::GeoIp.location( '195.65.29.254' )
assert(result) assert(result)
assert_equal('Switzerland', result['country_name']) assert_equal('Switzerland', result['country_name'])
@ -51,5 +60,14 @@ class GeoIpTest < ActiveSupport::TestCase
assert_equal(37.8668, result['latitude']) assert_equal(37.8668, result['latitude'])
assert_equal(-122.2536, result['longitude']) assert_equal(-122.2536, result['longitude'])
result = Service::GeoIp.location( '17.171.2.25' )
assert(result)
assert_equal('United States', result['country_name'])
assert_equal('Cupertino', result['city_name'])
assert_equal('US', result['country_code'])
assert_equal('NA', result['continent_code'])
assert_equal(37.323, result['latitude'])
assert_equal(-122.0322, result['longitude'])
end end
end end