Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
1 Antworten
1185 Ansichten

Hi, in this program, I'm trying to automatically correct a value if the user uses the wrong syntax, for example, if they enter '12h12,' I'd like to auto-correct it to '12:12.' Even though the corrected_value is modified correctly, it's not returned in my ERP, which retains the incorrect value. So, I assume the error comes from the following two lines: value = corrected_value self.write({field_name: value})

How can I fix this?"



# -*- coding: utf-8 -*-

import json

from openerp.exceptions import except_orm, ValidationError, Warning

from openerp import models, fields, api, _


import requests

import re


class x_days(models.Model):

  _name = 'x.days'

  name = fields.Char(string='Breaks', copy=False)

  breaks_monday = fields.Char(string='Monday breaks')

  breaks_tuesday = fields.Char(string='Tuesday breaks')

  breaks_wednesday = fields.Char(string='Wednesday breaks')

  breaks_thursday = fields.Char(string='Thursday breaks')

  breaks_friday = fields.Char(string='Friday breaks')


  def config(self):

    get_rcdst = self.search([])

    mon = get_rcdst.read(['breaks_monday'])

    tue = get_rcdst.read(['breaks_tuesday'])

    wed = get_rcdst.read(['breaks_wednesday'])

    thu = get_rcdst.read(['breaks_thursday'])

    fri = get_rcdst.read(['breaks_friday'])

    d = {'fri': [], 'mon': [], 'tue': [], 'thu': [], 'wed': []}

    l = [mon, tue, wed, thu, fri]

    for i in mon:

      d['mon'].append(i['breaks_monday'])

    for i in tue:

      d['tue'].append(i['breaks_tuesday'])

    for i in wed:

      d['wed'].append(i['breaks_wednesday'])

    for i in thu:

      d['thu'].append(i['breaks_thursday'])

    for i in fri:

      d['fri'].append(i['breaks_friday'])


    d = json.dumps(d)

    headers = {'Content-Type': 'application/json'}

    response = requests.post('http://172.19.0.35:5000/new_slot', data=d, headers=headers)


    if response.status_code == 200:

      print('Requête POST réussie')

      print('Réponse du serveur:', response.text)

    else:

      print('Échec de la requête POST')

      print('Code d état HTTP:', response.status_code)

    return 0


  @api.multi

  def write(self, vals):

    rc = super(x_days, self).write(vals)

    self.config()

    return rc


  @api.model

  def create(self, vals):

    rc = super(x_days, self).create(vals)

    self.config()

    return rc


  @api.onchange('breaks_monday', 'breaks_tuesday', 'breaks_wednesday', 'breaks_thursday', 'breaks_friday')

  def _onchange_my_field(self):

      for field_name in ['breaks_monday', 'breaks_tuesday', 'breaks_wednesday', 'breaks_thursday', 'breaks_friday']:

          value = getattr(self, field_name)

          if value:

              pattern = r'^\d{2}:\d{2}$'  # Syntaxe correcte "hh:mm"

              if not re.match(pattern, value):

                  corrected_value = self._try_to_correct_value(value)

                  if corrected_value:

                      value = corrected_value

                      self.write({field_name: value})

                  else:

                      raise ValidationError("Syntaxe incorrecte. Utilisez hh:mm.")

                      return 0



  def _try_to_correct_value(self, value):

    corrected_value = None

    if ',' in value:

        corrected_value = value.replace(',', ':')

    elif 'h' in value:

        corrected_value = value.replace('h', ':')

    return corrected_value


Avatar
Verwerfen
Beste Antwort

Hi,

Just try updating the following line of code 

value = corrected_value 

​self.write({field_name: value})

like this,

​self[field_name] = corrected_value

Thanks


Avatar
Verwerfen
Verknüpfte Beiträge Antworten Ansichten Aktivität
2
Okt. 23
1561
2
Aug. 23
3293
4
Aug. 23
19707
3
Okt. 22
10217
3
März 22
36636