Store collection's parent in database (bug #77)

This commit is contained in:
Guillaume Ayoub 2013-10-31 16:07:37 +01:00
parent a920518a26
commit 0c2c9c3a7e

View File

@ -45,10 +45,7 @@ class DBCollection(Base):
__tablename__ = "collection" __tablename__ = "collection"
path = Column(String, primary_key=True) path = Column(String, primary_key=True)
parent_path = Column(String, ForeignKey("collection.path")) parent_path = Column(String)
parent = relationship(
"DBCollection", backref="children", remote_side=[path])
class DBItem(Base): class DBItem(Base):
@ -152,6 +149,7 @@ class Collection(ical.Collection):
else: else:
db_collection = DBCollection() db_collection = DBCollection()
db_collection.path = self.path db_collection.path = self.path
db_collection.parent_path = "/".join(self.path.split("/")[:-1])
self.session.add(db_collection) self.session.add(db_collection)
for header in headers: for header in headers:
@ -197,10 +195,9 @@ class Collection(ical.Collection):
@classmethod @classmethod
def children(cls, path): def children(cls, path):
session = Session() session = Session()
if path: children = (
children = session.query(DBCollection).get(path).children session.query(DBCollection)
else: .filter_by(parent_path=path or "").all())
children = session.query(DBCollection).filter_by(parent=None).all()
collections = [cls(child.path) for child in children] collections = [cls(child.path) for child in children]
session.close() session.close()
return collections return collections
@ -212,7 +209,7 @@ class Collection(ical.Collection):
session = Session() session = Session()
result = ( result = (
session.query(DBCollection) session.query(DBCollection)
.filter_by(parent_path=path).count() > 0) .filter_by(parent_path=path or "").count() > 0)
session.close() session.close()
return result return result
@ -223,7 +220,7 @@ class Collection(ical.Collection):
session = Session() session = Session()
result = ( result = (
session.query(DBItem) session.query(DBItem)
.filter_by(collection_path=path).count() > 0) .filter_by(collection_path=path or "").count() > 0)
session.close() session.close()
return result return result