Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Nov. 24, 2015 07:13:11

leonidas
Registered: 2011-03-19
Posts: 57
Reputation: +  1  -
Profile   Send e-mail  

tryton domain issue -


@Cache('ir_rule.domain_get')
def domain_get(self, model_name, mode='read'):
assert mode in ['read', 'write', 'create', 'delete'], \
'Invalid domain mode for security'

# root user above constraint
if Transaction().user == 0:
if not Transaction().context.get('user'):
return '', []
with Transaction().set_user(Transaction().context['user']):
return self.domain_get(model_name)

pool = Pool()
rule_group_obj = pool.get('ir.rule.group')
model_obj = pool.get('ir.model')
rule_group_user_obj = pool.get('ir.rule.group-res.user')
rule_group_group_obj = pool.get('ir.rule.group-res.group')
user_group_obj = pool.get('res.user-res.group')
user_obj = pool.get('res.user')

cursor = Transaction().cursor
cursor.execute('SELECT r.id FROM "' + self._table + '" r ' \
'JOIN "' + rule_group_obj._table + '" g ' \
"ON (g.id = r.rule_group) " \
'JOIN "' + model_obj._table + '" m ON (g.model = m.id) ' \
"WHERE m.model = %s "
"AND g.perm_" + mode + " "
"AND (g.id IN (" \
'SELECT rule_group ' \
'FROM "' + rule_group_user_obj._table + '" ' \
'WHERE "user" = %s ' \
"UNION SELECT rule_group " \
'FROM "' + rule_group_group_obj._table + '" g_rel ' \
'JOIN "' + user_group_obj._table + '" u_rel ' \
'ON (g_rel."group" = u_rel."group") ' \
'WHERE u_rel."user" = %s) ' \
"OR default_p " \
"OR g.global_p)",
(model_name, Transaction().user, Transaction().user))
ids = [x[0] for x in cursor.fetchall()]
if not ids:
return '', []
obj = pool.get(model_name)
clause = {}
clause_global = {}
operand2query = self._operand_get('res.user', level=1,
recur=['many2one'], root_tech='user', root='User')
user_id = Transaction().user
with Transaction().set_user(0, set_context=True):
user = user_obj.browse(user_id)
# Use root user without context to prevent recursion
with contextlib.nested(Transaction().set_user(0),
Transaction().set_context(user=0)):
for rule in self.browse(ids):
dom = safe_eval("[('%s', '%s', %s)]" % \
(rule.field.name, rule.operator,
operand2query[rule.operand]), {
'user': user,
'time': time,
})

if rule.rule_group['global_p']:
clause_global.setdefault(rule.rule_group.id, ['OR'])
clause_global[rule.rule_group.id].append(dom)
else:
clause.setdefault(rule.rule_group.id, ['OR'])
clause[rule.rule_group.id].append(dom)

query = ''
val = []

# Test if there is no rule_group that have no rule
cursor.execute('SELECT g.id FROM "' + rule_group_obj._table + '" g ' \
'JOIN "' + model_obj._table + '" m ON (g.model = m.id) ' \
'WHERE m.model = %s ' \
'AND (g.id NOT IN (SELECT rule_group ' \
'FROM "' + self._table + '")) ' \
'AND (g.id IN (SELECT rule_group ' \
'FROM "' + rule_group_user_obj._table + '" ' \
'WHERE "user" = %s ' \
'UNION SELECT rule_group ' \
'FROM "' + rule_group_group_obj._table + '" g_rel ' \
'JOIN "' + user_group_obj._table + '" u_rel ' \
'ON g_rel."group" = u_rel."group" ' \
'WHERE u_rel."user" = %s))',
(model_name, user_id, user_id))
fetchone = cursor.fetchone()
if fetchone:
group_id = fetchone[0]
clause[group_id] = []
clause = clause.values()
clause.insert(0, 'OR')

clause_global = clause_global.values()

if clause_global:
clause_global.insert(0, 'AND')
clause = ['AND', clause_global, clause]

query, val, _, _ = obj.search_domain(clause, active_test=False)

return query, val



def search_total_amount(self, name, clause):
pool = Pool()
rule_obj = pool.get('ir.rule')
line_obj = pool.get('account.invoice.line')
tax_obj = pool.get('account.invoice.tax')
type_name = FIELDS[self.total_amount._type].sql_type(
self.total_amount)[0]
cursor = Transaction().cursor

invoice_query, invoice_val = rule_obj.domain_get('account.invoice')

cursor.execute('SELECT invoice FROM ('
'SELECT invoice, '
'COALESCE(SUM(quantity * unit_price), 0) '
'AS total_amount '
'FROM "' + line_obj._table + '" '
'JOIN "' + self._table + '" ON '
'("' + self._table + '".id = '
'"' + line_obj._table + '".invoice) '
'WHERE ' + invoice_query + ' '
'GROUP BY invoice '
'UNION '
'SELECT invoice, COALESCE(SUM(amount), 0) AS total_amount '
'FROM "' + tax_obj._table + '" '
'JOIN "' + self._table + '" ON '
'("' + self._table + '".id = '
'"' + tax_obj._table + '".invoice) '
'WHERE ' + invoice_query + ' '
'GROUP BY invoice '
') AS u '
'GROUP BY u.invoice '
'HAVING (CAST(SUM(u.total_amount) AS ' + type_name + ') '
+ clause[1] + ' %s)',
invoice_val + invoice_val + [str(clause[2])])
return [('id', 'in', [x[0] for x in cursor.fetchall()])]

Offline

#2 Nov. 10, 2017 02:17:12

KevinPsync
Registered: 2017-10-25
Posts: 22
Reputation: +  0  -
Profile   Send e-mail  

tryton domain issue -

This post is being moderated

Offline

Board footer

Moderator control

Enjoy the 18th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support