python - [solved]how to apply strptime(string,format) on StringProperty? -
[solved] mixed class method , instance method before. when
tried call strptime(cls.ts,format) inside @classmethod function , calling
ts = ndb.stringproperty() or stringproperty('ts'),but not string value inside ts.
the correct 1 remove @ decorator , call
strptime(self.ts,format) .
original question below
1:when token instance created , init_created() called inititalize ts string.
2:user request verification handler , token arg , token used retrieve token instance.
3:i use token instance call is_valid() .in is_valid , convert ts datetime obj, compare other datetime obj.
error 1 :
when set( 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')" not match format '%y-%b-%d / %h:%m:%s:%f'
error 2 : try way. set (without str() )
delta = (now-datetime.strptime(cls.ts,'%y-%b-%d / %h:%m:%s:%f')).total_seconds()
i get
typeerror: must string, not stringproperty
so question how correctly pass stringproperty strptime method.
thank .
below 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): = (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 , token_cls.is_valid() ) : template = jinja_environment.get_template('verification_error.html' ) pass must 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 if not (token_cls , 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 string, not stringproperty
your is_valid
method shouldn't have @classmethod
decorator, you're operating on token
class, , not on entity returned query. once you've removed decorator, idiomatic change cls
self
.
Comments
Post a Comment