## -*- coding: utf-8 -*-
from os.path import join
from subprocess import call

from .models import CatalogoLandsat


def add_image(image):
    """Function to add a Image to CatalogoLandsat."""
    CatalogoLandsat.objects.get_or_create(
        image=image.name,
        path=join('/mnt/csr/imagens/landsat8', image.scene.name),
        shape=image.scene.geom,
        data=image.scene.date,
        url_tms=join('http://10.1.25.66/imagens/tms/landsat',
            image.scene.name + '_r6g5b4_tms.xml')
    )


def make_tms(image):
    """Generate the TMS of an Image."""
    if image.type == 'r6g5b4':
        path = join('/csr/imagens/landsat8', image.file_path())
        call(['make_tms.sh', path])
    else:
        print('Image is not of r6g5b4 type.')
@@ -190,16 +190,129 @@
 %22%22%22%0A    
+if image.type == 'r6g5b4':%0A        if CatalogoLandsat.objects.filter(image=image.name).count() == 0:%0A            
 Catalogo
@@ -331,15 +331,8 @@
 cts.
-get_or_
 crea
@@ -339,24 +339,32 @@
 te(%0A        
+    
+    
 image=image.
@@ -369,16 +369,24 @@
 e.name,%0A
+        
         
@@ -451,16 +451,24 @@
         
+   
+     
 shape=im
@@ -491,16 +491,24 @@
         
+  
+      
 data=ima
@@ -530,16 +530,24 @@
         
+ 
+       
 url_tms=
@@ -592,16 +592,24 @@
 ndsat',%0A
+        
         
@@ -654,16 +654,169 @@
 l')%0A    
+        )%0A        else:%0A            print(('Catalogo Landsat to %25s already exists.' %25 image.name))%0A    else:%0A        print('Image is not of r6g5b4 type.'
 )%0A%0A%0Adef 
@@ -895,32 +895,110 @@
 pe == 'r6g5b4':%0A
+        if CatalogoLandsat.objects.filter(image=image.name).count() == 0:%0A    
         path = j
@@ -1053,16 +1053,20 @@
         
+    
 call(%5B'm
@@ -1085,16 +1085,85 @@
  path%5D)%0A
+        else:%0A            print(('%25s already has TMS' %25 image.name))%0A
     else

improve add_image and make_tms functions to avoid errors