From 0c1e18cad9fce4c03f94e51b64926f4e8e30f62d Mon Sep 17 00:00:00 2001 From: Swapnil Date: Tue, 30 Jan 2024 22:16:40 +0530 Subject: [PATCH] db: new columns & APIs --- database/main.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/database/main.py b/database/main.py index f142281..1711b80 100644 --- a/database/main.py +++ b/database/main.py @@ -16,7 +16,8 @@ class Token(db.Model): expiration_seconds = db.Column(db.Integer, nullable=False) readwise_api_key = db.Column(db.String(255)) active = db.Column(db.Boolean, default=True) - timestamp = db.Column(db.DateTime, default=datetime.utcnow) + created_at = db.Column(db.DateTime, default=datetime.utcnow) + updated_at = db.Column(db.DateTime, default=datetime.utcnow) def __repr__(self): return f'' @@ -65,7 +66,30 @@ def get_token_by_id(id): 'expiration_seconds': int(token.expiration_seconds), 'readwise_api_key': token.readwise_api_key, 'active': token.active, - 'timestamp': int(token.timestamp.timestamp()) + 'created_at': int(token.created_at.timestamp()), + 'updated_at': int(token.updated_at.timestamp()) + } + return jsonify({'token': token_info}), 200 + +# API to get the token based on the email +@app.route('/token', methods=['GET']) +def get_token_by_email(): + email = request.args.get('email') + if not email: + return jsonify({'error': 'Missing email query parameter'}), 400 + token = Token.query.filter_by(email=email, active=True).first() + if not token: + return '', 204 + token_info = { + 'id': token.id, + 'email': token.email, + 'access_token': token.access_token, + 'refresh_token': token.refresh_token, + 'expiration_seconds': int(token.expiration_seconds), + 'readwise_api_key': token.readwise_api_key, + 'active': token.active, + 'created_at': int(token.created_at.timestamp()), + 'updated_at': int(token.updated_at.timestamp()) } return jsonify({'token': token_info}), 200 @@ -79,6 +103,7 @@ def update_token_by_id(id): 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