Skip to content

Commit

Permalink
Fix geokrety#80: GeoKrety archived status
Browse files Browse the repository at this point in the history
  • Loading branch information
kumy committed Dec 29, 2018
1 parent 61250b7 commit 4fc0803
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 72 deletions.
18 changes: 9 additions & 9 deletions app/api/moves.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
from app.api.helpers.exceptions import ForbiddenException, UnprocessableEntity
from app.api.helpers.permission_manager import has_access
from app.api.helpers.utilities import has_relationships
from app.api.schema.moves import (MoveArchiveSchema, MoveCommentSchema,
MoveDippedSchema, MoveDroppedSchema,
MoveGrabbedSchema, MoveSeenSchema,
MoveWithCoordinatesSchema)
from app.api.schema.moves import (DefaultMoveSchema, MoveArchiveSchema,
MoveCommentSchema, MoveDippedSchema,
MoveDroppedSchema, MoveGrabbedSchema,
MoveSeenSchema)
from app.models import db
from app.models.geokret import Geokret
from app.models.move import Move
Expand Down Expand Up @@ -103,7 +103,7 @@ def create_object(self, data, kwargs):
update_geokret_and_moves(move.geokret_id, move.id)
return move

schema = MoveWithCoordinatesSchema
schema = DefaultMoveSchema
get_schema_kwargs = {'context': {'current_identity': current_identity}}
decorators = (
api.has_permission('auth_required', methods="POST"),
Expand Down Expand Up @@ -180,8 +180,8 @@ def update_object(self, data, qs, kwargs):

# Comming from Comment require tracking-code
if old_move.type == MOVE_TYPE_COMMENT and not data.get('tracking_code'):
raise UnprocessableEntity("Tracking code is missing",
{'pointer': '/data/attributes/tracking-code'})
raise UnprocessableEntity("Tracking code is missing",
{'pointer': '/data/attributes/tracking-code'})
# Now convert tracking-code to GeoKret ID
if 'tracking_code' in data:
geokrety_to_update.append(old_move.geokret.id)
Expand All @@ -203,7 +203,7 @@ def update_object(self, data, qs, kwargs):
model=Move, fetch_key_url="id"),
)
methods = ('GET', 'PATCH', 'DELETE')
schema = MoveDippedSchema
schema = DefaultMoveSchema
data_layer = {
'session': db.session,
'model': Move,
Expand All @@ -212,7 +212,7 @@ def update_object(self, data, qs, kwargs):

class MoveRelationship(ResourceRelationship):
methods = ['GET']
schema = MoveGrabbedSchema
schema = DefaultMoveSchema
data_layer = {
'session': db.session,
'model': Move,
Expand Down
12 changes: 6 additions & 6 deletions app/api/schema/geokrety.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Meta:
description = fields.Str()
missing = fields.Boolean(dump_only=True)
distance = fields.Integer(dump_only=True)
archived = fields.Boolean(dump_only=True)
caches_count = fields.Integer(dump_only=True)
pictures_count = fields.Integer(dump_only=True)
average_rating = fields.Float(dump_only=True)
Expand Down Expand Up @@ -83,29 +84,28 @@ class Meta:
related_view='v1.moves_list',
related_view_kwargs={'geokret_id': '<id>'},
many=True,
schema='MoveSchema',
schema='DefaultMoveSchema',
type_='move',
# include_resource_linkage=True
)

last_position = Relationship(
attribute='last_position_id',
attribute='last_position',
self_view='v1.geokret_last_position',
self_view_kwargs={'id': '<id>'},
related_view='v1.move_details',
related_view_kwargs={'geokret_last_position_id': '<id>'},
schema='MoveSchema',
schema='DefaultMoveSchema',
type_='move',
include_resource_linkage=True,
)

last_move = Relationship(
attribute='last_move_id',
attribute='last_move',
self_view='v1.geokret_last_move',
self_view_kwargs={'id': '<id>'},
related_view='v1.move_details',
related_view_kwargs={'geokret_last_move_id': '<id>'},
schema='MoveSchema',
schema='DefaultMoveSchema',
type_='move',
include_resource_linkage=True,
)
Expand Down
20 changes: 20 additions & 0 deletions app/api/schema/moves.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ class Meta:
)


class DefaultMoveSchema(MoveSchema):

class Meta:
type_ = 'move'
self_view = 'v1.move_details'
self_view_kwargs = {'id': '<id>'}
self_view_many = 'v1.moves_list'
inflect = dasherize
ordered = True
dateformat = "%Y-%m-%dT%H:%M:%S"

# fields managed differently depending on move type
waypoint = fields.Str(dump_only=True)
latitude = fields.Float(dump_only=True)
longitude = fields.Float(dump_only=True)
altitude = fields.Integer(dump_only=True)
country = fields.Str(dump_only=True)
distance = fields.Integer(dump_only=True)


class MoveWithCoordinatesSchema(MoveSchema):

@validates('waypoint')
Expand Down
49 changes: 22 additions & 27 deletions app/models/geokret.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
from datetime import datetime

from flask import request
from sqlalchemy import ForeignKeyConstraint, event
from sqlalchemy import event
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm.exc import NoResultFound

import bleach
import characterentities
from app.api.helpers.data_layers import MOVE_TYPE_DIPPED
from app.api.helpers.data_layers import (MOVE_TYPE_ARCHIVED, MOVE_TYPE_COMMENT,
MOVE_TYPE_DIPPED)
from app.api.helpers.utilities import has_attribute, round_microseconds
from app.models import db
from app.models.move import Move
Expand Down Expand Up @@ -98,31 +100,22 @@ class Geokret(db.Model):
nullable=True,
key='owner_id',
)

ForeignKeyConstraint(
['owner_id'], ['user.id'],
use_alter=True,
name='fk_geokret_owner'
)
owner = db.relationship("User", foreign_keys=[owner_id], backref="geokrety_owned")

holder_id = db.Column(
'hands_of',
db.Integer,
db.ForeignKey('gk-users.id', name='fk_geokret_holder'),
key='holder_id',
)
holder = db.relationship("User", foreign_keys=[holder_id], backref="geokrety_held")

ForeignKeyConstraint(
['holder_id'], ['user.id'],
use_alter=True,
name='fk_geokret_holder'
)

moves = db.relationship(
# This is used to compute the archived status
_moves = db.relationship(
'Move',
backref="geokret",
foreign_keys="Move.geokret_id",
cascade="all,delete",
lazy="dynamic",
)

last_position_id = db.Column(
Expand All @@ -133,24 +126,15 @@ class Geokret(db.Model):
nullable=True,
default=None,
)

ForeignKeyConstraint(
['last_position_id'], ['move.id'],
use_alter=True, name='fk_geokret_last_position'
)
last_position = db.relationship("Move", foreign_keys=[last_position_id], backref="spotted_geokret")

last_move_id = db.Column(
'ost_log_id',
db.Integer,
db.ForeignKey('gk-ruchy.id', name='fk_last_move', ondelete="SET NULL"),
key='last_move_id'
)

ForeignKeyConstraint(
['last_move_id'], ['move.id'],
use_alter=True,
name='fk_last_move'
)
last_move = db.relationship("Move", foreign_keys=[last_move_id], backref="moves")

# avatar_id = db.Column(
# 'avatarid',
Expand All @@ -159,6 +143,17 @@ class Geokret(db.Model):
# key='avatar_id'
# )

@hybrid_property
def archived(self):
"""Compute the archived status
"""
try:
last_move = self._moves.filter(Move.type != MOVE_TYPE_COMMENT)\
.order_by(Move.moved_on_datetime.desc()).limit(1).one()
return last_move.type == MOVE_TYPE_ARCHIVED
except NoResultFound:
return False

@property
def average_rating(self):
# TODO note should come from database
Expand Down
13 changes: 3 additions & 10 deletions app/models/move.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime, timedelta

from sqlalchemy import ForeignKeyConstraint, event
from sqlalchemy import event
from sqlalchemy.dialects.mysql import DOUBLE
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm.exc import NoResultFound
Expand Down Expand Up @@ -32,12 +32,7 @@ class Move(db.Model):
nullable=False,
default=None
)

ForeignKeyConstraint(
['geokret_id'], ['geokret.id'],
use_alter=True,
name='fk_geokret_moved'
)
geokret = db.relationship("Geokret", foreign_keys=[geokret_id], backref=db.backref("moves"))

latitude = db.Column(
'lat',
Expand Down Expand Up @@ -126,6 +121,7 @@ class Move(db.Model):
nullable=False,
default=None,
)
author = db.relationship("User", foreign_keys=[author_id], backref="moves")

username = db.Column(
'username',
Expand Down Expand Up @@ -173,9 +169,6 @@ class Move(db.Model):
nullable=True
)

# geokret = db.relationship('Geokret',
# backref=db.backref('moves', lazy=True))

@hybrid_property
def comment(self):
return characterentities.decode(self._comment)
Expand Down
18 changes: 0 additions & 18 deletions app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,6 @@ class User(db.Model):
backref="user",
cascade="all,delete",
)
geokrety_owned = db.relationship(
'Geokret',
backref="owner",
foreign_keys="Geokret.owner_id",
cascade="all,delete",
)
geokrety_held = db.relationship(
'Geokret',
backref="holder",
foreign_keys="Geokret.holder_id",
cascade="all,delete",
)
moves = db.relationship(
'Move',
backref="author",
foreign_keys="Move.author_id",
cascade="all,delete",
)

@hybrid_property
def password(self):
Expand Down
25 changes: 25 additions & 0 deletions tests/unittests/api/geokrety/test_geokrety_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,28 @@ def test_geokret_details_has_relationships_moves_data(self):
moves = self.blend_move(count=5, geokret=geokret, author=self.user_1, type=MOVE_TYPE_GRABBED)
response = self.send_get(geokret.id, user=self.user_1, args={'include': 'moves'})
response.assertHasRelationshipMovesDatas(moves)

@request_context
def test_geokret_details_has_archived_attribute(self):
geokret = self.blend_geokret(created_on_datetime="2018-12-29T21:39:13")
assert not geokret.archived
response = self.send_get(geokret.id, user=self.user_1)
response.assertHasAttribute('archived', False)

self.blend_move(geokret=geokret, author=self.user_1, type=MOVE_TYPE_GRABBED,
moved_on_datetime="2018-12-29T21:40:39")
assert not geokret.archived
response = self.send_get(geokret.id, user=self.user_1)
response.assertHasAttribute('archived', False)

self.blend_move(geokret=geokret, author=self.user_1, type=MOVE_TYPE_ARCHIVED,
moved_on_datetime="2018-12-29T21:40:45")
assert geokret.archived
response = self.send_get(geokret.id, user=self.user_1)
response.assertHasAttribute('archived', True)

self.blend_move(geokret=geokret, author=self.user_1, type=MOVE_TYPE_GRABBED,
moved_on_datetime="2018-12-29T21:51:27")
assert not geokret.archived
response = self.send_get(geokret.id, user=self.user_1)
response.assertHasAttribute('archived', False)
Loading

0 comments on commit 4fc0803

Please sign in to comment.