From 1b907f0d73c47f0f94d48b55372bf6a545180844 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 25 Jul 2016 22:13:38 +0200 Subject: [PATCH] Added autoload of text modules. --- app/controllers/getting_started_controller.rb | 1 - app/controllers/text_modules_controller.rb | 3 + app/controllers/users_controller.rb | 10 +++ app/models/locale.rb | 24 +++++ app/models/text_module.rb | 87 +++++++++++++++++++ app/models/translation.rb | 2 +- lib/auto_wizard.rb | 11 +++ 7 files changed, 136 insertions(+), 2 deletions(-) diff --git a/app/controllers/getting_started_controller.rb b/app/controllers/getting_started_controller.rb index aee2bef19..ee26be95e 100644 --- a/app/controllers/getting_started_controller.rb +++ b/app/controllers/getting_started_controller.rb @@ -43,7 +43,6 @@ curl http://localhost/api/v1/getting_started -v -u #{login}:#{password} end # return result - Calendar.init_setup(request.remote_ip) render json: { setup_done: setup_done, import_mode: Setting.get('import_mode'), diff --git a/app/controllers/text_modules_controller.rb b/app/controllers/text_modules_controller.rb index 40060f92f..b1f6f256a 100644 --- a/app/controllers/text_modules_controller.rb +++ b/app/controllers/text_modules_controller.rb @@ -99,6 +99,7 @@ curl http://localhost/api/v1/text_modules.json -v -u #{login}:#{password} -H "Co =end def create + deny_if_not_role(Z_ROLENAME_ADMIN) model_create_render(TextModule, params) end @@ -128,6 +129,7 @@ curl http://localhost/api/v1/text_modules.json -v -u #{login}:#{password} -H "Co =end def update + deny_if_not_role(Z_ROLENAME_ADMIN) model_update_render(TextModule, params) end @@ -145,6 +147,7 @@ curl http://localhost/api/v1/text_modules.json -v -u #{login}:#{password} -H "Co =end def destroy + deny_if_not_role(Z_ROLENAME_ADMIN) model_destory_render(TextModule, params) end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 67836bcaf..32bcda482 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -186,6 +186,16 @@ class UsersController < ApplicationController if user.email Service::Image.organization_suggest(user.email) end + + # load calendar + Calendar.init_setup(request.remote_ip) + + # load text modules + begin + TextModule.load(request.env['HTTP_ACCEPT_LANGUAGE'] || 'en-us') + rescue => e + logger.error "Unable to load text modules #{request.env['HTTP_ACCEPT_LANGUAGE'] || 'en-us'}: #{e.message}" + end end # send inviteation if needed / only if session exists diff --git a/app/models/locale.rb b/app/models/locale.rb index adf138c0d..989ca02b9 100644 --- a/app/models/locale.rb +++ b/app/models/locale.rb @@ -2,6 +2,20 @@ class Locale < ApplicationModel +=begin + +get locals to sync + +all: + + Locale.sync + +returns + + ['en-us', 'de-de', ...] + +=end + def self.to_sync locales = Locale.where(active: true) if Rails.env.test? @@ -15,6 +29,16 @@ class Locale < ApplicationModel locales end +=begin + +load locales from online + +all: + + Locale.load + +=end + def self.load url = 'https://i18n.zammad.com/api/v1/locales' diff --git a/app/models/text_module.rb b/app/models/text_module.rb index 408f0be43..bf5bf4734 100644 --- a/app/models/text_module.rb +++ b/app/models/text_module.rb @@ -4,4 +4,91 @@ class TextModule < ApplicationModel validates :name, presence: true validates :content, presence: true notify_clients_support + +=begin + +load text modules from online + + TextModule.load('de-de', overwrite_existing_item) # e. g. 'en-us' or 'de-de' + +=end + + def self.load(locale, overwrite_existing_item = false) + url = "https://i18n.zammad.com/api/v1/text_modules/#{locale}" + + result = UserAgent.get( + url, + {}, + { + json: true, + } + ) + + raise "Can't load text modules from #{url}" if !result + raise "Can't load text modules from #{url}: #{result.error}" if !result.success? + + ActiveRecord::Base.transaction do + result.data.each { |text_module| + exists = TextModule.find_by(foreign_id: text_module['foreign_id']) + if exists + next if !overwrite_existing_item + exists.update(text_module.symbolize_keys!) + else + text_module[:updated_by_id] = 1 + text_module[:created_by_id] = 1 + TextModule.create(text_module.symbolize_keys!) + end + } + end + true + end + +=begin + +push text_modules to online + + TextModule.push(locale) + +=end + + def self.push(locale) + + # only push changed text_modules + text_modules = TextModule.all #where(locale: locale) + text_modules_to_push = [] + text_modules.each { |text_module| + next if !text_module.active + text_modules_to_push.push text_module + } + + return true if text_modules_to_push.empty? + + url = 'https://i18n.zammad.com/api/v1/text_modules/thanks_for_your_support' + + translator_key = Setting.get('translator_key') + + result = UserAgent.post( + url, + { + locale: locale, + text_modules: text_modules_to_push, + fqdn: Setting.get('fqdn'), + translator_key: translator_key, + }, + { + json: true, + open_timeout: 6, + read_timeout: 16, + } + ) + raise "Can't push text_modules to #{url}: #{result.error}" if !result.success? + + # set new translator_key if given + if result.data['translator_key'] + translator_key = Setting.set('translator_key', result.data['translator_key']) + end + + true + end + end diff --git a/app/models/translation.rb b/app/models/translation.rb index fd8d7c3eb..6237f1dd8 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -102,7 +102,7 @@ push translations to online return true if translations_to_push.empty? - url = 'https://i18n.zammad.com/api/v1/thanks_for_your_support' + url = 'https://i18n.zammad.com/api/v1/translations/thanks_for_your_support' translator_key = Setting.get('translator_key') diff --git a/lib/auto_wizard.rb b/lib/auto_wizard.rb index 9801cc9cf..a15c6eabb 100644 --- a/lib/auto_wizard.rb +++ b/lib/auto_wizard.rb @@ -70,6 +70,17 @@ returns end end + # load text modules + if auto_wizard_hash['TextModuleLocale'] + if auto_wizard_hash['TextModuleLocale']['Locale'] + begin + TextModule.load(auto_wizard_hash['TextModuleLocale']['Locale']) + rescue => e + Rails.logger.error "Unable to load text modules #{auto_wizard_hash['TextModuleLocale']['Locale']}: #{e.message}" + end + end + end + # set Settings if auto_wizard_hash['Settings'] auto_wizard_hash['Settings'].each { |setting_data|