Created
May 28, 2014 15:17
-
-
Save renzon/aaafb25b42ba07a9efc5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#decorator 1 | |
from datetime import datetime | |
import functools | |
import logging | |
def query_checker(*keys_to_check): | |
""" Checks if a given key exists in the query string dictionary, if not, never executes the real handler and return status 400 """ | |
def real_decorator(func): | |
@functools.wraps(func) | |
def wraper(handler): | |
def eval(): | |
for k in keys_to_check: | |
if k not in handler.request.GET: | |
handler.response.status_int = 400 | |
handler.response.write('Query param: %s must be filled for this request' % keys_to_check) | |
return False | |
return True | |
return func(handler) if eval() else None | |
return wraper | |
return real_decorator | |
#decorator 2 | |
def log_time(func): | |
def wraper(handler): | |
start = datetime.now().replace(microsecond=0) | |
func(handler) | |
end = datetime.now().replace(microsecond=0) | |
logging.debug(" Time to run request in seconds: %s" % str((end-start).seconds)) | |
return wraper | |
#decorated function: | |
@log_time | |
@query_checker('link') | |
def get(self): | |
#implementacao | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment