Dot-Notation for "_id"
authorElrond <elrond+mediagoblin.org@samba-tng.org>
Sun, 13 Nov 2011 18:25:06 +0000 (19:25 +0100)
committerElrond <elrond+mediagoblin.org@samba-tng.org>
Tue, 15 Nov 2011 10:32:13 +0000 (11:32 +0100)
Note: Migrations can't use "Dot Notation"!

Migrations run on pymongo, not mongokit.
So they can't use the "Dot Notation".
This isn't really a big issue, as migrations are anyway
quite mongo specific.

14 files changed:
mediagoblin/auth/lib.py
mediagoblin/auth/views.py
mediagoblin/db/models.py
mediagoblin/decorators.py
mediagoblin/edit/lib.py
mediagoblin/edit/views.py
mediagoblin/process_media/__init__.py
mediagoblin/submit/views.py
mediagoblin/templates/mediagoblin/user_pages/media.html
mediagoblin/templates/mediagoblin/user_pages/user.html
mediagoblin/tests/test_auth.py
mediagoblin/tests/test_submission.py
mediagoblin/tools/pagination.py
mediagoblin/user_pages/views.py

index 653424cc7af49ad95f13e47fdc47a259b0c77f0c..cf4a2b8331557f8e20b93c35fafebc26cbab5141 100644 (file)
@@ -109,7 +109,7 @@ def send_verification_email(user, request):
          'verification_url': EMAIL_VERIFICATION_TEMPLATE.format(
                 host=request.host,
                 uri=request.urlgen('mediagoblin.auth.verify_email'),
-                userid=unicode(user['_id']),
+                userid=unicode(user._id),
                 verification_key=user['verification_key'])})
 
     # TODO: There is no error handling in place
@@ -144,7 +144,7 @@ def send_fp_verification_email(user, request):
          'verification_url': EMAIL_FP_VERIFICATION_TEMPLATE.format(
                 host=request.host,
                 uri=request.urlgen('mediagoblin.auth.verify_forgot_password'),
-                userid=unicode(user['_id']),
+                userid=unicode(user._id),
                 fp_verification_key=user['fp_verification_key'])})
 
     # TODO: There is no error handling in place
index 8888d23cc9fda929489552f055fcde60f5013a02..8412b81c46f755510dd4811d20f2c900908d214b 100644 (file)
@@ -87,7 +87,7 @@ def register(request):
             user.save(validate=True)
 
             # log the user in
-            request.session['user_id'] = unicode(user['_id'])
+            request.session['user_id'] = unicode(user._id)
             request.session.save()
 
             # send verification email
@@ -122,7 +122,7 @@ def login(request):
 
         if user and user.check_login(request.POST['password']):
             # set up login in session
-            request.session['user_id'] = unicode(user['_id'])
+            request.session['user_id'] = unicode(user._id)
             request.session.save()
 
             if request.POST.get('next'):
index 65c15917294db2d0e0cb68b406c966d423ac35ac..1c1bc2fd9ab47aef1ced53fb44ad9fc8bdde77e3 100644 (file)
@@ -219,7 +219,7 @@ class MediaEntry(Document):
 
     def get_comments(self):
         return self.db.MediaComment.find({
-                'media_entry': self['_id']}).sort('created', DESCENDING)
+                'media_entry': self._id}).sort('created', DESCENDING)
 
     def get_display_media(self, media_map,
                           fetch_order=common.DISPLAY_IMAGE_FETCHING_ORDER):
@@ -250,7 +250,7 @@ class MediaEntry(Document):
             {'slug': self['slug']})
 
         if duplicate:
-            self['slug'] = "%s-%s" % (self['_id'], self['slug'])
+            self['slug'] = "%s-%s" % (self._id, self['slug'])
 
     def url_for_self(self, urlgen):
         """
@@ -269,13 +269,13 @@ class MediaEntry(Document):
             return urlgen(
                 'mediagoblin.user_pages.media_home',
                 user=uploader['username'],
-                media=unicode(self['_id']))
+                media=unicode(self._id))
 
     def url_to_prev(self, urlgen):
         """
         Provide a url to the previous entry from this user, if there is one
         """
-        cursor = self.db.MediaEntry.find({'_id': {"$gt": self['_id']},
+        cursor = self.db.MediaEntry.find({'_id': {"$gt": self._id},
                                           'uploader': self['uploader'],
                                           'state': 'processed'}).sort(
                                                     '_id', ASCENDING).limit(1)
@@ -288,7 +288,7 @@ class MediaEntry(Document):
         """
         Provide a url to the next entry from this user, if there is one
         """
-        cursor = self.db.MediaEntry.find({'_id': {"$lt": self['_id']},
+        cursor = self.db.MediaEntry.find({'_id': {"$lt": self._id},
                                           'uploader': self['uploader'],
                                           'state': 'processed'}).sort(
                                                     '_id', DESCENDING).limit(1)
index b247e229ad0200a58e89be59f1bd1b1f7c387aa3..8f7532ec1704d1967ee9766c34135fd99a05d157 100644 (file)
@@ -60,7 +60,7 @@ def user_may_delete_media(controller):
         uploader = request.db.MediaEntry.find_one(
             {'_id': ObjectId(request.matchdict['media'])}).uploader()
         if not (request.user['is_admin'] or
-                request.user['_id'] == uploader['_id']):
+                request.user._id == uploader._id):
             return exc.HTTPForbidden()
 
         return controller(request, *args, **kwargs)
@@ -99,7 +99,7 @@ def get_user_media_entry(controller):
         media = request.db.MediaEntry.find_one(
             {'slug': request.matchdict['media'],
              'state': 'processed',
-             'uploader': user['_id']})
+             'uploader': user._id})
 
         # no media via slug?  Grab it via ObjectId
         if not media:
@@ -107,7 +107,7 @@ def get_user_media_entry(controller):
                 media = request.db.MediaEntry.find_one(
                     {'_id': ObjectId(request.matchdict['media']),
                      'state': 'processed',
-                     'uploader': user['_id']})
+                     'uploader': user._id})
             except InvalidId:
                 return render_404(request)
 
index b722e9c111b6f54c6c4c47f9208ec79894db84d1..458b704e83128b7fa7fc3802e4f94eaf2e4d630c 100644 (file)
@@ -17,7 +17,7 @@
 
 def may_edit_media(request, media):
     """Check, if the request's user may edit the media details"""
-    if media['uploader'] == request.user['_id']:
+    if media['uploader'] == request.user._id:
         return True
     if request.user['is_admin']:
         return True
index f3ebebe8de7517022f0c38c3f9d550ddafd731f8..5f781552cdd6137e43f289fc738ab606256f6554 100644 (file)
@@ -57,7 +57,7 @@ def edit_media(request, media):
         existing_user_slug_entries = request.db.MediaEntry.find(
             {'slug': request.POST['slug'],
              'uploader': media['uploader'],
-             '_id': {'$ne': media['_id']}}).count()
+             '_id': {'$ne': media._id}}).count()
 
         if existing_user_slug_entries:
             form.slug.errors.append(
@@ -78,7 +78,7 @@ def edit_media(request, media):
                 location=media.url_for_self(request.urlgen))
 
     if request.user['is_admin'] \
-            and media['uploader'] != request.user['_id'] \
+            and media['uploader'] != request.user._id \
             and request.method != 'POST':
         messages.add_message(
             request, messages.WARNING,
@@ -104,7 +104,7 @@ def edit_attachments(request, media):
 
             attachment_public_filepath \
                 = mg_globals.public_store.get_unique_filepath(
-                ['media_entries', unicode(media['_id']), 'attachment',
+                ['media_entries', unicode(media._id), 'attachment',
                  secure_filename(request.POST['attachment_file'].filename)])
 
             attachment_public_file = mg_globals.public_store.get_file(
index 3d6b418f2e4fc983445247eb8d6b374025578ad8..34d83e54bd5df6268f23c4d39be52e80871c1847 100644 (file)
@@ -32,7 +32,7 @@ MEDIUM_SIZE = 640, 640
 def create_pub_filepath(entry, filename):
     return mgg.public_store.get_unique_filepath(
             ['media_entries',
-             unicode(entry['_id']),
+             unicode(entry._id),
              filename])
 
 
@@ -56,7 +56,7 @@ class ProcessMedia(Task):
         try:
             process_image(entry)
         except BaseProcessingFail, exc:
-            mark_entry_failed(entry[u'_id'], exc)
+            mark_entry_failed(entry._id, exc)
             return
 
         entry['state'] = u'processed'
index 25f7d79dc8d6172a8e8d34e35c6d3f350b20e43b..bd63bd188705a70008190f17a5f59debcbb63d86 100644 (file)
@@ -52,7 +52,7 @@ def submit_start(request):
 
             # create entry and save in database
             entry = request.db.MediaEntry()
-            entry['_id'] = ObjectId()
+            entry._id = ObjectId()
             entry['title'] = (
                 unicode(request.POST['title'])
                 or unicode(splitext(filename)[0]))
@@ -62,7 +62,7 @@ def submit_start(request):
                 entry['description'])
 
             entry['media_type'] = u'image'  # heh
-            entry['uploader'] = request.user['_id']
+            entry['uploader'] = request.user._id
 
             # Process the user's folksonomy "tags"
             entry['tags'] = convert_to_tag_list_of_dicts(
@@ -74,7 +74,7 @@ def submit_start(request):
             # Now store generate the queueing related filename
             queue_filepath = request.app.queue_store.get_unique_filepath(
                 ['media_entries',
-                 unicode(entry['_id']),
+                 unicode(entry._id),
                  secure_filename(filename)])
 
             # queue appropriately
@@ -105,7 +105,7 @@ def submit_start(request):
             # conditions with changes to the document via processing code)
             try:
                 process_media.apply_async(
-                    [unicode(entry['_id'])], {},
+                    [unicode(entry._id)], {},
                     task_id=task_id)
             except BaseException as exc:
                 # The purpose of this section is because when running in "lazy"
@@ -116,7 +116,7 @@ def submit_start(request):
                 #
                 # ... not completely the diaper pattern because the
                 # exception is re-raised :)
-                mark_entry_failed(entry[u'_id'], exc)
+                mark_entry_failed(entry._id, exc)
                 # re-raise the exception
                 raise
 
index 1e495b98f517433460f967c2478aa3f85ac6e3e6..4b02b684b4d4e30034258f18490ec55603dac690 100644 (file)
         {% for comment in comments %}
           {% set comment_author = comment.author() %}
            {% if pagination.active_id == comment._id %}
-              <div class="comment_wrapper comment_active" id="comment-{{ comment['_id'] }}">
+              <div class="comment_wrapper comment_active" id="comment-{{ comment._id }}">
                <a name="comment" id="comment"></a>
             {% else %}
-              <div class="comment_wrapper" id="comment-{{ comment['_id'] }}">
+              <div class="comment_wrapper" id="comment-{{ comment._id }}">
            {% endif %}
 
             <div class="comment_content">{% autoescape False %}{{ comment.content_html }}
@@ -83,7 +83,7 @@
                 {{ comment_author['username'] }}</a>
               {% trans %}at{% endtrans %} 
               <a href="{{ request.urlgen('mediagoblin.user_pages.media_home.view_comment',
-                      comment = comment['_id'],
+                      comment = comment._id,
                       user = media.uploader().username,
                       media = media._id) }}#comment">
                 {{ comment.created.strftime("%I:%M%p %Y-%m-%d") }}
     <div class="grid_5 omega">
       {% include "mediagoblin/utils/prev_next.html" %}
 
-      {% if media['uploader'] == request.user['_id'] or 
+      {% if media['uploader'] == request.user._id or 
                                  request.user['is_admin'] %}
         <h3>{% trans %}Actions{% endtrans %}</h3>
         <p>
       {% endif %}
 
       {% if app_config['allow_attachments']
-            and (media['uploader'] == request.user['_id']
+            and (media['uploader'] == request.user._id
                  or request.user['is_admin']) %}
         <p>
           <a href="{{ request.urlgen('mediagoblin.edit.attachments',
index d65da05510c7096ca9c135152be9bc5cdd6fd77f..1de7f611d516a9c31a5fb16147939bd18e7e8d2a 100644 (file)
@@ -90,7 +90,7 @@
     </h1>
 
     {% if not user['url'] and not user['profile'] %}
-      {% if request.user['_id'] == user['_id'] %}
+      {% if request.user._id == user._id %}
         <div class="grid_6 alpha empty_space">
           <p>
             {% trans %}Here's a spot to tell others about yourself.{% endtrans %}
     {% else %}
       <div class="grid_6 alpha">
         {% include "mediagoblin/utils/profile.html" %}
-        {% if request.user['_id'] == user['_id'] or request.user['is_admin'] %}
+        {% if request.user._id == user._id or request.user['is_admin'] %}
           <a href="{{ request.urlgen('mediagoblin.edit.profile') }}?username={{
                           user.username }}">
             {%- trans %}Edit profile{% endtrans -%}
         {% include "mediagoblin/utils/feed_link.html" %}
       </div>
     {% else %}
-      {% if request.user['_id'] == user['_id'] %}
+      {% if request.user._id == user._id %}
         <div class="grid_10 omega empty_space">
           <p>
             {% trans -%}
index 40961eca02621263837b222a8d6edc1b78e4bd3e..153c6e53824e88fca163c6090349544238222b11 100644 (file)
@@ -168,7 +168,7 @@ def test_register_views(test_app):
     ## Make sure user is logged in
     request = template.TEMPLATE_TEST_CONTEXT[
         'mediagoblin/user_pages/user.html']['request']
-    assert request.session['user_id'] == unicode(new_user['_id'])
+    assert request.session['user_id'] == unicode(new_user._id)
 
     ## Make sure we get email confirmation, and try verifying
     assert len(mail.EMAIL_TEST_INBOX) == 1
@@ -185,7 +185,7 @@ def test_register_views(test_app):
 
     ### user should have these same parameters
     assert parsed_get_params['userid'] == [
-        unicode(new_user['_id'])]
+        unicode(new_user._id)]
     assert parsed_get_params['token'] == [
         new_user['verification_key']]
 
@@ -193,7 +193,7 @@ def test_register_views(test_app):
     template.clear_test_template_context()
     response = test_app.get(
         "/auth/verify_email/?userid=%s&token=total_bs" % unicode(
-            new_user['_id']))
+            new_user._id))
     response.follow()
     context = template.TEMPLATE_TEST_CONTEXT[
         'mediagoblin/user_pages/user.html']
@@ -269,7 +269,7 @@ def test_register_views(test_app):
 
     # user should have matching parameters
     new_user = mg_globals.database.User.find_one({'username': 'happygirl'})
-    assert parsed_get_params['userid'] == [unicode(new_user['_id'])]
+    assert parsed_get_params['userid'] == [unicode(new_user._id)]
     assert parsed_get_params['token'] == [new_user['fp_verification_key']]
 
     ### The forgotten password token should be set to expire in ~ 10 days
@@ -280,7 +280,7 @@ def test_register_views(test_app):
     template.clear_test_template_context()
     response = test_app.get(
         "/auth/forgot_password/verify/?userid=%s&token=total_bs" % unicode(
-            new_user['_id']), status=400)
+            new_user._id), status=400)
     assert response.status == '400 Bad Request'
 
     ## Try using an expired token to change password, shouldn't work
@@ -412,7 +412,7 @@ def test_authentication_views(test_app):
     # Make sure user is in the session
     context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html']
     session = context['request'].session
-    assert session['user_id'] == unicode(test_user['_id'])
+    assert session['user_id'] == unicode(test_user._id)
 
     # Successful logout
     # -----------------
index 1c657e6c6606fdc063178177462a545abf217191..dec7118b76281ebef4d63b981f19c4c88e33b9bc 100644 (file)
@@ -177,7 +177,7 @@ class TestSubmission:
             request.urlgen('mediagoblin.user_pages.media_confirm_delete',
                            # No work: user=media.uploader().username,
                            user=self.test_user['username'],
-                           media=media['_id']),
+                           media=media._id),
             # no value means no confirm
             {})
 
@@ -197,7 +197,7 @@ class TestSubmission:
             request.urlgen('mediagoblin.user_pages.media_confirm_delete',
                            # No work: user=media.uploader().username,
                            user=self.test_user['username'],
-                           media=media['_id']),
+                           media=media._id),
             {'confirm': 'y'})
 
         response.follow()
@@ -208,7 +208,7 @@ class TestSubmission:
         # Does media entry still exist?
         assert_false(
             request.db.MediaEntry.find(
-                {'_id': media['_id']}).count())
+                {'_id': media._id}).count())
 
     def test_malicious_uploads(self):
         # Test non-suppoerted file with non-supported extension
index bc20ec909fd1df4ffca195fb4ce4d6acf316ae54..5ebc3c5a67d9eeb000f0540e203ff784261d4573 100644 (file)
@@ -53,7 +53,7 @@ class Pagination(object):
             cursor = copy.copy(self.cursor)
 
             for (doc, increment) in izip(cursor, count(0)):
-                if doc['_id'] == jump_to_id:
+                if doc._id == jump_to_id:
                     self.page = 1 + int(floor(increment / self.per_page))
 
                     self.active_id = jump_to_id
index 2090d6fda8a84d9e5aeefaba004a3fdd96adf676..82865bb412ffc722a9ef9e3172f639184abcc366 100644 (file)
@@ -45,7 +45,7 @@ def user_home(request, page):
             {'user': user})
 
     cursor = request.db.MediaEntry.find(
-        {'uploader': user['_id'],
+        {'uploader': user._id,
          'state': 'processed'}).sort('created', DESCENDING)
 
     pagination = Pagination(page, cursor)
@@ -78,7 +78,7 @@ def user_gallery(request, page):
         return render_404(request)
 
     cursor = request.db.MediaEntry.find(
-        {'uploader': user['_id'],
+        {'uploader': user._id,
          'state': 'processed'}).sort('created', DESCENDING)
 
     pagination = Pagination(page, cursor)
@@ -135,8 +135,8 @@ def media_post_comment(request, media):
     assert request.method == 'POST'
 
     comment = request.db.MediaComment()
-    comment['media_entry'] = media['_id']
-    comment['author'] = request.user['_id']
+    comment['media_entry'] = media._id
+    comment['author'] = request.user._id
     comment['content'] = unicode(request.POST['comment_content'])
     comment['content_html'] = cleaned_markdown_conversion(comment['content'])
 
@@ -179,7 +179,7 @@ def media_confirm_delete(request, media):
                 location=media.url_for_self(request.urlgen))
 
     if ((request.user[u'is_admin'] and
-         request.user[u'_id'] != media.uploader()[u'_id'])):
+         request.user._id != media.uploader()._id)):
         messages.add_message(
             request, messages.WARNING,
             _("You are about to delete another user's media. "
@@ -207,7 +207,7 @@ def atom_feed(request):
         return render_404(request)
 
     cursor = request.db.MediaEntry.find({
-                 'uploader': user['_id'],
+                 'uploader': user._id,
                  'state': 'processed'}) \
                  .sort('created', DESCENDING) \
                  .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
@@ -251,7 +251,7 @@ def processing_panel(request):
     #
     # Make sure we have permission to access this user's panel.  Only
     # admins and this user herself should be able to do so.
-    if not (user[u'_id'] == request.user[u'_id']
+    if not (user._id == request.user._id
             or request.user.is_admin):
         # No?  Let's simply redirect to this user's homepage then.
         return redirect(
@@ -260,12 +260,12 @@ def processing_panel(request):
 
     # Get media entries which are in-processing
     processing_entries = request.db.MediaEntry.find(
-        {'uploader': user['_id'],
+        {'uploader': user._id,
          'state': 'processing'}).sort('created', DESCENDING)
 
     # Get media entries which have failed to process
     failed_entries = request.db.MediaEntry.find(
-        {'uploader': user['_id'],
+        {'uploader': user._id,
          'state': 'failed'}).sort('created', DESCENDING)
 
     # Render to response