Ticket #451: trac#451-convert_mongokit.2.diff

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

Replace transitional calls to 'find_one()', 'find()', and 'one()'.

  • mediagoblin/db/base.py

    diff --git a/mediagoblin/db/base.py b/mediagoblin/db/base.py
    index 699a503..c0cefdc 100644
    a b Session = scoped_session(sessionmaker())  
    2424class GMGTableBase(object):
    2525    query = Session.query_property()
    2626
    27     @classmethod
    28     def find(cls, query_dict):
    29         return cls.query.filter_by(**query_dict)
    30 
    31     @classmethod
    32     def find_one(cls, query_dict):
    33         return cls.query.filter_by(**query_dict).first()
    34 
    35     @classmethod
    36     def one(cls, query_dict):
    37         return cls.find(query_dict).one()
    38 
    3927    def get(self, key):
    4028        return getattr(self, key)
    4129
  • mediagoblin/db/util.py

    diff --git a/mediagoblin/db/util.py b/mediagoblin/db/util.py
    index 6ffec44..8431361 100644
    a b from mediagoblin.db.models import MediaEntry, Tag, MediaTag, Collection  
    2424
    2525
    2626def atomic_update(table, query_dict, update_values):
    27     table.find(query_dict).update(update_values,
     27    table.query.filter_by(**query_dict).update(update_values,
    2828        synchronize_session=False)
    2929    Session.commit()
    3030
  • 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/gmg_commands/import_export.py

    diff --git a/mediagoblin/gmg_commands/import_export.py b/mediagoblin/gmg_commands/import_export.py
    index d51a1e3..98ec617 100644
    a b def _import_media(db, args):  
    6363    # TODO: Add import of queue files
    6464    queue_cache = BasicFileStorage(args._cache_path['queue'])
    6565
    66     for entry in db.MediaEntry.find():
     66    for entry in db.MediaEntry.query.filter_by():
    6767        for name, path in entry.media_files.items():
    6868            _log.info('Importing: {0} - {1}'.format(
    6969                    entry.title.encode('ascii', 'replace'),
    def _export_media(db, args):  
    204204    # TODO: Add export of queue files
    205205    queue_cache = BasicFileStorage(args._cache_path['queue'])
    206206
    207     for entry in db.MediaEntry.find():
     207    for entry in db.MediaEntry.query.filter_by():
    208208        for name, path in entry.media_files.items():
    209209            _log.info(u'Exporting {0} - {1}'.format(
    210210                    entry.title,
  • mediagoblin/gmg_commands/users.py

    diff --git a/mediagoblin/gmg_commands/users.py b/mediagoblin/gmg_commands/users.py
    index 1f32945..e44b0aa 100644
    a b def adduser(args):  
    4040
    4141    db = mg_globals.database
    4242    users_with_username = \
    43         db.User.find({
    44             'username': args.username.lower(),
    45         }).count()
     43        db.User.query.filter_by(
     44            username=args.username.lower()
     45        ).count()
    4646
    4747    if users_with_username:
    4848        print u'Sorry, a user with that name already exists.'
    def makeadmin(args):  
    7171
    7272    db = mg_globals.database
    7373
    74     user = db.User.one({'username': unicode(args.username.lower())})
     74    user = db.User.query.filter_by(
     75        username=unicode(args.username.lower())).one()
    7576    if user:
    7677        user.is_admin = True
    7778        user.save()
    def changepw(args):  
    9495
    9596    db = mg_globals.database
    9697
    97     user = db.User.one({'username': unicode(args.username.lower())})
     98    user = db.User.query.filter_by(
     99        username=unicode(args.username.lower())).one()
    98100    if user:
    99101        user.pw_hash = auth.gen_password_hash(args.password)
    100102        user.save()
  • 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..2ac167c 100644
    a b class TestSubmission:  
    7777        return {'upload_files': [('file', filename)]}
    7878
    7979    def check_comments(self, request, media_id, count):
    80         comments = request.db.MediaComment.find({'media_entry': media_id})
     80        comments = request.db.MediaComment.query.filter_by(media_entry=media_id)
    8181        assert count == len(list(comments))
    8282
    8383    def test_missing_fields(self):
    class TestSubmission:  
    122122        assert 'mediagoblin/user_pages/user.html' in context
    123123
    124124    def check_media(self, request, find_data, count=None):
    125         media = MediaEntry.find(find_data)
     125        media = MediaEntry.query.filter_by(**find_data)
    126126        if count is not None:
    127127            assert media.count() == count
    128128            if count == 0:
    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