update db models, simplify things

main
lza_menace 3 years ago
parent 3dd606dcd0
commit f88636c958

@ -30,17 +30,13 @@ def upgrade():
sa.Column('register_date', sa.DateTime(), nullable=True), sa.Column('register_date', sa.DateTime(), nullable=True),
sa.Column('last_login_date', sa.DateTime(), nullable=True), sa.Column('last_login_date', sa.DateTime(), nullable=True),
sa.Column('verified', sa.Boolean(), 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('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', sa.String(length=180), nullable=True),
sa.Column('nonce_date', sa.DateTime(), 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.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('handle') sa.UniqueConstraint('id')
) )
op.create_table('memes', op.create_table('memes',
sa.Column('id', sa.String(length=80), nullable=False), sa.Column('id', sa.String(length=80), nullable=False),
@ -60,16 +56,9 @@ def upgrade():
sa.UniqueConstraint('meme_ipfs_hash'), sa.UniqueConstraint('meme_ipfs_hash'),
sa.UniqueConstraint('meta_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(): def downgrade():
op.drop_table('moderators')
op.drop_table('memes') op.drop_table('memes')
op.drop_table('users') op.drop_table('users')
op.drop_table('remotes') op.drop_table('remotes')

@ -1,7 +1,7 @@
import click import click
from flask import Blueprint from flask import Blueprint
from tubbymemes.models import Moderator, User from tubbymemes.models import User
from tubbymemes.factory import db from tubbymemes.factory import db
@ -13,42 +13,41 @@ def list():
""" """
List current server moderators. List current server moderators.
""" """
for mod in Moderator.query.all(): for mod in User.query.filter(User.moderator == True):
click.echo(mod.user.handle) click.echo(f'{mod.user.id} - {mod.user.public_address}')
@bp.cli.command('add') @bp.cli.command('add')
@click.argument('moderator_handle') @click.argument('address')
def add(moderator_handle): 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: if user:
mod = Moderator.query.filter(Moderator.user_id == user.id).first() if not user.moderator:
if mod is None: user.moderator = True
m = Moderator(user_id=user.id)
db.session.add(m)
db.session.commit() db.session.commit()
click.echo(f'[+] Added moderator status to `{moderator_handle}`') click.echo(f'[+] Added moderator status to {address}')
else: else:
click.echo('[.] That is not a valid user.') click.echo('[.] User is already a moderator')
else:
click.echo('[!] No user with that address.')
@bp.cli.command('remove') @bp.cli.command('remove')
@click.argument('moderator_handle') @click.argument('address')
def remove(moderator_handle): 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: if user:
mod = Moderator.query.filter(Moderator.user_id == user.id).first() if user.moderator:
if mod: user.moderator = False
db.session.delete(mod)
db.session.commit() db.session.commit()
click.echo(f'[-] Removed moderator status from `{moderator_handle}`') # noqa click.echo(f'[-] Removed moderator status from {address}')
else: else:
click.echo('[.] That user is not a moderator.') click.echo('[.] That user is not a moderator.')
else: else:
click.echo('[.] That is not a valid user.') click.echo('[!] No user with that address.')

@ -12,18 +12,6 @@ from tubbymemes import config
def rand_id(): def rand_id():
return uuid4().hex 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): class User(db.Model):
__tablename__ = 'users' __tablename__ = 'users'
@ -31,16 +19,11 @@ class User(db.Model):
register_date = db.Column(db.DateTime, default=datetime.utcnow) register_date = db.Column(db.DateTime, default=datetime.utcnow)
last_login_date = db.Column(db.DateTime, nullable=True) last_login_date = db.Column(db.DateTime, nullable=True)
verified = db.Column(db.Boolean, default=False) verified = db.Column(db.Boolean, default=False)
moderator = db.Column(db.Boolean, default=False)
public_address = db.Column(db.String(180)) public_address = db.Column(db.String(180))
nonce = db.Column(db.String(180), default=rand_id()) nonce = db.Column(db.String(180), default=rand_id())
nonce_date = db.Column(db.DateTime, default=datetime.utcnow) nonce_date = db.Column(db.DateTime, default=datetime.utcnow)
handle = db.Column(db.String(40), unique=True) ens_address = db.Column(db.String(80), 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')
memes = db.relationship('Meme', back_populates='user') memes = db.relationship('Meme', back_populates='user')
def as_dict(self): def as_dict(self):
@ -48,7 +31,7 @@ class User(db.Model):
for c in inspect(self).mapper.column_attrs if c.key != 'nonce'} for c in inspect(self).mapper.column_attrs if c.key != 'nonce'}
def __repr__(self): def __repr__(self):
return str(self.handle) return str(self.id)
@property @property
def is_authenticated(self): def is_authenticated(self):
@ -62,25 +45,12 @@ class User(db.Model):
def is_anonymous(self): def is_anonymous(self):
return False return False
@property
def is_admin(self):
return self.admin
def is_moderator(self): def is_moderator(self):
return len(self.moderator) > 0 return self.moderator
def get_id(self): def get_id(self):
return self.id 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): def generate_nonce(self):
return rand_id() return rand_id()

@ -27,15 +27,12 @@ def memes():
@bp.route('/user_exists') @bp.route('/user_exists')
def 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. This logic will help the login/connect MetaMask flow.
""" """
if 'public_address' in request.args: if 'public_address' in request.args:
query_str = 'public_address' query_str = 'public_address'
query_field = User.public_address query_field = User.public_address
elif 'handle' in request.args:
query_str = 'handle'
query_field = User.handle
else: else:
return jsonify({'success': False}) 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']) @bp.route('/authenticate/metamask', methods=['POST'])
def authenticate_metamask(): def authenticate_metamask():
""" """
@ -104,8 +59,8 @@ def authenticate_metamask():
(`personal_sign` method). (`personal_sign` method).
This route will verify the signed data against the user's public ETH 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 address. If no user exists, they get an entry in the database.
default handle assigned. If user does exist, they get logged in. If user does exist, they get logged in.
""" """
data = request.get_json() data = request.get_json()
if current_user.is_authenticated: if current_user.is_authenticated:
@ -143,8 +98,6 @@ def authenticate_metamask():
) )
db.session.add(user) db.session.add(user)
db.session.commit() db.session.commit()
user.handle = f'anon{user.id}-{rand_str}'
db.session.commit()
user.login() user.login()
return jsonify({ return jsonify({
'success': True, 'success': True,

Loading…
Cancel
Save