Projet

Général

Profil

Bug #40235

time data '' does not match format '%d/%m/%Y'

Ajouté par Nicolas Roche il y a environ 4 ans. Mis à jour il y a environ 4 ans.

Statut:
Fermé
Priorité:
Normal
Assigné à:
Version cible:
-
Début:
27 février 2020
Echéance:
% réalisé:

0%

Temps estimé:
Patch proposed:
Oui
Planning:
Non

Description

Exception:
  type = '<class 'ValueError'>', value = 'time data '' does not match format '%d/%m/%Y''
...

  File "/usr/lib/python3.5/_strptime.py", line 504, in _strptime_time
   502     """Return a time struct based on the input string and the
   503     format string.""" 
>  504     tt = _strptime(data_string, format)[0]
   505     return time.struct_time(tt[:time._STRUCT_TM_ITEMS])
   506

  locals:
     data_string = ''
     format = '%d/%m/%Y'

  File "/usr/lib/python3/dist-packages/wcs/fields.py", line 1238, in convert_value_from_str
  1236
  1237     def convert_value_from_str(self, value):
> 1238         return time.strptime(value, date_format())
  1239
  1240     def convert_value_to_str(self, value):

  locals:
     self = <DateField 5 'Date de naissance'>
     value = ''

Fichiers


Demandes liées

Lié à w.c.s. - Bug #40216: KeyError, optionsFermé27 février 2020

Actions
Lié à w.c.s. - Bug #40190: source de données dépendant d'un champ prérempli de la même (première) pageFermé25 février 2020

Actions

Révisions associées

Révision f506dc14 (diff)
Ajouté par Nicolas Roche il y a environ 4 ans

fields: convert empty date to None (#40235)

Historique

#1

Mis à jour par Thomas Noël il y a environ 4 ans

Il manque du contexte (trace trop tronquée). J'imagine que cela arrive dans une action "Donnée de traitement" où un champ de type date reçoit une chaine vide, qui ne peut donc être convertie en date, et voilà, crash.

Est-ce ça ?

#2

Mis à jour par Thomas Noël il y a environ 4 ans

Thomas Noël a écrit :

Est-ce ça ?

Et ce n'est pas cela. Suite de la trace :

...

File "/usr/lib/python3/dist-packages/wcs/fields.py", line 1238, in convert_value_from_str
  1236
  1237     def convert_value_from_str(self, value):
> 1238         return time.strptime(value, date_format())
  1239
  1240     def convert_value_to_str(self, value):

  locals:
     self = <DateField 5 'Date de naissance'>
     value = ''

  File "/usr/lib/python3/dist-packages/wcs/formdef.py", line 712, in get_field_data
   710         d[field.id] = widget.parse()
   711         if d.get(field.id) is not None and field.convert_value_from_str:
>  712             d[field.id] = field.convert_value_from_str(d[field.id])
   713         if d.get(field.id) is not None and field.store_display_value:
   714             display_value = field.store_display_value(d, field.id)

  locals:
     d = {'5': ''}
     field = <DateField 5 'Date de naissance'>
     self = <FormDef "....." id:246>
     widget = <DateWidget at 7f21ee5f6e10: f5>

  File "/usr/lib/python3/dist-packages/wcs/formdef.py", line 734, in get_data
   732             widget = form.get_widget('f%s' % field.id)
   733             if widget:
>  734                 d.update(self.get_field_data(field, widget))
   735         return d
   736

  locals:
     d = {'2': 'XXXXXX', '3': 'XXXXXXX'}
     field = <DateField 5 'Date de naissance'>
     form = <wcs.qommon.form.Form object at 0x7f21ee6846a0>
     self = <FormDef "..." id:246>
     widget = <DateWidget at 7f21ee5f6e10: f5>

  File "/usr/lib/python3/dist-packages/wcs/forms/root.py", line 405, in page
   403                 # include prefilled data
   404                 transient_formdata = self.get_transient_formdata()
>  405                 transient_formdata.data.update(self.formdef.get_data(form))
   406             else:
   407                 req.form = {}

Ca viendrait donc en conséquence de #40190 :/

#3

Mis à jour par Nicolas Roche il y a environ 4 ans

  • Assigné à Nicolas Roche supprimé

Je pensais renvoyer None (comme pour les autres conversions) mais :

Thomas‎: nope, on a besoin du crash en cas d'erreur

#4

Mis à jour par Thomas Noël il y a environ 4 ans

  • Assigné à mis à Nicolas Roche

Je pense que la solution de Nicolas va dans le bon sens :

--- a/wcs/fields.py
+++ b/wcs/fields.py
@@ -1235,7 +1235,10 @@ class DateField(WidgetField):
         return date_value

     def convert_value_from_str(self, value):
-        return time.strptime(value, date_format())
+        try:
+            return time.strptime(value, date_format())
+        except ValueError:
+            return None

Mais à l'approche de la mise en prod, je ne mesure pas les conséquences, et la couverture de #40190 n'est clairement pas totale.

Je reverte #40190 (et sa suite #40216) et on y reviendra la semaine prochaine (sur eux et sur le présent ticket)

#5

Mis à jour par Thomas Noël il y a environ 4 ans

#6

Mis à jour par Thomas Noël il y a environ 4 ans

  • Lié à Bug #40190: source de données dépendant d'un champ prérempli de la même (première) page ajouté
#7

Mis à jour par Thomas Noël il y a environ 4 ans

  • Tracker changé de Development à Bug
#8

Mis à jour par Frédéric Péters il y a environ 4 ans

Je reverte #40190 (et sa suite #40216) et on y reviendra la semaine prochaine (sur eux et sur le présent ticket)

Je préférerais pas.

#9

Mis à jour par Frédéric Péters il y a environ 4 ans

Mais trop tard, tant pis.

#10

Mis à jour par Frédéric Péters il y a environ 4 ans

Quant au patch; plutôt taper un test dédié dans tests/test_fields.py que taper ça au milieu d'autre chose.

#11

Mis à jour par Frédéric Péters il y a environ 4 ans

Et plutôt faire un if not value: return None que tout attraper.

#13

Mis à jour par Nicolas Roche il y a environ 4 ans

Comme ça je ne touche pas au test sur les fiches, désolé de ne pas y avoir pensé.

#14

Mis à jour par Frédéric Péters il y a environ 4 ans

  • Statut changé de Solution proposée à Résolu (à déployer)

Voilà c'est poussé :

commit f506dc14b9ba3ff27696073b459058bcfffe8e2a
Author: Nicolas ROCHE <nroche@entrouvert.com>
Date:   Thu Feb 27 14:43:58 2020 +0100

    fields: convert empty date to None (#40235)

Et j'ai dans la foulée pousser un autre test, test_convert_from_empty_string, qui vérifie convert_value_from_str pour tous les champs.

#15

Mis à jour par Frédéric Péters il y a environ 4 ans

  • Statut changé de Résolu (à déployer) à Solution déployée

Formats disponibles : Atom PDF