Ticket #451: trac#451-convert_mongokit.diff

File trac#451-convert_mongokit.diff, 9.8 KB (added by Ben Sturmfels, 11 years ago)

Replace transitional calls to 'first_one()' with 'filter_by().first()'.

  • mediagoblin/db/base.py

    diff --git a/mediagoblin/db/base.py b/mediagoblin/db/base.py
    index 699a503..bd4738b 100644
    a b class GMGTableBase(object):  
    2929        return cls.query.filter_by(**query_dict)
    3030
    3131    @classmethod
    32     def find_one(cls, query_dict):
    33         return cls.query.filter_by(**query_dict).first()
    34 
    35     @classmethod
    3632    def one(cls, query_dict):
    3733        return cls.find(query_dict).one()
    3834
  • mediagoblin/decorators.py

    diff --git a/mediagoblin/decorators.py b/mediagoblin/decorators.py
    index ece222f..ca7be53 100644
    a b def user_may_alter_collection(controller):  
    8787    """
    8888    @wraps(controller)
    8989    def wrapper(request, *args, **kwargs):
    90         creator_id = request.db.User.find_one(
    91             {'username': request.matchdict['user']}).id
     90        creator_id = request.db.User.query.filter_by(
     91            username=request.matchdict['user']).first().id
    9292        if not (request.user.is_admin or
    9393                request.user.id == creator_id):
    9494            raise Forbidden()
    def get_user_collection(controller):  
    162162    """
    163163    @wraps(controller)
    164164    def wrapper(request, *args, **kwargs):
    165         user = request.db.User.find_one(
    166             {'username': request.matchdict['user']})
     165        user = request.db.User.query.filter_by(
     166            username=request.matchdict['user']).first()
    167167
    168168        if not user:
    169169            return render_404(request)
    170170
    171         collection = request.db.Collection.find_one(
    172             {'slug': request.matchdict['collection'],
    173              'creator': user.id})
     171        collection = request.db.Collection.query.filter_by(
     172            slug=request.matchdict['collection'],
     173            creator=user.id).first()
    174174
    175175        # Still no collection?  Okay, 404.
    176176        if not collection:
    def get_user_collection_item(controller):  
    187187    """
    188188    @wraps(controller)
    189189    def wrapper(request, *args, **kwargs):
    190         user = request.db.User.find_one(
    191             {'username': request.matchdict['user']})
     190        user = request.db.User.query.filter_by(
     191            username=request.matchdict['user']).first()
    192192
    193193        if not user:
    194194            return render_404(request)
    195195
    196         collection_item = request.db.CollectionItem.find_one(
    197             {'id': request.matchdict['collection_item'] })
     196        collection_item = request.db.CollectionItem.query.filter_by(
     197            id=request.matchdict['collection_item']).first()
    198198
    199199        # Still no collection item?  Okay, 404.
    200200        if not collection_item:
  • mediagoblin/edit/views.py

    diff --git a/mediagoblin/edit/views.py b/mediagoblin/edit/views.py
    index 7a8d618..6aa2acd 100644
    a b def edit_collection(request, collection):  
    305305                form.slug.data, collection.id)
    306306
    307307        # Make sure there isn't already a Collection with this title
    308         existing_collection = request.db.Collection.find_one({
    309                 'creator': request.user.id,
    310                 'title':form.title.data})
     308        existing_collection = request.db.Collection.query.filter_by(
     309                creator=request.user.id,
     310                title=form.title.data).first()
    311311
    312312        if existing_collection and existing_collection.id != collection.id:
    313313            messages.add_message(
  • mediagoblin/submit/views.py

    diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py
    index 64e6791..11d0d06 100644
    a b def add_collection(request, media=None):  
    133133        collection.generate_slug()
    134134
    135135        # Make sure this user isn't duplicating an existing collection
    136         existing_collection = request.db.Collection.find_one({
    137                 'creator': request.user.id,
    138                 'title':collection.title})
     136        existing_collection = request.db.Collection.query.filter_by(
     137                creator=request.user.id,
     138                title=collection.title).first()
    139139
    140140        if existing_collection:
    141141            add_message(request, messages.ERROR,
  • mediagoblin/tests/test_auth.py

    diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py
    index 5bd8bf2..61503d3 100644
    a b def test_register_views(test_app):  
    9393    assert 'mediagoblin/user_pages/user.html' in template.TEMPLATE_TEST_CONTEXT
    9494
    9595    ## Make sure user is in place
    96     new_user = mg_globals.database.User.find_one(
    97         {'username': u'happygirl'})
     96    new_user = mg_globals.database.User.query.filter_by(
     97        username=u'happygirl').first()
    9898    assert new_user
    9999    assert new_user.status == u'needs_email_verification'
    100100    assert new_user.email_verified == False
    def test_register_views(test_app):  
    128128
    129129    # assert context['verification_successful'] == True
    130130    # TODO: Would be good to test messages here when we can do so...
    131     new_user = mg_globals.database.User.find_one(
    132         {'username': u'happygirl'})
     131    new_user = mg_globals.database.User.query.filter_by(
     132        username=u'happygirl').first()
    133133    assert new_user
    134134    assert new_user.status == u'needs_email_verification'
    135135    assert new_user.email_verified == False
    def test_register_views(test_app):  
    142142        'mediagoblin/user_pages/user.html']
    143143    # assert context['verification_successful'] == True
    144144    # TODO: Would be good to test messages here when we can do so...
    145     new_user = mg_globals.database.User.find_one(
    146         {'username': u'happygirl'})
     145    new_user = mg_globals.database.User.query.filter_by(
     146        username=u'happygirl').first()
    147147    assert new_user
    148148    assert new_user.status == u'active'
    149149    assert new_user.email_verified == True
  • mediagoblin/tests/test_edit.py

    diff --git a/mediagoblin/tests/test_edit.py b/mediagoblin/tests/test_edit.py
    index acc638d..d70d047 100644
    a b class TestUserEdit(object):  
    190190        assert urlparse.urlsplit(res.location)[2] == '/'
    191191
    192192        # Email shouldn't be saved
    193         email_in_db = mg_globals.database.User.find_one(
    194             {'email': 'new@example.com'})
     193        email_in_db = mg_globals.database.User.query.filter_by(
     194            email='new@example.com').first()
    195195        email = User.query.filter_by(username='chris').first().email
    196196        assert email_in_db is None
    197197        assert email == 'chris@example.com'
  • mediagoblin/tests/test_openid.py

    diff --git a/mediagoblin/tests/test_openid.py b/mediagoblin/tests/test_openid.py
    index c85f631..bba46db 100644
    a b class TestOpenIDPlugin(object):  
    186186            openid_plugin_app.get('/auth/logout')
    187187
    188188            # Get user and detach from session
    189             test_user = mg_globals.database.User.find_one({
    190                 'username': u'chris'})
     189            test_user = mg_globals.database.User.query.filter_by(
     190                username=u'chris').first()
    191191            Session.expunge(test_user)
    192192
    193193            # Log back in
    class TestOpenIDPlugin(object):  
    314314            assert 'mediagoblin/edit/edit_account.html' in template.TEMPLATE_TEST_CONTEXT
    315315
    316316            # OpenID Added?
    317             new_openid = mg_globals.database.OpenIDUserURL.find_one(
    318                 {'openid_url': u'http://add.myopenid.com'})
     317            new_openid = mg_globals.database.OpenIDUserURL.query.filter_by(
     318                openid_url=u'http://add.myopenid.com').first()
    319319            assert new_openid
    320320
    321321        _test_add()
    class TestOpenIDPlugin(object):  
    365365            assert 'mediagoblin/edit/edit_account.html' in template.TEMPLATE_TEST_CONTEXT
    366366
    367367            # OpenID deleted?
    368             new_openid = mg_globals.database.OpenIDUserURL.find_one(
    369                 {'openid_url': u'http://add.myopenid.com'})
     368            new_openid = mg_globals.database.OpenIDUserURL.query.filter_by(
     369                openid_url=u'http://add.myopenid.com').first()
    370370            assert not new_openid
    371371
    372372        _test_delete(self, test_user)
  • mediagoblin/tests/test_submission.py

    diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py
    index 162b2d1..45678a4 100644
    a b class TestSubmission:  
    240240
    241241        request = context['request']
    242242
    243         media = request.db.MediaEntry.find_one({
    244             u'title': u'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE'})
     243        media = request.db.MediaEntry.query.filter_by(
     244            title=u'UNIQUE_TITLE_PLS_DONT_CREATE_OTHER_MEDIA_WITH_THIS_TITLE').first()
    245245
    246246        assert media.media_type == 'mediagoblin.media_types.image'
    247247
    class TestSubmission:  
    252252        response, context = self.do_post({'title': title}, do_follow=True,
    253253                                         **self.upload_data(filename))
    254254        self.check_url(response, '/u/{0}/'.format(self.test_user.username))
    255         entry = mg_globals.database.MediaEntry.find_one({'title': title})
     255        entry = mg_globals.database.MediaEntry.query.filter_by(title=title).first()
    256256        assert entry.state == 'failed'
    257257        assert entry.fail_error == u'mediagoblin.processing:BadMediaFail'
    258258
  • mediagoblin/tests/tools.py

    diff --git a/mediagoblin/tests/tools.py b/mediagoblin/tests/tools.py
    index 2584c62..98361ad 100644
    a b def assert_db_meets_expected(db, expected):  
    164164    for collection_name, collection_data in expected.iteritems():
    165165        collection = db[collection_name]
    166166        for expected_document in collection_data:
    167             document = collection.find_one({'id': expected_document['id']})
     167            document = collection.query.filter_by(id=expected_document['id']).first()
    168168            assert document is not None  # make sure it exists
    169169            assert document == expected_document  # make sure it matches
    170170