Package madrona :: Package layers :: Package management :: Package commands :: Module create_privatekml
[hide private]

Source Code for Module madrona.layers.management.commands.create_privatekml

 1  from django.core.management.base import BaseCommand, AppCommand 
 2  from django.conf import settings 
 3  from optparse import make_option 
 4  import os 
 5  import glob 
 6  from madrona.layers.models import PrivateKml 
 7  from django.contrib.auth.models import User, Group 
 8   
9 -class Command(BaseCommand):
10 help = "Populates the PrivateKml table from the PRIVATE_KML_ROOT contents .. a good starting point" 11 args = '[optional group name to share all KMLs with]' 12 13 # Validation is called explicitly each time the server is reloaded. 14 requires_model_validation = False 15
16 - def handle(self, groupname=None, *args, **options):
17 for pkml in PrivateKml.objects.all(): 18 pkml.delete() 19 20 if groupname: 21 g = Group.objects.get(name=groupname) 22 23 if not os.path.exists(settings.PRIVATE_KML_ROOT): 24 raise Exception("Please create or set up a PRIVATE_KML_ROOT directory (currently set to %s" % 25 settings.PRIVATE_KML_ROOT) 26 for d in os.listdir(settings.PRIVATE_KML_ROOT): 27 path = os.path.join(settings.PRIVATE_KML_ROOT,d) 28 kmls = glob.glob(os.path.join(path,'*.km[z,l]')) 29 if len(kmls) == 0: 30 print "No KML/KMZ found in %s" % path 31 continue 32 33 for kml in kmls: 34 basename = os.path.basename(kml).split('.')[0] 35 privatekml_name = d + '_' + basename 36 print "Creating", privatekml_name 37 try: 38 pkml = PrivateKml.objects.create(name=privatekml_name[:99],base_kml=kml) 39 if groupname: 40 pkml.sharing_groups.add(g) 41 print " created %s from %s" % (pkml,kml) 42 except Exception as e: 43 print " couldn't create privatekml from %s" % kml 44 print " ", e
45