schema changes
This commit is contained in:
parent
9071f5ff7a
commit
536e49d1b9
5 changed files with 104 additions and 21 deletions
|
|
@ -6,4 +6,5 @@ source .env && \
|
||||||
case "$1" in
|
case "$1" in
|
||||||
("+") pw_migrate create --auto --auto-source=coriplus.models --directory=src/migrations --database="$DATABASE_URL" "${@:2}" ;;
|
("+") 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 migrate --directory=src/migrations --database="$DATABASE_URL" "${@:2}" ;;
|
||||||
|
(\\) pw_migrate rollback --directory=src/migrations --database="$DATABASE_URL" "${@:2}" ;;
|
||||||
esac
|
esac
|
||||||
|
|
@ -29,19 +29,25 @@ class BaseModel(Model):
|
||||||
# A user. The user is separated from its page.
|
# A user. The user is separated from its page.
|
||||||
class User(BaseModel):
|
class User(BaseModel):
|
||||||
# The unique username.
|
# The unique username.
|
||||||
username = CharField(unique=True)
|
username = CharField(30, unique=True)
|
||||||
# The user's full name (here for better search since 0.8)
|
# The user's full name (here for better search since 0.8)
|
||||||
full_name = TextField()
|
full_name = CharField(80)
|
||||||
# The password hash.
|
# The password hash.
|
||||||
password = CharField()
|
password = CharField(256)
|
||||||
# An email address.
|
# An email address.
|
||||||
email = CharField()
|
email = CharField(256)
|
||||||
# The date of birth (required because of Terms of Service)
|
# The date of birth (required because of Terms of Service)
|
||||||
birthday = DateField()
|
birthday = DateField()
|
||||||
# The date joined
|
# The date joined
|
||||||
join_date = DateTimeField()
|
join_date = DateTimeField()
|
||||||
# A disabled flag. 0 = active, 1 = disabled by user, 2 = banned
|
# A disabled flag. 0 = active, 1 = disabled by user, 2 = banned
|
||||||
is_disabled = IntegerField(default=0)
|
is_disabled = IntegerField(default=0)
|
||||||
|
# Short description of user.
|
||||||
|
biography = CharField(256, default='')
|
||||||
|
# Personal website.
|
||||||
|
website = TextField(null=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Helpers for flask_login
|
# Helpers for flask_login
|
||||||
def get_id(self):
|
def get_id(self):
|
||||||
|
|
@ -110,15 +116,12 @@ class UserAdminship(BaseModel):
|
||||||
# User profile.
|
# User profile.
|
||||||
# Additional info for identifying users.
|
# Additional info for identifying users.
|
||||||
# New in 0.6
|
# New in 0.6
|
||||||
|
# Deprecated in 0.10 and merged with User
|
||||||
class UserProfile(BaseModel):
|
class UserProfile(BaseModel):
|
||||||
user = ForeignKeyField(User, primary_key=True)
|
user = ForeignKeyField(User, primary_key=True)
|
||||||
biography = TextField(default='')
|
biography = TextField(default='')
|
||||||
location = IntegerField(null=True)
|
location = IntegerField(null=True)
|
||||||
year = IntegerField(null=True)
|
|
||||||
website = TextField(null=True)
|
website = TextField(null=True)
|
||||||
instagram = TextField(null=True)
|
|
||||||
facebook = TextField(null=True)
|
|
||||||
telegram = TextField(null=True)
|
|
||||||
@property
|
@property
|
||||||
def full_name(self):
|
def full_name(self):
|
||||||
'''
|
'''
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,11 @@
|
||||||
{% set profile = user.profile %}
|
|
||||||
<div class="card infobox">
|
<div class="card infobox">
|
||||||
<h3>{{ profile.full_name }}</h3>
|
<h3>{{ user.full_name }}</h3>
|
||||||
<p>{{ profile.biography|enrich }}</p>
|
<p>{{ user.biography|enrich }}</p>
|
||||||
{% if profile.location %}
|
{% if user.website %}
|
||||||
<p><span class="weak">Location:</span> {{ profile.location|locationdata }}</p>
|
{% set website = user.website %}
|
||||||
{% endif %}
|
|
||||||
{% if profile.website %}
|
|
||||||
{% set website = profile.website %}
|
|
||||||
{% set website = website if website.startswith(('http://', 'https://')) else 'http://' + 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 %}
|
{% endif %}
|
||||||
<p>
|
<p>
|
||||||
<strong>{{ user.messages|count }}</strong> messages
|
<strong>{{ user.messages|count }}</strong> messages
|
||||||
|
|
|
||||||
|
|
@ -137,11 +137,12 @@ def user_follow(username):
|
||||||
from_user=cur_user,
|
from_user=cur_user,
|
||||||
to_user=user,
|
to_user=user,
|
||||||
created_date=datetime.datetime.now())
|
created_date=datetime.datetime.now())
|
||||||
except IntegrityError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
flash('You are following %s' % user.username)
|
|
||||||
push_notification('follow', user, user=cur_user.id)
|
push_notification('follow', user, user=cur_user.id)
|
||||||
|
flash('You are now following %s' % user.username)
|
||||||
|
except IntegrityError:
|
||||||
|
flash(f'Error following {user.username}')
|
||||||
|
|
||||||
|
|
||||||
return redirect(url_for('website.user_detail', username=user.username))
|
return redirect(url_for('website.user_detail', username=user.username))
|
||||||
|
|
||||||
@bp.route('/+<username>/unfollow/', methods=['POST'])
|
@bp.route('/+<username>/unfollow/', methods=['POST'])
|
||||||
|
|
|
||||||
81
src/migrations/002_move_columns_from_userprofile.py
Normal file
81
src/migrations/002_move_columns_from_userprofile.py
Normal 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))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue