db: API to deactivate token & fixes

This commit is contained in:
Swapnil 2024-01-31 11:47:11 +05:30
parent e80e88bb99
commit 65656ff559
Signed by: swapnil
GPG Key ID: 58029C48BB100574

View File

@ -34,6 +34,7 @@ def create_token():
access_token = data.get('access_token') access_token = data.get('access_token')
refresh_token = data.get('refresh_token') refresh_token = data.get('refresh_token')
expiration_seconds = data.get('expiration_seconds') expiration_seconds = data.get('expiration_seconds')
readwise_api_key = data.get('readwise_api_key')
required_fields = ['email', 'access_token', 'refresh_token', 'expiration_seconds'] required_fields = ['email', 'access_token', 'refresh_token', 'expiration_seconds']
missing_fields = [field for field in required_fields if not data.get(field)] missing_fields = [field for field in required_fields if not data.get(field)]
@ -46,7 +47,13 @@ def create_token():
if existing_token: if existing_token:
return jsonify({'error': 'An active token with this email already exists'}), 400 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.add(new_token)
db.session.commit() db.session.commit()
@ -101,16 +108,24 @@ def update_token_by_id(id):
token.access_token = data.get('access_token', token.access_token) token.access_token = data.get('access_token', token.access_token)
token.refresh_token = data.get('refresh_token', token.refresh_token) token.refresh_token = data.get('refresh_token', token.refresh_token)
token.expiration_seconds = data.get('expiration_seconds', token.expiration_seconds) 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.readwise_api_key = data.get('readwise_api_key', token.readwise_api_key)
token.updated_at = datetime.utcnow() token.updated_at = datetime.utcnow()
db.session.commit() db.session.commit()
return '', 204 return '', 204
# deactivate token
@app.route('/token/<id>/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 # get all tokens
@app.route('/token/all', methods=['GET']) @app.route('/token/all', methods=['GET'])
def get_all_tokens(): 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 = [{ tokens_info = [{
'id': token.id, 'id': token.id,
'email': token.email, 'email': token.email,