Skip to content

Commit

Permalink
cleaned up irregular context position method and json context kwarg arg
Browse files Browse the repository at this point in the history
  • Loading branch information
rvalyi committed Jan 17, 2014
1 parent fe37286 commit d486b8a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 24 deletions.
18 changes: 17 additions & 1 deletion lib/ooor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,20 @@ module Ooor
module OoorBehavior
extend ActiveSupport::Concern
module ClassMethods

attr_accessor :default_config, :default_session, :cache_store

IRREGULAR_CONTEXT_POSITIONS = {
import_data: 5,
fields_view_get: 2,
search: 4,
name_search: 3,
read_group: 5,
fields_get: 1,
read: 2,
perm_read: 1,
check_recursion: 1
}

def new(config={})
Ooor.default_config = config.merge(generate_constants: true)
Expand Down Expand Up @@ -75,6 +87,10 @@ def logger=(logger)
@logger = logger
end

def irregular_context_position(method)
IRREGULAR_CONTEXT_POSITIONS.merge(default_config[:irregular_context_positions] || {})[method.to_sym]
end

end


Expand Down
6 changes: 3 additions & 3 deletions lib/ooor/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def create(attributes = {}, context={}, default_get_list=false, reload=true)

#OpenERP search method
def search(domain=[], offset=0, limit=false, order=false, context={}, count=false)
rpc_execute(:search, to_openerp_domain(domain), offset, limit, order, context, count, context_index: 4)
rpc_execute(:search, to_openerp_domain(domain), offset, limit, order, context, count)
end

def name_search(name='', domain=[], operator='ilike', context={}, limit=100)
rpc_execute(:name_search, name, to_openerp_domain(domain), operator, context, limit, context_index: 3)
rpc_execute(:name_search, name, to_openerp_domain(domain), operator, context, limit)
end

def rpc_execute(method, *args)
Expand Down Expand Up @@ -146,7 +146,7 @@ def update_attributes(attributes, context={}, reload=true)
end

#compatible with the Rails way but also supports OpenERP context
def update(context={}, reload=true)
def update(context={}, reload=true) #TODO use http://apidock.com/rails/ActiveRecord/Dirty to minimize data to save back
rpc_execute('write', [self.id], to_openerp_hash, context)
reload_fields(context) if reload
@persisted = true
Expand Down
2 changes: 1 addition & 1 deletion lib/ooor/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Report
module ClassMethods
#Added methods to obtain report data for a model
def report(report_name, ids, report_type='pdf', context={}) #TODO move to ReportService
context = connection.object.inject_session_context(context)[0]
context = connection.object.inject_session_context(:report, false, context)[0]
uid = @connection.config[:user_id]
pass = @connection.config[:password]
db = @connection.config[:database]
Expand Down
20 changes: 9 additions & 11 deletions lib/ooor/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def object_service(service, obj, method, *args)
unless @session.config[:user_id]
@session.common.login(@session.config[:database], @session.config[:username], @session.config[:password])
end
args = inject_session_context(*args)
args = inject_session_context(service, method, *args)
uid = @session.config[:user_id]
db = @session.config[:database]
@session.logger.debug "OOOR object service: rpc_method: #{service}, db: #{db}, uid: #{uid}, pass: #, obj: #{obj}, method: #{method}, *args: #{args.inspect}"
Expand All @@ -107,19 +107,17 @@ def object_service(service, obj, method, *args)
retry
end

def inject_session_context(*args)
if args[-1].is_a? Hash #context
if args[-1][:context_index] #in some legacy methods, context isn't the last arg
i = args[-1][:context_index]
args.delete_at -1
c = HashWithIndifferentAccess.new(args[i])
args[i] = @session.connection_session.merge(c)
elsif args[-1][:context]
def inject_session_context(service, method, *args)
if service == :object && (i = Ooor.irregular_context_position(method)) && args.size >= i
c = HashWithIndifferentAccess.new(args[i])
args[i] = @session.session_context(c)
elsif args[-1].is_a? Hash #context
if args[-1][:context]
c = HashWithIndifferentAccess.new(args[-1][:context])
args[-1][:context] = @session.connection_session.merge(c)
args[-1][:context] = @session.session_context(c)
else
c = HashWithIndifferentAccess.new(args[-1])
args[-1] = @session.connection_session.merge(c)
args[-1] = @session.session_context(c)
end
end
args
Expand Down
6 changes: 5 additions & 1 deletion lib/ooor/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def global_login(options)
load_models(config[:models], options[:reload])
end

def session_context(context={})
connection_session.merge(web_session.slice('lang', 'tz')).merge(context) # not just lang and tz?
end

def const_get(model_key, lang=nil);
if config[:aliases]
if lang && alias_data = config[:aliases][lang]
Expand All @@ -56,7 +60,7 @@ def load_models(model_names=config[:models], reload=config[:reload])
end
domain = model_names ? [['model', 'in', model_names]] : []
search_domain = domain - [1]
model_ids = object.object_service(:execute, "ir.model", :search, search_domain, 0, false, false, {}, false, {:context_index=>4})
model_ids = object.object_service(:execute, "ir.model", :search, search_domain, 0, false, false, {}, false)
models_records = object.object_service(:execute, "ir.model", :read, model_ids, ['model', 'name']) #TODO use search_read
models_records.each do |opts|
options = HashWithIndifferentAccess.new(opts.merge(scope_prefix: config[:scope_prefix], reload: reload, generate_constants: config[:generate_constants]))
Expand Down
11 changes: 4 additions & 7 deletions lib/ooor/transport/json_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ def oe_service(session_info, service, obj, method, *args)
params = {"model"=>obj, "id"=>args[0], "signal"=>method}
elsif service == :execute
url = '/web/dataset/call_kw'
if args.last.is_a?(Hash)
context = args.pop
if (i = Ooor.irregular_context_position(method)) && args.size < i
kwargs = {"context"=> args[i]}
else
context = {}
end
params = {"model"=>obj, "method"=> method, "kwargs"=>{}, "args"=>args, "context"=>context}
if ['search', 'read'].index(method) || args[0].is_a?(Array) && args.size == 1 && args[0].any? {|e| !e.is_a?(Integer)} #TODO make it more robust
params["kwargs"] = {"context"=>context}
kwargs = {}
end
params = {"model"=>obj, "method"=> method, "kwargs"=> kwargs, "args"=>args}#, "context"=>context}
else
url = "/web/dataset/#{service}"
params = args[0].merge({"model"=>obj})
Expand Down

0 comments on commit d486b8a

Please sign in to comment.