From f88636c958a5e89658561b0bb4de5055e88f60ed Mon Sep 17 00:00:00 2001 From: lza_menace Date: Thu, 10 Mar 2022 20:46:02 -0800 Subject: [PATCH] update db models, simplify things --- .../versions/53c117fcef3f_init_tubbymemes.py | 17 ++---- tubbymemes/cli/mod.py | 43 ++++++++------- tubbymemes/models.py | 38 ++----------- tubbymemes/routes/api.py | 53 ++----------------- 4 files changed, 31 insertions(+), 120 deletions(-) diff --git a/alembic/versions/53c117fcef3f_init_tubbymemes.py b/alembic/versions/53c117fcef3f_init_tubbymemes.py index b275a82..b46d186 100644 --- a/alembic/versions/53c117fcef3f_init_tubbymemes.py +++ b/alembic/versions/53c117fcef3f_init_tubbymemes.py @@ -30,17 +30,13 @@ def upgrade(): sa.Column('register_date', sa.DateTime(), nullable=True), sa.Column('last_login_date', sa.DateTime(), nullable=True), sa.Column('verified', sa.Boolean(), nullable=True), + sa.Column('moderator', sa.Boolean(), nullable=True), sa.Column('public_address', sa.String(length=180), nullable=True), + sa.Column('ens_address', sa.String(length=80), nullable=True), sa.Column('nonce', sa.String(length=180), nullable=True), sa.Column('nonce_date', sa.DateTime(), nullable=True), - sa.Column('handle', sa.String(length=40), nullable=True), - sa.Column('bio', sa.String(length=600), nullable=True), - sa.Column('profile_image', sa.String(length=300), nullable=True), - sa.Column('ipfs_hash', sa.String(length=100), nullable=True), - sa.Column('wownero_address', sa.String(length=120), nullable=True), - sa.Column('website_url', sa.String(length=120), nullable=True), sa.PrimaryKeyConstraint('id'), - sa.UniqueConstraint('handle') + sa.UniqueConstraint('id') ) op.create_table('memes', sa.Column('id', sa.String(length=80), nullable=False), @@ -60,16 +56,9 @@ def upgrade(): sa.UniqueConstraint('meme_ipfs_hash'), sa.UniqueConstraint('meta_ipfs_hash') ) - op.create_table('moderators', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('user_id', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), - sa.PrimaryKeyConstraint('id') - ) def downgrade(): - op.drop_table('moderators') op.drop_table('memes') op.drop_table('users') op.drop_table('remotes') diff --git a/tubbymemes/cli/mod.py b/tubbymemes/cli/mod.py index d4ed2ea..b1602cd 100644 --- a/tubbymemes/cli/mod.py +++ b/tubbymemes/cli/mod.py @@ -1,7 +1,7 @@ import click from flask import Blueprint -from tubbymemes.models import Moderator, User +from tubbymemes.models import User from tubbymemes.factory import db @@ -13,42 +13,41 @@ def list(): """ List current server moderators. """ - for mod in Moderator.query.all(): - click.echo(mod.user.handle) + for mod in User.query.filter(User.moderator == True): + click.echo(f'{mod.user.id} - {mod.user.public_address}') @bp.cli.command('add') -@click.argument('moderator_handle') -def add(moderator_handle): +@click.argument('address') +def add(address): """ - Add server moderators by user handle. + Add server moderators by address. """ - user = User.query.filter(User.handle == moderator_handle).first() + user = User.query.filter(User.public_address == address).first() if user: - mod = Moderator.query.filter(Moderator.user_id == user.id).first() - if mod is None: - m = Moderator(user_id=user.id) - db.session.add(m) + if not user.moderator: + user.moderator = True db.session.commit() - click.echo(f'[+] Added moderator status to `{moderator_handle}`') + click.echo(f'[+] Added moderator status to {address}') + else: + click.echo('[.] User is already a moderator') else: - click.echo('[.] That is not a valid user.') + click.echo('[!] No user with that address.') @bp.cli.command('remove') -@click.argument('moderator_handle') -def remove(moderator_handle): +@click.argument('address') +def remove(address): """ - Remove server moderator by user handle. + Remove server moderator by address. """ - user = User.query.filter(User.handle == moderator_handle).first() + user = User.query.filter(User.public_address == address).first() if user: - mod = Moderator.query.filter(Moderator.user_id == user.id).first() - if mod: - db.session.delete(mod) + if user.moderator: + user.moderator = False db.session.commit() - click.echo(f'[-] Removed moderator status from `{moderator_handle}`') # noqa + click.echo(f'[-] Removed moderator status from {address}') else: click.echo('[.] That user is not a moderator.') else: - click.echo('[.] That is not a valid user.') + click.echo('[!] No user with that address.') diff --git a/tubbymemes/models.py b/tubbymemes/models.py index 283c675..e53c2e3 100644 --- a/tubbymemes/models.py +++ b/tubbymemes/models.py @@ -12,18 +12,6 @@ from tubbymemes import config def rand_id(): return uuid4().hex - -class Moderator(db.Model): - __tablename__ = 'moderators' - - id = db.Column(db.Integer, primary_key=True) - user_id = db.Column(db.Integer, db.ForeignKey('users.id')) - user = db.relationship('User', back_populates='moderator') - - def __rep__(self): - return self.user.handle - - class User(db.Model): __tablename__ = 'users' @@ -31,16 +19,11 @@ class User(db.Model): register_date = db.Column(db.DateTime, default=datetime.utcnow) last_login_date = db.Column(db.DateTime, nullable=True) verified = db.Column(db.Boolean, default=False) + moderator = db.Column(db.Boolean, default=False) public_address = db.Column(db.String(180)) nonce = db.Column(db.String(180), default=rand_id()) nonce_date = db.Column(db.DateTime, default=datetime.utcnow) - handle = db.Column(db.String(40), unique=True) - bio = db.Column(db.String(600), nullable=True) - profile_image = db.Column(db.String(300), nullable=True) - ipfs_hash = db.Column(db.String(100), nullable=True) - wownero_address = db.Column(db.String(120), nullable=True) - website_url = db.Column(db.String(120), nullable=True) - moderator = db.relationship('Moderator', back_populates='user') + ens_address = db.Column(db.String(80), unique=True) memes = db.relationship('Meme', back_populates='user') def as_dict(self): @@ -48,7 +31,7 @@ class User(db.Model): for c in inspect(self).mapper.column_attrs if c.key != 'nonce'} def __repr__(self): - return str(self.handle) + return str(self.id) @property def is_authenticated(self): @@ -62,25 +45,12 @@ class User(db.Model): def is_anonymous(self): return False - @property - def is_admin(self): - return self.admin - def is_moderator(self): - return len(self.moderator) > 0 + return self.moderator def get_id(self): return self.id - def get_profile_image(self, full=True): - if self.profile_image: - if full: - return url_for('meta.uploaded_file', filename=self.profile_image) # noqa - else: - return self.profile_image - else: - return '/static/img/logo.png' - def generate_nonce(self): return rand_id() diff --git a/tubbymemes/routes/api.py b/tubbymemes/routes/api.py index 1dbf46d..75e1fca 100644 --- a/tubbymemes/routes/api.py +++ b/tubbymemes/routes/api.py @@ -27,15 +27,12 @@ def memes(): @bp.route('/user_exists') def user_exists(): """ - Check to see if a given user exists (handle or wallet address). + Check to see if a given user exists (wallet address). This logic will help the login/connect MetaMask flow. """ if 'public_address' in request.args: query_str = 'public_address' query_field = User.public_address - elif 'handle' in request.args: - query_str = 'handle' - query_field = User.handle else: return jsonify({'success': False}) @@ -54,48 +51,6 @@ def user_exists(): }) -@bp.route('/update/user', methods=['POST']) -def update_user(): - if not current_user.is_authenticated: - return jsonify({ - 'success': False, - 'message': 'Must be authenticated in order to update.' - }) - - data = request.get_json() - _u = User.query.get(int(data['user_id'])) - - if _u: - if current_user.id == _u.id: - user_exists = User.query.filter( - User.handle == data['handle'] - ).first() - if user_exists and not current_user.id == user_exists.id: - return jsonify({ - 'success': False, - 'message': 'That user handle already exists on this server' - }) - _u.wownero_address = data['wownero_address'] - _u.ipfs_hash = data['ipfs_hash'] - _u.handle = data['handle'] - db.session.commit() - return jsonify({ - 'success': True, - 'message': 'Updated user record.' - }) - else: - return jsonify({ - 'success': False, - 'message': 'Cannot edit another record.' - }) - - else: - return jsonify({ - 'success': False, - 'message': 'User does not exist.' - }) - - @bp.route('/authenticate/metamask', methods=['POST']) def authenticate_metamask(): """ @@ -104,8 +59,8 @@ def authenticate_metamask(): (`personal_sign` method). This route will verify the signed data against the user's public ETH - address. If no user exists, they get an entry in the database with a - default handle assigned. If user does exist, they get logged in. + address. If no user exists, they get an entry in the database. + If user does exist, they get logged in. """ data = request.get_json() if current_user.is_authenticated: @@ -143,8 +98,6 @@ def authenticate_metamask(): ) db.session.add(user) db.session.commit() - user.handle = f'anon{user.id}-{rand_str}' - db.session.commit() user.login() return jsonify({ 'success': True,