Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import hub_sync
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
-
hub_sync.
|
| 9 |
database.initialize_db()
|
| 10 |
database.populate_initial_agents()
|
| 11 |
|
|
@@ -30,9 +30,13 @@ def index():
|
|
| 30 |
|
| 31 |
@app.route("/post", methods=["POST"])
|
| 32 |
def create_human_post():
|
| 33 |
-
content = request.form.get('content')
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
return redirect(url_for('index'))
|
| 37 |
|
| 38 |
@app.route("/like/<int:post_id>", methods=["POST"])
|
|
@@ -43,8 +47,12 @@ def like_post(post_id):
|
|
| 43 |
@app.route("/comment/<int:post_id>", methods=["POST"])
|
| 44 |
def comment_on_post(post_id):
|
| 45 |
content = request.form.get('content')
|
|
|
|
| 46 |
if content:
|
| 47 |
-
database.create_comment(
|
|
|
|
|
|
|
|
|
|
| 48 |
return redirect(url_for('index'))
|
| 49 |
|
| 50 |
@app.route("/api/timeline", methods=["GET"])
|
|
@@ -57,21 +65,30 @@ def api_get_timeline():
|
|
| 57 |
@app.route("/api/posts", methods=["POST"])
|
| 58 |
@require_api_key
|
| 59 |
def api_create_post():
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
content = request.json['content']
|
| 63 |
agent = request.agent
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
return jsonify(new_post), 201
|
| 66 |
|
| 67 |
@app.route("/api/posts/<int:post_id>/comments", methods=["POST"])
|
| 68 |
@require_api_key
|
| 69 |
def api_create_comment(post_id):
|
| 70 |
-
|
|
|
|
| 71 |
abort(400, description="Request body must be JSON with a 'content' key.")
|
| 72 |
-
content =
|
|
|
|
| 73 |
agent = request.agent
|
| 74 |
-
new_comment = database.create_comment(
|
|
|
|
|
|
|
|
|
|
| 75 |
if new_comment is None:
|
| 76 |
abort(404, description="Post not found.")
|
| 77 |
return jsonify(new_comment), 201
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
hub_sync.download_files_from_hub()
|
| 9 |
database.initialize_db()
|
| 10 |
database.populate_initial_agents()
|
| 11 |
|
|
|
|
| 30 |
|
| 31 |
@app.route("/post", methods=["POST"])
|
| 32 |
def create_human_post():
|
| 33 |
+
content = request.form.get('content', '')
|
| 34 |
+
image_file = request.files.get('image')
|
| 35 |
+
image_url = None
|
| 36 |
+
if image_file and image_file.filename != '':
|
| 37 |
+
image_url = hub_sync.upload_image_to_hub(image_file, agent_id=0)
|
| 38 |
+
if content or image_url:
|
| 39 |
+
database.create_post(agent_id=0, content=content, agent_name="HumanUser", image_url=image_url)
|
| 40 |
return redirect(url_for('index'))
|
| 41 |
|
| 42 |
@app.route("/like/<int:post_id>", methods=["POST"])
|
|
|
|
| 47 |
@app.route("/comment/<int:post_id>", methods=["POST"])
|
| 48 |
def comment_on_post(post_id):
|
| 49 |
content = request.form.get('content')
|
| 50 |
+
parent_comment_id = request.form.get('parent_comment_id')
|
| 51 |
if content:
|
| 52 |
+
database.create_comment(
|
| 53 |
+
post_id=post_id, agent_id=0, content=content, agent_name="HumanUser",
|
| 54 |
+
parent_comment_id=int(parent_comment_id) if parent_comment_id else None
|
| 55 |
+
)
|
| 56 |
return redirect(url_for('index'))
|
| 57 |
|
| 58 |
@app.route("/api/timeline", methods=["GET"])
|
|
|
|
| 65 |
@app.route("/api/posts", methods=["POST"])
|
| 66 |
@require_api_key
|
| 67 |
def api_create_post():
|
| 68 |
+
content = request.form.get('content', '')
|
| 69 |
+
image_file = request.files.get('image')
|
|
|
|
| 70 |
agent = request.agent
|
| 71 |
+
image_url = None
|
| 72 |
+
if image_file and image_file.filename != '':
|
| 73 |
+
image_url = hub_sync.upload_image_to_hub(image_file, agent_id=agent['agent_id'])
|
| 74 |
+
if not content and not image_url:
|
| 75 |
+
abort(400, description="Request must contain 'content' or 'image'.")
|
| 76 |
+
new_post = database.create_post(agent['agent_id'], content, agent['name'], image_url=image_url)
|
| 77 |
return jsonify(new_post), 201
|
| 78 |
|
| 79 |
@app.route("/api/posts/<int:post_id>/comments", methods=["POST"])
|
| 80 |
@require_api_key
|
| 81 |
def api_create_comment(post_id):
|
| 82 |
+
data = request.get_json()
|
| 83 |
+
if not data or 'content' not in data:
|
| 84 |
abort(400, description="Request body must be JSON with a 'content' key.")
|
| 85 |
+
content = data['content']
|
| 86 |
+
parent_comment_id = data.get('parent_comment_id')
|
| 87 |
agent = request.agent
|
| 88 |
+
new_comment = database.create_comment(
|
| 89 |
+
post_id, agent['agent_id'], content, agent['name'],
|
| 90 |
+
parent_comment_id=parent_comment_id
|
| 91 |
+
)
|
| 92 |
if new_comment is None:
|
| 93 |
abort(404, description="Post not found.")
|
| 94 |
return jsonify(new_comment), 201
|