From 65656ff559def5d4ecfa904e6266b524d14bca5b Mon Sep 17 00:00:00 2001 From: Swapnil Date: Wed, 31 Jan 2024 11:47:11 +0530 Subject: [PATCH] db: API to deactivate token & fixes --- database/main.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/database/main.py b/database/main.py index 9e1835f..2bb1ec9 100644 --- a/database/main.py +++ b/database/main.py @@ -34,6 +34,7 @@ def create_token(): access_token = data.get('access_token') refresh_token = data.get('refresh_token') expiration_seconds = data.get('expiration_seconds') + readwise_api_key = data.get('readwise_api_key') required_fields = ['email', 'access_token', 'refresh_token', 'expiration_seconds'] missing_fields = [field for field in required_fields if not data.get(field)] @@ -46,7 +47,13 @@ def create_token(): if existing_token: return jsonify({'error': 'An active token with this email already exists'}), 400 - new_token = Token(email=email, access_token=access_token, refresh_token=refresh_token, expiration_seconds=expiration_seconds) + new_token = Token( + email=email, + access_token=access_token, + refresh_token=refresh_token, + expiration_seconds=expiration_seconds, + readwise_api_key=readwise_api_key + ) db.session.add(new_token) db.session.commit() @@ -101,16 +108,24 @@ def update_token_by_id(id): token.access_token = data.get('access_token', token.access_token) token.refresh_token = data.get('refresh_token', token.refresh_token) token.expiration_seconds = data.get('expiration_seconds', token.expiration_seconds) - token.active = data.get('active', token.active) token.readwise_api_key = data.get('readwise_api_key', token.readwise_api_key) token.updated_at = datetime.utcnow() db.session.commit() return '', 204 +# deactivate token +@app.route('/token//deactivate', methods=['POST']) +def deactivate_token(id): + token = Token.query.get_or_404(id) + token.active = False + db.session.commit() + return '', 204 + # get all tokens @app.route('/token/all', methods=['GET']) def get_all_tokens(): - tokens = Token.query.all() + only_active = request.args.get('only_active') + tokens = Token.query.all() if not only_active else Token.query.filter_by(active=True).all() tokens_info = [{ 'id': token.id, 'email': token.email,