diff options
Diffstat (limited to 'meetingtools/apps/content/views.py')
-rw-r--r-- | meetingtools/apps/content/views.py | 68 |
1 files changed, 27 insertions, 41 deletions
diff --git a/meetingtools/apps/content/views.py b/meetingtools/apps/content/views.py index 7bc0792..d300b86 100644 --- a/meetingtools/apps/content/views.py +++ b/meetingtools/apps/content/views.py @@ -1,29 +1,30 @@ # -*- coding: utf-8 -*- __author__ = 'lundberg' -from django.contrib.auth.decorators import login_required, permission_required -from django.db.models import Sum, Count -from django.contrib.auth.models import User -from django.contrib.humanize.templatetags.humanize import naturalday -from django.http import HttpResponseForbidden, HttpResponseBadRequest -from meetingtools.apps.stats.models import UserMeetingTransaction -from iso8601 import iso8601 -from time import mktime -from meetingtools.multiresponse import json_response, respond_to -from meetingtools.apps.stats.forms import StatCaledarForm +from django.contrib.auth.decorators import login_required +from django.db.models import Sum +from django.http import HttpResponseForbidden +from django.core.cache import cache +from meetingtools.multiresponse import respond_to from django.shortcuts import get_object_or_404 -from tagging.models import Tag, TaggedItem +from tagging.models import Tag from meetingtools.apps.content.models import Content from meetingtools.apps.cluster.models import ACCluster +from meetingtools.apps.content import tasks @login_required def user(request, username=None): if username is None: username = request.user.username - content = Content.objects.filter(creator__username=username) - total_bytecount = content.aggregate(Sum('bytecount')) - return respond_to(request,{'text/html': 'apps/content/user.html'}, + content = cache.get('%s-content' % username) + total_bytecount = cache.get('%s-bytecount' % username) + if not content or total_bytecount: + content = Content.objects.filter(creator__username=username) + total_bytecount = content.aggregate(Sum('bytecount')) + cache.set('%s-content' % username, content, 900) + cache.set('%s-bytecount' % username, total_bytecount, 900) + return respond_to(request, {'text/html': 'apps/content/user.html'}, {'username': username, 'content': content, 'total_bytecount': total_bytecount}) @@ -35,20 +36,12 @@ def cluster(request, cluster_name=None): clusters = ACCluster.objects.all().values('name') if cluster_name: - total_bytecount = 0 - domains = [] acc = get_object_or_404(ACCluster, name=cluster_name) - tags = Tag.objects.usage_for_model(Content, filters={'sco__acc': acc}) - for tag in sorted(tags): - if tag.name.startswith('domain:'): - qs = TaggedItem.objects.get_by_model(Content, tag) - d = { - 'domain': tag.name.split('domain:')[1], - 'domain_bytes': qs.aggregate(Sum('bytecount'))['bytecount__sum'], - 'number_of_files': len(qs) - } - total_bytecount += d['domain_bytes'] - domains.append(d) + domains = cache.get('%s-domains' % acc) + total_bytecount = cache.get('%s-bytecount' % acc) + if not domains or not total_bytecount: + domains, total_bytecount = tasks.get_cluster_content(acc) + return respond_to(request, {'text/html': 'apps/content/cluster.html'}, {'clusters': clusters, 'cluster_name': cluster_name, 'domains': domains, 'total_bytecount': total_bytecount}) @@ -63,20 +56,13 @@ def domain(request, domain_name): if not request.user.is_staff: return HttpResponseForbidden - users = [] - tag = get_object_or_404(Tag, name='domain:%s' % domain_name) - qs = TaggedItem.objects.get_by_model(Content, tag) - total_files = len(qs) - total_bytecount = qs.aggregate(Sum('bytecount'))['bytecount__sum'] - creators = qs.values('creator').annotate(num_files=Count('creator')) - for creator in creators: - domain_user = get_object_or_404(User, pk=creator['creator']) - d = { - 'username': domain_user.username, - 'number_of_files': creator['num_files'], - 'bytecount': Content.objects.filter(creator=domain_user).aggregate(Sum('bytecount'))['bytecount__sum'] - } - users.append(d) + domain_tag = get_object_or_404(Tag, name='domain:%s' % domain_name) + users = cache.get('%s-users' % domain_tag) + total_files = cache.get('%s-files' % domain_tag) + total_bytecount = cache.get('%s-bytecount' % domain_tag) + if not users or not total_files or not total_bytecount: + users, total_files, total_bytecount = tasks.get_domain_content(domain_tag) + return respond_to(request, {'text/html': 'apps/content/domain.html'}, {'domain': domain_name, 'total_bytecount': total_bytecount, 'total_files': total_files, 'users': users}) |