Passa al contenuto
Odoo Menu
  • Accedi
  • Provalo gratis
  • App
    Finanze
    • Contabilità
    • Fatturazione
    • Note spese
    • Fogli di calcolo (BI)
    • Documenti
    • Firma
    Vendite
    • CRM
    • Vendite
    • Punto vendita Negozio
    • Punto vendita Ristorante
    • Abbonamenti
    • Noleggi
    Siti web
    • Configuratore sito web
    • E-commerce
    • Blog
    • Forum
    • Live chat
    • E-learning
    Supply chain
    • Magazzino
    • Produzione
    • PLM
    • Acquisti
    • Manutenzione
    • Qualità
    Risorse umane
    • Dipendenti
    • Assunzioni
    • Ferie
    • Valutazioni
    • Referral dipendenti
    • Parco veicoli
    Marketing
    • Social marketing
    • E-mail marketing
    • SMS marketing
    • Eventi
    • Marketing automation
    • Sondaggi
    Servizi
    • Progetti
    • Fogli ore
    • Assistenza sul campo
    • Helpdesk
    • Pianificazione
    • Appuntamenti
    Produttività
    • Comunicazioni
    • Approvazioni
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    App di terze parti Odoo Studio Piattaforma cloud Odoo
  • Settori
    Retail
    • Libreria
    • Negozio di abbigliamento
    • Negozio di arredamento
    • Alimentari
    • Ferramenta
    • Negozio di giocattoli
    Cibo e ospitalità
    • Bar e pub
    • Ristorante
    • Fast food
    • Pensione
    • Grossista di bevande
    • Hotel
    Agenzia immobiliare
    • Agenzia immobiliare
    • Studio di architettura
    • Edilizia
    • Gestione immobiliare
    • Impresa di giardinaggio
    • Associazione di proprietari immobiliari
    Consulenza
    • Società di contabilità
    • Partner Odoo
    • Agenzia di marketing
    • Studio legale
    • Selezione del personale
    • Audit e certificazione
    Produzione
    • Tessile
    • Metallo
    • Arredamenti
    • Alimentare
    • Birrificio
    • Ditta di regalistica aziendale
    Benessere e sport
    • Club sportivo
    • Negozio di ottica
    • Centro fitness
    • Centro benessere
    • Farmacia
    • Parrucchiere
    Commercio
    • Tuttofare
    • Hardware e assistenza IT
    • Ditta di installazione di pannelli solari
    • Calzolaio
    • Servizi di pulizia
    • Servizi di climatizzazione
    Altro
    • Organizzazione non profit
    • Ente per la tutela ambientale
    • Agenzia di cartellonistica pubblicitaria
    • Studio fotografico
    • Punto noleggio di biciclette
    • Rivenditore di software
    Carica tutti i settori
  • Community
    Apprendimento
    • Tutorial
    • Documentazione
    • Certificazioni 
    • Formazione
    • Blog
    • Podcast
    Potenzia la tua formazione
    • Programma educativo
    • Scale Up! Business Game
    • Visita Odoo
    Ottieni il software
    • Scarica
    • Versioni a confronto
    • Note di versione
    Collabora
    • Github
    • Forum
    • Eventi
    • Traduzioni
    • Diventa nostro partner
    • Servizi per partner
    • Registra la tua società di contabilità
    Ottieni servizi
    • Trova un partner
    • Trova un contabile
    • Incontra un esperto
    • Servizi di implementazione
    • Testimonianze dei clienti
    • Supporto
    • Aggiornamenti
    GitHub Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Richiedi una demo
  • Prezzi
  • Aiuto

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Contabilità
  • Magazzino
  • PoS
  • Progetti
  • MRP
All apps
È necessario essere registrati per interagire con la community.
Tutti gli articoli Persone Badge
Etichette (Mostra tutto)
odoo accounting v14 pos v15
Sul forum
È necessario essere registrati per interagire con la community.
Tutti gli articoli Persone Badge
Etichette (Mostra tutto)
odoo accounting v14 pos v15
Sul forum
Assistenza

Evaluation of python expression

Iscriviti

Ricevi una notifica quando c'è un'attività per questo post

La domanda è stata contrassegnata
pythonodoo8.0
1 Rispondi
10867 Visualizzazioni
Avatar
dorsaf dhouibi

I tried to evaluate a python code that i write it in a text field.

this is the error that i face

TypeError: unsupported operand type(s) for %: 'ValidationError' and 'exceptions.ValueError'

First of all i have this class condition:

class condition(models.Model):
_name = 'condition'


expression_cond = fields.Selection(
string="Expression",
selection=[('python', 'Expression Python'), ],
required=True,
default="python")

value_cond = fields.Text(
string="Valeur",
required=True,
default="""
# Python code.
# You can use the following variables :
# - self: ORM model of the record which is checked
# - object: same as order or line, browse_record of the sale order or
# - pool: ORM model pool (i.e. self.pool)
# - cr: database cursor
# - context: current context
# - invoice: test for invoice tests
# Note: returned value have to be set in the variable 'result'
"""
)

name = fields.Char(string="Condition", required=True, )

The second class is the rules; i create this calss to select a condition and to verifier if the python expression is correct

class rules(models.Model):
_name = 'rules'

name = fields.Char(string="Régles" ,required=True)
code = fields.Char(string="Code" ,required=True)
condition_id = fields.Many2one(comodel_name="condition", string="Condition ", required=True, )
amount_type_id = fields.Many2one(comodel_name="amount.type", string="Méthode de Calcul", required=True, )

i create those method in this class

@api.model
def _rules_eval_context(self, obj_name, rec):
invoice = self.env['account.invoice']
return {obj_name: rec,
'invoice': invoice,
'self': self.pool.get(rec._name),
'object': rec,
'obj': rec,
'pool': self.pool,
'cr': self._cr,
'uid': self._uid,
}

@api.model
def _rule_eval(self,rule, obj_name, rec):
rule =self.condition_id.value_cond
expr = rule
space = self._rules_eval_context(obj_name, rec)

)

try:
safe_eval(expr,
space,
mode='exec',
nocopy=True) # nocopy allows to return 'result'
except Exception as e:
raise ValidationError("Wrong python code defined for Condition : %s") % (e)
return space.get('result', False)

@api.onchange('condition_id')
def _detect_rules(self):

rule = self.condition_id.value_cond
if self._rule_eval(rule,'invoice', self):
print("*****dectect rules")
else:
print("*********wrong detection")

 this is my expression that i creat 

if invoice.amout_total >=1000 :
    result=True

 you can find the pictures on this link

https://drive.google.com/drive/folders/0BwRD3KEH7ZiVS0VFemxTNjJkSHc?usp=sharing










ff

0
Avatar
Abbandona
Avatar
Annadurai
Risposta migliore

Hello, Try this

raise   ValidationError("Wrong python code defined for Condition : %s") % (e.name)
1
Avatar
Abbandona
dorsaf dhouibi
Autore

thanks but its give me this error

AttributeError: 'exceptions.TypeError' object has no attribute 'name'

Niyas Raphy (Walnut Software Solutions)

Please check this sample and just adjust accordingly,

raise ValidationError(_("The amount of the payment '%s' is too high. The maximum permitted is %s.") % (payment.name, str(9) * (max_digits - 3) + ".99"))

dorsaf dhouibi
Autore

its work thanks Annadurai

i don't have the error anymore but i still don't find a solution how to create a python expression to test for example if the amount_total of an invoice >=1000

dorsaf dhouibi
Autore

Sorry thanks Niyas :)

Annadurai

do upvote :)

Ti stai godendo la conversazione? Non leggere soltanto, partecipa anche tu!

Crea un account oggi per scoprire funzionalità esclusive ed entrare a far parte della nostra fantastica community!

Registrati
Post correlati Risposte Visualizzazioni Attività
create a getter for odoo 8 Risolto
python odoo8.0
Avatar
Avatar
2
lug 17
4670
recuperate fields of budget lines
python odoo8.0
Avatar
0
ago 16
3762
Using Blob to save in postgres via odoo
python odoo8.0
Avatar
Avatar
1
lug 16
5864
How to add auto refreshing for hr_attendance in odoo?
python odoo8.0
Avatar
0
giu 16
3798
Team Manager
python python2.7 odoo8.0
Avatar
0
ago 17
3689
Community
  • Tutorial
  • Documentazione
  • Forum
Open source
  • Scarica
  • Github
  • Runbot
  • Traduzioni
Servizi
  • Hosting Odoo.sh
  • Supporto
  • Aggiornamenti
  • Sviluppi personalizzati
  • Formazione
  • Trova un contabile
  • Trova un partner
  • Diventa nostro partner
Chi siamo
  • La nostra azienda
  • Branding
  • Contattaci
  • Lavora con noi
  • Eventi
  • Podcast
  • Blog
  • Clienti
  • Note legali • Privacy
  • Sicurezza
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo è un gestionale di applicazioni aziendali open source pensato per coprire tutte le esigenze della tua azienda: CRM, Vendite, E-commerce, Magazzino, Produzione, Fatturazione elettronica, Project Management e molto altro.

Il punto di forza di Odoo è quello di offrire un ecosistema unico di app facili da usare, intuitive e completamente integrate tra loro.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now