Revision 883c1f94 snf-cyclades-app/synnefo/api/subnets.py

b/snf-cyclades-app/synnefo/api/subnets.py
37 37
from django.conf.urls import patterns
38 38
from django.http import HttpResponse
39 39
from django.utils import simplejson as json
40
from django.db.models import Q
40 41

  
41 42
from snf_django.lib.api import utils
42
#from synnefo.db.models import Subnet
43
from synnefo.db.models import Subnet
43 44
from synnefo.logic import subnets
44 45
from synnefo.api import util
45 46

  
......
77 78
@api.api_method(http_method='GET', user_required=True, logger=log)
78 79
def list_subnets(request):
79 80
    """List all subnets of a user"""
80
    subnet_list = subnets.list_subnets(request.user_uniq)
81
    subnets_dict = [subnet_to_dict(sub)
82
                    for sub in subnet_list.order_by('id')]
81
    userid = request.user_uniq
82
    subnets_list = Subnet.objects.filter(Q(network__public=True) |
83
                                         (Q(network__userid=userid) &
84
                                          Q(network__public=False)))\
85
                                 .order_by("id")
86
    subnets_list = subnets_list.prefetch_related("ip_pools")\
87
                               .select_related("network")
88
    subnets_list = api.utils.filter_modified_since(request,
89
                                                   objects=subnets_list)
90

  
91
    subnets_dict = [subnet_to_dict(sub) for sub in subnets_list]
83 92

  
84 93
    data = json.dumps({'subnets': subnets_dict})
85 94

  
......
155 164
def get_subnet(request, sub_id):
156 165
    """Show info of a specific subnet"""
157 166
    user_id = request.user_uniq
158
    subnet = subnets.get_subnet(sub_id)
167
    subnet = subnets.get_subnet(sub_id, prefetch_related=True)
159 168

  
160 169
    if (subnet.network.userid != user_id) and (subnet.network.public is False):
161 170
        raise api.faults.Unauthorized("You're not allowed to view this subnet")
......
210 219

  
211 220
    network = subnet.network
212 221
    d = {'id': str(subnet.id),
213
         'network_id': str(network.id),
222
         'network_id': str(subnet.network_id),
214 223
         'name': subnet.name if subnet.name is not None else "",
215 224
         'tenant_id': network.userid,
216 225
         'user_id': network.userid,

Also available in: Unified diff