prefetch metadata elements for profile page
from json import dumps
+from django.contrib import messages
+from django.contrib.auth import authenticate, login as auth_login
from django.contrib.auth.decorators import login_required
-from django.core.exceptions import ValidationError, ObjectDoesNotExist, MultipleObjectsReturned
-from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
-from django.contrib.auth import authenticate, login as auth_login
-from django.views.generic import TemplateView
-from django.http import HttpResponse
-from django.shortcuts import redirect
from django.contrib.messages import info, error
-from django.utils.translation import ugettext_lazy as _
-from django.db.models import Q
+from django.core.exceptions import ValidationError, ObjectDoesNotExist, MultipleObjectsReturned
+from django.core.mail import send_mail
+from django.core.urlresolvers import reverse
from django.db import transaction
+from django.db.models import Q, Prefetch
+from django.db.models.query import prefetch_related_objects
+from django.http import HttpResponse
from django.http import HttpResponseRedirect
-from django.contrib import messages
-from django.core.mail import send_mail
-from django.utils.http import int_to_base36
+from django.shortcuts import redirect
+from django.shortcuts import render
from django.template.response import TemplateResponse
-
+from django.utils.http import int_to_base36
+from django.utils.translation import ugettext_lazy as _
+from django.views.generic import TemplateView
+from mezzanine.accounts.forms import LoginForm
from mezzanine.conf import settings
from mezzanine.generic.views import initial_validation
-from mezzanine.utils.views import set_cookie, is_spam
from mezzanine.utils.cache import add_cache_bypass
from mezzanine.utils.email import send_verification_mail, send_approve_mail, subject_template, \
default_token_generator, send_mail_template
from mezzanine.utils.urls import login_redirect, next_url
-from mezzanine.accounts.forms import LoginForm
-from django.shortcuts import render
+from mezzanine.utils.views import set_cookie, is_spam
-from hs_core.views.utils import run_ssh_command
+from hs_access_control.models import GroupMembershipRequest
from hs_core.hydroshare.utils import user_from_id
from hs_core.models import Party
-from hs_access_control.models import GroupMembershipRequest
+from hs_core.views.utils import run_ssh_command, get_metadata_contenttypes
from hs_dictionary.models import University, UncategorizedTerm
-from theme.forms import ThreadedCommentForm
from theme.forms import RatingForm, UserProfileForm, UserForm
+from theme.forms import ThreadedCommentForm
from theme.models import UserProfile
from theme.utils import get_quota_message
-
from .forms import SignupForm
@@ -90,10 +89,16 @@ class UserProfileView(TemplateView):
# get resource attributes used in profile page
resources = resources.only('title', 'resource_type', 'created')
# prefetch resource metadata elements
- resources = resources.prefetch_related('content_object___title')
- resources = resources.prefetch_related('content_object___description')
- resources = resources.prefetch_related('content_object__creators')
-
+ meta_contenttypes = get_metadata_contenttypes()
+ for ct in meta_contenttypes:
+ # get a list of resources having metadata that is an instance of a specific
+ # metadata class (e.g., CoreMetaData)
+ res_list = [res for res in resources if res.content_type == ct]
+ prefetch_related_objects(res_list,
+ Prefetch('content_object__creators'),
+ Prefetch('content_object___description'),
+ Prefetch('content_object___title')
+ )
return {
'profile_user': u,
'resources': resources,
