Remove mongolisms from user_pages.view #451

This commit is contained in:
Sebastian Spaeth 2012-12-21 11:25:38 +01:00
parent f6bc033603
commit af008743ca
2 changed files with 45 additions and 53 deletions

View File

@ -27,6 +27,7 @@ add_route('mediagoblin.user_pages.media_confirm_delete',
'/u/<string:user>/m/<string:media>/confirm-delete/', '/u/<string:user>/m/<string:media>/confirm-delete/',
'mediagoblin.user_pages.views:media_confirm_delete') 'mediagoblin.user_pages.views:media_confirm_delete')
# Submission handling of new comments. TODO: only allow for POST methods
add_route('mediagoblin.user_pages.media_post_comment', add_route('mediagoblin.user_pages.media_post_comment',
'/u/<string:user>/m/<string:media>/comment/add/', '/u/<string:user>/m/<string:media>/comment/add/',
'mediagoblin.user_pages.views:media_post_comment') 'mediagoblin.user_pages.views:media_post_comment')

View File

@ -19,7 +19,8 @@ import datetime
from mediagoblin import messages, mg_globals from mediagoblin import messages, mg_globals
from mediagoblin.db.util import DESCENDING, ObjectId from mediagoblin.db.util import DESCENDING, ObjectId
from mediagoblin.db.sql.models import MediaEntry, Collection, CollectionItem from mediagoblin.db.sql.models import (MediaEntry, Collection, CollectionItem,
User)
from mediagoblin.tools.response import render_to_response, render_404, redirect from mediagoblin.tools.response import render_to_response, render_404, redirect
from mediagoblin.tools.translate import pass_to_ugettext as _ from mediagoblin.tools.translate import pass_to_ugettext as _
from mediagoblin.tools.pagination import Pagination from mediagoblin.tools.pagination import Pagination
@ -41,8 +42,10 @@ _log.setLevel(logging.DEBUG)
@uses_pagination @uses_pagination
def user_home(request, page): def user_home(request, page):
"""'Homepage' of a User()""" """'Homepage' of a User()"""
user = request.db.User.find_one({ # TODO: decide if we only want homepages for active users, we can
'username': request.matchdict['user']}) # then use the @get_active_user decorator and also simplify the
# template html.
user = User.query.filter_by(username=request.matchdict['user']).first()
if not user: if not user:
return render_404(request) return render_404(request)
elif user.status != u'active': elif user.status != u'active':
@ -51,9 +54,8 @@ def user_home(request, page):
'mediagoblin/user_pages/user.html', 'mediagoblin/user_pages/user.html',
{'user': user}) {'user': user})
cursor = request.db.MediaEntry.find( cursor = MediaEntry.query.filter_by(uploader = user.id).\
{'uploader': user.id, filter_by(state = u'processed').sort('created', DESCENDING)
'state': u'processed'}).sort('created', DESCENDING)
pagination = Pagination(page, cursor) pagination = Pagination(page, cursor)
media_entries = pagination() media_entries = pagination()
@ -82,6 +84,7 @@ def user_gallery(request, page, url_user=None):
cursor = MediaEntry.query.filter_by( cursor = MediaEntry.query.filter_by(
uploader=url_user.id, uploader=url_user.id,
state=u'processed').order_by(MediaEntry.created.desc()) state=u'processed').order_by(MediaEntry.created.desc())
# Paginate gallery # Paginate gallery
pagination = Pagination(page, cursor) pagination = Pagination(page, cursor)
media_entries = pagination() media_entries = pagination()
@ -107,7 +110,7 @@ def media_home(request, media, page, **kwargs):
""" """
'Homepage' of a MediaEntry() 'Homepage' of a MediaEntry()
""" """
if ObjectId(request.matchdict.get('comment')): if request.matchdict.get('comment', None):
pagination = Pagination( pagination = Pagination(
page, media.get_comments( page, media.get_comments(
mg_globals.app_config['comments_ascending']), mg_globals.app_config['comments_ascending']),
@ -437,18 +440,15 @@ def atom_feed(request):
""" """
generates the atom feed with the newest images generates the atom feed with the newest images
""" """
user = User.query.filter(User.username==request.matchdict['user']).\
user = request.db.User.find_one({ filter(User.status == u'active').first()
'username': request.matchdict['user'],
'status': u'active'})
if not user: if not user:
return render_404(request) return render_404(request)
cursor = request.db.MediaEntry.find({ cursor = MediaEntry.query.filter_by(MediaEntry.uploader == user.id).\
'uploader': user.id, filter_by(MediaEntry.state == u'processed').\
'state': u'processed'}) \ sort('created', DESCENDING).\
.sort('created', DESCENDING) \ limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
""" """
ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI) ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
@ -501,20 +501,18 @@ def collection_atom_feed(request):
""" """
generates the atom feed with the newest images from a collection generates the atom feed with the newest images from a collection
""" """
user = User.query.filter(User.username == request.matchdict['user']).\
user = request.db.User.find_one({ filter_by(User.status == u'active').first()
'username': request.matchdict['user'],
'status': u'active'})
if not user: if not user:
return render_404(request) return render_404(request)
collection = request.db.Collection.find_one({ collection = Collection.query.filter_by(
'creator': user.id, creator=user.id,
'slug': request.matchdict['collection']}) slug=request.matchdict['collection']).first()
cursor = request.db.CollectionItem.find({ cursor = CollectionItem.query.filter_by(
'collection': collection.id}) \ collection=collection.id) \
.sort('added', DESCENDING) \ .sort(CollectionItem.added.desc()) \
.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS) .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
""" """
@ -572,44 +570,37 @@ def processing_panel(request):
Show to the user what media is still in conversion/processing... Show to the user what media is still in conversion/processing...
and what failed, and why! and what failed, and why!
""" """
# Get the user user = User.query.filter_by(username=request.matchdict['user']).first()
user = request.db.User.find_one( # TODO: XXX: Should this be a decorator?
{'username': request.matchdict['user'],
'status': u'active'})
# Make sure the user exists and is active
if not user:
return render_404(request)
elif user.status != u'active':
return render_to_response(
request,
'mediagoblin/user_pages/user.html',
{'user': user})
# XXX: Should this be a decorator?
# #
# Make sure we have permission to access this user's panel. Only # Make sure we have permission to access this user's panel. Only
# admins and this user herself should be able to do so. # admins and this user herself should be able to do so.
if not (user.id == request.user.id if not (user == request.user
or request.user.is_admin): or request.user.is_admin):
# No? Let's simply redirect to this user's homepage then. # No? Simply redirect to this user's homepage.
print user
print request.user
return redirect( return redirect(
request, 'mediagoblin.user_pages.user_home', request, 'mediagoblin.user_pages.user_home',
user=request.matchdict['user']) user=user.username)
# Get media entries which are in-processing # Get media entries which are in-processing
processing_entries = request.db.MediaEntry.find( processing_entries = MediaEntry.query.\
{'uploader': user.id, filter(MediaEntry.uploader == user.id).\
'state': u'processing'}).sort('created', DESCENDING) filter(MediaEntry.state == u'processing').\
order_by(MediaEntry.created.desc())
# Get media entries which have failed to process # Get media entries which have failed to process
failed_entries = request.db.MediaEntry.find( failed_entries = MediaEntry.query.\
{'uploader': user.id, filter(MediaEntry.uploader == user.id).\
'state': u'failed'}).sort('created', DESCENDING) filter(MediaEntry.state == u'failed').\
order_by(MediaEntry.created.desc())
processed_entries = request.db.MediaEntry.find( processed_entries = MediaEntry.query.\
{'uploader': user.id, filter(MediaEntry.uploader == user.id).\
'state': u'processed'}).sort('created', DESCENDING).limit(10) filter(MediaEntry.state == u'processed').\
order_by(MediaEntry.created.desc()).\
limit(10)
# Render to response # Render to response
return render_to_response( return render_to_response(