move collection list filtering logic to db query
-from django.http import HttpResponseRedirect
+from django.http import HttpResponseRedirect, HttpResponseForbidden
+from django.db.models import Q
from mezzanine.pages.page_processors import processor_for
from hs_core import page_processors
@@ -13,20 +14,6 @@ def landing_page(request, page):
content_model = page.get_content_model()
edit_resource = page_processors.check_resource_mode(request)
- user = request.user
- if user.is_authenticated():
- user_all_accessible_resource_list = get_my_resources_list(user)
- else: # anonymous user
- user_all_accessible_resource_list = list(BaseResource.discoverable_resources.all())
-
- # resource is collectable if
- # 1) Shareable=True
- # 2) OR, current user is a owner of it
- user_all_collectable_resource_list = []
- for res in user_all_accessible_resource_list:
- if res.raccess.shareable or res.raccess.owners.filter(pk=user.pk).exists():
- user_all_collectable_resource_list.append(res)
-
# current contained resources list
collection_items_list = list(content_model.resources.all())
@@ -36,16 +23,20 @@ def landing_page(request, page):
extended_metadata_layout=None,
request=request)
if edit_resource:
- candidate_resources_list = []
- for res in user_all_collectable_resource_list:
- if content_model.short_id == res.short_id:
- continue # skip current collection resource object
- elif res in content_model.resources.all():
- continue # skip resources that are already in current collection
+ user = request.user
+ if not user.is_authenticated():
+ return HttpResponseForbidden();
+ user_all_accessible_resource_list = get_my_resources_list(user)
- candidate_resources_list.append(res)
+ # resource is collectable if
+ # 1) Shareable=True
+ # 2) OR, current user is a owner of it
+ # 3) exclude this resource as well as resources already in the collection
+ user_all_accessible_resource_list.exclude(short_id=content_model.short_id)\
+ .exclude(id__in=content_model.resources.all())\
+ .exclude(Q(raccess__shareable=False) | Q(raccess__owners__contains=user.pk))
- context['collection_candidate'] = candidate_resources_list
+ context['collection_candidate'] = user_all_accessible_resource_list.all()
context['collection_res_id'] = content_model.short_id
elif isinstance(context, HttpResponseRedirect):
# resource view mode
