Pergunta

[solved] i mixed up class method and instance method before. so when i

tried to call strptime(cls.ts,format) inside @classmethod function , i was actually calling

ts = ndb.StringProperty() or StringProperty('ts'),but not the string value inside ts.

the correct one is to remove @ decorator and call

strptime(self.ts,format) .

original question below

1:when the Token instance is created , init_created() is called to inititalize ts to a string.

2:user request the verification handler , with token as a arg , the token is used to retrieve the Token instance.

3:i use the Token instance to call is_valid() .In is_valid , i convert ts back to datetime obj, to compare to other datetime obj.

error 1 :

when i set( with the str() method )

delta = (now-datetime.strptime(str(cls.ts),'%Y-%b-%d / %H:%M:%S:%f')).total_seconds()

i get

ValueError: time data "StringProperty('ts')" does not match format '%Y-%b-%d / %H:%M:%S:%f'

error 2 : so i try another way. i set (without str() )

delta = (now-datetime.strptime(cls.ts,'%Y-%b-%d / %H:%M:%S:%f')).total_seconds()

i get

TypeError: must be string, not StringProperty

so my question is how to correctly pass a stringproperty to strptime method.

thank you very much .

below is my code:

class Token(ndb.Model):
     ts = ndb.StringProperty()

     @classmethod
     def init_created(cls):
          ts = (datetime.utcnow() + timedelta(hours=8)).strftime(format='%Y-%b-%d / %H:%M:%S:%f')
          return ts
     @classmethod
     def is_valid(cls):
          now = (datetime.utcnow() + timedelta(hours=8))
          delta = (now-datetime.strptime(cls.ts,'%Y-%b-%d / %H:%M:%S:%f')).total_seconds()
          return (delta<expire_time)


class Verification(webapp2.RequestHandler):
     def get(self , token ):
         token_cls = Token.query(Token.token == token ).get()
         if not (token_cls and token_cls.is_valid() ) :
              template = jinja_environment.get_template('verification_error.html' )
              pass


    must be string, not StringProperty
Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s/1.374948928179626607/main.py", line 216, in get
    if not (token_cls and token_cls.is_valid() ) :
  File "/base/data/home/apps/s~/1.374948928179626607/main.py", line 86, in is_valid
    delta = (now-datetime.strptime(cls.ts,'%Y-%b-%d / %H:%M:%S:%f')).total_seconds()
TypeError: must be string, not StringProperty
Foi útil?

Solução

Your is_valid method shouldn't have the @classmethod decorator, as with it you're operating on the Token class, and not on the entity returned from your query. Once you've removed the decorator, it would be idiomatic to change cls to self.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top