From 80d74af7a3bd995bcf5e1d6d9018ef495d68f681 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Sun, 2 Jan 2022 23:50:34 -0800 Subject: [PATCH] setting up ez export api --- suchwowx/models.py | 5 +++++ suchwowx/routes/api.py | 14 +++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/suchwowx/models.py b/suchwowx/models.py index 53c3ff7..8eefec7 100644 --- a/suchwowx/models.py +++ b/suchwowx/models.py @@ -3,6 +3,7 @@ from datetime import datetime from flask import url_for from flask_login import login_user +from sqlalchemy import inspect from suchwowx.factory import db from suchwowx import config @@ -107,6 +108,10 @@ class Meme(db.Model): user_id = db.Column(db.Integer, db.ForeignKey('users.id')) user = db.relationship('User', back_populates='memes') + def as_dict(self): + return {c.key: getattr(self, c.key) + for c in inspect(self).mapper.column_attrs} + def __repr__(self): return str(f'meme-{self.id}') diff --git a/suchwowx/routes/api.py b/suchwowx/routes/api.py index cbf65cb..93099fb 100644 --- a/suchwowx/routes/api.py +++ b/suchwowx/routes/api.py @@ -5,12 +5,24 @@ from flask_login import current_user from suchwowx.factory import db from suchwowx.helpers import verify_signature -from suchwowx.models import User +from suchwowx.models import User, Meme bp = Blueprint('api', 'api', url_prefix='/api/v1') +@bp.route('/memes') +def memes(): + """ + List all the approved memes on the server to allow remote syncing. + """ + all_memes = dict() + memes = Meme.query.filter(Meme.approved == True).all() + for meme in memes: + all_memes[meme.id] = meme.as_dict() + return jsonify(all_memes) + + @bp.route('/user_exists') def user_exists(): """