Revision 883c1f94

b/snf-cyclades-app/synnefo/api/networks.py
87 87

  
88 88
    user_networks = Network.objects.filter(Q(userid=request.user_uniq) |
89 89
                                           Q(public=True))\
90
                                   .prefetch_related("subnets")
90
                                   .order_by('id')
91
    if detail:
92
        user_networks = user_networks.prefetch_related("subnets")
91 93

  
92 94
    user_networks = api.utils.filter_modified_since(request,
93 95
                                                    objects=user_networks)
94 96

  
95 97
    network_dicts = [network_to_dict(network, detail)
96
                     for network in user_networks.order_by('id')]
98
                     for network in user_networks]
97 99

  
98 100
    if request.serialization == 'xml':
99 101
        data = render_to_string('list_networks.xml', {
......
167 169
    d = {'id': str(network.id), 'name': network.name}
168 170
    d['links'] = util.network_to_links(network.id)
169 171
    if detail:
172
        # Loop over subnets. Do not perform any extra query because of prefetch
173
        # related!
174
        subnet_ids = []
175
        for subnet in network.subnets.all():
176
            subnet_ids.append(network.id)
177

  
170 178
        state = "SNF:DRAINED" if network.drained else network.state
171 179
        d['user_id'] = network.userid
172 180
        d['tenant_id'] = network.userid
......
177 185
        d['public'] = network.public
178 186
        d['router:external'] = network.external_router
179 187
        d['admin_state_up'] = True
180
        d['subnets'] = list(network.subnets.values_list('id', flat=True))
188
        d['subnets'] = subnet_ids
181 189
        d['SNF:floating_ip_pool'] = network.floating_ip_pool
182 190
    return d
183 191

  
b/snf-cyclades-app/synnefo/api/ports.py
85 85

  
86 86
    user_ports = NetworkInterface.objects.filter(userid=request.user_uniq)
87 87

  
88
    if detail:
89
        user_ports = user_ports.prefetch_related("ips")
90

  
88 91
    port_dicts = [port_to_dict(port, detail)
89 92
                  for port in user_ports.order_by('id')]
90 93

  
......
246 249
        d['mac_address'] = port.mac
247 250
        d['status'] = port.state
248 251
        d['device_owner'] = port.device_owner
249
        d['network_id'] = str(port.network.id)
252
        d['network_id'] = str(port.network_id)
250 253
        d['updated'] = api.utils.isoformat(port.updated)
251 254
        d['created'] = api.utils.isoformat(port.created)
252 255
        d['fixed_ips'] = []
253 256
        for ip in port.ips.all():
254 257
            d['fixed_ips'].append({"ip_address": ip.address,
255
                                   "subnet": str(ip.subnet.id)})
256
        sg_list = list(port.security_groups.values_list('id', flat=True))
257
        d['security_groups'] = map(str, sg_list)
258
                                   "subnet": str(ip.subnet_id)})
259
        # Avoid extra queries until security groups are implemented!
260
        #sg_list = list(port.security_groups.values_list('id', flat=True))
261
        d['security_groups'] = []
258 262

  
259 263
    return d
260 264

  
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,
b/snf-cyclades-app/synnefo/logic/subnets.py
148 148
    return sub
149 149

  
150 150

  
151
def get_subnet(sub_id):
151
def get_subnet(sub_id, prefetch_related=False):
152 152
    """Show info of a specific subnet"""
153 153
    log.debug('get_subnet %s', sub_id)
154 154
    try:
155
        subnet = Subnet.objects.get(id=sub_id)
155
        subnets = Subnet.objects
156
        if prefetch_related:
157
            subnets = subnets.select_related("network")
158
            subnets = subnets.prefetch_related("ip_pools")
159
        return subnets.get(id=sub_id)
156 160
    except Subnet.DoesNotExist:
157 161
        raise api.faults.ItemNotFound("Subnet not found")
158 162

  
159
    return subnet
160

  
161 163

  
162 164
def delete_subnet():
163 165
    """Delete a subnet, raises BadRequest

Also available in: Unified diff