Skip to content
Snippets Groups Projects
Verified Commit 18207b6d authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Make usable (and use) parent cache containers.

parent 44bdc69b
No related branches found
No related tags found
No related merge requests found
......@@ -69,7 +69,7 @@ class GpxImporter(object):
logger.info('Creating owner %s' % owner_name)
owner_account = GeocacherAccount(name=owner_name,
platform_prefix=listing.platform_prefix)
Geocacher(accounts=[owner_account])
Geocacher(accounts=[owner_account], master_account=owner_account)
listing.owner_account = owner_account
# Find or create geocache parent
......@@ -94,14 +94,15 @@ class GpxImporter(object):
if other_listing.geocache:
listing.geocache = other_listing.geocache
else:
listing.geocache = Geocache(listings=[other_listing])
listing.geocache = Geocache(listings=[other_listing],
master_listing=listing)
logger.info('Linked listing %s to %s' % (code, other_code))
else:
# Create new geocache parent
listing.geocache = Geocache()
listing.geocache = Geocache(master_listing=listing, listings=[listing])
else:
# Create new geocache parent
listing.geocache = Geocache()
listing.geocache = Geocache(master_listing=listing, listings=[listing])
# Import logs
if cache_node.getElementsByTagName('groundspeak:logs'):
......@@ -113,7 +114,7 @@ class GpxImporter(object):
if not finder_account:
logger.info('Creating finder %s' % finder_name)
finder_account = GeocacherAccount(name=finder_name, platform_prefix=listing.platform_prefix)
Geocacher(accounts=[finder_account])
Geocacher(accounts=[finder_account], master_account=finder_account)
# Get log data
date = dateutil.parser.parse(xml_get_text(log_node, 'groundspeak:date'))
......
......@@ -2,7 +2,7 @@ import logging
from dictalchemy import make_class_dictable
from ..model import Base, GeocacherAccount, Listing
from ..model import Base, Geocache, Geocacher
logger = logging.getLogger('librestash')
......@@ -18,18 +18,22 @@ class CacheExporter(object):
if found_by:
# Find geocacher and found caches
# FIXME use parent containers instead
geocacher = session.query(GeocacherAccount).filter_by(name=found_by).first()
caches = geocacher.found_listings
geocacher = session.query(Geocacher).filter_by(name=found_by).first()
caches = geocacher.found_geocaches
else:
# Export all caches if no filter was specified
# FIXME use parent containers instead
caches = session.query(Listing).all()
caches = session.query(Geocache).all()
# Convert to list of dicts
# FIXME account for parent containers
return [cache.asdict(exclude=['id', 'owner_account_id', 'geocache_id'],
include=['code', 'date', 'platform_prefix', 'latitude', 'longitude'],
follow={'logs': {'exclude': ['id', 'geocacher_account_id', 'listing_id']},
'waypoints': {'exclude': ['id', 'listing_id', 'master_of_id']}
}) for cache in caches]
return [cache.asdict(exclude=['id'],
include=['name', 'type', 'size', 'difficulty', 'terrain', 'summary', 'description',
'hints', 'date', 'latitude', 'longitude'],
follow={'listings': {'include': ['code', 'date', 'latitude', 'longitude',
'platform_prefix'],
'exclude': ['id', 'geocache_id', 'owner_account_id',
'master_of_id'],
'follow': {'logs': {'exclude': ['id', 'geocacher_account_id',
'listing_id']},
'waypoints': {'exclude': ['id', 'listing_id',
'master_of_id']}
}}}) for cache in caches]
......@@ -95,7 +95,8 @@ class Listing(Base):
logs = relationship('Log', back_populates='listing')
geocache_id = Column(Integer, ForeignKey('geocaches.id'))
geocache = relationship('Geocache', back_populates='listings')
geocache = relationship('Geocache', back_populates='listings',
primaryjoin='Listing.geocache_id == Geocache.id')
owner_account_id = Column(Integer, ForeignKey('geocacher_accounts.id'))
owner_account = relationship('GeocacherAccount', back_populates='listings')
......@@ -106,9 +107,14 @@ class Listing(Base):
longitude = association_proxy('master_waypoint', 'longitude')
platform_prefix = association_proxy('master_waypoint', 'platform_prefix')
def found_by(self, geocacher):
master_of_id = Column(Integer, ForeignKey('geocaches.id'))
master_of = relationship('Geocache', back_populates='master_listing',
primaryjoin='Listing.master_of_id == Geocache.id')
def found_by(self, geocacher_account):
session = object_session(self)
found_logs = session.query(Log).filter_by(listing=self, geocacher=geocacher,
found_logs = session.query(Log).filter_by(listing=self,
geocacher_account=geocacher_account,
type='Found it').count()
return found_logs > 0
......@@ -117,7 +123,35 @@ class Geocache(Base):
id = Column(Integer, primary_key=True)
listings = relationship('Listing', back_populates='geocache')
master_listing = relationship('Listing', uselist=False,
primaryjoin='Listing.master_of_id == Geocache.id')
listings = relationship('Listing', back_populates='geocache',
primaryjoin='Listing.geocache_id == Geocache.id')
name = association_proxy('master_listing', 'name')
type = association_proxy('master_listing', 'type')
date = association_proxy('master_listing', 'date')
size = association_proxy('master_listing', 'size')
difficulty = association_proxy('master_listing', 'difficulty')
terrain = association_proxy('master_listing', 'terrain')
summary = association_proxy('master_listing', 'summary')
description = association_proxy('master_listing', 'description')
hints = association_proxy('master_listing', 'hints')
latitude = association_proxy('master_listing', 'latitude')
longitude = association_proxy('master_listing', 'longitude')
def found_by(self, geocacher):
session = object_session(self)
for listing in self.listings:
geocacher_account = session.query(GeocacherAccount).filter_by(geocacher=geocacher,
platform_prefix=listing.platform_prefix).scalar()
if geocacher_account:
found_logs = session.query(Log).filter_by(listing=listing,
geocacher_account=geocacher_account,
type='Found it').count()
if found_logs > 0:
return True
return False
class GeocacherAccount(Base):
__tablename__ = 'geocacher_accounts'
......@@ -128,11 +162,16 @@ class GeocacherAccount(Base):
platform_prefix = Column(String(2))
geocacher_id = Column(Integer, ForeignKey('geocachers.id'))
geocacher = relationship('Geocacher', back_populates='accounts')
geocacher = relationship('Geocacher', back_populates='accounts',
primaryjoin='GeocacherAccount.geocacher_id == Geocacher.id')
listings = relationship('Listing', back_populates='owner_account')
logs = relationship('Log', back_populates='geocacher_account')
master_of_id = Column(Integer, ForeignKey('geocachers.id'))
master_of = relationship('Geocacher', back_populates='master_account',
primaryjoin='GeocacherAccount.master_of_id == Geocacher.id')
@property
def found_listings(self):
session = object_session(self)
......@@ -144,4 +183,17 @@ class Geocacher(Base):
id = Column(Integer, primary_key=True)
accounts = relationship('GeocacherAccount', back_populates='geocacher')
master_account = relationship('GeocacherAccount', uselist=False,
primaryjoin='GeocacherAccount.master_of_id == Geocacher.id')
accounts = relationship('GeocacherAccount', back_populates='geocacher',
primaryjoin='GeocacherAccount.geocacher_id == Geocacher.id')
name = association_proxy('master_account', 'name')
@property
def found_geocaches(self):
session = object_session(self)
found_caches = []
for account in self.accounts:
found_caches += [listing.geocache for listing in account.found_listings]
return set(found_caches)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment