app/soc/views/models/host.py
changeset 725 6180b32d990f
parent 716 26dc12dae79f
child 744 cd9bf163473c
--- a/app/soc/views/models/host.py	Fri Dec 12 00:35:28 2008 +0000
+++ b/app/soc/views/models/host.py	Fri Dec 12 00:35:51 2008 +0000
@@ -22,7 +22,9 @@
   ]
 
 
+from soc.logic import accounts
 from soc.logic import dicts
+from soc.logic.models import host as host_logic
 from soc.logic.models import user as user_logic
 from soc.logic.models import sponsor as sponsor_logic
 from soc.views import helper
@@ -35,6 +37,8 @@
 import soc.views.helper
 import soc.views.models.sponsor
 
+tolist = list
+
 
 class CreateForm(helper.forms.BaseForm):
   """Django form displayed when creating a Host.
@@ -88,6 +92,7 @@
     rights = {}
     rights['create'] = [access.checkIsHost]
     rights['edit'] = [access.checkIsHost]
+    rights['list'] = [access.checkIsHost]
 
     new_params = {}
     new_params['rights'] = rights
@@ -112,6 +117,27 @@
 
     super(View, self).__init__(params=params)
 
+  def list(self, request, access_type,
+               page_name=None, params=None, filter=None):
+    """See base.View.list
+
+    Passes a filter to base.View.list so that only hosts from a sponsor
+    that this user is host for are listed.
+    """
+
+    user = user_logic.logic.getForCurrentAccount()
+
+    # Don't bother looking up everything if there's no user
+    if user and (not accounts.isDeveloper(user=user)):
+      hosts = host_logic.logic.getForFields({'user': user})
+      sponsors = tolist((host.scope for host in hosts))
+
+      new_filter = {'scope': sponsors}
+      filter = dicts.merge(filter, new_filter)
+
+    return super(View, self).list(request, access_type, page_name=page_name,
+                                  params=params, filter=filter)
+
   def _editPost(self, request, entity, fields):
     """See base.View._editPost().
     """