From: Elrond Date: Sun, 25 Dec 2011 15:01:59 +0000 (+0100) Subject: Allow .id instead of ._id for the Mongo backend X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=58f96a13e463fb417968cc92ec2ac5d4404641e4;p=mediagoblin.git Allow .id instead of ._id for the Mongo backend To allow easier migration to the SQLAlchemy style .id give the User and MediaEntry mongo classes an alias attribute of .id that maps to ['_id']. Use it in the upload process, because this was one of the last positions with a ['_id'] instead of ._id (due to a bug in mongokit). --- diff --git a/mediagoblin/db/mongo/models.py b/mediagoblin/db/mongo/models.py index 57af137d..99c7905d 100644 --- a/mediagoblin/db/mongo/models.py +++ b/mediagoblin/db/mongo/models.py @@ -25,6 +25,17 @@ from mediagoblin.tools.pagination import Pagination from mediagoblin.tools import url from mediagoblin.db.mixin import UserMixin, MediaEntryMixin, MediaCommentMixin + +class MongoPK(object): + """An alias for the _id primary key""" + def __get__(self, instance, cls): + return instance['_id'] + def __set__(self, instance, val): + instance['_id'] = val + def __delete__(self, instance): + del instance['_id'] + + ################### # Custom validators ################### @@ -87,6 +98,8 @@ class User(Document, UserMixin): 'status': u'needs_email_verification', 'is_admin': False} + id = MongoPK() + class MediaEntry(Document, MediaEntryMixin): """ @@ -205,6 +218,8 @@ class MediaEntry(Document, MediaEntryMixin): 'created': datetime.datetime.utcnow, 'state': u'unprocessed'} + id = MongoPK() + def get_comments(self, ascending=False): if ascending: order = ASCENDING diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py index df5b15c0..1e145e9d 100644 --- a/mediagoblin/submit/views.py +++ b/mediagoblin/submit/views.py @@ -59,7 +59,7 @@ def submit_start(request): # create entry and save in database entry = request.db.MediaEntry() - entry['_id'] = ObjectId() + entry.id = ObjectId() entry.media_type = unicode(media_type) entry.title = ( unicode(request.POST['title'])