Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify requirements with code adjustments #2421

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ ENV VIRTUAL_ENV=/venv3
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
ENV ALLOW_ALEMBIC_UPGRADE=yes
ENV FLASK_APP=anyway
ENV FLASK_DEBUG=1
ENV FLASK_ENV=development

COPY . /anyway

EXPOSE 5000

RUN flask assets clean
RUN flask --app anyway:app assets clean

ENTRYPOINT ["/anyway/docker-entrypoint.sh"]
CMD FLASK_APP=anyway flask run --host 0.0.0.0
CMD FLASK_APP=anyway flask --app anyway:app run --host 0.0.0.0
14 changes: 7 additions & 7 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def run_migrations_offline():
script output.

"""
from anyway.app_and_db import db

url = os.environ.get("DATABASE_URL")
context.configure(url=url, target_metadata=db.Model.metadata, literal_binds=True)
from anyway.app_and_db import db, app
with app.app_context():
url = os.environ.get("DATABASE_URL")
context.configure(url=url, target_metadata=db.Model.metadata, literal_binds=True)

with context.begin_transaction():
context.run_migrations()
Expand All @@ -54,9 +54,9 @@ def run_migrations_online():
and associate a connection with the context.

"""
from anyway.app_and_db import db

connectable = db.engine
from anyway.app_and_db import db, app
with app.app_context():
connectable = db.engine

with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=db.Model.metadata)
Expand Down
4 changes: 2 additions & 2 deletions alembic/versions/312c9eb92e40_add_cbs_locations_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def upgrade():
sa.PrimaryKeyConstraint('id')
)
conn = op.get_bind()
conn.execute("""INSERT INTO cbs_locations
conn.execute(sa.text("""INSERT INTO cbs_locations
(SELECT ROW_NUMBER() OVER (ORDER BY road1) as id, LOCATIONS.*
FROM
(SELECT DISTINCT road1,
Expand All @@ -52,7 +52,7 @@ def upgrade():
WHERE (provider_code=1
OR provider_code=3)
AND (longitude is not null
AND latitude is not null)) LOCATIONS)""")
AND latitude is not null)) LOCATIONS)"""))
# ### end Alembic commands ###


Expand Down
9 changes: 5 additions & 4 deletions alembic/versions/4c4b79f8c4a_adding_geom_gix_to_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@
depends_on = None

from alembic import op
import sqlalchemy as sa


def upgrade():
### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
conn.execute('CREATE INDEX geom_gix ON markers USING GIST (geography(geom));')
conn.execute('CREATE INDEX discussions_gix ON discussions USING GIST (geography(geom));')
conn.execute(sa.text('CREATE INDEX geom_gix ON markers USING GIST (geography(geom));'))
conn.execute(sa.text('CREATE INDEX discussions_gix ON discussions USING GIST (geography(geom));'))

### end Alembic commands ###


def downgrade():
### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
conn.execute('DROP INDEX geom_gix;')
conn.execute('DROP INDEX discussions_gix;')
conn.execute(sa.text('DROP INDEX geom_gix;'))
conn.execute(sa.text('DROP INDEX discussions_gix;'))
### end Alembic commands ###
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,32 @@
depends_on = None

from alembic import op
import sqlalchemy as sa


def upgrade():
### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
conn.execute('CREATE EXTENSION IF NOT EXISTS postgis;')
conn.execute('CREATE EXTENSION IF NOT EXISTS postgis_topology;')
conn.execute("SELECT AddGeometryColumn('public','markers','geom',4326,'POINT',2);")
conn.execute('UPDATE markers SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);')
conn.execute('CREATE INDEX idx_markers_geom ON markers USING GIST(geom);')
conn.execute("SELECT AddGeometryColumn('public','discussions','geom',4326,'POINT',2);")
conn.execute('UPDATE discussions SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);')
conn.execute('CREATE INDEX idx_discussions_geom ON discussions USING GIST(geom);')
conn.execute(sa.text('CREATE EXTENSION IF NOT EXISTS postgis;'))
conn.execute(sa.text('CREATE EXTENSION IF NOT EXISTS postgis_topology;'))
conn.execute(sa.text("SELECT AddGeometryColumn('public','markers','geom',4326,'POINT',2);"))
conn.execute(sa.text('UPDATE markers SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);'))
conn.execute(sa.text('CREATE INDEX idx_markers_geom ON markers USING GIST(geom);'))
conn.execute(sa.text("SELECT AddGeometryColumn('public','discussions','geom',4326,'POINT',2);"))
conn.execute(sa.text('UPDATE discussions SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);'))
conn.execute(sa.text('CREATE INDEX idx_discussions_geom ON discussions USING GIST(geom);'))

### end Alembic commands ###


def downgrade():
### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
conn.execute('DROP INDEX idx_markers_geom;')
conn.execute(sa.text('DROP INDEX idx_markers_geom;'))
op.drop_column('markers', 'geom')
conn.execute('DROP INDEX idx_discussions_geom;')
conn.execute(sa.text('DROP INDEX idx_discussions_geom;'))
op.drop_column('discussions', 'geom')
conn.execute('DROP EXTENSION postgis_topology;')
conn.execute('DROP EXTENSION postgis;')
conn.execute('DROP SCHEMA IF EXISTS topology CASCADE;')
conn.execute(sa.text('DROP EXTENSION postgis_topology;'))
conn.execute(sa.text('DROP EXTENSION postgis;'))
conn.execute(sa.text('DROP SCHEMA IF EXISTS topology CASCADE;'))
### end Alembic commands ###
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ def downgrade():
op.create_index('provider_and_id_idx_involved', 'involved', ['provider_and_id'], unique=False)
op.create_index('provider_and_id_idx_vehicles', 'vehicles', ['provider_and_id'], unique=False)
conn = op.get_bind()
conn.execute('CREATE INDEX geom_gix ON markers USING GIST (geography(geom));')
conn.execute('CREATE INDEX discussions_gix ON discussions USING GIST (geography(geom));')
conn.execute(sa.text('CREATE INDEX geom_gix ON markers USING GIST (geography(geom));'))
conn.execute(sa.text('CREATE INDEX discussions_gix ON discussions USING GIST (geography(geom));'))
# ### end Alembic commands ###
6 changes: 3 additions & 3 deletions alembic/versions/7f629b4c8891_add_news_flash_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
conn = op.get_bind()
conn.execute("ALTER TABLE news_flash ALTER COLUMN id SET DEFAULT nextval('news_flash_id_seq');")
conn.execute("ALTER SEQUENCE news_flash_id_seq OWNED BY news_flash.id;")
conn.execute("SELECT setval('news_flash_id_seq', COALESCE(max(id), 1)) FROM news_flash;")
conn.execute(sa.text("ALTER TABLE news_flash ALTER COLUMN id SET DEFAULT nextval('news_flash_id_seq');"))
conn.execute(sa.text("ALTER SEQUENCE news_flash_id_seq OWNED BY news_flash.id;"))
conn.execute(sa.text("SELECT setval('news_flash_id_seq', COALESCE(max(id), 1)) FROM news_flash;"))
op.add_column('news_flash', sa.Column('district_hebrew', sa.Text(), nullable=True))
op.add_column('news_flash', sa.Column('non_urban_intersection_hebrew', sa.Text(), nullable=True))
op.add_column('news_flash', sa.Column('region_hebrew', sa.Text(), nullable=True))
Expand Down
44 changes: 22 additions & 22 deletions anyway/accidents_around_schools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from anyway.backend_constants import BE_CONST
from anyway.models import AccidentMarker, Involved, School
from anyway.app_and_db import db
from anyway.app_and_db import db, app

SUBTYPE_ACCIDENT_WITH_PEDESTRIAN = 1
LOCATION_ACCURACY_PRECISE = True
Expand Down Expand Up @@ -48,27 +48,26 @@ def acc_inv_query(longitude, latitude, distance, start_date, end_date, school):
pol_str = "POLYGON(({0} {1},{0} {3},{2} {3},{2} {1},{0} {1}))".format(
base_x, base_y, distance_x, distance_y
)

query_obj = (
db.session.query(Involved, AccidentMarker)
.join(AccidentMarker, AccidentMarker.provider_and_id == Involved.provider_and_id)
.filter(AccidentMarker.geom.intersects(pol_str))
.filter(Involved.injured_type == INJURED_TYPE_PEDESTRIAN)
.filter(AccidentMarker.provider_and_id == Involved.provider_and_id)
.filter(
or_(
(AccidentMarker.provider_code == BE_CONST.CBS_ACCIDENT_TYPE_1_CODE),
(AccidentMarker.provider_code == BE_CONST.CBS_ACCIDENT_TYPE_3_CODE),
with app.app_context():
query_obj = (
db.session.query(Involved, AccidentMarker)
.join(AccidentMarker, AccidentMarker.provider_and_id == Involved.provider_and_id)
.filter(AccidentMarker.geom.intersects(pol_str))
.filter(Involved.injured_type == INJURED_TYPE_PEDESTRIAN)
.filter(AccidentMarker.provider_and_id == Involved.provider_and_id)
.filter(
or_(
(AccidentMarker.provider_code == BE_CONST.CBS_ACCIDENT_TYPE_1_CODE),
(AccidentMarker.provider_code == BE_CONST.CBS_ACCIDENT_TYPE_3_CODE),
)
)
)
.filter(AccidentMarker.created >= start_date)
.filter(AccidentMarker.created < end_date)
.filter(AccidentMarker.location_accuracy == LOCATION_ACCURACY_PRECISE_INT)
.filter(AccidentMarker.yishuv_symbol != YISHUV_SYMBOL_NOT_EXIST)
.filter(Involved.age_group.in_([1, 2, 3, 4]))
) # ages 0-19

df = pd.read_sql_query(query_obj.with_labels().statement, query_obj.session.bind)
.filter(AccidentMarker.created >= start_date)
.filter(AccidentMarker.created < end_date)
.filter(AccidentMarker.location_accuracy == LOCATION_ACCURACY_PRECISE_INT)
.filter(AccidentMarker.yishuv_symbol != YISHUV_SYMBOL_NOT_EXIST)
.filter(Involved.age_group.in_([1, 2, 3, 4]))
) # ages 0-19
df = pd.read_sql_query(query_obj.with_labels().statement, db.get_engine())

if LOCATION_ACCURACY_PRECISE:
location_accurate = 1
Expand Down Expand Up @@ -110,7 +109,8 @@ def acc_inv_query(longitude, latitude, distance, start_date, end_date, school):

def main(start_date, end_date, distance, output_path):
schools_query = sa.select([School])
df_schools = pd.read_sql_query(schools_query, db.session.bind)
with app.app_context():
df_schools = pd.read_sql_query(schools_query, db.get_engine())
df_total = pd.DataFrame()
df_schools = df_schools.drop_duplicates( # pylint: disable=no-member
["yishuv_name", "longitude", "latitude"]
Expand Down
71 changes: 0 additions & 71 deletions anyway/base.py

This file was deleted.

6 changes: 3 additions & 3 deletions anyway/database.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
try:
from anyway.app_and_db import db

Base = db.Model
from anyway.app_and_db import db, app
with app.app_context():
Base = db.Model
except ModuleNotFoundError:
from sqlalchemy.ext.declarative.api import declarative_base

Expand Down
10 changes: 5 additions & 5 deletions anyway/db_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def create_markers_hebrew_view(self):
.join(ProviderCode,
and_(AccidentMarker.provider_code == ProviderCode.id),
isouter=True)
return select(selected_columns) \
return select(*selected_columns) \
.select_from(from_clause)

def create_involved_hebrew_view(self):
Expand Down Expand Up @@ -501,7 +501,7 @@ def create_involved_hebrew_view(self):
Involved.accident_year == LateDeceased.year,
Involved.provider_code == LateDeceased.provider_code),
isouter=True)
return select(selected_columns) \
return select(*selected_columns) \
.select_from(from_clause)

def create_involved_hebrew_markers_hebrew_view(self):
Expand Down Expand Up @@ -681,7 +681,7 @@ def create_involved_hebrew_markers_hebrew_view(self):
InvolvedView.accident_year == VehiclesView.accident_year,
InvolvedView.car_id == VehiclesView.car_id),
isouter=True)
return select(selected_columns) \
return select(*selected_columns) \
.select_from(from_clause)

def create_vehicles_hebrew_view(self):
Expand Down Expand Up @@ -748,7 +748,7 @@ def create_vehicles_hebrew_view(self):
Vehicle.accident_year == VehicleDamage.year,
Vehicle.provider_code == VehicleDamage.provider_code),
isouter=True)
return select(selected_columns) \
return select(*selected_columns) \
.select_from(from_clause)

def create_vehicles_markers_hebrew_view(self):
Expand Down Expand Up @@ -879,7 +879,7 @@ def create_vehicles_markers_hebrew_view(self):
and_(VehiclesView.provider_code == AccidentMarkerView.provider_code,
VehiclesView.accident_id == AccidentMarkerView.id,
VehiclesView.accident_year == AccidentMarkerView.accident_year))
return select(selected_columns) \
return select(*selected_columns) \
.select_from(from_clause)

VIEWS = Views()
Loading
Loading