Skip to content

Commit 7efccc3

Browse files
committed
first commit
0 parents  commit 7efccc3

16 files changed

+322
-0
lines changed

.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FLASK_ENV=development
2+
FLASK_APP=app.py
3+
DATABASE_URL="postgresql:///wordcount_dev"
4+
APP_SETTINGS=config.DevelopmentConfig

__pycache__/app.cpython-38.pyc

1.08 KB
Binary file not shown.

__pycache__/config.cpython-38.pyc

1.14 KB
Binary file not shown.

__pycache__/models.cpython-38.pyc

884 Bytes
Binary file not shown.

app.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from flask import Flask
2+
from flask_sqlalchemy import SQLAlchemy
3+
import os
4+
from flask_migrate import Migrate
5+
6+
app = Flask(__name__)
7+
app.config.from_object(os.environ['APP_SETTINGS'])
8+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
9+
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
10+
11+
db = SQLAlchemy(app)
12+
migrate = Migrate(app, db)
13+
14+
15+
from models import Result
16+
17+
18+
@app.route('/')
19+
def hello():
20+
return "Hello World!"
21+
22+
23+
@app.route('/<name>')
24+
def hello_name(name):
25+
return "Hello {}!".format(name)
26+
27+
28+
@app.route('/result/<url>')
29+
def result(url):
30+
obj = Result(url,"{'data':'yes'}","{'data':'yes'}")
31+
db.session.add(obj)
32+
db.session.commit()
33+
return "{}".format(url)
34+
35+
36+
if __name__ == '__main__':
37+
app.run()
38+

config.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
basedir = os.path.abspath(os.path.dirname(__file__))
3+
4+
5+
class Config(object):
6+
DEBUG = False
7+
TESTING = False
8+
CSRF_ENABLED = True
9+
SECRET_KEY = 'this-really-needs-to-be-changed'
10+
SQLALCHEMY_DATABASE_URI = os.environ['DATABASE_URL']
11+
12+
13+
class ProductionConfig(Config):
14+
DEBUG = False
15+
16+
17+
class StagingConfig(Config):
18+
DEVELOPMENT = True
19+
DEBUG = True
20+
21+
22+
class DevelopmentConfig(Config):
23+
DEVELOPMENT = True
24+
DEBUG = True
25+
26+
27+
class TestingConfig(Config):
28+
TESTING = True

manage.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
from flask_script import Manager
3+
from flask_migrate import MigrateCommand
4+
5+
from app import app, db
6+
7+
8+
app.config.from_object(os.environ['APP_SETTINGS'])
9+
10+
manager = Manager(app)
11+
12+
manager.add_command('db', MigrateCommand)
13+
14+
15+
if __name__ == '__main__':
16+
manager.run()

migrations/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.
2.11 KB
Binary file not shown.

migrations/alembic.ini

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# template used to generate migration files
5+
# file_template = %%(rev)s_%%(slug)s
6+
7+
# set to 'true' to run the environment during
8+
# the 'revision' command, regardless of autogenerate
9+
# revision_environment = false
10+
11+
12+
# Logging configuration
13+
[loggers]
14+
keys = root,sqlalchemy,alembic,flask_migrate
15+
16+
[handlers]
17+
keys = console
18+
19+
[formatters]
20+
keys = generic
21+
22+
[logger_root]
23+
level = WARN
24+
handlers = console
25+
qualname =
26+
27+
[logger_sqlalchemy]
28+
level = WARN
29+
handlers =
30+
qualname = sqlalchemy.engine
31+
32+
[logger_alembic]
33+
level = INFO
34+
handlers =
35+
qualname = alembic
36+
37+
[logger_flask_migrate]
38+
level = INFO
39+
handlers =
40+
qualname = flask_migrate
41+
42+
[handler_console]
43+
class = StreamHandler
44+
args = (sys.stderr,)
45+
level = NOTSET
46+
formatter = generic
47+
48+
[formatter_generic]
49+
format = %(levelname)-5.5s [%(name)s] %(message)s
50+
datefmt = %H:%M:%S

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.get_engine().url).replace(
26+
'%', '%%'))
27+
target_metadata = current_app.extensions['migrate'].db.metadata
28+
29+
# other values from the config, defined by the needs of env.py,
30+
# can be acquired:
31+
# my_important_option = config.get_main_option("my_important_option")
32+
# ... etc.
33+
34+
35+
def run_migrations_offline():
36+
"""Run migrations in 'offline' mode.
37+
38+
This configures the context with just a URL
39+
and not an Engine, though an Engine is acceptable
40+
here as well. By skipping the Engine creation
41+
we don't even need a DBAPI to be available.
42+
43+
Calls to context.execute() here emit the given string to the
44+
script output.
45+
46+
"""
47+
url = config.get_main_option("sqlalchemy.url")
48+
context.configure(
49+
url=url, target_metadata=target_metadata, literal_binds=True
50+
)
51+
52+
with context.begin_transaction():
53+
context.run_migrations()
54+
55+
56+
def run_migrations_online():
57+
"""Run migrations in 'online' mode.
58+
59+
In this scenario we need to create an Engine
60+
and associate a connection with the context.
61+
62+
"""
63+
64+
# this callback is used to prevent an auto-migration from being generated
65+
# when there are no changes to the schema
66+
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
67+
def process_revision_directives(context, revision, directives):
68+
if getattr(config.cmd_opts, 'autogenerate', False):
69+
script = directives[0]
70+
if script.upgrade_ops.is_empty():
71+
directives[:] = []
72+
logger.info('No changes in schema detected.')
73+
74+
connectable = current_app.extensions['migrate'].db.get_engine()
75+
76+
with connectable.connect() as connection:
77+
context.configure(
78+
connection=connection,
79+
target_metadata=target_metadata,
80+
process_revision_directives=process_revision_directives,
81+
**current_app.extensions['migrate'].configure_args
82+
)
83+
84+
with context.begin_transaction():
85+
context.run_migrations()
86+
87+
88+
if context.is_offline_mode():
89+
run_migrations_offline()
90+
else:
91+
run_migrations_online()

migrations/script.py.mako

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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"}
Binary file not shown.

migrations/versions/db920660c030_.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""empty message
2+
3+
Revision ID: db920660c030
4+
Revises:
5+
Create Date: 2021-07-15 15:07:24.729517
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
from sqlalchemy.dialects import postgresql
11+
12+
# revision identifiers, used by Alembic.
13+
revision = 'db920660c030'
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('result',
22+
sa.Column('id', sa.Integer(), nullable=False),
23+
sa.Column('url', sa.String(), nullable=True),
24+
sa.Column('result_all', postgresql.JSON(astext_type=sa.Text()), nullable=True),
25+
sa.Column('result_no_stop_words', postgresql.JSON(astext_type=sa.Text()), nullable=True),
26+
sa.PrimaryKeyConstraint('id')
27+
)
28+
# ### end Alembic commands ###
29+
30+
31+
def downgrade():
32+
# ### commands auto generated by Alembic - please adjust! ###
33+
op.drop_table('result')
34+
# ### end Alembic commands ###

models.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from app import db
2+
from sqlalchemy.dialects.postgresql import JSON
3+
4+
5+
class Result(db.Model):
6+
__tabelname__ = 'results'
7+
8+
id = db.Column(db.Integer, primary_key=True)
9+
url = db.Column(db.String())
10+
result_all = db.Column(JSON)
11+
result_no_stop_words = db.Column(JSON)
12+
13+
def __init__(self, url, result_all, result_no_stop_words):
14+
self.url = url
15+
self.result_all = result_all
16+
self.result_no_stop_words = result_no_stop_words
17+
18+
def __repr__(self):
19+
return '<id {}>'.format(self.id)

requirements.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
alembic==1.6.5
2+
click==8.0.1
3+
Flask==2.0.1
4+
Flask-Migrate==3.0.1
5+
Flask-Script==2.0.6
6+
Flask-SQLAlchemy==2.5.1
7+
greenlet==1.1.0
8+
itsdangerous==2.0.1
9+
Jinja2==3.0.1
10+
Mako==1.1.4
11+
MarkupSafe==2.0.1
12+
python-dateutil==2.8.2
13+
python-dotenv==0.18.0
14+
python-editor==1.0.4
15+
six==1.16.0
16+
SQLAlchemy==1.4.20
17+
Werkzeug==2.0.1

0 commit comments

Comments
 (0)