email-assistant/indexer/indexer/main.py

127 lines
3.6 KiB
Python
Raw Normal View History

2018-02-03 08:06:55 +00:00
import json
import os
import sys
2019-11-12 00:26:25 +00:00
import flask
2018-02-03 08:06:55 +00:00
from flask import jsonify
from flask import request
from flask.ext.sqlalchemy import SQLAlchemy
app = flask.Flask(__name__)
2019-11-12 00:26:25 +00:00
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get(
"SQLALCHEMY_DATABASE_URI", "sqlite:///../tokens.db"
)
2019-11-12 00:26:25 +00:00
app.config["SQLALCHEMY_ECHO"] = True
app.config["DEBUG"] = True
2018-02-03 08:06:55 +00:00
db = SQLAlchemy(app)
2019-11-12 00:26:25 +00:00
2018-02-03 08:06:55 +00:00
class EmailToken(db.Model):
"""Model to store the indexed tokens"""
2019-11-12 00:26:25 +00:00
2018-02-03 08:06:55 +00:00
id = db.Column(db.Integer, primary_key=True)
subject = db.Column(db.String(1024))
token = db.Column(db.String(1024))
token_type = db.Column(db.String(1024))
token_metadata = db.Column(db.String(2048))
disabled = db.Column(db.Boolean, default=False)
def get_token_metadata(self):
if self.token_metadata:
return json.loads(self.token_metadata)
return None
def as_dict(self):
return {
2019-11-12 00:26:25 +00:00
"id": self.id,
"subject": self.subject,
"token": self.token,
"type": self.token_type,
"metadata": self.get_token_metadata(),
"disabled": self.disabled,
2018-02-03 08:06:55 +00:00
}
@classmethod
def from_json(cls, data):
2019-11-12 00:26:25 +00:00
metadata = data.get("metadata")
2018-02-03 08:21:31 +00:00
try:
metadata = json.dumps(metadata)
except TypeError as err:
2019-11-12 00:26:25 +00:00
print("Error dumping metadata", err, file=sys.stderr)
2018-02-03 08:06:55 +00:00
return cls(
2019-11-12 00:26:25 +00:00
subject=data.get("subject"),
token=data.get("token"),
token_type=data.get("type"),
2018-02-03 08:06:55 +00:00
token_metadata=metadata,
2019-11-12 00:26:25 +00:00
disabled=data.get("disabled", False),
2018-02-03 08:06:55 +00:00
)
@classmethod
def jsonify_all(cls, token_type=None, desc=False):
query = cls.query
2018-02-03 08:06:55 +00:00
if token_type:
2019-11-12 00:26:25 +00:00
print("Filtering query by token type", file=sys.stderr)
query = query.filter_by(token_type=token_type)
if desc:
query = query.order_by(cls.id.desc())
return jsonify(tokens=[token.as_dict() for token in query.all()])
2018-02-03 08:06:55 +00:00
2019-11-12 00:26:25 +00:00
@app.route("/")
2018-02-03 08:06:55 +00:00
def check():
2019-11-12 00:26:25 +00:00
return "OK"
2018-02-03 08:06:55 +00:00
2019-11-12 00:26:25 +00:00
@app.route("/token", methods=["POST"])
2018-02-03 08:06:55 +00:00
def create_tokens():
"""Creates a token from posted JSON request"""
2018-02-06 01:56:37 +00:00
new_token = EmailToken.from_json(request.get_json(force=True))
existing_token = EmailToken.query.filter_by(
token=new_token.token,
token_type=new_token.token_type,
).first()
print(
2019-11-12 00:26:25 +00:00
"Received token with value {} and type {}".format(
new_token.token, new_token.token_type
),
file=sys.stderr,
2018-02-06 01:56:37 +00:00
)
2019-11-12 00:26:25 +00:00
print("Existing token? ", existing_token, file=sys.stderr)
2018-02-06 01:56:37 +00:00
if not existing_token:
2019-11-12 00:26:25 +00:00
print("No existing token, creating a new one", file=sys.stderr)
2018-02-06 01:56:37 +00:00
db.session.add(new_token)
db.session.commit()
db.session.refresh(new_token)
2019-11-12 00:26:25 +00:00
return jsonify(success=True, created=True, record=new_token.as_dict())
2018-02-03 08:06:55 +00:00
else:
2019-11-12 00:26:25 +00:00
print("Found an existing token", file=sys.stderr)
return jsonify(success=True, created=False, record=existing_token.as_dict())
2018-02-03 08:06:55 +00:00
2019-11-12 00:26:25 +00:00
@app.route("/token", methods=["GET"])
2018-02-03 08:06:55 +00:00
def list_all_tokens():
"""Lists all tokens with an optional type filter"""
2019-11-12 00:26:25 +00:00
token_type = request.args.get("filter_type")
desc = request.args.get("desc", False)
print("Asked to filter by ", token_type, file=sys.stderr)
return EmailToken.jsonify_all(token_type=token_type, desc=desc)
2018-02-03 08:06:55 +00:00
2019-11-12 00:26:25 +00:00
@app.route("/token/<int:token_id>", methods=["GET"])
2018-02-03 08:06:55 +00:00
def get_token(token_id):
"""Gets a token by its primary key id"""
token = EmailToken.query.get(token_id)
return jsonify(token.as_dict())
2019-11-12 00:26:25 +00:00
if __name__ == "__main__":
2018-02-03 08:06:55 +00:00
db.create_all()
2019-11-12 00:26:25 +00:00
app.run(host="0.0.0.0", port=5000)