Skip to content
This repository was archived by the owner on Feb 3, 2024. It is now read-only.

Commit 440c721

Browse files
authored
Merge pull request #60 from mxmeinhold/reporting-and-hiding
2 parents 144a732 + a3cb5fb commit 440c721

30 files changed

+1136
-131
lines changed

app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
app.run(host=app.config['IP'], port=app.config['PORT'], threaded = False)
55

66
application = app
7+

config.env.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@
2323
# CSH_LDAP credentials
2424
LDAP_BIND_DN = os.environ.get("LDAP_BIND_DN", default="cn=quotefault,ou=Apps,dc=csh,dc=rit,dc=edu")
2525
LDAP_BIND_PW = os.environ.get("LDAP_BIND_PW", default=None)
26+
MAIL_USERNAME = os.environ.get("MAIL_USERNAME", default=None)
2627
MAIL_PASSWORD = os.environ.get("MAIL_PASSWORD", default=None)
2728
MAIL_SERVER = os.environ.get("MAIL_SERVER", default=None)
29+
MAIL_PORT = os.environ.get("MAIL_PORT", default=465)
30+
MAIL_USE_SSL = True
31+

config.sample.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@
2020
# CSH_LDAP credentials
2121
LDAP_BIND_DN = ''
2222
LDAP_BIND_PW = ''
23+
MAIL_USERNAME = ''
2324
MAIL_PASSWORD = ''
2425
MAIL_SERVER = ''
26+
MAIL_PORT = 465
27+
MAIL_USE_SSL = True
28+

migrations/README

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## What are Alembic and migrations?
2+
Migrations give a versioned history of changes to the SQL Schema for this project.
3+
A `revision` or `migration` defines changes to the structure of the database (such as adding/dropping tables or columns), as well as procedures for migrating existing data to the new structure. Migrations allow for safe(ish) rollbacks, and make it easier to make incremental changes to the database.
4+
5+
## Applying migrations
6+
Once you have your db URI in `config.py`, run `flask db upgrade` to bring the db up to the latest revision.
7+
8+
## Creating new revisions
9+
When you change `models.py`, you'll need to make a new migration.
10+
Run `flask db revision -m "<descriptive title>"` to autogenerate one.
11+
You should go manually inspect the created revision to make sure it does what you expect.
12+
Once you've got your changes all set, be sure to commit the migration file in the same commit as your changes to `models.py`

migrations/alembic.ini

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts
5+
script_location = migrations
6+
7+
# template used to generate migration files
8+
# file_template = %%(rev)s_%%(slug)s
9+
10+
# timezone to use when rendering the date
11+
# within the migration file as well as the filename.
12+
# string value is passed to dateutil.tz.gettz()
13+
# leave blank for localtime
14+
# timezone =
15+
16+
# max length of characters to apply to the
17+
# "slug" field
18+
#truncate_slug_length = 40
19+
20+
# set to 'true' to run the environment during
21+
# the 'revision' command, regardless of autogenerate
22+
# revision_environment = false
23+
24+
# set to 'true' to allow .pyc and .pyo files without
25+
# a source .py file to be detected as revisions in the
26+
# versions/ directory
27+
# sourceless = false
28+
29+
# version location specification; this defaults
30+
# to migrations/versions. When using multiple version
31+
# directories, initial revisions must be specified with --version-path
32+
# version_locations = %(here)s/bar %(here)s/bat migrations/versions
33+
34+
# the output encoding used when revision files
35+
# are written from script.py.mako
36+
# output_encoding = utf-8
37+
38+
sqlalchemy.url = driver://user:pass@localhost/dbname
39+
40+
41+
# Logging configuration
42+
[loggers]
43+
keys = root,sqlalchemy,alembic
44+
45+
[handlers]
46+
keys = console
47+
48+
[formatters]
49+
keys = generic
50+
51+
[logger_root]
52+
level = WARN
53+
handlers = console
54+
qualname =
55+
56+
[logger_sqlalchemy]
57+
level = WARN
58+
handlers =
59+
qualname = sqlalchemy.engine
60+
61+
[logger_alembic]
62+
level = INFO
63+
handlers =
64+
qualname = alembic
65+
66+
[handler_console]
67+
class = StreamHandler
68+
args = (sys.stderr,)
69+
level = NOTSET
70+
formatter = generic
71+
72+
[formatter_generic]
73+
format = %(levelname)-5.5s [%(name)s] %(message)s
74+
datefmt = %H:%M:%S
75+

migrations/env.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from __future__ import with_statement
2+
3+
import logging
4+
from logging.config import fileConfig
5+
6+
from flask import current_app
7+
8+
from alembic import context
9+
10+
# this is the Alembic Config object, which provides
11+
# access to the values within the .ini file in use.
12+
config = context.config
13+
14+
# Interpret the config file for Python logging.
15+
# This line sets up loggers basically.
16+
fileConfig(config.config_file_name)
17+
logger = logging.getLogger('alembic.env')
18+
19+
# add your model's MetaData object here
20+
# for 'autogenerate' support
21+
# from myapp import mymodel
22+
# target_metadata = mymodel.Base.metadata
23+
config.set_main_option(
24+
'sqlalchemy.url',
25+
str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%'))
26+
target_metadata = current_app.extensions['migrate'].db.metadata
27+
28+
# other values from the config, defined by the needs of env.py,
29+
# can be acquired:
30+
# my_important_option = config.get_main_option("my_important_option")
31+
# ... etc.
32+
33+
34+
def run_migrations_offline():
35+
"""Run migrations in 'offline' mode.
36+
37+
This configures the context with just a URL
38+
and not an Engine, though an Engine is acceptable
39+
here as well. By skipping the Engine creation
40+
we don't even need a DBAPI to be available.
41+
42+
Calls to context.execute() here emit the given string to the
43+
script output.
44+
45+
"""
46+
url = config.get_main_option("sqlalchemy.url")
47+
context.configure(
48+
url=url, target_metadata=target_metadata, literal_binds=True
49+
)
50+
51+
with context.begin_transaction():
52+
context.run_migrations()
53+
54+
55+
def run_migrations_online():
56+
"""Run migrations in 'online' mode.
57+
58+
In this scenario we need to create an Engine
59+
and associate a connection with the context.
60+
61+
"""
62+
63+
# this callback is used to prevent an auto-migration from being generated
64+
# when there are no changes to the schema
65+
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
66+
def process_revision_directives(context, revision, directives):
67+
if getattr(config.cmd_opts, 'autogenerate', False):
68+
script = directives[0]
69+
if script.upgrade_ops.is_empty():
70+
directives[:] = []
71+
logger.info('No changes in schema detected.')
72+
73+
connectable = current_app.extensions['migrate'].db.engine
74+
75+
with connectable.connect() as connection:
76+
context.configure(
77+
connection=connection,
78+
target_metadata=target_metadata,
79+
process_revision_directives=process_revision_directives,
80+
**current_app.extensions['migrate'].configure_args
81+
)
82+
83+
with context.begin_transaction():
84+
context.run_migrations()
85+
86+
87+
if context.is_offline_mode():
88+
run_migrations_offline()
89+
else:
90+
run_migrations_online()
91+

migrations/script.py.mako

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""${message}
2+
3+
Revision ID: ${up_revision}
4+
Revises: ${down_revision | comma,n}
5+
Create Date: ${create_date}
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
${imports if imports else ""}
11+
12+
# revision identifiers, used by Alembic.
13+
revision = ${repr(up_revision)}
14+
down_revision = ${repr(down_revision)}
15+
branch_labels = ${repr(branch_labels)}
16+
depends_on = ${repr(depends_on)}
17+
18+
19+
def upgrade():
20+
${upgrades if upgrades else "pass"}
21+
22+
23+
def downgrade():
24+
${downgrades if downgrades else "pass"}
25+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Report Reviewed Column Added
2+
3+
Revision ID: 3b8a4c7fbcc2
4+
Revises: 76898f8ac346
5+
Create Date: 2021-11-07 22:31:04.835460
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '3b8a4c7fbcc2'
14+
down_revision = '76898f8ac346'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
op.add_column('report', sa.Column('reviewed', sa.Boolean(), nullable=False, default=False))
21+
22+
23+
def downgrade():
24+
op.drop_column('report', 'reviewed')
25+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Initial schema
2+
3+
Revision ID: 4f95c173f1d9
4+
Revises:
5+
Create Date: 2021-04-23 01:02:40.157049
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '4f95c173f1d9'
14+
down_revision = None
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.create_table('api_key',
22+
sa.Column('id', sa.Integer(), nullable=False),
23+
sa.Column('hash', sa.String(length=64), nullable=True),
24+
sa.Column('owner', sa.String(length=80), nullable=True),
25+
sa.Column('reason', sa.String(length=120), nullable=True),
26+
sa.PrimaryKeyConstraint('id'),
27+
sa.UniqueConstraint('hash'),
28+
sa.UniqueConstraint('owner', 'reason', name='unique_key')
29+
)
30+
op.create_table('quote',
31+
sa.Column('id', sa.Integer(), nullable=False),
32+
sa.Column('submitter', sa.String(length=80), nullable=False),
33+
sa.Column('quote', sa.String(length=200), nullable=False),
34+
sa.Column('speaker', sa.String(length=50), nullable=False),
35+
sa.Column('quote_time', sa.DateTime(), nullable=False),
36+
sa.PrimaryKeyConstraint('id'),
37+
sa.UniqueConstraint('quote')
38+
)
39+
op.create_table('vote',
40+
sa.Column('id', sa.Integer(), nullable=False),
41+
sa.Column('quote_id', sa.Integer(), nullable=True),
42+
sa.Column('voter', sa.String(length=200), nullable=False),
43+
sa.Column('direction', sa.Integer(), nullable=False),
44+
sa.Column('updated_time', sa.DateTime(), nullable=False),
45+
sa.ForeignKeyConstraint(['quote_id'], ['quote.id'], ),
46+
sa.PrimaryKeyConstraint('id')
47+
)
48+
# ### end Alembic commands ###
49+
50+
51+
def downgrade():
52+
# ### commands auto generated by Alembic - please adjust! ###
53+
op.drop_table('vote')
54+
op.drop_table('quote')
55+
op.drop_table('api_key')
56+
# ### end Alembic commands ###
57+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Add reports
2+
3+
Revision ID: 76898f8ac346
4+
Revises: 4f95c173f1d9
5+
Create Date: 2021-04-23 01:18:03.934757
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '76898f8ac346'
14+
down_revision = '4f95c173f1d9'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
op.create_table('report',
21+
sa.Column('id', sa.Integer(), nullable=False, autoincrement=True),
22+
sa.Column('quote_id', sa.Integer(), nullable=False),
23+
sa.Column('reporter', sa.Text(), nullable=False),
24+
sa.Column('reason', sa.Text(), nullable=False),
25+
sa.ForeignKeyConstraint(['quote_id'], ['quote.id'], ),
26+
sa.PrimaryKeyConstraint('id')
27+
)
28+
op.add_column('quote', sa.Column('hidden', sa.Boolean(), nullable=False, default=False))
29+
30+
31+
def downgrade():
32+
op.drop_column('quote', 'hidden')
33+
op.drop_table('report')
34+

0 commit comments

Comments
 (0)