#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

from django.core.management.base import BaseCommand
from django.db import connections
from bson.objectid import ObjectId

from smartshark.models import Project
from smartshark.utils import projectUtils
from smartshark.utils.projectUtils import delete_file_from_gridfs_for_project


class Command(BaseCommand):
    help = 'Deletes all data of a project'

    def handle(self, *args, **options):

        for p in Project.objects.all():
            self.stdout.write(p.name)
        try:
            l = input("Which project should be deleted? ")
            project = Project.objects.all().get(name__iexact=l)
            self.stdout.write("Calculate data tree for {}".format(project.name))
        except (Project.DoesNotExist, Project.MultipleObjectsReturned) as e:
            self.stdout.write(self.style.ERROR('Error loading project: {}'.format(e)))
            sys.exit(-1)

        schemas = projectUtils.getPlugins()

        # Analyze the schema
        deb = []
        x = projectUtils.findDependencyOfSchema('project', schemas.values(), [])
        project_schema = projectUtils.SchemaReference('project', '_id', x)
        deb.append(project_schema)

        projectUtils.count_on_dependency_tree(project_schema, ObjectId(project.mongo_id))
        self._print_dependency_tree(deb, project)

        l = input("Continue with data deletion? (y/n) ")
        if(l == "yes" or l == "y"):
            self.stdout.write('Deleting project from the MongoDB')

            # Before everything else we need to delete the file from the gridfs
            delete_file_from_gridfs_for_project(ObjectId(project.mongo_id))

            projectUtils.delete_on_dependency_tree(project_schema, ObjectId(project.mongo_id))
            self.stdout.write(self.style.SUCCESS('Successfully deleted project from the MongoDB'))

            connections['default'].close()

            self.stdout.write('Deleting project from the serverSHARK')
            project.delete()
            self.stdout.write(self.style.SUCCESS('Successfully deleted project from the serverSHARK'))
        else:
            self.stdout.write(self.style.ERROR('No data deleted'))

    def _print_dependency_tree(self, deb, project):
        self.stdout.write("Project data of {}".format(project.name))
        for dependency in deb:
            self.stdout.write('{} ({})'.format(dependency.collection_name, dependency.count))
            self._print_sub_dependency(dependency.dependencys, 1)

    def _print_sub_dependency(self, deb, depth):
        for dependency in deb:
            self.stdout.write('{} └── {} ({})'.format('  ' * (depth - 1), dependency.collection_name, dependency.count))
            self._print_sub_dependency(dependency.dependencys, depth + 1)

@@ -1356,16 +1356,40 @@
 oject)%0A%0A
+        while True:%0A    
         
@@ -1449,16 +1449,18 @@
     
-if(l 
+    if l
 ==
- 
 %22yes
@@ -1469,18 +1469,19 @@
 or l
- 
 ==
- 
 %22y%22
-)
 :%0A
+    
     
@@ -1556,16 +1556,20 @@
         
+    
 # Before
@@ -1628,16 +1628,20 @@
  gridfs%0A
+    
         
@@ -1713,32 +1713,36 @@
 ))%0A%0A            
+    
 projectUtils.del
@@ -1800,32 +1800,36 @@
 ject.mongo_id))%0A
+    
             self
@@ -1924,16 +1924,20 @@
         
+    
 connecti
@@ -1964,32 +1964,36 @@
 ()%0A%0A            
+   
+ 
 self.stdout.writ
@@ -2043,24 +2043,28 @@
             
+    
 project.dele
@@ -2072,32 +2072,36 @@
 e()%0A            
+   
+ 
 self.stdout.writ
@@ -2187,14 +2187,62 @@
     
+       
-else:%0A
+ break%0A            elif l==%22no%22 or l==%22n%22:%0A    
     
@@ -2303,16 +2303,140 @@
 leted'))
+%0A                break%0A            else:%0A                self.stdout.write(self.style.ERROR('Only (y)es and (n)o accepted'))
 %0A%0A    de

Repeat question if input is neither yes or no