Project

General

Profile

Development #45320

recherche par date de naissance : 25/09 provoque un « month must be in 1..12 »

Added by Thomas Noël over 1 year ago. Updated about 1 year ago.

Status:
Résolu (à déployer)
Priority:
Normal
Target version:
-
Start date:
21 Jul 2020
Due date:
% Done:

0%

Estimated time:
Patch proposed:
Yes
Planning:
No

Description

Internal Server Error: /rsu/search/

ValueError at /rsu/search/
month must be in 1..12

Request Method: GET
Request URL: https://zoo/rsu/search/?q=prenom%20nom%2025/09
Django Version: 1.11.29
Python Executable: /usr/bin/uwsgi-core
Python Version: 2.7.16
Python Path: ['.', '', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
Server time: mar, 21 Jul 2020 15:04:53 +0200
Installed Applications:
''
Installed Middleware:
''

Traceback:

File "/usr/lib/python2.7/dist-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/usr/lib/python2.7/dist-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/usr/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch
  495.             response = self.handle_exception(exc)

File "/usr/lib/python2.7/dist-packages/rest_framework/views.py" in handle_exception
  455.             self.raise_uncaught_exception(exc)

File "/usr/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch
  492.             response = handler(request, *args, **kwargs)

File "/usr/lib/python2.7/dist-packages/zoo/zoo_nanterre/api_views.py" in get
  174.             search = search.search_query(request.GET['q'])

File "/usr/lib/python2.7/dist-packages/zoo/zoo_nanterre/utils.py" in search_query
  245.                 self = self.search_birthdate(birthdate, window_days=0)

File "/usr/lib/python2.7/dist-packages/zoo/zoo_nanterre/utils.py" in search_birthdate
  306.                 after = datetime.date(int(birthdate['year']), int(birthdate['month']), 1)

Exception Type: ValueError at /rsu/search/
Exception Value: month must be in 1..12

il faudrait être un peu rusé et chercher plutôt le 25 septembre. Et pour quelque chose comme 12/09, chercher 12 septembre et septembre 2009.


Files

Associated revisions

Revision a3bda805 (diff)
Added by Benjamin Dauvergne about 1 year ago

nanterre: ignore invalid dates during free text search (#45320)

History

#1

Updated by Thomas Noël over 1 year ago

Et sans chercher à être rusé, d'abord déjà ne pas considérer 25/09 comme un mois/année.

#2

Updated by Benjamin Dauvergne over 1 year ago

Même en corrigeant les regexp pour un ordre plus logique, je te l'accorde, des alternatives il faudra tout même intercepter les ValueError, on ne peut pas matcher des dates valides avec des regexp ou alors une regexp vraiment vraiment super compliquée.

#3

Updated by Thomas Noël over 1 year ago

Je pensais à quelque chose d'un peu bête-et-méchant, que PersonSearch.search_date renvoie juste self quand birthdate n'est en fait pas une date utilisable :

diff --git a/zoo/zoo_nanterre/utils.py b/zoo/zoo_nanterre/utils.py
index a44a5cd..ea07cc2 100644
--- a/zoo/zoo_nanterre/utils.py
+++ b/zoo/zoo_nanterre/utils.py
@@ -278,7 +278,6 @@ class PersonSearch(object):
         return self

     def search_birthdate(self, birthdate, window_days=None):
-        self = copy.deepcopy(self)

         if hasattr(birthdate, 'year'):
             if hasattr(birthdate, 'date'):
@@ -299,16 +298,21 @@ class PersonSearch(object):
                 else:
                     year += 2000
             birthdate['year'] = str(year)
-            if birthdate['day']:
-                before = after = datetime.date(
-                        int(birthdate['year']), int(birthdate['month']), int(birthdate['day']))
-            elif birthdate['month']:
-                after = datetime.date(int(birthdate['year']), int(birthdate['month']), 1)
-                before = ((after + datetime.timedelta(days=31)).replace(day=1)
-                          - datetime.timedelta(days=1))
-            else:
-                after = datetime.date(int(birthdate['year']), 1, 1)
-                before = datetime.date(int(birthdate['year']), 12, 31)
+            try:
+                if birthdate['day']:
+                    before = after = datetime.date(
+                            int(birthdate['year']), int(birthdate['month']), int(birthdate['day']))
+                elif birthdate['month']:
+                    after = datetime.date(int(birthdate['year']), int(birthdate['month']), 1)
+                    before = ((after + datetime.timedelta(days=31)).replace(day=1)
+                              - datetime.timedelta(days=1))
+                else:
+                    after = datetime.date(int(birthdate['year']), 1, 1)
+                    before = datetime.date(int(birthdate['year']), 12, 31)
+            except ValueError:  # invalid date
+                return self
+
+        self = copy.deepcopy(self)

         first_january_same_year = None
         if before == after and window_days is not None:

Ca te semble un mauvaise piste ?

#4

Updated by Benjamin Dauvergne about 1 year ago

  • Assignee set to Benjamin Dauvergne
#5

Updated by Benjamin Dauvergne about 1 year ago

#6

Updated by Thomas Noël about 1 year ago

  • Status changed from Solution proposée to Solution validée

Yep, impec, merci !

#7

Updated by Benjamin Dauvergne about 1 year ago

  • Status changed from Solution validée to Résolu (à déployer)
commit a3bda805cc6fc0d0c7939ce3de218935ef6f21d0
Author: Benjamin Dauvergne <bdauvergne@entrouvert.com>
Date:   Fri Sep 18 08:54:48 2020 +0200

    nanterre: ignore invalid dates during free text search (#45320)

Also available in: Atom PDF