def decorator(*args, **kwargs): try: return func(*args, **kwargs) except AttributeError: return pack_resp('Type error.', 400) except Exception as e: logger.error(e) return pack_resp('Server error.', 500)
def get_games(id=None): if not id: games = Game.get_all() return pack_resp(games) game = Game.get_first(id=id) if not game: return pack_resp('No this item.') return pack_resp(game)
def get_articles(id=None): if not id: articles = Article.get_all(status=0) return pack_resp(articles) article = Article.get_first(id=id, status=0) if not article: return pack_resp('No this item.') return pack_resp(article) # TODO: add put / update uri # TODO: ip record.
def add_user(): username = request.json.get('username') password = request.json.get('password') if not (username and password): return pack_resp('Not enough params.', 400) if User.isExist(username): return pack_resp('Username has been registered.') user = User(username=username) user.hash_password(password) user = User.add(user) if not user: return pack_resp('Create failed.', 500) return pack_resp({'username': user.username}, 201)
def decorator(*args, **kwargs): auth = request.authorization if auth is None and 'Authorization' in request.headers: try: auth_type, token = request.headers['Authorization'].split( None, 1) auth = Authorization(auth_type, {'token': token}) except ValueError: pass if auth is None or auth.type.lower() != 'basic': return pack_resp('Unauthorized.', 401) user = User.isExist(auth.username) if user and user.verify_password(auth.password): pass else: print(auth.username) user = User.verify_auth_token(auth.username) if not user: return pack_resp('Unauthorized.', 401) g.user = user return func(*args, **kwargs)
def get_auth_token(): token = g.user.generate_auth_token() return pack_resp({'token': token.decode('ascii')})
def decorator(*args, **kwargs): if not hasattr(g, 'user') or g.user.role != -1: return pack_resp('Forbidden.', 403) return func(*args, **kwargs)