diff --git a/mediagoblin/db/base.py b/mediagoblin/db/base.py
index 699a503..c0cefdc 100644
a
|
b
|
Session = scoped_session(sessionmaker())
|
24 | 24 | class GMGTableBase(object): |
25 | 25 | query = Session.query_property() |
26 | 26 | |
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 | | |
39 | 27 | def get(self, key): |
40 | 28 | return getattr(self, key) |
41 | 29 | |
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
|
24 | 24 | |
25 | 25 | |
26 | 26 | def 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, |
28 | 28 | synchronize_session=False) |
29 | 29 | Session.commit() |
30 | 30 | |
diff --git a/mediagoblin/decorators.py b/mediagoblin/decorators.py
index ece222f..ca7be53 100644
a
|
b
|
def user_may_alter_collection(controller):
|
87 | 87 | """ |
88 | 88 | @wraps(controller) |
89 | 89 | 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 |
92 | 92 | if not (request.user.is_admin or |
93 | 93 | request.user.id == creator_id): |
94 | 94 | raise Forbidden() |
… |
… |
def get_user_collection(controller):
|
162 | 162 | """ |
163 | 163 | @wraps(controller) |
164 | 164 | 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() |
167 | 167 | |
168 | 168 | if not user: |
169 | 169 | return render_404(request) |
170 | 170 | |
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() |
174 | 174 | |
175 | 175 | # Still no collection? Okay, 404. |
176 | 176 | if not collection: |
… |
… |
def get_user_collection_item(controller):
|
187 | 187 | """ |
188 | 188 | @wraps(controller) |
189 | 189 | 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() |
192 | 192 | |
193 | 193 | if not user: |
194 | 194 | return render_404(request) |
195 | 195 | |
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() |
198 | 198 | |
199 | 199 | # Still no collection item? Okay, 404. |
200 | 200 | if not collection_item: |
diff --git a/mediagoblin/edit/views.py b/mediagoblin/edit/views.py
index 7a8d618..6aa2acd 100644
a
|
b
|
def edit_collection(request, collection):
|
305 | 305 | form.slug.data, collection.id) |
306 | 306 | |
307 | 307 | # 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() |
311 | 311 | |
312 | 312 | if existing_collection and existing_collection.id != collection.id: |
313 | 313 | messages.add_message( |
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):
|
63 | 63 | # TODO: Add import of queue files |
64 | 64 | queue_cache = BasicFileStorage(args._cache_path['queue']) |
65 | 65 | |
66 | | for entry in db.MediaEntry.find(): |
| 66 | for entry in db.MediaEntry.query.filter_by(): |
67 | 67 | for name, path in entry.media_files.items(): |
68 | 68 | _log.info('Importing: {0} - {1}'.format( |
69 | 69 | entry.title.encode('ascii', 'replace'), |
… |
… |
def _export_media(db, args):
|
204 | 204 | # TODO: Add export of queue files |
205 | 205 | queue_cache = BasicFileStorage(args._cache_path['queue']) |
206 | 206 | |
207 | | for entry in db.MediaEntry.find(): |
| 207 | for entry in db.MediaEntry.query.filter_by(): |
208 | 208 | for name, path in entry.media_files.items(): |
209 | 209 | _log.info(u'Exporting {0} - {1}'.format( |
210 | 210 | entry.title, |
diff --git a/mediagoblin/gmg_commands/users.py b/mediagoblin/gmg_commands/users.py
index 1f32945..e44b0aa 100644
a
|
b
|
def adduser(args):
|
40 | 40 | |
41 | 41 | db = mg_globals.database |
42 | 42 | 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() |
46 | 46 | |
47 | 47 | if users_with_username: |
48 | 48 | print u'Sorry, a user with that name already exists.' |
… |
… |
def makeadmin(args):
|
71 | 71 | |
72 | 72 | db = mg_globals.database |
73 | 73 | |
74 | | user = db.User.one({'username': unicode(args.username.lower())}) |
| 74 | user = db.User.query.filter_by( |
| 75 | username=unicode(args.username.lower())).one() |
75 | 76 | if user: |
76 | 77 | user.is_admin = True |
77 | 78 | user.save() |
… |
… |
def changepw(args):
|
94 | 95 | |
95 | 96 | db = mg_globals.database |
96 | 97 | |
97 | | user = db.User.one({'username': unicode(args.username.lower())}) |
| 98 | user = db.User.query.filter_by( |
| 99 | username=unicode(args.username.lower())).one() |
98 | 100 | if user: |
99 | 101 | user.pw_hash = auth.gen_password_hash(args.password) |
100 | 102 | user.save() |
diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py
index 64e6791..11d0d06 100644
a
|
b
|
def add_collection(request, media=None):
|
133 | 133 | collection.generate_slug() |
134 | 134 | |
135 | 135 | # 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() |
139 | 139 | |
140 | 140 | if existing_collection: |
141 | 141 | add_message(request, messages.ERROR, |
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):
|
93 | 93 | assert 'mediagoblin/user_pages/user.html' in template.TEMPLATE_TEST_CONTEXT |
94 | 94 | |
95 | 95 | ## 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() |
98 | 98 | assert new_user |
99 | 99 | assert new_user.status == u'needs_email_verification' |
100 | 100 | assert new_user.email_verified == False |
… |
… |
def test_register_views(test_app):
|
128 | 128 | |
129 | 129 | # assert context['verification_successful'] == True |
130 | 130 | # 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() |
133 | 133 | assert new_user |
134 | 134 | assert new_user.status == u'needs_email_verification' |
135 | 135 | assert new_user.email_verified == False |
… |
… |
def test_register_views(test_app):
|
142 | 142 | 'mediagoblin/user_pages/user.html'] |
143 | 143 | # assert context['verification_successful'] == True |
144 | 144 | # 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() |
147 | 147 | assert new_user |
148 | 148 | assert new_user.status == u'active' |
149 | 149 | assert new_user.email_verified == True |
diff --git a/mediagoblin/tests/test_edit.py b/mediagoblin/tests/test_edit.py
index acc638d..d70d047 100644
a
|
b
|
class TestUserEdit(object):
|
190 | 190 | assert urlparse.urlsplit(res.location)[2] == '/' |
191 | 191 | |
192 | 192 | # 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() |
195 | 195 | email = User.query.filter_by(username='chris').first().email |
196 | 196 | assert email_in_db is None |
197 | 197 | assert email == 'chris@example.com' |
diff --git a/mediagoblin/tests/test_openid.py b/mediagoblin/tests/test_openid.py
index c85f631..bba46db 100644
a
|
b
|
class TestOpenIDPlugin(object):
|
186 | 186 | openid_plugin_app.get('/auth/logout') |
187 | 187 | |
188 | 188 | # 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() |
191 | 191 | Session.expunge(test_user) |
192 | 192 | |
193 | 193 | # Log back in |
… |
… |
class TestOpenIDPlugin(object):
|
314 | 314 | assert 'mediagoblin/edit/edit_account.html' in template.TEMPLATE_TEST_CONTEXT |
315 | 315 | |
316 | 316 | # 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() |
319 | 319 | assert new_openid |
320 | 320 | |
321 | 321 | _test_add() |
… |
… |
class TestOpenIDPlugin(object):
|
365 | 365 | assert 'mediagoblin/edit/edit_account.html' in template.TEMPLATE_TEST_CONTEXT |
366 | 366 | |
367 | 367 | # 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() |
370 | 370 | assert not new_openid |
371 | 371 | |
372 | 372 | _test_delete(self, test_user) |
diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py
index 162b2d1..2ac167c 100644
a
|
b
|
class TestSubmission:
|
77 | 77 | return {'upload_files': [('file', filename)]} |
78 | 78 | |
79 | 79 | 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) |
81 | 81 | assert count == len(list(comments)) |
82 | 82 | |
83 | 83 | def test_missing_fields(self): |
… |
… |
class TestSubmission:
|
122 | 122 | assert 'mediagoblin/user_pages/user.html' in context |
123 | 123 | |
124 | 124 | def check_media(self, request, find_data, count=None): |
125 | | media = MediaEntry.find(find_data) |
| 125 | media = MediaEntry.query.filter_by(**find_data) |
126 | 126 | if count is not None: |
127 | 127 | assert media.count() == count |
128 | 128 | if count == 0: |
… |
… |
class TestSubmission:
|
240 | 240 | |
241 | 241 | request = context['request'] |
242 | 242 | |
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() |
245 | 245 | |
246 | 246 | assert media.media_type == 'mediagoblin.media_types.image' |
247 | 247 | |
… |
… |
class TestSubmission:
|
252 | 252 | response, context = self.do_post({'title': title}, do_follow=True, |
253 | 253 | **self.upload_data(filename)) |
254 | 254 | 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() |
256 | 256 | assert entry.state == 'failed' |
257 | 257 | assert entry.fail_error == u'mediagoblin.processing:BadMediaFail' |
258 | 258 | |
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):
|
164 | 164 | for collection_name, collection_data in expected.iteritems(): |
165 | 165 | collection = db[collection_name] |
166 | 166 | 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() |
168 | 168 | assert document is not None # make sure it exists |
169 | 169 | assert document == expected_document # make sure it matches |
170 | 170 | |