db: new columns & APIs

This commit is contained in:
Swapnil 2024-01-30 22:16:40 +05:30
parent 97c3354de8
commit 0c1e18cad9
Signed by: swapnil
GPG Key ID: 58029C48BB100574

View File

@ -16,7 +16,8 @@ class Token(db.Model):
expiration_seconds = db.Column(db.Integer, nullable=False) expiration_seconds = db.Column(db.Integer, nullable=False)
readwise_api_key = db.Column(db.String(255)) readwise_api_key = db.Column(db.String(255))
active = db.Column(db.Boolean, default=True) 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): def __repr__(self):
return f'<Token {self.id}>' return f'<Token {self.id}>'
@ -65,7 +66,30 @@ def get_token_by_id(id):
'expiration_seconds': int(token.expiration_seconds), 'expiration_seconds': int(token.expiration_seconds),
'readwise_api_key': token.readwise_api_key, 'readwise_api_key': token.readwise_api_key,
'active': token.active, '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 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.expiration_seconds = data.get('expiration_seconds', token.expiration_seconds)
token.active = data.get('active', token.active) 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()
db.session.commit() db.session.commit()
return '', 204 return '', 204