schema changes

This commit is contained in:
Yusur 2025-11-12 11:02:53 +01:00
parent 9071f5ff7a
commit 536e49d1b9
5 changed files with 104 additions and 21 deletions

View file

@ -6,4 +6,5 @@ source .env && \
case "$1" in
("+") pw_migrate create --auto --auto-source=coriplus.models --directory=src/migrations --database="$DATABASE_URL" "${@:2}" ;;
("@") pw_migrate migrate --directory=src/migrations --database="$DATABASE_URL" "${@:2}" ;;
(\\) pw_migrate rollback --directory=src/migrations --database="$DATABASE_URL" "${@:2}" ;;
esac

View file

@ -29,19 +29,25 @@ class BaseModel(Model):
# A user. The user is separated from its page.
class User(BaseModel):
# The unique username.
username = CharField(unique=True)
username = CharField(30, unique=True)
# The user's full name (here for better search since 0.8)
full_name = TextField()
full_name = CharField(80)
# The password hash.
password = CharField()
password = CharField(256)
# An email address.
email = CharField()
email = CharField(256)
# The date of birth (required because of Terms of Service)
birthday = DateField()
# The date joined
join_date = DateTimeField()
# A disabled flag. 0 = active, 1 = disabled by user, 2 = banned
is_disabled = IntegerField(default=0)
# Short description of user.
biography = CharField(256, default='')
# Personal website.
website = TextField(null=True)
# Helpers for flask_login
def get_id(self):
@ -110,15 +116,12 @@ class UserAdminship(BaseModel):
# User profile.
# Additional info for identifying users.
# New in 0.6
# Deprecated in 0.10 and merged with User
class UserProfile(BaseModel):
user = ForeignKeyField(User, primary_key=True)
biography = TextField(default='')
location = IntegerField(null=True)
year = IntegerField(null=True)
website = TextField(null=True)
instagram = TextField(null=True)
facebook = TextField(null=True)
telegram = TextField(null=True)
@property
def full_name(self):
'''

View file

@ -1,14 +1,11 @@
{% set profile = user.profile %}
<div class="card infobox">
<h3>{{ profile.full_name }}</h3>
<p>{{ profile.biography|enrich }}</p>
{% if profile.location %}
<p><span class="weak">Location:</span> {{ profile.location|locationdata }}</p>
{% endif %}
{% if profile.website %}
{% set website = profile.website %}
<h3>{{ user.full_name }}</h3>
<p>{{ user.biography|enrich }}</p>
{% if user.website %}
{% set website = user.website %}
{% set website = website if website.startswith(('http://', 'https://')) else 'http://' + website %}
<p><span class="weak">Website:</span> {{ profile.website|urlize }}</p>
<p><span class="weak">Website:</span> {{ website|urlize }}</p>
{% endif %}
<p>
<strong>{{ user.messages|count }}</strong> messages

View file

@ -137,11 +137,12 @@ def user_follow(username):
from_user=cur_user,
to_user=user,
created_date=datetime.datetime.now())
push_notification('follow', user, user=cur_user.id)
flash('You are now following %s' % user.username)
except IntegrityError:
pass
flash(f'Error following {user.username}')
flash('You are following %s' % user.username)
push_notification('follow', user, user=cur_user.id)
return redirect(url_for('website.user_detail', username=user.username))
@bp.route('/+<username>/unfollow/', methods=['POST'])

View file

@ -0,0 +1,81 @@
"""Peewee migrations -- 002_move_columns_from_userprofile.py.
Some examples (model - class or model name)::
> Model = migrator.orm['table_name'] # Return model in current state by name
> Model = migrator.ModelClass # Return model in current state by name
> migrator.sql(sql) # Run custom SQL
> migrator.run(func, *args, **kwargs) # Run python function with the given args
> migrator.create_model(Model) # Create a model (could be used as decorator)
> migrator.remove_model(model, cascade=True) # Remove a model
> migrator.add_fields(model, **fields) # Add fields to a model
> migrator.change_fields(model, **fields) # Change fields
> migrator.remove_fields(model, *field_names, cascade=True)
> migrator.rename_field(model, old_field_name, new_field_name)
> migrator.rename_table(model, new_table_name)
> migrator.add_index(model, *col_names, unique=False)
> migrator.add_not_null(model, *field_names)
> migrator.add_default(model, field_name, default)
> migrator.add_constraint(model, name, sql)
> migrator.drop_index(model, *col_names)
> migrator.drop_not_null(model, *field_names)
> migrator.drop_constraints(model, *constraints)
"""
from contextlib import suppress
import peewee as pw
from peewee_migrate import Migrator
with suppress(ImportError):
import playhouse.postgres_ext as pw_pext
def migrate(migrator: Migrator, database: pw.Database, *, fake=False):
"""Write your migrations here."""
migrator.add_fields(
'user',
biography=pw.CharField(max_length=256, default=""),
website=pw.TextField(null=True))
migrator.change_fields('user', username=pw.CharField(max_length=30, unique=True))
migrator.change_fields('user', full_name=pw.CharField(max_length=80))
migrator.change_fields('user', password=pw.CharField(max_length=256))
migrator.change_fields('user', email=pw.CharField(max_length=256))
migrator.sql("""
UPDATE "user" SET biography = (SELECT p.biography FROM userprofile p WHERE p.user_id = id LIMIT 1),
website = (SELECT p.website FROM userprofile p WHERE p.user_id = id LIMIT 1);
""")
migrator.remove_fields('userprofile', 'year', 'instagram', 'facebook', 'telegram')
def rollback(migrator: Migrator, database: pw.Database, *, fake=False):
"""Write your rollback migrations here."""
migrator.add_fields(
'userprofile',
year=pw.IntegerField(null=True),
instagram=pw.TextField(null=True),
facebook=pw.TextField(null=True),
telegram=pw.TextField(null=True))
migrator.remove_fields('user', 'biography', 'website')
migrator.change_fields('user', username=pw.CharField(max_length=255, unique=True))
migrator.change_fields('user', full_name=pw.TextField())
migrator.change_fields('user', password=pw.CharField(max_length=255))
migrator.change_fields('user', email=pw.CharField(max_length=255))