Commit f4df4a4f authored by Sofia Papagiannaki's avatar Sofia Papagiannaki
Browse files

Reduce database interaction in group_detail

parent a9b7a981
......@@ -182,20 +182,19 @@ class AstakosGroup(Group):
@property
def members(self):
l = []
for m in self.membership_set.all():
m.person.is_approved = m.is_approved
l.append(m.person)
return l
q = self.membership_set.select_related().all()
return [m.person for m in q]
@property
def approved_members(self):
return [m.person for m in self.membership_set.all() if m.is_approved]
q = self.membership_set.select_related().all()
return [m.person for m in q if m.is_approved]
@property
def quota(self):
d = defaultdict(int)
for q in self.astakosgroupquota_set.all():
query = self.astakosgroupquota_set.select_related().all()
for q in query:
d[q.resource] += q.uplimit
return d
......@@ -203,6 +202,10 @@ class AstakosGroup(Group):
def owners(self):
return self.owner.all()
@property
def owner_details(self):
return self.owner.select_related().all()
@owners.setter
def owners(self, l):
self.owner = l
......
......@@ -3,6 +3,8 @@
{% load filters %}
{% block page.body %}
{% with object.owners as owners %}
{% with object.quota as quota %}
<div class="projects">
<h2>
......@@ -63,7 +65,8 @@
<dt>Activated</dt>
<dd>{% if object.is_enabled %}Yes{% else %}No{% endif %}</dd>
<dt>Owner</dt>
<dd>{% for o in object.owner.all %}
{{ o.owners }}
<dd>{% for o in owners %}
{% if user == o %}
Me
{% else%}
......@@ -77,13 +80,13 @@
<div class="full-dotted">
<h3>RESOURCES:</h3>
{% if object.quota %}
{% if quota %}
<dl class="alt-style">
{% for k in object.quota|dkeys %}
{% for k in quota|dkeys %}
<dt>{{ k }}</dt>
<dd>{{ object.quota|lookup:k }}&nbsp;</dd>
<dd>{{ quota|lookup:k }}&nbsp;</dd>
{% endfor %}
</dl>
......@@ -119,14 +122,14 @@
<tr>
<td>{{p.email}}</td>
<td>{{p.realname}}</td>
{% if p in object.owner.all %}
{% if p in owners %}
<td>Owner</td>
{% else %}
{% if p.is_approved %}
<td>Approved</td>
{% else %}
<td>Pending
{% if user in object.owner.all %}
{% if o.is_owner %}
<a href="{% url approve_member m.group.id m.person.id %}">Accept</a>
<a href="{% url disapprove_member m.group.id m.person.id %}">Reject</a>
{% endif %}
......@@ -176,4 +179,6 @@
</div>
{% endwith %}
{% endwith %}
{% endblock %}
......@@ -110,12 +110,6 @@ def paginate(l, args):
page = globals()['page'] = paginator.page(page_number)
return page
@register.filter
def concat(str1, str2):
if not str2:
return str(str1)
return '%s%s%s' % (str1, DELIM, str2)
@register.filter
def concat(str1, str2):
if not str2:
......
......@@ -792,7 +792,7 @@ def group_list(request):
@signed_terms_required
@login_required
def group_detail(request, group_id):
q = AstakosGroup.objects.filter(pk=group_id)
q = AstakosGroup.objects.select_related().filter(pk=group_id)
q = q.extra(select={
'is_member': """SELECT CASE WHEN EXISTS(
SELECT id FROM im_membership
......@@ -800,9 +800,9 @@ def group_detail(request, group_id):
AND person_id = %s)
THEN 1 ELSE 0 END""" % request.user.id,
'is_owner': """SELECT CASE WHEN EXISTS(
SELECT id FROM im_astakosuser_owner
WHERE astakosgroup_id = im_astakosgroup.group_ptr_id
AND astakosuser_id = %s)
SELECT id FROM im_astakosuser_owner
WHERE astakosgroup_id = im_astakosgroup.group_ptr_id
AND astakosuser_id = %s)
THEN 1 ELSE 0 END""" % request.user.id,
'kindname': """SELECT name FROM im_groupkind
WHERE id = im_astakosgroup.kind_id"""})
......@@ -812,8 +812,9 @@ def group_detail(request, group_id):
mimetype = None
try:
obj = q.get()
except ObjectDoesNotExist:
raise Http404("No %s found matching the query" % (model._meta.verbose_name))
except AstakosGroup.DoesNotExist:
raise Http404("No %s found matching the query" % (
model._meta.verbose_name))
update_form = AstakosGroupUpdateForm(instance=obj)
addmembers_form = AddGroupMembersForm()
......@@ -970,8 +971,7 @@ def group_leave(request, group_id):
try:
m = Membership.objects.select_related().get(
group__id=group_id,
person=request.user
)
person=request.user)
except Membership.DoesNotExist:
return HttpResponseBadRequest(_('Invalid membership.'))
if request.user in m.group.owner.all():
......@@ -983,9 +983,7 @@ def group_leave(request, group_id):
template_name='im/astakosgroup_list.html',
post_delete_redirect=reverse(
'group_detail',
kwargs=dict(group_id=group_id)
)
)
kwargs=dict(group_id=group_id)))
def handle_membership(func):
......@@ -994,8 +992,7 @@ def handle_membership(func):
try:
m = Membership.objects.select_related().get(
group__id=group_id,
person__id=user_id
)
person__id=user_id)
except Membership.DoesNotExist:
return HttpResponseBadRequest(_('Invalid membership.'))
else:
......@@ -1006,8 +1003,7 @@ def handle_membership(func):
template='im/astakosgroup_detail.html',
context_instance=get_context(request),
object=m.group,
quota=m.group.quota
)
quota=m.group.quota)
return wrapper
......@@ -1047,15 +1043,13 @@ def resource_list(request):
return render_response(
template='im/astakosuserquota_list.html',
context_instance=get_context(request),
quota=request.user.quota
)
quota=request.user.quota)
def group_create_list(request):
return render_response(
template='im/astakosgroup_create_list.html',
context_instance=get_context(request),
)
context_instance=get_context(request),)
@signed_terms_required
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment