<reponame>synw/django-vvphotos
from __future__ import print_function
import os
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from vvphotos.models import Album
from django.core.urlresolvers import reverse


class Command(BaseCommand):
    help = 'Build routes for vvphotos'

    def handle(self, *args, **options):
        albums = Album.objects.filter(status="published")
        print("Building routes ...")
        routes = []
        i = 1
        for album in albums:
            rest_url = reverse("vvphotos-album", kwargs={'slug':album.slug})
            url = "/photos/"+str(album.slug)+"/"
            val = "page('"+url+"', function(ctx, next) { app.loadAlbum('"+rest_url+"', '"+album.title+"') } );"
            routes.append(val)
            print(str(i)+": "+url)
            i += 1
        routes_str = '{# ************ Autogenerated file: do not edit ************ #}\n'+'\n'.join(routes)
        # check directories
        dirpath = settings.BASE_DIR+"/templates/vvphotos"
        if not os.path.isdir(dirpath) is True:
            print("Creating directory templates/vvphotos")
            os.makedirs(dirpath)
        dirpath = settings.BASE_DIR+"/templates/vvphotos/auto"
        if not os.path.isdir(dirpath) is True:
            os.makedirs(dirpath)
            print("Creating directory templates/vvphotos/auto")
        # update
        filepath=settings.BASE_DIR+"/templates/vvphotos/auto/routes.js"
        print("Updating templates/vvphotos/auto/routes.js")
        #~ write the file
        filex = open(filepath, "w")
        filex.write(routes_str)
        filex.close()
        print("OK: "+str(i-1)+" routes built")
        return

from __future__ import print_function
import os
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from vvphotos.models import Album
from django.core.urlresolvers import reverse


class Command(BaseCommand):
    help = 'Build routes for vvphotos'

    def handle(self, *args, **options):
        albums = Album.objects.filter(status="published")
        print("Building routes ...")
        routes = []
        i = 1
        for album in albums:
            rest_url = reverse("vvphotos-album", kwargs={'slug':album.slug})
            url = "/photos/"+str(album.slug)+"/"
            val = "page('"+url+"', function(ctx, next) { app.loadAlbum('"+rest_url+"', '"+album.title+"') } );"
            routes.append(val)
            print(str(i)+": "+url)
            i += 1
        routes_str = '{# ************ Autogenerated file: do not edit ************ #}\n'+'\n'.join(routes)
        # check directories
        dirpath = settings.BASE_DIR+"/templates/vvphotos"
        if not os.path.isdir(dirpath) is True:
            print("Creating directory templates/vvphotos")
            os.makedirs(dirpath)
        dirpath = settings.BASE_DIR+"/templates/vvphotos/auto"
        if not os.path.isdir(dirpath) is True:
            os.makedirs(dirpath)
            print("Creating directory templates/vvphotos/auto")
        # update
        filepath=settings.BASE_DIR+"/templates/vvphotos/auto/routes.js"
        print("Updating templates/vvphotos/auto/routes.js")
        #~ write the file
        filex = open(filepath, "w")
        filex.write(routes_str)
        filex.close()
        print("OK: "+str(i-1)+" routes built")
        return
# ************ Autogenerated file: do not edit ************ #}\n'+'\n'.join(routes) # check directories # update #~ write the file