commit bdedfd9d79be7f1c00764b5d56a326d30b30b474
Author: Christos Stavrakakis <cstavr@grnet.gr>
Date:   Thu Dec 19 15:53:52 2013 +0200

    cyclades: Add 'userid' and 'public' to subnets
    
    Extend 'Subnet' model with 'userid' and 'public' attributes. This is
    required in order to avoid an expensive join when listing subnets. This
    commit also includes a database migration for existing subnets.

diff --git a/snf-cyclades-app/synnefo/api/subnets.py b/snf-cyclades-app/synnefo/api/subnets.py
index 1f9f615..738b9be 100644
--- a/snf-cyclades-app/synnefo/api/subnets.py
+++ b/snf-cyclades-app/synnefo/api/subnets.py
@@ -80,12 +80,11 @@ def subnet_demux(request, sub_id):
 def list_subnets(request, detail=True):
     """List all subnets of a user"""
     userid = request.user_uniq
-    subnets_list = Subnet.objects.filter(Q(network__public=True) |
-                                         (Q(network__userid=userid) &
-                                          Q(network__public=False)))\
+    subnets_list = Subnet.objects.filter(Q(public=True) |
+                                         (Q(userid=userid) &
+                                          Q(public=False)))\
                                  .order_by("id")
-    subnets_list = subnets_list.prefetch_related("ip_pools")\
-                               .select_related("network")
+    subnets_list = subnets_list.prefetch_related("ip_pools")
     subnets_list = api.utils.filter_modified_since(request,
                                                    objects=subnets_list)
 
@@ -215,12 +214,12 @@ def subnet_to_dict(subnet):
     allocation_pools = [render_ip_pool(pool)
                         for pool in subnet.ip_pools.all()]
 
-    network = subnet.network
     d = {'id': str(subnet.id),
          'network_id': str(subnet.network_id),
          'name': subnet.name if subnet.name is not None else "",
-         'tenant_id': network.userid,
-         'user_id': network.userid,
+         'tenant_id': subnet.userid,
+         'user_id': subnet.userid,
+         'public': subnet.public,
          'gateway_ip': subnet.gateway,
          'ip_version': subnet.ipversion,
          'cidr': subnet.cidr,
diff --git a/snf-cyclades-app/synnefo/db/migrations/0096_auto__add_field_subnet_userid__add_field_subnet_public.py b/snf-cyclades-app/synnefo/db/migrations/0096_auto__add_field_subnet_userid__add_field_subnet_public.py
new file mode 100644
index 0000000..9cbb78b
--- /dev/null
+++ b/snf-cyclades-app/synnefo/db/migrations/0096_auto__add_field_subnet_userid__add_field_subnet_public.py
@@ -0,0 +1,243 @@
+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        # Adding field 'Subnet.userid'
+        db.add_column('db_subnet', 'userid',
+                      self.gf('django.db.models.fields.CharField')(max_length=128, null=True, db_index=True),
+                      keep_default=False)
+
+        # Adding field 'Subnet.public'
+        db.add_column('db_subnet', 'public',
+                      self.gf('django.db.models.fields.BooleanField')(default=False, db_index=True),
+                      keep_default=False)
+
+
+    def backwards(self, orm):
+        # Deleting field 'Subnet.userid'
+        db.delete_column('db_subnet', 'userid')
+
+        # Deleting field 'Subnet.public'
+        db.delete_column('db_subnet', 'public')
+
+
+    models = {
+        'db.backend': {
+            'Meta': {'ordering': "['clustername']", 'object_name': 'Backend'},
+            'clustername': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '128'}),
+            'ctotal': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+            'dfree': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+            'disk_templates': ('synnefo.db.fields.SeparatedValuesField', [], {'null': 'True'}),
+            'drained': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'dtotal': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+            'hash': ('django.db.models.fields.CharField', [], {'max_length': '40'}),
+            'hypervisor': ('django.db.models.fields.CharField', [], {'default': "'kvm'", 'max_length': '32'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'index': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0', 'unique': 'True'}),
+            'mfree': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+            'mtotal': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+            'offline': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'password_hash': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True', 'blank': 'True'}),
+            'pinst_cnt': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+            'port': ('django.db.models.fields.PositiveIntegerField', [], {'default': '5080'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'username': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True', 'blank': 'True'})
+        },
+        'db.backendnetwork': {
+            'Meta': {'unique_together': "(('network', 'backend'),)", 'object_name': 'BackendNetwork'},
+            'backend': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'networks'", 'on_delete': 'models.PROTECT', 'to': "orm['db.Backend']"}),
+            'backendjobid': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}),
+            'backendjobstatus': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
+            'backendlogmsg': ('django.db.models.fields.TextField', [], {'null': 'True'}),
+            'backendopcode': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
+            'backendtime': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(1, 1, 1, 0, 0)'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'mac_prefix': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
+            'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'backend_networks'", 'on_delete': 'models.PROTECT', 'to': "orm['db.Network']"}),
+            'operstate': ('django.db.models.fields.CharField', [], {'default': "'PENDING'", 'max_length': '30'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'})
+        },
+        'db.bridgepooltable': {
+            'Meta': {'object_name': 'BridgePoolTable'},
+            'available_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
+            'base': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'offset': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
+            'reserved_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
+            'size': ('django.db.models.fields.IntegerField', [], {})
+        },
+        'db.flavor': {
+            'Meta': {'unique_together': "(('cpu', 'ram', 'disk', 'disk_template'),)", 'object_name': 'Flavor'},
+            'cpu': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'disk': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+            'disk_template': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'ram': ('django.db.models.fields.IntegerField', [], {'default': '0'})
+        },
+        'db.ipaddress': {
+            'Meta': {'unique_together': "(('network', 'address', 'deleted'),)", 'object_name': 'IPAddress'},
+            'address': ('django.db.models.fields.CharField', [], {'max_length': '64'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'floating_ip': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'ipversion': ('django.db.models.fields.IntegerField', [], {}),
+            'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'ips'", 'on_delete': 'models.PROTECT', 'to': "orm['db.Network']"}),
+            'nic': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'ips'", 'null': 'True', 'on_delete': 'models.SET_NULL', 'to': "orm['db.NetworkInterface']"}),
+            'serial': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'ips'", 'null': 'True', 'on_delete': 'models.SET_NULL', 'to': "orm['db.QuotaHolderSerial']"}),
+            'subnet': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'ips'", 'on_delete': 'models.PROTECT', 'to': "orm['db.Subnet']"}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+            'userid': ('django.db.models.fields.CharField', [], {'max_length': '128', 'db_index': 'True'})
+        },
+        'db.ipaddresslog': {
+            'Meta': {'object_name': 'IPAddressLog'},
+            'active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'address': ('django.db.models.fields.CharField', [], {'max_length': '64', 'db_index': 'True'}),
+            'allocated_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'network_id': ('django.db.models.fields.IntegerField', [], {}),
+            'released_at': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
+            'server_id': ('django.db.models.fields.IntegerField', [], {})
+        },
+        'db.ippooltable': {
+            'Meta': {'object_name': 'IPPoolTable'},
+            'available_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
+            'base': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'offset': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
+            'reserved_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
+            'size': ('django.db.models.fields.IntegerField', [], {}),
+            'subnet': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'ip_pools'", 'null': 'True', 'on_delete': 'models.PROTECT', 'to': "orm['db.Subnet']"})
+        },
+        'db.macprefixpooltable': {
+            'Meta': {'object_name': 'MacPrefixPoolTable'},
+            'available_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
+            'base': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'offset': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
+            'reserved_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
+            'size': ('django.db.models.fields.IntegerField', [], {})
+        },
+        'db.network': {
+            'Meta': {'object_name': 'Network'},
+            'action': ('django.db.models.fields.CharField', [], {'default': 'None', 'max_length': '32', 'null': 'True'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+            'drained': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'external_router': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'flavor': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
+            'floating_ip_pool': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'link': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
+            'mac_prefix': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
+            'machines': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['db.VirtualMachine']", 'through': "orm['db.NetworkInterface']", 'symmetrical': 'False'}),
+            'mode': ('django.db.models.fields.CharField', [], {'max_length': '16', 'null': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+            'public': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+            'serial': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'network'", 'null': 'True', 'on_delete': 'models.SET_NULL', 'to': "orm['db.QuotaHolderSerial']"}),
+            'state': ('django.db.models.fields.CharField', [], {'default': "'PENDING'", 'max_length': '32'}),
+            'tags': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+            'userid': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True', 'db_index': 'True'})
+        },
+        'db.networkinterface': {
+            'Meta': {'object_name': 'NetworkInterface'},
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'device_owner': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'}),
+            'firewall_profile': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'index': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
+            'mac': ('django.db.models.fields.CharField', [], {'max_length': '32', 'unique': 'True', 'null': 'True'}),
+            'machine': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'nics'", 'null': 'True', 'on_delete': 'models.PROTECT', 'to': "orm['db.VirtualMachine']"}),
+            'name': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '128', 'null': 'True'}),
+            'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'nics'", 'on_delete': 'models.PROTECT', 'to': "orm['db.Network']"}),
+            'security_groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['db.SecurityGroup']", 'null': 'True', 'symmetrical': 'False'}),
+            'state': ('django.db.models.fields.CharField', [], {'default': "'ACTIVE'", 'max_length': '32'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+            'userid': ('django.db.models.fields.CharField', [], {'max_length': '128', 'db_index': 'True'})
+        },
+        'db.quotaholderserial': {
+            'Meta': {'ordering': "['serial']", 'object_name': 'QuotaHolderSerial'},
+            'accept': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'pending': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'db_index': 'True'}),
+            'resolved': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'serial': ('django.db.models.fields.BigIntegerField', [], {'primary_key': 'True', 'db_index': 'True'})
+        },
+        'db.securitygroup': {
+            'Meta': {'object_name': 'SecurityGroup'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '128'})
+        },
+        'db.subnet': {
+            'Meta': {'object_name': 'Subnet'},
+            'cidr': ('django.db.models.fields.CharField', [], {'max_length': '64'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+            'dhcp': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'dns_nameservers': ('synnefo.db.fields.SeparatedValuesField', [], {'null': 'True'}),
+            'gateway': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
+            'host_routes': ('synnefo.db.fields.SeparatedValuesField', [], {'null': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'ipversion': ('django.db.models.fields.IntegerField', [], {'default': '4'}),
+            'name': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '128', 'null': 'True'}),
+            'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'subnets'", 'on_delete': 'models.PROTECT', 'to': "orm['db.Network']"}),
+            'public': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+            'userid': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True', 'db_index': 'True'})
+        },
+        'db.virtualmachine': {
+            'Meta': {'object_name': 'VirtualMachine'},
+            'action': ('django.db.models.fields.CharField', [], {'default': 'None', 'max_length': '30', 'null': 'True'}),
+            'backend': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'virtual_machines'", 'null': 'True', 'on_delete': 'models.PROTECT', 'to': "orm['db.Backend']"}),
+            'backend_hash': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'}),
+            'backendjobid': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}),
+            'backendjobstatus': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
+            'backendlogmsg': ('django.db.models.fields.TextField', [], {'null': 'True'}),
+            'backendopcode': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
+            'backendtime': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(1, 1, 1, 0, 0)'}),
+            'buildpercentage': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+            'flavor': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['db.Flavor']", 'on_delete': 'models.PROTECT'}),
+            'hostid': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'imageid': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'operstate': ('django.db.models.fields.CharField', [], {'default': "'BUILD'", 'max_length': '30'}),
+            'serial': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'virtual_machine'", 'null': 'True', 'on_delete': 'models.SET_NULL', 'to': "orm['db.QuotaHolderSerial']"}),
+            'suspended': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'task': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
+            'task_job_id': ('django.db.models.fields.BigIntegerField', [], {'null': 'True'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+            'userid': ('django.db.models.fields.CharField', [], {'max_length': '100', 'db_index': 'True'})
+        },
+        'db.virtualmachinediagnostic': {
+            'Meta': {'ordering': "['-created']", 'object_name': 'VirtualMachineDiagnostic'},
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'details': ('django.db.models.fields.TextField', [], {'null': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'level': ('django.db.models.fields.CharField', [], {'max_length': '20'}),
+            'machine': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'diagnostics'", 'to': "orm['db.VirtualMachine']"}),
+            'message': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'source': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'source_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True'})
+        },
+        'db.virtualmachinemetadata': {
+            'Meta': {'unique_together': "(('meta_key', 'vm'),)", 'object_name': 'VirtualMachineMetadata'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'meta_key': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
+            'meta_value': ('django.db.models.fields.CharField', [], {'max_length': '500'}),
+            'vm': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'metadata'", 'to': "orm['db.VirtualMachine']"})
+        }
+    }
+
+    complete_apps = ['db']
\ No newline at end of file
diff --git a/snf-cyclades-app/synnefo/db/migrations/0097_userid_to_subnets.py b/snf-cyclades-app/synnefo/db/migrations/0097_userid_to_subnets.py
new file mode 100644
index 0000000..7754e34
--- /dev/null
+++ b/snf-cyclades-app/synnefo/db/migrations/0097_userid_to_subnets.py
@@ -0,0 +1,234 @@
+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import DataMigration
+from django.db import models
+
+class Migration(DataMigration):
+
+    def forwards(self, orm):
+        "Write your forwards methods here."
+        # Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..."
+        for network in orm.Network.objects.all():
+            network.subnets.update(userid=network.userid,
+                                   public=network.public)
+
+    def backwards(self, orm):
+        "Write your backwards methods here."
+        pass
+
+    models = {
+        'db.backend': {
+            'Meta': {'ordering': "['clustername']", 'object_name': 'Backend'},
+            'clustername': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '128'}),
+            'ctotal': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+            'dfree': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+            'disk_templates': ('synnefo.db.fields.SeparatedValuesField', [], {'null': 'True'}),
+            'drained': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'dtotal': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+            'hash': ('django.db.models.fields.CharField', [], {'max_length': '40'}),
+            'hypervisor': ('django.db.models.fields.CharField', [], {'default': "'kvm'", 'max_length': '32'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'index': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0', 'unique': 'True'}),
+            'mfree': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+            'mtotal': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+            'offline': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'password_hash': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True', 'blank': 'True'}),
+            'pinst_cnt': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+            'port': ('django.db.models.fields.PositiveIntegerField', [], {'default': '5080'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'username': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True', 'blank': 'True'})
+        },
+        'db.backendnetwork': {
+            'Meta': {'unique_together': "(('network', 'backend'),)", 'object_name': 'BackendNetwork'},
+            'backend': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'networks'", 'on_delete': 'models.PROTECT', 'to': "orm['db.Backend']"}),
+            'backendjobid': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}),
+            'backendjobstatus': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
+            'backendlogmsg': ('django.db.models.fields.TextField', [], {'null': 'True'}),
+            'backendopcode': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
+            'backendtime': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(1, 1, 1, 0, 0)'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'mac_prefix': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
+            'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'backend_networks'", 'on_delete': 'models.PROTECT', 'to': "orm['db.Network']"}),
+            'operstate': ('django.db.models.fields.CharField', [], {'default': "'PENDING'", 'max_length': '30'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'})
+        },
+        'db.bridgepooltable': {
+            'Meta': {'object_name': 'BridgePoolTable'},
+            'available_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
+            'base': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'offset': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
+            'reserved_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
+            'size': ('django.db.models.fields.IntegerField', [], {})
+        },
+        'db.flavor': {
+            'Meta': {'unique_together': "(('cpu', 'ram', 'disk', 'disk_template'),)", 'object_name': 'Flavor'},
+            'cpu': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'disk': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+            'disk_template': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'ram': ('django.db.models.fields.IntegerField', [], {'default': '0'})
+        },
+        'db.ipaddress': {
+            'Meta': {'unique_together': "(('network', 'address', 'deleted'),)", 'object_name': 'IPAddress'},
+            'address': ('django.db.models.fields.CharField', [], {'max_length': '64'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'floating_ip': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'ipversion': ('django.db.models.fields.IntegerField', [], {}),
+            'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'ips'", 'on_delete': 'models.PROTECT', 'to': "orm['db.Network']"}),
+            'nic': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'ips'", 'null': 'True', 'on_delete': 'models.SET_NULL', 'to': "orm['db.NetworkInterface']"}),
+            'serial': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'ips'", 'null': 'True', 'on_delete': 'models.SET_NULL', 'to': "orm['db.QuotaHolderSerial']"}),
+            'subnet': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'ips'", 'on_delete': 'models.PROTECT', 'to': "orm['db.Subnet']"}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+            'userid': ('django.db.models.fields.CharField', [], {'max_length': '128', 'db_index': 'True'})
+        },
+        'db.ipaddresslog': {
+            'Meta': {'object_name': 'IPAddressLog'},
+            'active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'address': ('django.db.models.fields.CharField', [], {'max_length': '64', 'db_index': 'True'}),
+            'allocated_at': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'network_id': ('django.db.models.fields.IntegerField', [], {}),
+            'released_at': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
+            'server_id': ('django.db.models.fields.IntegerField', [], {})
+        },
+        'db.ippooltable': {
+            'Meta': {'object_name': 'IPPoolTable'},
+            'available_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
+            'base': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'offset': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
+            'reserved_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
+            'size': ('django.db.models.fields.IntegerField', [], {}),
+            'subnet': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'ip_pools'", 'null': 'True', 'on_delete': 'models.PROTECT', 'to': "orm['db.Subnet']"})
+        },
+        'db.macprefixpooltable': {
+            'Meta': {'object_name': 'MacPrefixPoolTable'},
+            'available_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
+            'base': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'offset': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
+            'reserved_map': ('django.db.models.fields.TextField', [], {'default': "''"}),
+            'size': ('django.db.models.fields.IntegerField', [], {})
+        },
+        'db.network': {
+            'Meta': {'object_name': 'Network'},
+            'action': ('django.db.models.fields.CharField', [], {'default': 'None', 'max_length': '32', 'null': 'True'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+            'drained': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'external_router': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'flavor': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
+            'floating_ip_pool': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'link': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True'}),
+            'mac_prefix': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
+            'machines': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['db.VirtualMachine']", 'through': "orm['db.NetworkInterface']", 'symmetrical': 'False'}),
+            'mode': ('django.db.models.fields.CharField', [], {'max_length': '16', 'null': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+            'public': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+            'serial': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'network'", 'null': 'True', 'on_delete': 'models.SET_NULL', 'to': "orm['db.QuotaHolderSerial']"}),
+            'state': ('django.db.models.fields.CharField', [], {'default': "'PENDING'", 'max_length': '32'}),
+            'tags': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+            'userid': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True', 'db_index': 'True'})
+        },
+        'db.networkinterface': {
+            'Meta': {'object_name': 'NetworkInterface'},
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'device_owner': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'}),
+            'firewall_profile': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'index': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
+            'mac': ('django.db.models.fields.CharField', [], {'max_length': '32', 'unique': 'True', 'null': 'True'}),
+            'machine': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'nics'", 'null': 'True', 'on_delete': 'models.PROTECT', 'to': "orm['db.VirtualMachine']"}),
+            'name': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '128', 'null': 'True'}),
+            'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'nics'", 'on_delete': 'models.PROTECT', 'to': "orm['db.Network']"}),
+            'security_groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['db.SecurityGroup']", 'null': 'True', 'symmetrical': 'False'}),
+            'state': ('django.db.models.fields.CharField', [], {'default': "'ACTIVE'", 'max_length': '32'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+            'userid': ('django.db.models.fields.CharField', [], {'max_length': '128', 'db_index': 'True'})
+        },
+        'db.quotaholderserial': {
+            'Meta': {'ordering': "['serial']", 'object_name': 'QuotaHolderSerial'},
+            'accept': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'pending': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'db_index': 'True'}),
+            'resolved': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'serial': ('django.db.models.fields.BigIntegerField', [], {'primary_key': 'True', 'db_index': 'True'})
+        },
+        'db.securitygroup': {
+            'Meta': {'object_name': 'SecurityGroup'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '128'})
+        },
+        'db.subnet': {
+            'Meta': {'object_name': 'Subnet'},
+            'cidr': ('django.db.models.fields.CharField', [], {'max_length': '64'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+            'dhcp': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'dns_nameservers': ('synnefo.db.fields.SeparatedValuesField', [], {'null': 'True'}),
+            'gateway': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
+            'host_routes': ('synnefo.db.fields.SeparatedValuesField', [], {'null': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'ipversion': ('django.db.models.fields.IntegerField', [], {'default': '4'}),
+            'name': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '128', 'null': 'True'}),
+            'network': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'subnets'", 'on_delete': 'models.PROTECT', 'to': "orm['db.Network']"}),
+            'public': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+            'userid': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True', 'db_index': 'True'})
+        },
+        'db.virtualmachine': {
+            'Meta': {'object_name': 'VirtualMachine'},
+            'action': ('django.db.models.fields.CharField', [], {'default': 'None', 'max_length': '30', 'null': 'True'}),
+            'backend': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'virtual_machines'", 'null': 'True', 'on_delete': 'models.PROTECT', 'to': "orm['db.Backend']"}),
+            'backend_hash': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True'}),
+            'backendjobid': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True'}),
+            'backendjobstatus': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
+            'backendlogmsg': ('django.db.models.fields.TextField', [], {'null': 'True'}),
+            'backendopcode': ('django.db.models.fields.CharField', [], {'max_length': '30', 'null': 'True'}),
+            'backendtime': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(1, 1, 1, 0, 0)'}),
+            'buildpercentage': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+            'flavor': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['db.Flavor']", 'on_delete': 'models.PROTECT'}),
+            'hostid': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'imageid': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'operstate': ('django.db.models.fields.CharField', [], {'default': "'BUILD'", 'max_length': '30'}),
+            'serial': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'virtual_machine'", 'null': 'True', 'on_delete': 'models.SET_NULL', 'to': "orm['db.QuotaHolderSerial']"}),
+            'suspended': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'task': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
+            'task_job_id': ('django.db.models.fields.BigIntegerField', [], {'null': 'True'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+            'userid': ('django.db.models.fields.CharField', [], {'max_length': '100', 'db_index': 'True'})
+        },
+        'db.virtualmachinediagnostic': {
+            'Meta': {'ordering': "['-created']", 'object_name': 'VirtualMachineDiagnostic'},
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'details': ('django.db.models.fields.TextField', [], {'null': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'level': ('django.db.models.fields.CharField', [], {'max_length': '20'}),
+            'machine': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'diagnostics'", 'to': "orm['db.VirtualMachine']"}),
+            'message': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'source': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'source_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True'})
+        },
+        'db.virtualmachinemetadata': {
+            'Meta': {'unique_together': "(('meta_key', 'vm'),)", 'object_name': 'VirtualMachineMetadata'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'meta_key': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
+            'meta_value': ('django.db.models.fields.CharField', [], {'max_length': '500'}),
+            'vm': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'metadata'", 'to': "orm['db.VirtualMachine']"})
+        }
+    }
+
+    complete_apps = ['db']
+    symmetrical = True
diff --git a/snf-cyclades-app/synnefo/db/models.py b/snf-cyclades-app/synnefo/db/models.py
index 1d80136..1041e9b 100644
--- a/snf-cyclades-app/synnefo/db/models.py
+++ b/snf-cyclades-app/synnefo/db/models.py
@@ -609,6 +609,10 @@ class Network(models.Model):
 class Subnet(models.Model):
     SUBNET_NAME_LENGTH = 128
 
+    userid = models.CharField('User ID of the owner', max_length=128,
+                              null=True, db_index=True)
+    public = models.BooleanField(default=False, db_index=True)
+
     network = models.ForeignKey('Network', null=False, db_index=True,
                                 related_name="subnets",
                                 on_delete=models.PROTECT)
diff --git a/snf-cyclades-app/synnefo/db/models_factory.py b/snf-cyclades-app/synnefo/db/models_factory.py
index 77d2bec..d6aeefd 100644
--- a/snf-cyclades-app/synnefo/db/models_factory.py
+++ b/snf-cyclades-app/synnefo/db/models_factory.py
@@ -199,6 +199,8 @@ class SubnetFactory(factory.DjangoModelFactory):
     dhcp = True
     dns_nameservers = []
     host_routes = []
+    userid = factory.LazyAttribute(lambda self: self.network.userid)
+    public = factory.LazyAttribute(lambda self: self.network.public)
 
 
 class IPv4SubnetFactory(SubnetFactory):
diff --git a/snf-cyclades-app/synnefo/logic/subnets.py b/snf-cyclades-app/synnefo/logic/subnets.py
index 6c677ad..2dd4c81 100644
--- a/snf-cyclades-app/synnefo/logic/subnets.py
+++ b/snf-cyclades-app/synnefo/logic/subnets.py
@@ -117,6 +117,7 @@ def _create_subnet(network_id, user_id, cidr, name, ipversion=4, gateway=None,
                                    "name is too long")
     sub = Subnet.objects.create(name=name, network=network, cidr=cidr,
                                 ipversion=ipversion, gateway=gateway,
+                                userid=network.userid, public=network.public,
                                 dhcp=dhcp, host_routes=host_routes,
                                 dns_nameservers=dns_nameservers)
 
@@ -155,12 +156,8 @@ def get_subnet(subnet_id, user_id, for_update=False):
     try:
         objects = Subnet.objects
         subnet_id = int(subnet_id)
-        subnet = objects.get(id=subnet_id)
-        if (subnet.network.userid != user_id) and (subnet.network.public is
-                                                   False):
-            raise api.faults.Unauthorized("You're not allowed to view this "
-                                          "subnet")
-        return subnet
+        return objects.get(Q(userid=user_id) | Q(public=True),
+                           id=subnet_id)
     except (ValueError, TypeError):
         raise faults.BadRequest("Invalid subnet ID '%s'" % subnet_id)
     except Subnet.DoesNotExist:
