mirror of
https://github.com/jwinterm/tubbymemes.git
synced 2026-10-17 05:21:28 +02:00
update db models, simplify things
This commit is contained in:
@@ -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')
|
||||
|
||||
+21
-22
@@ -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.')
|
||||
|
||||
+4
-34
@@ -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()
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user