From 6170af215a883bd05845dd7c27bcefe8f7b317f0 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Fri, 31 Dec 2021 11:41:00 -0800 Subject: [PATCH] get avax chain sync working --- .../f6f695a38339_add_meme_sync_flag.py | 27 +++++++++++++++++++ suchwowx/cli/cli.py | 16 +++++++++-- suchwowx/models.py | 1 + suchwowx/routes/meme.py | 16 ++++++----- 4 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 alembic/versions/f6f695a38339_add_meme_sync_flag.py diff --git a/alembic/versions/f6f695a38339_add_meme_sync_flag.py b/alembic/versions/f6f695a38339_add_meme_sync_flag.py new file mode 100644 index 0000000..8edf8a6 --- /dev/null +++ b/alembic/versions/f6f695a38339_add_meme_sync_flag.py @@ -0,0 +1,27 @@ +"""add meme sync flag + +Revision ID: f6f695a38339 +Revises: f15cd9fa0f06 +Create Date: 2021-12-31 11:05:28.153450 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'f6f695a38339' +down_revision = 'f15cd9fa0f06' +branch_labels = None +depends_on = None + + +def upgrade(): + with op.batch_alter_table('memes', schema=None) as batch_op: + batch_op.add_column(sa.Column('synced', sa.Boolean(), nullable=True)) + + + +def downgrade(): + with op.batch_alter_table('memes', schema=None) as batch_op: + batch_op.drop_column('synced') diff --git a/suchwowx/cli/cli.py b/suchwowx/cli/cli.py index bb6c005..2ca52d1 100644 --- a/suchwowx/cli/cli.py +++ b/suchwowx/cli/cli.py @@ -47,13 +47,25 @@ def sync_avax(): db.session.commit() print(f'[+] Created user {user_exists.handle}') res = requests.get(f'{config.IPFS_SERVER}/ipfs/{deets[5]}', timeout=30).json() + if not 'image' in res: + print('No image IPFS hash, skipping') + continue + meme_ipfs_hash = res['image'].split('ipfs://')[1] + filename = token_urlsafe(24) + print(f'[+] Downloading image hash {meme_ipfs_hash} as {filename}') + r = requests.get(f'{config.IPFS_SERVER}/ipfs/{meme_ipfs_hash}', stream=True) + with open(f'{config.DATA_FOLDER}/uploads/{filename}', 'wb') as f: + for chunk in r.iter_content(chunk_size = 16*1024): + f.write(chunk) meme = Meme( title=res['name'], + file_name=filename, description=res['description'], user_id=user_exists.id, meta_ipfs_hash=deets[5], - meme_ipfs_hash=res['image'].split('ipfs://')[1], - minted=True + meme_ipfs_hash=meme_ipfs_hash, + minted=True, + synced=True ) db.session.add(meme) db.session.commit() diff --git a/suchwowx/models.py b/suchwowx/models.py index e59e746..53c3ff7 100644 --- a/suchwowx/models.py +++ b/suchwowx/models.py @@ -103,6 +103,7 @@ class Meme(db.Model): description = db.Column(db.String(400), nullable=True) minted = db.Column(db.Boolean, default=False) approved = db.Column(db.Boolean, default=False) + synced = db.Column(db.Boolean, default=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) user = db.relationship('User', back_populates='memes') diff --git a/suchwowx/routes/meme.py b/suchwowx/routes/meme.py index 3d0fcaf..7bceaee 100644 --- a/suchwowx/routes/meme.py +++ b/suchwowx/routes/meme.py @@ -26,8 +26,11 @@ def index(): @bp.route('/mod') def mod(): + if not current_user.is_authenticated: + flash('You must be logged in and have MetaMask wallet connected.', 'warning') + return redirect(url_for('meme.index')) if not current_user.is_moderator(): - flash('You are not a moderator', 'warning') + flash('You are not a moderator.', 'warning') return redirect(url_for('meme.index')) memes = Meme.query.filter( Meme.approved != True @@ -133,11 +136,12 @@ def approve(meme_id, action): existing_meme_ipfs = Meme.query.filter( Meme.meme_ipfs_hash == res[1] ).first() - if existing_meta_ipfs or existing_meme_ipfs: - flash('Cannot use an existing IPFS hash for either metadata or memes.', 'warning') # noqa - return redirect(url_for('meme.show', meme_id=meme.id)) - meme.meta_ipfs_hash = res[0] - meme.meme_ipfs_hash = res[1] + if meme.synced is False: + if existing_meta_ipfs or existing_meme_ipfs: + flash('Cannot use an existing IPFS hash for either metadata or memes on new posts.', 'warning') # noqa + return redirect(url_for('meme.show', meme_id=meme.id)) + meme.meta_ipfs_hash = res[0] + meme.meme_ipfs_hash = res[1] meme.approved = True db.session.commit() flash('Approved meme and published new meme to local IPFS server.', 'success')