diff --git a/database/main.py b/database/main.py index 2bb1ec9..602b670 100644 --- a/database/main.py +++ b/database/main.py @@ -22,6 +22,18 @@ class Token(db.Model): def __repr__(self): return f'' +# This table stores email-wise last annotation timestamp +# only one entry per email +class AnnotationLastUpdate(db.Model): + id = db.Column(db.String(36), primary_key=True, default=str(uuid.uuid4())) + email = db.Column(db.String(255), nullable=False) + last_update_time = db.Column(db.DateTime, nullable=False) + created_at = db.Column(db.DateTime, default=datetime.utcnow) + updated_at = db.Column(db.DateTime, default=datetime.utcnow) + + def __repr__(self): + return f'' + # Create an application context with app.app_context(): db.create_all() @@ -115,7 +127,7 @@ def update_token_by_id(id): # deactivate token @app.route('/token//deactivate', methods=['POST']) -def deactivate_token(id): +def deactivate_token_by_id(id): token = Token.query.get_or_404(id) token.active = False db.session.commit() @@ -139,5 +151,50 @@ def get_all_tokens(): } for token in tokens] return jsonify({'tokens': tokens_info}), 200 +# API to create or update the last annotation timestamp +@app.route('/annotation_last_update', methods=['POST']) +def create_or_update_annotation_last_update(): + data = request.get_json() + email = data.get('email') + last_update_time = data.get('last_update_time') + + required_fields = ['email', 'last_update_time'] + missing_fields = [field for field in required_fields if not data.get(field)] + + if missing_fields: + return jsonify({'error': f'Missing required fields: {", ".join(missing_fields)}'}), 400 + + existing_annotation_last_update = AnnotationLastUpdate.query.filter_by(email=email).first() + if existing_annotation_last_update: + existing_annotation_last_update.last_update_time = last_update_time + existing_annotation_last_update.updated_at = datetime.utcnow() + db.session.commit() + return '', 204 + else: + new_annotation_last_update = AnnotationLastUpdate( + email=email, + last_update_time=last_update_time + ) + db.session.add(new_annotation_last_update) + db.session.commit() + return '', 204 + +# API to get the last annotation timestamp based on the email +@app.route('/annotation_last_update/', methods=['GET']) +def get_annotation_last_update_by_email(email): + if not email: + return jsonify({'error': 'Missing email query parameter'}), 400 + annotation_last_update = AnnotationLastUpdate.query.filter_by(email=email).first() + if not annotation_last_update: + return '', 204 + annotation_last_update_info = { + 'id': annotation_last_update.id, + 'email': annotation_last_update.email, + 'last_update_time': int(annotation_last_update.last_update_time.timestamp()), + 'created_at': int(annotation_last_update.created_at.timestamp()), + 'updated_at': int(annotation_last_update.updated_at.timestamp()) + } + return jsonify(annotation_last_update_info), 200 + if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=True)