Show More
Commit Description:
fixed dependency errors in spec
Commit Description:
fixed dependency errors in spec
References:
File last commit:
Show/Diff file:
Action:
app/models/configuration.rb
| 183 lines
| 4.0 KiB
| text/x-ruby
| RubyLexer
|
|
r134 | require 'yaml' | ||
|
r122 | # | ||
# This class also contains various login of the system. | ||||
# | ||||
|
r76 | class Configuration < ActiveRecord::Base | ||
|
r122 | SYSTEM_MODE_CONF_KEY = 'system.mode' | ||
|
r162 | TEST_REQUEST_EARLY_TIMEOUT_KEY = 'contest.test_request.early_timeout' | ||
|
r122 | |||
|
r275 | cattr_accessor :cache | ||
cattr_accessor :config_cache | ||||
|
r290 | cattr_accessor :task_grading_info_cache | ||
|
r275 | cattr_accessor :contest_time_str | ||
cattr_accessor :contest_time | ||||
|
r146 | |||
|
r275 | # set @@cache = true to only reload once. | ||
Configuration.cache = false | ||||
Configuration.config_cache = nil | ||||
|
r290 | Configuration.task_grading_info_cache = nil | ||
|
r76 | |||
def self.get(key) | ||||
|
r275 | if Configuration.cache | ||
if Configuration.config_cache == nil | ||||
|
r146 | self.read_config | ||
end | ||||
|
r275 | return Configuration.config_cache[key] | ||
|
r146 | else | ||
return Configuration.read_one_key(key) | ||||
|
r76 | end | ||
end | ||||
def self.[](key) | ||||
self.get(key) | ||||
end | ||||
def self.reload | ||||
self.read_config | ||||
end | ||||
def self.clear | ||||
|
r275 | Configuration.config_cache = nil | ||
|
r76 | end | ||
|
r162 | def self.cache? | ||
|
r275 | Configuration.cache | ||
|
r162 | end | ||
|
r158 | def self.enable_caching | ||
|
r275 | Configuration.cache = true | ||
|
r158 | end | ||
|
r122 | # | ||
# View decision | ||||
# | ||||
def self.show_submitbox_to?(user) | ||||
mode = get(SYSTEM_MODE_CONF_KEY) | ||||
return false if mode=='analysis' | ||||
if (mode=='contest') | ||||
return false if (user.site!=nil) and | ||||
|
r129 | ((user.site.started!=true) or (user.site.finished?)) | ||
|
r122 | end | ||
return true | ||||
end | ||||
def self.show_tasks_to?(user) | ||||
|
r217 | if time_limit_mode? | ||
return false if not user.contest_started? | ||||
|
r122 | end | ||
return true | ||||
end | ||||
|
r128 | |||
|
r134 | def self.show_grading_result | ||
return (get(SYSTEM_MODE_CONF_KEY)=='analysis') | ||||
end | ||||
|
r128 | def self.allow_test_request(user) | ||
mode = get(SYSTEM_MODE_CONF_KEY) | ||||
|
r162 | early_timeout = get(TEST_REQUEST_EARLY_TIMEOUT_KEY) | ||
|
r128 | if (mode=='contest') | ||
|
r162 | return false if ((user.site!=nil) and | ||
((user.site.started!=true) or | ||||
(early_timeout and (user.site.time_left < 30.minutes)))) | ||||
|
r128 | end | ||
return false if mode=='analysis' | ||||
return true | ||||
end | ||||
|
r134 | |||
def self.task_grading_info | ||||
|
r290 | if Configuration.task_grading_info_cache==nil | ||
|
r134 | read_grading_info | ||
end | ||||
|
r290 | return Configuration.task_grading_info_cache | ||
|
r134 | end | ||
|
r130 | |||
|
r217 | def self.standard_mode? | ||
return get(SYSTEM_MODE_CONF_KEY) == 'standard' | ||||
end | ||||
def self.contest_mode? | ||||
|
r170 | return get(SYSTEM_MODE_CONF_KEY) == 'contest' | ||
end | ||||
|
r217 | def self.indv_contest_mode? | ||
return get(SYSTEM_MODE_CONF_KEY) == 'indv-contest' | ||||
end | ||||
|
r278 | def self.multicontests? | ||
|
r293 | g = get('system.multicontests') | ||
|
r278 | return get('system.multicontests') == true | ||
end | ||||
|
r217 | def self.time_limit_mode? | ||
mode = get(SYSTEM_MODE_CONF_KEY) | ||||
return ((mode == 'contest') or (mode == 'indv-contest')) | ||||
end | ||||
def self.analysis_mode? | ||||
return get(SYSTEM_MODE_CONF_KEY) == 'analysis' | ||||
end | ||||
def self.contest_time_limit | ||||
contest_time_str = Configuration['contest.time_limit'] | ||||
|
r275 | if not defined? Configuration.contest_time_str | ||
Configuration.contest_time_str = nil | ||||
|
r217 | end | ||
|
r275 | if Configuration.contest_time_str != contest_time_str | ||
Configuration.contest_time_str = contest_time_str | ||||
|
r217 | if tmatch = /(\d+):(\d+)/.match(contest_time_str) | ||
h = tmatch[1].to_i | ||||
m = tmatch[2].to_i | ||||
|
r275 | Configuration.contest_time = h.hour + m.minute | ||
|
r217 | else | ||
|
r275 | Configuration.contest_time = nil | ||
|
r217 | end | ||
end | ||||
|
r275 | return Configuration.contest_time | ||
|
r217 | end | ||
|
r76 | protected | ||
|
r146 | |||
def self.convert_type(val,type) | ||||
case type | ||||
when 'string' | ||||
return val | ||||
when 'integer' | ||||
return val.to_i | ||||
when 'boolean' | ||||
return (val=='true') | ||||
end | ||||
end | ||||
|
r76 | def self.read_config | ||
|
r275 | Configuration.config_cache = {} | ||
|
r76 | Configuration.find(:all).each do |conf| | ||
key = conf.key | ||||
val = conf.value | ||||
|
r275 | Configuration.config_cache[key] = Configuration.convert_type(val,conf.value_type) | ||
|
r146 | end | ||
end | ||||
|
r76 | |||
|
r146 | def self.read_one_key(key) | ||
conf = Configuration.find_by_key(key) | ||||
|
r155 | if conf | ||
return Configuration.convert_type(conf.value,conf.value_type) | ||||
else | ||||
return nil | ||||
end | ||||
|
r76 | end | ||
|
r134 | |||
def self.read_grading_info | ||||
f = File.open(TASK_GRADING_INFO_FILENAME) | ||||
|
r290 | Configuration.task_grading_info_cache = YAML.load(f) | ||
|
r134 | f.close | ||
end | ||||
|
r76 | |||
end | ||||