Skip to content

Commit

Permalink
feat:#1067 add public transportation rating to route
Browse files Browse the repository at this point in the history
  • Loading branch information
Nayor committed May 15, 2024
1 parent f8fb90d commit af19ddd
Show file tree
Hide file tree
Showing 12 changed files with 922 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""Add column public_transportation_rating to route.py
Revision ID: b59d3efaf2a1
Revises: 626354ffcda0
Create Date: 2024-05-07 16:13:17.458223
"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'b59d3efaf2a1'
down_revision = '626354ffcda0'
branch_labels = None
depends_on = None


def upgrade():

new_public_transportation_rating_type = sa.Enum(
'good service',
'seasonal service',
'poor service',
'nearby service',
'unknown service',
'no service',
name='public_transportation_ratings',
schema='guidebook'
)
new_public_transportation_rating_type.create(op.get_bind())
op.alter_column(
"waypoints",
"public_transportation_rating",
type_=new_public_transportation_rating_type,
postgresql_using="public_transportation_rating::text::guidebook.public_transportation_ratings",
schema="guidebook",
)
op.alter_column(
'waypoints_archives',
'public_transportation_rating',
type_=new_public_transportation_rating_type,
postgresql_using='public_transportation_rating::text::guidebook.public_transportation_ratings',
schema='guidebook'
)
op.execute("DROP TYPE IF EXISTS guidebook.public_transportation_rating")
op.add_column(
'routes',
sa.Column(
'public_transportation_rating',
new_public_transportation_rating_type,
nullable=True),
schema='guidebook')
op.add_column(
'routes_archives',
sa.Column(
'public_transportation_rating',
new_public_transportation_rating_type,
nullable=True),
schema='guidebook')


def downgrade():
op.drop_column('routes', 'public_transportation_rating', schema='guidebook')
op.drop_column('routes_archives', 'public_transportation_rating', schema='guidebook')
op.execute("UPDATE guidebook.waypoints SET public_transportation_rating = NULL WHERE public_transportation_rating = 'unknown service'")
op.execute("UPDATE guidebook.waypoints_archives SET public_transportation_rating = NULL WHERE public_transportation_rating = 'unknown service'")
new_public_transportation_rating_type = sa.Enum(
'good service',
'seasonal service',
'poor service',
'nearby service',
'unknown service',
'no service',
name='public_transportation_rating',
schema='guidebook'
)
new_public_transportation_rating_type.create(op.get_bind())
op.alter_column(
'waypoints',
'public_transportation_rating',
type_=new_public_transportation_rating_type,
postgresql_using='public_transportation_rating::text::guidebook.public_transportation_rating',
schema='guidebook'
)
op.alter_column(
'waypoints_archives',
'public_transportation_rating',
type_=new_public_transportation_rating_type,
postgresql_using='public_transportation_rating::text::guidebook.public_transportation_rating',
schema='guidebook'
)
op.execute("DROP TYPE IF EXISTS guidebook.public_transportation_ratings")
1 change: 1 addition & 0 deletions c2corg_api/models/common/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
'seasonal service', # service saisonnier
'poor service', # service reduit
'nearby service', # service a proximite
'unknown service', # service inconnu
'no service' # pas de service
]

Expand Down
2 changes: 2 additions & 0 deletions c2corg_api/models/common/fields_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'height_diff_down',
'height_diff_access',
'height_diff_difficulties',
'public_transportation_rating',
'lift_access',
'route_types',
'orientations',
Expand All @@ -40,6 +41,7 @@
'height_diff_up',
'height_diff_down',
'height_diff_difficulties',
'public_transportation_rating',
'activities',
'quality',
'orientations'
Expand Down
46 changes: 23 additions & 23 deletions c2corg_api/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,18 @@ def almost_equals(self, other):
return self._almost_equals(self.geom, other.geom) and \
self._almost_equals(self.geom_detail, other.geom_detail)

def get_shape(self, geom):
if isinstance(geom, WKBElement):
g = wkb_to_shape(geom)
proj = geom.srid
else:
# WKT are used in the tests.
split = str.split(geom, ';')
proj = int(str.split(split[0], '=')[1])
str1 = split[1]
g = wkt.loads(str1)
return g, proj

def _almost_equals(self, geom, other_geom):
if geom is None and other_geom is None:
return True
Expand All @@ -357,29 +369,8 @@ def _almost_equals(self, geom, other_geom):
elif geom is None and other_geom is not None:
return False

g1 = None
proj1 = None
if isinstance(geom, WKBElement):
g1 = wkb_to_shape(geom)
proj1 = geom.srid
else:
# WKT are used in the tests.
split1 = str.split(geom, ';')
proj1 = int(str.split(split1[0], '=')[1])
str1 = split1[1]
g1 = wkt.loads(str1)

g2 = None
proj2 = None
if isinstance(other_geom, WKBElement):
g2 = wkb_to_shape(other_geom)
proj2 = other_geom.srid
else:
# WKT are used in the tests.
split2 = str.split(other_geom, ';')
proj2 = int(str.split(split2[0], '=')[1])
str2 = split2[1]
g2 = wkt.loads(str2)
g1, proj1 = self.get_shape(geom)
g2, proj2 = self.get_shape(other_geom)

# https://github.com/Toblerity/Shapely/blob/
# 8df2b1b718c89e7d644b246ab07ad3670d25aa6a/shapely/geometry/base.py#L673
Expand All @@ -401,6 +392,15 @@ def _almost_equals(self, geom, other_geom):

return g1.almost_equals(g2, decimals)

def distance(self, geom, other_geom):
if geom is None or other_geom is None:
return None

g1, _ = self.get_shape(geom)
g2, _ = self.get_shape(other_geom)

return g1.distance(g2)


DocumentGeometry.lon_lat = column_property(
func.ST_AsGeoJSON(func.ST_Transform(DocumentGeometry.geom, 4326)),
Expand Down
4 changes: 3 additions & 1 deletion c2corg_api/models/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ class _RouteMixin(object):

slackline_height = Column(SmallInteger)

public_transportation_rating = Column(enums.public_transportation_rating)


attributes = [
'main_waypoint_id', 'activities', 'elevation_min', 'elevation_max',
Expand All @@ -167,7 +169,7 @@ class _RouteMixin(object):
'hiking_mtb_exposition', 'snowshoe_rating', 'mtb_up_rating',
'mtb_down_rating', 'mtb_length_asphalt', 'mtb_length_trail',
'mtb_height_diff_portages', 'rock_types', 'climbing_outdoor_type',
'slackline_type', 'slackline_height']
'slackline_type', 'slackline_height', 'public_transportation_rating']


class Route(_RouteMixin, Document):
Expand Down
12 changes: 11 additions & 1 deletion c2corg_api/models/waypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ def update(self, other):
super(Waypoint, self).update(other)
copy_attributes(other, self, attributes)

def get_update_type(self, old_versions):
update_types = super(Waypoint, self).get_update_type(old_versions)

if self.public_transportation_rating != \
old_versions.get('public_transportation_rating', None):
update_types[0].append('public_transportation_rating')

return update_types


class ArchiveWaypoint(_WaypointMixin, ArchiveDocument):
"""
Expand Down Expand Up @@ -328,5 +337,6 @@ class ArchiveWaypointLocale(_WaypointLocaleMixin, ArchiveDocumentLocale):
schema_create_waypoint = get_create_schema(schema_waypoint)
schema_update_waypoint = get_update_schema(schema_waypoint)
schema_association_waypoint = restrict_schema(schema_waypoint, [
'elevation', 'locales.title', 'locales.access_period', 'geometry.geom', 'public_transportation_rating'
'elevation', 'locales.title', 'locales.access_period', 'geometry.geom',
'public_transportation_rating'
])
4 changes: 2 additions & 2 deletions c2corg_api/tests/views/test_document_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ def image_service_mock(url, request):
def test_delete_collaborative_doc(self):
# Collaborative documents cannot be deleted, even by their authors:
headers = self.add_authorization_header(username='contributor')
return self.app.delete_json(
self.app.delete_json(
self._prefix + str(self.waypoint4.document_id), {},
headers=headers, status=400)

Expand All @@ -731,7 +731,7 @@ def test_delete_collaborative_doc(self):
def test_delete_personal_doc_not_author(self):
# Personal documents cannot be deleted by anyone:
headers = self.add_authorization_header(username='contributor2')
return self.app.delete_json(
self.app.delete_json(
self._prefix + str(self.article1.document_id), {},
headers=headers, status=400)

Expand Down
Loading

0 comments on commit af19ddd

Please sign in to comment.