diff options
| author | Xinhui | 2016-08-04 16:05:06 +0200 |
|---|---|---|
| committer | Xinhui | 2016-08-04 16:05:38 +0200 |
| commit | 9a818eeb8991bf0e07f2c9a9837930d153a0492f (patch) | |
| tree | e4d45fff9931d39a0e9209a35e8620b23da2f86d | |
| parent | f398b39b96c54ac9454ad85d96c97ead263dc920 (diff) | |
| download | chouette-core-9a818eeb8991bf0e07f2c9a9837930d153a0492f.tar.bz2 | |
Rake task organisations:sync
| -rw-r--r-- | Gemfile | 1 | ||||
| -rw-r--r-- | Gemfile.lock | 1 | ||||
| -rw-r--r-- | app/models/organisation.rb | 8 | ||||
| -rw-r--r-- | app/models/user.rb | 15 | ||||
| -rw-r--r-- | config/environments/development.rb | 5 | ||||
| -rw-r--r-- | lib/tasks/organisations.rake | 32 |
6 files changed, 48 insertions, 14 deletions
@@ -37,6 +37,7 @@ gem 'spring', group: :development # API Rest gem 'sawyer', '~> 0.6.0' gem 'faraday_middleware', '~> 0.9.1' +gem 'faraday', '~> 0.9.1' platforms :ruby do gem 'therubyracer', '~> 0.12' diff --git a/Gemfile.lock b/Gemfile.lock index 23d8e4d65..d543d272d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -516,6 +516,7 @@ DEPENDENCIES enumerize (~> 0.10.0) factory_girl_rails (~> 4.0) fakeweb + faraday (~> 0.9.1) faraday_middleware (~> 0.9.1) font-awesome-sass (~> 4.2.0) foreigner (~> 1.7.4) diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 9d6e92825..c219bcbc9 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -21,4 +21,12 @@ class Organisation < ActiveRecord::Base def add_rule_parameter_set RuleParameterSet.default_for_all_modes( self).save end + + def self.sync_or_create code:, name: + find_or_create_by(code: code) do |org| + org.name = name + org.code = code + org.synced_at = Time.now + end + end end diff --git a/app/models/user.rb b/app/models/user.rb index 5534347cb..a8b8a01a0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -32,20 +32,7 @@ class User < ActiveRecord::Base extra = extra_attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} self.name = extra[:full_name] self.email = extra[:email] - self.organisation = self.cas_assign_or_create_organisation( - { - code: extra[:organisation_code], - name: extra[:organisation_name] - } - ) - end - - def cas_assign_or_create_organisation code:, name: - Organisation.find_or_create_by(code: code) do |organisation| - organisation.name = name - organisation.code = code - Rails.logger.debug "Cas auth - creating new organisation name: #{name} code: #{code}" - end + self.organisation = Organisation.sync_or_create code: extra[:organisation_code], name: extra[:organisation_name] end private diff --git a/config/environments/development.rb b/config/environments/development.rb index a197dd366..b895fa9a3 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -63,6 +63,11 @@ Rails.application.configure do # type: "cas", # cas_server: "http://stif-portail-dev.af83.priv/sessions" # } + config.stif_portail_api = + { + key: "411e6b8d259bc9900c0caf9db6072013", + url: "http://localhost:3000" + } # file to data for demo config.demo_data = "tmp/demo.zip" diff --git a/lib/tasks/organisations.rake b/lib/tasks/organisations.rake new file mode 100644 index 000000000..a887cd277 --- /dev/null +++ b/lib/tasks/organisations.rake @@ -0,0 +1,32 @@ +namespace :organisations do + def api_retrieve_organisation + last_sync = User.minimum(:synced_at) + conf = Rails.application.config.stif_portail_api + + conn = Faraday.new(:url => conf[:url]) do |c| + c.headers['Authorization'] = "Token token=\"#{conf[:key]}\"" + c.request :url_encoded + c.adapter Faraday.default_adapter + end + + response = conn.get '/api/v1/organizations' + JSON.parse response.body if response.status == 200 + end + + def sync_organisations data + data.each do |org| + Organisation.sync_or_create(code: org['code'], name: org['name']).tap do |organisation| + organisation.name = org['name'] + organisation.synced_at = Time.now + organisation.save if organisation.changed? + puts "Organisation #{organisation.name} has been updated" + end + end + end + + desc "Sync organisations from stif portail" + task sync: :environment do + data = api_retrieve_organisation + sync_organisations(data) if data + end +end |
