Commits

Dan Watson committed eb046f3

Allow authors to view their own unpublished leaves

  • Participants
  • Parent commits 6e0458a

Comments (0)

Files changed (9)

leaves/helpers.py

 from leaves.models import Tag
-from leaves.utils import get_site
 
 def tags():
-	return Tag.objects.filter(sites=get_site())
+	return Tag.on_site.all()

leaves/managers.py

 	appropriate values for pub_date and exp_date.
 	"""
 
-	def active(self, select_related=True):
-		qs = filter_active_leaves(self)
+	def active(self, user=None, select_related=True):
+		qs = filter_active_leaves(self, user)
 		if select_related:
 			related = ['author_user', 'subclass']
 			if self.model.__name__ == 'Leaf':
+				# When querying on the abstract base class, pre-select all subclass tables too.
 				related += [c.__name__.lower() for c in get_leaf_classes()]
 			qs = qs.select_related(*related)
 		return qs
 
-	def stream(self, select_related=True):
-		return self.active(select_related).filter(subclass__in=config.stream_types.all(), stream=True)
+	def stream(self, user=None, select_related=True):
+		return self.active(user, select_related).filter(subclass__in=config.stream_types.all(), stream=True)

leaves/plugins/blog/helpers.py

 from leaves.plugins.blog.models import Category
-from leaves.utils import get_site
 
 def categories():
-	return Category.objects.filter(sites=get_site())
+	return Category.on_site.all()

leaves/plugins/blog/views.py

 from leaves.utils import get_page
 
 def view_post(request, year, month, day, slug):
-	post = get_object_or_404(Post.on_site.active(), pub_date__year=year, pub_date__month=month, pub_date__day=day, slug=slug)
+	post = get_object_or_404(Post.on_site.active(request.user), pub_date__year=year, pub_date__month=month, pub_date__day=day, slug=slug)
 	return view_leaf(request, post)
 
 def view_category(request, slug):

leaves/plugins/pages/helpers.py

 from leaves.plugins.pages.models import Page
-from leaves.utils import get_site
 
 def pages():
-	return Page.objects.filter(sites=get_site())
+	return Page.on_site.active()
 
 def navigable_pages():
-	return Page.objects.filter(sites=get_site(), navigable=True)
+	return Page.on_site.active().filter(navigable=True)

leaves/plugins/pages/views.py

 from leaves.views import view_leaf
 
 def view_page(request, slug):
-	page = get_object_or_404(Page.on_site.active(), slug=slug)
+	page = get_object_or_404(Page.on_site.active(request.user), slug=slug)
 	return view_leaf(request, page)

leaves/plugins/photos/views.py

 import os
 
 def view_photo(request, photo_id):
-	photo = get_object_or_404(Photo.on_site.active(), pk=photo_id)
+	photo = get_object_or_404(Photo.on_site.active(request.user), pk=photo_id)
 	return view_leaf(request, photo)
 
 def view_gallery(request, gallery_id):
-	gallery = get_object_or_404(Gallery.on_site.active(), pk=gallery_id)
+	gallery = get_object_or_404(Gallery.on_site.active(request.user), pk=gallery_id)
 	return view_leaf(request, gallery)
 
 def thumbnail(request, photo_id):
-	photo = get_object_or_404(Photo.on_site.active(), pk=photo_id)
+	photo = get_object_or_404(Photo.on_site.active(request.user), pk=photo_id)
 	try:
 		name = '%s_full.png' % photo_id
 		if 'width' in request.GET:
 		return HttpResponse('')
 
 def original(request, photo_id, filename):
-	photo = get_object_or_404(Photo.on_site.active(), pk=photo_id)
+	photo = get_object_or_404(Photo.on_site.active(request.user), pk=photo_id)
 	try:
 		name = photo.get_filename()
 		mime = mimetypes.guess_type(name)[0]
 helpers = HelperProxy()
 config = ConfigProxy()
 
-def filter_active_leaves(queryset):
+def filter_active_leaves(queryset, user=None):
 	"""
 	Filter the passed QuerySet object to only include published leaves with
 	appropriate values for pub_date and exp_date.
 	"""
 	now = datetime.datetime.now()
-	return queryset.filter(
-		models.Q(**{'pub_date__lte': now}),
-		models.Q(**{'exp_date__isnull': True}) | models.Q(**{'exp_date__gt': now}),
-		models.Q(**{'status': 'published'}),
-	)
+	q = models.Q(**{'exp_date__isnull': True}) | models.Q(**{'exp_date__gt': now})
+	q &= models.Q(**{'pub_date__lte': now})
+	q &= models.Q(**{'status': 'published'})
+	if user and user.is_authenticated():
+		q |= models.Q(**{'author_user': user})
+	return queryset.filter(q)
 
 def get_page(request, queryset, per_page=None):
 	"""
 	if isinstance(leaf_id, Leaf):
 		leaf = leaf_id.resolve()
 	else:
-		leaf = get_object_or_404(Leaf.on_site.active(), pk=leaf_id)
+		leaf = get_object_or_404(Leaf.on_site.active(request.user), pk=leaf_id)
 		leaf = leaf.resolve()
 	if request.method == 'GET':
 		leaf.num_views += 1