Re: [issue2551126] AttributeError: 'str' object has no attribute 'local'

"John P. Rouillard" <[email protected]>
Newsgroups gmane.comp.bug-tracking.roundup.devel
Message-ID <[email protected]>
Hi all:

Note Reda probably won't see this unless s/he looks at the archive.
But I wanted this discussion to have a waider audience.

In message <[email protected]>,
reda writes:
>
>New submission from reda:
>
>I am using roundup 1.6.1 and i get the following exception with the
>Date property "chiffrageDelai":
>
><type 'exceptions.AttributeError'> : 'str' object has no attribute 'local'
>
>Les informations de déboguage suivent
>
>    Dans python expression "context.chiffrageDelai.pretty('%Y-%m-%d')"
>    Lors de l'évaluation de l'expression à la ligne 485
[...]
>    options 	{'ok_message': [], 'error_message': ['Erreur de modification\xc2\xa0: Cl\xc3\xa9 doc incorrecte : sans']}
[...]
>    request <HTMLRequest {'_context': None, 'startwith': 0, 'show':
>          <roundup.support.TruthDict instance at 0x7fe2cad59560>,
>          'classname': 'issue', 'special_char': '@', 'dispname': None,
>          'group': [], '_client': <roundup.cgi.client.Client instance at
>          0x7fe2cb45eb48>, 'template': 'item', 'input': <function
>          input_html4 at 0x7fe2cd3d8398>, 'columns': [], 'sort': [],
>          'env': {'CONTENT_LENGTH': '4030', 'HTTP_AUTHORIZATION': None,
>          'SERVER_PORT': '8080', 'SERVER_NAME': 'xxxxxxx.com',
>          'HTTP_COOKIE':
>          'roundup_session_REX=MTYxNDk2NDQwMy41NjAuMDQwMDI5NjY0NzM0OA',
>          'HTTP_X_FORWARDED_FOR': '10.107.48.53', 'HTTP_ACCEPT_LANGUAGE':
>          'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3', 'HTTP_REFERER':
>          'https://xxxxxx.com/rex/issue30774', 'HTTP_X_FORWARDED_HOST':
>          'xxxxxxx.com', 'SCRIPT_NAME': '', 'REQUEST_METHOD': 'POST',
>          'HTTP_HOST': 'xxxxxx.com', 'PATH_INFO': 'issue30774',
>          'CONTENT_TYPE': 'multipart/form-data;
>          boundary=---------------------------36321429803449636767669947981',
>          'TRACKER_NAME': 'rex', 'HTTP_ORIGIN': 'https://xxxxxxx.com'},
>          'form': FieldStorage(None, None, [FieldStorage('@template',
>          None, 'item'), FieldStorage('title', None, 'foating point
>          error'), FieldStorage('priority', None, '2'),
>          FieldStorage('type', None, '1'), FieldStorage('status', None,
>          '5'), FieldStorage('produit', None, '3'),
>          FieldStorage('version', None, '85'), FieldStorage('nosy', None,
>          'jerome'), FieldStorage('projet', None, '1'),
>          FieldStorage('validReal', None, 'yes'),
>          FieldStorage('versCible', None, '-1'),
>          FieldStorage('assignedto', None, '728'),
>          FieldStorage('intervenant', None, '3'),
>          FieldStorage('etatIntervenant', None, '3'),
>          FieldStorage('chiffrageCharge', None, '0.5'),
>          FieldStorage('chiffrageDelai', None, '2021-03-25'),
>          FieldStorage('realiseCharge', None, '0.5'),
>          FieldStorage('realiseDelai', None, '2021-03-25'),
>          FieldStorage('validation', None, 'test fourni'),
>          FieldStorage('corrVexpl', None, 'no'), FieldStorage('fauxVexpl',
>          None, ''), FieldStorage('corrVdev', None, 'no'),
>          FieldStorage('fauxVdev', None, ''), FieldStorage('impactDoc',
>          None, 'sans objet'), FieldStorage('nbJours', None, '0.5'),
>          FieldStorage('@note', None, ''), FieldStorage('@file', '', ''),
>          FieldStorage('@file@comment', None, ''),
>          FieldStorage('@lastactivity', None, '2021-03-26.15:30:00'),
>          FieldStorage('@csrf', None,
>          '8df2abf461f1eb851382cd3a88b0b3b6e35eca8519bed2356315b5088bcda918'),
>          FieldStorage('@action', None, 'edit'),
>          FieldStorage('submit_button', None, 'Soumettre les
>          changements')]), 'nodeid': '30774', 'base':
>          'https://xxxxxxx.com/rex/', 'user': <HTMLItem(0x7fe2cad4a690)
>          user 728>, 'search_text': None, 'pagesize': 50, 'language':
>          ['fr', 'fr_FR', 'en_US', 'en'], 'filterspec': {}, 'filter': [],
>          'client': <roundup.cgi.client.Client instance at
>          0x7fe2cb45eb48>}>
[...]
>    attrs 	{'tal:condition': 'python:context.chiffrageDelai.isset()', 'name': 'chiffrageDelai', 'tal:attributes': "value python:context.chiffrageDelai.pretty('%Y-%m-%d')"}
[...]
>    Un problème est apparu dans votre modèle « issue.item.html ».
>
>Historique complet :
>
>Traceback (most recent call last):
[...]
>  File "/usr/local/lib/python2.7/dist-packages/roundup/cgi/TAL/TALInterpreter.py", line 359, in attrAction_tal
>    evalue = self.engine.evaluateText(item[3])
>  File "/usr/local/lib/python2.7/dist-packages/roundup/cgi/PageTemplates/TALES.py", line 231, in evaluateText
>    text = self.evaluate(expr)
>  File "/usr/local/lib/python2.7/dist-packages/roundup/cgi/PageTemplates/TALES.py", line 225, in evaluate
>    return expression(self)
>  File "/usr/local/lib/python2.7/dist-packages/roundup/cgi/PageTemplates/PythonExpr.py", line 67, in __call__
>    return f()
>  File "<string>", line 2, in f
>  File "/usr/local/lib/python2.7/dist-packages/roundup/cgi/templating.py", line 1958, in pretty
>    return self._value.local(offset).pretty(format)

In the DateHTMLProperty, the plain method has code that reads:

      ...
      try:
         return str(self._value.local(offset))
      except AttributeError:
         # not a date value, e.g. from unsaved form data
         return str(self._value)

I think there was an error when the form was submitted by reda:

  Erreur de modification\xc2\xa0: Cl\xc3\xa9 doc incorrecte : sans.

this means the date chiffrageDelai remains a string. But it is treated as a
date at the end of the pretty() method. I think the fix is to wrap the if
statement at the end of pretty() like so:

      ...
      try:
        if not self._value:
            return ''
        elif format is not self._marker:
            return self._value.local(offset).pretty(format)
        else:
            return self._value.local(offset).pretty()
      except AttributeError:
         # not a date value, e.g. from unsaved form data
         return str(self._value)

The other thought I had was to check the type of self._value:

        if type(self._value) in (str, type(None)):
	    return(str(self._value)
	    
        if not self._value:
            return ''
        elif format is not self._marker:
            return self._value.local(offset).pretty(format)
        else:
            return self._value.local(offset).pretty()

but I think try/except is more pythonic.

Ralf or Bern, I though we had this issue before when fixing lost user
data when the form was invalid. I can't seem to find the checkins
when this work was done or even the version released with the fixes
but... Also I did not find any tests for this condition.

Does this analysis sound right? If so would my first fix be preferred?
Also am I correct that this fix will need to be applied to the other
methods in DateHTMLProperty.

--
		-- rouilj
John Rouillard
===========================================================================
My employers don't acknowledge my existence much less my opinions.


_______________________________________________
Roundup-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/roundup-devel
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.