[yast-commit] <rest-service> jr-basemodel : time plugin now use base module
ref: refs/heads/jr-basemodel commit 95cdbdd07a1b1d10cb5916fd7f59922e9ccc17f1 Author: Josef Reidinger <jreidinger@suse.cz> Date: Mon Dec 14 13:21:01 2009 +0100 time plugin now use base module --- .../time/app/controllers/systemtime_controller.rb | 4 +- plugins/time/app/models/systemtime.rb | 129 +++++++++----------- .../test/functional/systemtime_controller_test.rb | 37 +++++- plugins/time/test/unit/systemtime_test.rb | 51 +++----- 4 files changed, 109 insertions(+), 112 deletions(-) diff --git a/plugins/time/app/controllers/systemtime_controller.rb b/plugins/time/app/controllers/systemtime_controller.rb index 3eef247..2b3e7e2 100644 --- a/plugins/time/app/controllers/systemtime_controller.rb +++ b/plugins/time/app/controllers/systemtime_controller.rb @@ -16,14 +16,13 @@ class SystemtimeController < ApplicationController # Sets time settings. Requires write permissions for time YaPI. def update yapi_perm_check "time.write" - root = params[:systemtime] if root == nil logger.error "Response doesn't contain systemtime key" raise InvalidParameters.new :timezone => "Missing" end - systemtime = Systemtime.create_from_xml(root) + systemtime = Systemtime.new(root) systemtime.save show end @@ -36,7 +35,6 @@ class SystemtimeController < ApplicationController # Shows time settings. Requires read permission for time YaPI. def show yapi_perm_check "time.read" - systemtime = Systemtime.find respond_to do |format| diff --git a/plugins/time/app/models/systemtime.rb b/plugins/time/app/models/systemtime.rb index dd773c4..0c715d7 100644 --- a/plugins/time/app/models/systemtime.rb +++ b/plugins/time/app/models/systemtime.rb @@ -2,31 +2,48 @@ # Provides set and gets resources from YaPI time module. # Main goal is handle YaPI specific calls and data formats. Provides cleaned # and well defined data. -class Systemtime - - @@timezones = Array.new() +class Systemtime < BaseModel::Base # Date settings format is dd/mm/yyyy attr_accessor :date + validates_format_of :date, :with => /^\d{2}\/\d{2}\/\d{4}$/, :allow_nil => true # time settings format is hh:mm:ss attr_accessor :time + validates_format_of :time, :with => /^\d{2}:\d{2}:\d{2}$/, :allow_nil => true # Current timezone as id attr_accessor :timezone + #check if zone exists + validates_each :timezone, :allow_nil => true do |model,attr,zone| + contain = false + unless model.timezones.nil? + model.timezones.each do |z| + contain = true if z["entries"][zone] + end + model.errors.add attr, "Unknown timezone" unless contain + end + end # Utc status possible values is UTCOnly, UTC and localtime see yast2-country doc attr_accessor :utcstatus + validates_inclusion_of :utcstatus, :in => [true,false], :allow_nil => true + attr_accessor :timezones + validates_presence_of :timezones + # do not massload timezones, as it is read-only + attr_protected :timezones + + after_save :restart_collectd private # Creates argument for dbus call which specify what data is requested. # Available timezones is cached so request it only if it is necessary. # return:: hash with requested keys - def Systemtime.create_read_question #:doc: + def self.create_read_question #:doc: ret = { "timezone" => "true", "utcstatus" => "true", - "currenttime" => "true" + "currenttime" => "true", + "zones" => "true" } - ret["zones"]= @@timezones.empty? ? "true" : "false" return ret end @@ -40,34 +57,39 @@ class Systemtime @date = timedate[0..timedate.index(" - ")-1] #convert date to format for datepicker @date.sub!(/^(\d+)-(\d+)-(\d+)/,'\3/\2/\1') - @utcstatus= response["utcstatus"] - @timezone = response["timezone"] - if response["zones"] - @@timezones = response["zones"] + @utcstatus = + case response["utcstatus"] + when "UTC" then true + when "localtime" then false + when "UTCOnly" then nil + else + Rails.logger.warn "Unknown key in utcstatus #{response["utcstatus"]}" + nil #set nill, maybe exception??? end + @timezone = response["timezone"] + @timezones = response["zones"] end - #Getter for static timezones - def Systemtime.timezones - return @@timezones - end - - def Systemtime.create_from_xml(xmlroot) - systemtime = Systemtime.new - systemtime.time = xmlroot[:time] - systemtime.date = xmlroot[:date] - systemtime.timezone = xmlroot[:timezone] - systemtime.utcstatus = xmlroot[:utcstatus] - return systemtime - end - - def initialize + def to_xml(options={}) + tmp =@timezones + @timezones = @timezones.clone + #transform hash before serialization + @timezones.each do |zone| + newentry = [] + zone["entries"].each do |k,v| + newentry << { "id" => k, "name" => v } + end + zone["entries"] = newentry + end + res = super options + @timezones = tmp + return res end # fills time instance with data from YaPI. # # +warn+: Doesn't take any parameters. - def Systemtime.find + def self.find ret = Systemtime.new() ret.parse_response YastService.Call("YaPI::TIME::Read",create_read_question) return ret @@ -75,17 +97,14 @@ class Systemtime # Saves data from model to system via YaPI. Saves only setted data, # so it support partial safe (e.g. save only new timezone if rest of fields is not set). - def save + def update settings = {} RAILS_DEFAULT_LOGGER.info "called write with #{settings.inspect}" - if @timezone and !@timezone.empty? - settings["timezone"] = @timezone + settings["timezone"] = @timezone unless @timezone.blank? + unless @utcstatus.nil? + settings["utcstatus"] = @utcstatus ? "UTC" : "localtime" end - if @utcstatus and !@utcstatus.empty? - settings["utcstatus"] = @utcstatus - end - if (@date and !@date.empty?) and - (@time and !@time.empty?) + unless @date.blank? || @time.blank? date = @date.split("/") datetime = "#{date[2]}-#{date[0]}-#{date[1]} - "+@time settings["currenttime"] = datetime @@ -100,6 +119,9 @@ class Systemtime #XXX hack to avoid dbus timeout durign moving time to future #FIXME use correct exception end + end + + def restart_collectd #restart collectd as moving in time confuse status module (bnc#557929) begin ret = YastService.Call("YaPI::SERVICES::Execute",{ @@ -111,43 +133,6 @@ class Systemtime Rails.logger.warn "Exception thrown by DBus while restarting collectd #{e.inspect}" #restarting collectd is optional, so it should not do anything end - - end - - def to_xml( options = {} ) - xml = options[:builder] ||= Builder::XmlMarkup.new(options) - xml.instruct! unless options[:skip_instruct] - - xml.systemtime do - xml.time @time - xml.date @date - xml.timezone @timezone - xml.utcstatus @utcstatus - xml.timezones({:type => "array"}) do - @@timezones.each do |region| - if not region.empty? - xml.region do - xml.name region["name"] - xml.central region["central"] - xml.entries({:type => "array"}) do - region["entries"].each do |id,name| - xml.timezone do - xml.id id - xml.name name - end - end - end - - end - end - end - end - end + true end - - def to_json( options = {} ) - hash = Hash.from_xml(to_xml()) - return hash.to_json - end - end diff --git a/plugins/time/test/functional/systemtime_controller_test.rb b/plugins/time/test/functional/systemtime_controller_test.rb index bddd2de..382ca26 100644 --- a/plugins/time/test/functional/systemtime_controller_test.rb +++ b/plugins/time/test/functional/systemtime_controller_test.rb @@ -6,15 +6,39 @@ require File.expand_path( File.join("test","plugin_basic_tests"), RailsParent.pa class SystemtimeControllerTest < ActionController::TestCase fixtures :accounts + INITIAL_DATA = { + :timezone => "Europe/Prague", + :time => "12:18:00", + :date => "02/07/2009", + :utcstatus => "true" } + TEST_TIMEZONES = [{ + "name" => "Europe", + "central" => "Europe/Prague", + "entries" => { + "Europe/Prague" => "Czech Republic", + "Europe/Kiev" => "Ukraine (Kiev)" + } + }, + { + "name" => "USA", + "central" => "America/Chicago", + "entries" => { + "America/Chicago" => "Central (Chicago)", + "America/Kentucky/Monticello" => "Kentucky (Monticello)" + } + } + ] DATA = {:systemtime => { :timezone => "Europe/Prague", :utcstatus => "true" }} def setup - @model_class = Systemtime + @model_class = Systemtime - Systemtime.stubs(:find).returns(Systemtime.new) + time_mock = Systemtime.new(INITIAL_DATA) + time_mock.timezones = TEST_TIMEZONES + Systemtime.stubs(:find).returns(time_mock) @controller = SystemtimeController.new @request = ActionController::TestRequest.new @@ -40,10 +64,13 @@ class SystemtimeControllerTest < ActionController::TestCase def mock_save YastService.stubs(:Call).with { |params,settings| - params == "YaPI::TIME::Write" and - settings["timezone"] == DATA[:systemtime][:timezone] and - settings["utcstatus"] == DATA[:systemtime][:utcstatus] and + ret = params == "YaPI::TIME::Write" && + settings["timezone"] == DATA[:systemtime][:timezone] && + settings["utcstatus"] == DATA[:systemtime][:utcstatus] && ! settings.include?("currenttime") + ret2 = params == "YaPI::SERVICES::Execute" + return ret || ret2 } + Systemtime.stubs(:permission_check) end end diff --git a/plugins/time/test/unit/systemtime_test.rb b/plugins/time/test/unit/systemtime_test.rb index 4f8fb4e..df11fae 100644 --- a/plugins/time/test/unit/systemtime_test.rb +++ b/plugins/time/test/unit/systemtime_test.rb @@ -32,28 +32,29 @@ class SystemtimeTest < ActiveSupport::TestCase READ_RESPONSE = { "zones"=> TEST_TIMEZONES, "timezone"=> "Europe/Prague", - "utcstatus"=> "true", + "utcstatus"=> "UTC", "time" => "2009-07-02 - 12:18:00" } WRITE_ARGUMENTS_NONE = { "timezone"=> "America/Kentucky/Monticello", - "utcstatus"=> "false" + "utcstatus"=> "localtime" } WRITE_ARGUMENTS_TIME = { "timezone"=> "America/Kentucky/Monticello", - "utcstatus"=> "false", + "utcstatus"=> "localtime", "time" => "2009-07-02 - 12:18:00" } WRITE_ARGUMENTS_NTP = { :timezone=> "America/Kentucky/Monticello", - :utcstatus=> "false", + :utcstatus=> "localtime", } def setup @model = Systemtime.new + Systemtime.stubs(:permission_check) end @@ -61,11 +62,11 @@ class SystemtimeTest < ActiveSupport::TestCase YastService.stubs(:Call).with("YaPI::TIME::Read",READ_ARGUMENTS).returns(READ_RESPONSE) @model = Systemtime.find - assert_equal("02/07/2009", @model.date) - assert_equal("12:18:00", @model.time) - assert_equal("Europe/Prague", @model.timezone) - assert_equal("true", @model.utcstatus) - assert_equal(TEST_TIMEZONES,Systemtime.timezones) + assert_equal "02/07/2009", @model.date + assert_equal "12:18:00", @model.time + assert_equal "Europe/Prague", @model.timezone + assert_equal true, @model.utcstatus + assert_equal TEST_TIMEZONES, @model.timezones end def test_setter_without_time @@ -77,8 +78,9 @@ class SystemtimeTest < ActiveSupport::TestCase }).once @model.timezone = "America/Kentucky/Monticello" - @model.utcstatus = "false" - @model.save + @model.utcstatus = false + @model.timezones = TEST_TIMEZONES + assert @model.save, "Errors during saving: "+@model.errors.collect{ |e| e.inspect}.join("\n") end def test_setter_with_time @@ -90,10 +92,11 @@ class SystemtimeTest < ActiveSupport::TestCase }).once @model.timezone = "America/Kentucky/Monticello" - @model.utcstatus = "false" + @model.utcstatus = false @model.date = "02/07/2009" @model.time = "12:18:00" - @model.save + @model.timezones = TEST_TIMEZONES + assert @model.save, "Errors during saving: "+@model.errors.collect{ |e| e.inspect}.join("\n") end def test_setter_with_move_to_future @@ -109,19 +112,16 @@ class SystemtimeTest < ActiveSupport::TestCase }).once @model.timezone = "America/Kentucky/Monticello" - @model.utcstatus = "false" + @model.utcstatus = false @model.date = "02/07/2009" @model.time = "12:18:00" + @model.timezones = TEST_TIMEZONES assert_nothing_raised do - @model.save + assert @model.save, "Errors during saving: "+@model.errors.collect{ |e| e.inspect}.join("\n") end end def test_xml - #inject Timezones to set available timezone for direct testing - def @model.timezones=(val) - @@timezones=val - end data = READ_RESPONSE @model.timezone = data["timezone"] @@ -139,24 +139,11 @@ class SystemtimeTest < ActiveSupport::TestCase assert_equal("02/07/2009", response["date"]) zone_response = TEST_TIMEZONES - zone_response.each { |zone| - entries = [] - zone["entries"].each { |k,v| - entries.push({"id"=>k,"name"=>v}) - } - zone["entries"] = entries - } - assert_equal(zone_response.sort { |a,b| a["name"] <=> b["name"] }, response["timezones"].sort { |a,b| a["name"] <=> b["name"] }) end def test_json - #inject Timezones to set available timezone for direct testing - def @model.timezones=(val) - @@timezones=val - end - data = READ_RESPONSE @model.timezone = data["timezone"] -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org
participants (1)
-
Josef Reidinger