Find a file
TLSM 9e2ceb28b0 Fix four routes with auth_desired misimplemented.
PR #374 removed `@auth_required` from a number of routes and changed
those which used their `v` parameters to `v=None` and removed the `v`
parameter from those which didn't internally use it. 1841134b47

PR #392 re-added `@auth_desired` to those routes to ensure the
templates rendered with awareness of the current logged-in user
(matters for search, header bar, etc). 9f042c1aeb

However, 500 errors occurred on /random_post, /random_user, /id/<uid>,
and /u/<username>. Those were the four which had their `v` parameter
removed entirely. This has been re-added, which fixes the bug.

The way to understand auth_required vs auth_desired is that they are
nearly identical, with the sole difference than auth_required
checks if v is None and aborts with 401 if so. This means that
auth_desired routes must handle the v=None case. They are the same in
that they always try to give a `v` kwarg to the decorated function,
which was the root cause of those four routes erroring.

Recommended style: the vast majority of routes which return a rendered
template should be auth_desired, because the top-level templates often
draw extensively from `v` state even when the route handler does not.
When a route is either auth_desired or auth_required, it should have a
`v` parameter, which we typically give as the first positional
parameter.
2022-11-06 03:11:59 -06:00
.github Simplify migrations and redesign utility scripts. 2022-06-29 00:18:14 -05:00
files Fix four routes with auth_desired misimplemented. 2022-11-06 03:11:59 -06:00
migrations Remove games. 2022-09-05 23:44:18 -05:00
util Fix: is no longer needed thanks to WORKDIR 2022-10-27 21:50:50 -05:00
.dockerignore Add a .dockerignore to make builds faster and smaller. 2022-05-14 02:12:55 -05:00
.editorconfig Add .editorconfig. 2022-05-08 23:14:58 -05:00
.gitattributes Convert text file line endings to LF. 2022-05-09 07:20:47 -05:00
.gitignore Fix some deprecations and factor out a helper function (#387) 2022-10-27 22:15:48 -05:00
dependabot.yml Add trailing LF to text files 2022-05-10 21:05:51 -04:00
docker-compose.yml Initial e2e testing 2022-05-13 02:55:29 -05:00
Dockerfile Use Python 3.11 with Poetry 2022-11-03 09:09:29 -05:00
env Hide votes for posts and comments under 24 hours old 2022-10-27 21:50:08 -05:00
LICENSE Convert text file line endings to LF. 2022-05-09 07:20:47 -05:00
pg_hba.conf Add trailing LF to text files 2022-05-10 21:05:51 -04:00
poetry.lock Use Python 3.11 with Poetry 2022-11-03 09:09:29 -05:00
pyproject.toml Use Python 3.11 with Poetry 2022-11-03 09:09:29 -05:00
readme.md Simplify migrations and redesign utility scripts. 2022-06-29 00:18:14 -05:00
redis.conf Convert text file line endings to LF. 2022-05-09 07:20:47 -05:00
schema.sql Try restoring schema.sql 2022-05-22 21:29:10 -04:00
seed-db.sql Fixed a merge conflict that I somehow missed and removed marsey images from seed db 2022-05-22 08:27:13 -05:00
site_settings.json Add new settings for comment filtering 2022-07-09 06:33:41 -05:00
supervisord.conf Add migrations using alembic. 2022-05-17 18:55:17 -05:00

Build status

This code runs https://www.themotte.org .

Installation (Windows/Linux/MacOS)

1 - Install Docker on your machine.

Docker installation

2 - If hosting on localhost and/or without HTTPS, change"SESSION_COOKIE_SECURE" in __main__.py to "False"

3 - Run the following commands in the terminal:

git clone https://github.com/themotte/rDrama/

cd rDrama

docker-compose up

4 - That's it! Visit localhost in your browser.

5 - Optional: to change the domain from "localhost" to something else and configure the site settings, as well as integrate it with the external services the website uses, please edit the variables in the env file and then restart the docker container.

Run the E2E tests:

./util/test.py

Database Stuff

What is a migration?

Prior to the fork of themotte from rDrama, there were no database migrations, and the database schema was stored in schema.sql. Any time a column or such was added to a model, one hoped that the author remembered to update schema.sql to add that column. One of the first changes we made after forking this repo was to add database migrations using Alembic.

Database migrations are instructions for how to convert an out-of-date database into an up-to-date database. This can involve changing the schema, or changing data within the database.

Why use database migrations

Database migrations allow us to specify where data moves when there are schema changes. This is important when we're live -- if we rename the comments.ban\_reason column to comments.reason\_banned for naming consistency or whatever, and we do this by dropping the ban\_reason column and adding a reason\_banned column, we will lose all user data in that column. We don't want to do this. With migrations, we could instead specify that the operation in question should be a column rename, or, if the database engine does not support renaming columns, that we should do a three-step process of "add new column, migrate data over, drop old column".

Database schema change workflow

As an example, let's say we want to add a column is\_flagged to the comments table.

  1. Update the Comment model in files/classes/comment.py
	from files.__main__ import Base
	class Comment(Base):
		__tablename__ = "comments"
		id = Column(Integer, primary_key=True)
		...
+		is_flagged = Column(Boolean, default=False, nullable=False)
  1. Autogenerate a migration with a descriptive message. To do this, run
flask db revision --autogenerate --message="add is_flagged field to comments"

from the flask server within the directory the flask app is being served from, with an env var of FLASK\_APP="files/cli:app". If you are running flask using docker-compose as described above, this looks like

docker-compose exec -T files bash -c 'cd /service/; FLASK_APP="files/cli:app" flask "$@"' . db revision --autogenerate --message="add is_flagged field to comments"

This will create a migration in the migrations/versions directory with a name like migrations/versions/2022\_05\_23\_05\_38\_40\_9c27db0b3918\_add\_is\_flagged\_field\_to\_comments.py and content like

"""add is_flagged field to comments
Revision ID: 9c27db0b3918
Revises: 16d6335dd9a3
Create Date: 2022-05-23 05:38:40.398762+00:00
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '9c27db0b3918'
down_revision = '16d6335dd9a3'
branch_labels = None
depends_on = None
def upgrade():
    op.add_column('comments', sa.Column('is_flagged', sa.Boolean(), nullable=False))
def downgrade():
    op.drop_column('comments', 'is_flagged')
  1. Examine the autogenerated migration to make sure that everything looks right (it adds the column you expected it to add and nothing else, all constraints are named, etc. If you see a None in one of the alembic operations, e.g. op.create\_foreign\_key\_something(None, 'usernotes', 'users', ['author\_id']), please replace it with a descriptive string before you commit the migration).
  2. Run the migration to make sure it works. You can run a migration with the command
flask db upgrade

which, if you're using the docker-compose, looks like

docker-compose exec -T files bash -c 'cd /service/; FLASK_APP="files/cli:app" flask "$@"' . db upgrade

Or with the util scripts:

./util/migrate.py upgrade

Running migrations someone else checked in

If you've just merged schema changes that another dev made, you can get your local database up to date by:

  • Open two terminals in the root of the project
  • Run docker-compose up in one terminal
  • Run this command in the other:
    ./util/test.py upgrade
    
    (or see above section for manual upgrade command)

You should not have to reboot your container, though it might be a good idea to do so anyway if the changes you are merging in are nontrivial (particularly if there have been changes to docker-compose.yml or Dockerfile).

So what's up with schema.sql, can I just change that?

No, please do not do that. Instead, please make a migration as described above.