from django.core.management.base import BaseCommand
from corehq.apps.domain.models import Domain
import csv


class Command(BaseCommand):
    help = 'Find domains with secure submissions and image questions'

    def handle(self, *args, **options):
        with open('domain_results.csv', 'wb+') as csvfile:
            csv_writer = csv.writer(
                csvfile,
                delimiter=',',
                quotechar='|',
                quoting=csv.QUOTE_MINIMAL
            )

            csv_writer.writerow(['domain', 'app', 'domain_creator'])

            for domain in Domain.get_all(include_docs=True):
                if domain.secure_submissions:
                    for app in domain.full_applications(include_builds=False):
                        for module in app.modules:
                            for form in module.forms:
                                for question in form.get_questions(app.langs):
                                    if question['type'] == 'Image':
                                        csv_writer.writerow([
                                            domain.name,
                                            app.name,
                                            domain.creating_user
                                        ])

@@ -214,419 +214,48 @@
 def 
-handle(self, *args, **options):%0A        with open('domain_results.csv', 'wb+') as csvfile:%0A            csv_writer = csv.writer(%0A                csvfile,%0A                delimiter=',',%0A                quotechar='%7C',%0A                quoting=csv.QUOTE_MINIMAL%0A            )%0A%0A            csv_writer.writerow(%5B'domain', 'app', 'domain_creator'%5D)%0A%0A            for domain in Domain.get_all(include_docs=True):%0A        
+check_domain(self, domain, csv_writer):%0A
     
@@ -296,32 +296,24 @@
             
-    
-    
 for app in d
@@ -371,32 +371,24 @@
             
-        
 for module i
@@ -418,32 +418,24 @@
             
-   
-     
 for form in 
@@ -448,24 +448,16 @@
 .forms:%0A
-        
         
@@ -547,24 +547,16 @@
         
-  
-      
 if quest
@@ -571,28 +571,40 @@
 e'%5D 
-== 
+in (
 'Image'
-:%0A        
+, 'Audio', 'Video'):%0A
     
@@ -653,32 +653,119 @@
 terow(%5B%0A        
+                            domain.name,%0A                                    app.name,%0A
                 
@@ -783,37 +783,45 @@
          domain.
-name,
+creating_user
 %0A               
@@ -820,32 +820,44 @@
                 
+         %5D)%0A
                 
@@ -865,32 +865,163 @@
     
-app.name,%0A              
+           return%0A%0A    def handle(self, *args, **options):%0A        with open('domain_results.csv', 'wb+') as csvfile:%0A            csv_writer = csv.writer(%0A
     
@@ -1022,32 +1022,43 @@
 (%0A              
+  csvfile,%0A
                 
@@ -1058,35 +1058,102 @@
        d
-omain.creating_user
+elimiter=',',%0A                quotechar='%7C',%0A                quoting=csv.QUOTE_MINIMAL
 %0A       
@@ -1149,32 +1149,35 @@
 MAL%0A            
+)%0A%0A
                 
@@ -1168,31 +1168,185 @@
             
-                %5D
+csv_writer.writerow(%5B'domain', 'app', 'domain_creator'%5D)%0A%0A            for domain in Domain.get_all(include_docs=True):%0A                self.check_domain(domain, csv_writer
 )%0A

Add audio/video support and bail on findings