From 71717fd5316607500159f782b10ca91cf9684bfd Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Tue, 25 Dec 2012 20:52:25 +0100 Subject: [PATCH] Remove ObjectId from the tree This was one of the last remaining Mongo holdouts and has been removed from the tree herewith. Good bye, ObjectId. Signed-off-by: Sebastian Spaeth --- mediagoblin/db/sql/fake.py | 9 --------- mediagoblin/db/util.py | 2 +- mediagoblin/decorators.py | 22 ++++++++++------------ mediagoblin/processing/task.py | 5 ++--- mediagoblin/tools/pagination.py | 2 +- mediagoblin/tools/request.py | 2 +- mediagoblin/user_pages/views.py | 4 ++-- 7 files changed, 17 insertions(+), 29 deletions(-) diff --git a/mediagoblin/db/sql/fake.py b/mediagoblin/db/sql/fake.py index bb91dbcb..51c5127b 100644 --- a/mediagoblin/db/sql/fake.py +++ b/mediagoblin/db/sql/fake.py @@ -21,17 +21,8 @@ calm the rest of the code base. Or provide super minimal implementations. Currently: -- ObjectId "class": It's a function mostly doing - int(init_arg) to convert string primary keys into - integer primary keys. - DESCENDING "constant" """ DESCENDING = object() # a unique object for this "constant" - - -def ObjectId(value=None): - if value is None: - return None - return int(value) diff --git a/mediagoblin/db/util.py b/mediagoblin/db/util.py index f14f79aa..b7e93134 100644 --- a/mediagoblin/db/util.py +++ b/mediagoblin/db/util.py @@ -15,6 +15,6 @@ # along with this program. If not, see . #TODO: check now after mongo removal if we can't rip out a layer of abstraction -from mediagoblin.db.sql.fake import ObjectId, DESCENDING +from mediagoblin.db.sql.fake import DESCENDING from mediagoblin.db.sql.util import atomic_update, check_media_slug_used, \ media_entries_for_tag_slug, check_collection_slug_used diff --git a/mediagoblin/decorators.py b/mediagoblin/decorators.py index ce5cb59c..f6169060 100644 --- a/mediagoblin/decorators.py +++ b/mediagoblin/decorators.py @@ -20,8 +20,7 @@ from urlparse import urljoin from werkzeug.exceptions import Forbidden from werkzeug.urls import url_quote -from mediagoblin.db.util import ObjectId -from mediagoblin.db.sql.models import User +from mediagoblin.db.sql.models import MediaEntry, User from mediagoblin.tools.response import redirect, render_404 @@ -70,8 +69,7 @@ def user_may_delete_media(controller): """ @wraps(controller) def wrapper(request, *args, **kwargs): - uploader_id = request.db.MediaEntry.find_one( - {'id': ObjectId(request.matchdict['media'])}).uploader + uploader_id = MediaEntry.query.get(request.matchdict['media']).uploader if not (request.user.is_admin or request.user.id == uploader_id): raise Forbidden() @@ -132,12 +130,12 @@ def get_user_media_entry(controller): 'state': u'processed', 'uploader': user.id}) - # no media via slug? Grab it via ObjectId + # no media via slug? Grab it via object id if not media: - media = request.db.MediaEntry.find_one( - {'id': ObjectId(request.matchdict['media']), - 'state': u'processed', - 'uploader': user.id}) + media = MediaEntry.query.filter_by( + id=request.matchdict['media'], + state=u'processed', + uploader=user.id).first() # Still no media? Okay, 404. if not media: return render_404(request) @@ -206,9 +204,9 @@ def get_media_entry_by_id(controller): """ @wraps(controller) def wrapper(request, *args, **kwargs): - media = request.db.MediaEntry.find_one( - {'id': ObjectId(request.matchdict['media']), - 'state': u'processed'}) + media = MediaEntry.query.filter_by( + id=request.matchdict['media'], + state=u'processed').first() # Still no media? Okay, 404. if not media: return render_404(request) diff --git a/mediagoblin/processing/task.py b/mediagoblin/processing/task.py index 06a26bb7..b7e761f2 100644 --- a/mediagoblin/processing/task.py +++ b/mediagoblin/processing/task.py @@ -19,7 +19,7 @@ import logging from celery.task import Task from mediagoblin import mg_globals as mgg -from mediagoblin.db.util import ObjectId +from mediagoblin.db.sql.models import MediaEntry from mediagoblin.processing import mark_entry_failed, BaseProcessingFail from mediagoblin.tools.processing import json_processing_callback @@ -41,8 +41,7 @@ class ProcessMedia(Task): Pass the media entry off to the appropriate processing function (for now just process_image...) """ - entry = mgg.database.MediaEntry.one( - {'id': ObjectId(media_id)}) + entry = MediaEntry.query.get(media_id) # Try to process, and handle expected errors. try: diff --git a/mediagoblin/tools/pagination.py b/mediagoblin/tools/pagination.py index 4c9b3118..d0f08c94 100644 --- a/mediagoblin/tools/pagination.py +++ b/mediagoblin/tools/pagination.py @@ -40,7 +40,7 @@ class Pagination(object): - page: requested page - per_page: number of objects per page - cursor: db cursor - - jump_to_id: ObjectId, sets the page to the page containing the + - jump_to_id: object id, sets the page to the page containing the object with id == jump_to_id. """ self.page = page diff --git a/mediagoblin/tools/request.py b/mediagoblin/tools/request.py index a58d919d..f40ab360 100644 --- a/mediagoblin/tools/request.py +++ b/mediagoblin/tools/request.py @@ -15,7 +15,7 @@ # along with this program. If not, see . import logging -from mediagoblin.db.util import ObjectId +from mediagoblin.db.sql.models import User _log = logging.getLogger(__name__) diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py index 28ef90d6..78c020ac 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -18,7 +18,7 @@ import logging import datetime from mediagoblin import messages, mg_globals -from mediagoblin.db.util import DESCENDING, ObjectId +from mediagoblin.db.util import DESCENDING from mediagoblin.db.sql.models import (MediaEntry, Collection, CollectionItem, User) from mediagoblin.tools.response import render_to_response, render_404, redirect @@ -116,7 +116,7 @@ def media_home(request, media, page, **kwargs): page, media.get_comments( mg_globals.app_config['comments_ascending']), MEDIA_COMMENTS_PER_PAGE, - ObjectId(request.matchdict.get('comment'))) + request.matchdict.get('comment')) else: pagination = Pagination( page, media.get_comments( -- 2.25.1