Move DBModel._id -> DBModel.id
We were refering to model._id in most of the code base as this is what Mongo uses. However, each use of _id required a) fixup of queries: e.g. what we did in our find() and find_one() functions moving all '_id' to 'id'. It also required using AliasFields to make the ._id attribute available. This all means lots of superfluous fixing and transitioning in a SQL world. It will also not work in the long run. Much newer code already refers to the objects by model.id (e.g. in the oauth plugin), which will break with Mongo. So let's be honest, rip out the _id mongoism and live with .id as the one canonical way to address objects. This commit modifies all users and providers of model._id to use model.id instead. This patch works with or without Mongo removed first, but will break Mongo usage (even more than before) I have not bothered to fixup db.mongo.* and db.sql.convert (which converts from Mongo to SQL) Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
This commit is contained in:
@@ -99,14 +99,14 @@ class MediaEntryMixin(object):
|
||||
|
||||
@property
|
||||
def slug_or_id(self):
|
||||
return (self.slug or self._id)
|
||||
return (self.slug or self.id)
|
||||
|
||||
|
||||
def url_for_self(self, urlgen, **extra_args):
|
||||
"""
|
||||
Generate an appropriate url for ourselves
|
||||
|
||||
Use a slug if we have one, else use our '_id'.
|
||||
Use a slug if we have one, else use our 'id'.
|
||||
"""
|
||||
uploader = self.get_uploader
|
||||
|
||||
@@ -208,13 +208,13 @@ class CollectionMixin(object):
|
||||
|
||||
@property
|
||||
def slug_or_id(self):
|
||||
return (self.slug or self._id)
|
||||
return (self.slug or self.id)
|
||||
|
||||
def url_for_self(self, urlgen, **extra_args):
|
||||
"""
|
||||
Generate an appropriate url for ourselves
|
||||
|
||||
Use a slug if we have one, else use our '_id'.
|
||||
Use a slug if we have one, else use our 'id'.
|
||||
"""
|
||||
creator = self.get_creator
|
||||
|
||||
|
||||
@@ -42,28 +42,15 @@ class GMGQuery(Query):
|
||||
Session = scoped_session(sessionmaker(query_cls=GMGQuery))
|
||||
|
||||
|
||||
def _fix_query_dict(query_dict):
|
||||
if '_id' in query_dict:
|
||||
query_dict['id'] = query_dict.pop('_id')
|
||||
|
||||
|
||||
class GMGTableBase(object):
|
||||
query = Session.query_property()
|
||||
|
||||
@classmethod
|
||||
def find(cls, query_dict=None):
|
||||
if query_dict is None:
|
||||
query_dict = {}
|
||||
|
||||
_fix_query_dict(query_dict)
|
||||
def find(cls, query_dict):
|
||||
return cls.query.filter_by(**query_dict)
|
||||
|
||||
@classmethod
|
||||
def find_one(cls, query_dict=None):
|
||||
if query_dict is None:
|
||||
query_dict = {}
|
||||
|
||||
_fix_query_dict(query_dict)
|
||||
def find_one(cls, query_dict):
|
||||
return cls.query.filter_by(**query_dict).first()
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -83,8 +83,6 @@ class User(Base, UserMixin):
|
||||
## TODO
|
||||
# plugin data would be in a separate model
|
||||
|
||||
_id = SimpleFieldAlias("id")
|
||||
|
||||
def __repr__(self):
|
||||
return '<{0} #{1} {2} {3} "{4}">'.format(
|
||||
self.__class__.__name__,
|
||||
@@ -161,8 +159,6 @@ class MediaEntry(Base, MediaEntryMixin):
|
||||
# media_data
|
||||
# fail_error
|
||||
|
||||
_id = SimpleFieldAlias("id")
|
||||
|
||||
def get_comments(self, ascending=False):
|
||||
order_col = MediaComment.created
|
||||
if not ascending:
|
||||
@@ -359,8 +355,6 @@ class MediaComment(Base, MediaCommentMixin):
|
||||
|
||||
get_author = relationship(User)
|
||||
|
||||
_id = SimpleFieldAlias("id")
|
||||
|
||||
|
||||
class Collection(Base, CollectionMixin):
|
||||
__tablename__ = "core__collections"
|
||||
@@ -383,8 +377,6 @@ class Collection(Base, CollectionMixin):
|
||||
return CollectionItem.query.filter_by(
|
||||
collection=self.id).order_by(order_col)
|
||||
|
||||
_id = SimpleFieldAlias("id")
|
||||
|
||||
|
||||
class CollectionItem(Base, CollectionItemMixin):
|
||||
__tablename__ = "core__collection_items"
|
||||
@@ -400,8 +392,6 @@ class CollectionItem(Base, CollectionItemMixin):
|
||||
|
||||
get_media_entry = relationship(MediaEntry)
|
||||
|
||||
_id = SimpleFieldAlias("id")
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint('collection', 'media_entry'),
|
||||
{})
|
||||
|
||||
Reference in New Issue
Block a user