from __future__ import absolute_import
from __future__ import unicode_literals
from couchexport.export import export_raw
from custom.fri.models import FRIMessageBankMessage, FRIRandomizedMessage, FRIExtraMessage
from django.core.management.base import BaseCommand
from io import open


class Command(BaseCommand):

    def write_result_to_file(self, model_name, result, fields):
        with open('%s.xlsx' % model_name, 'wb') as f:
            headers = fields
            excel_data = []

            for obj in result:
                excel_data.append((getattr(obj, field) for field in fields))

            export_raw(
                ((model_name, headers), ),
                ((model_name, excel_data), ),
                f
            )

    def handle(self, **options):
        self.write_result_to_file(
            'FRIMessageBankMessage',
            FRIMessageBankMessage.view('fri/message_bank', include_docs=True).all(),
            ('_id', 'domain', 'risk_profile', 'message', 'fri_id')
        )

        self.write_result_to_file(
            'FRIRandomizedMessage',
            FRIRandomizedMessage.view('fri/randomized_message', include_docs=True).all(),
            ('_id', 'domain', 'case_id', 'message_bank_message_id', 'order')
        )

        self.write_result_to_file(
            'FRIExtraMessage',
            FRIExtraMessage.view('fri/extra_message', include_docs=True).all(),
            ('_id', 'domain', 'message_id', 'message')
        )

from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from custom.fri.models import FRIMessageBankMessage, FRIRandomizedMessage, FRIExtraMessage
from django.core.management.base import BaseCommand
from six import moves


class Command(BaseCommand):

    def delete_docs(self, model_name, result):
        print("\nHandling %s" % model_name)

        result = list(result)
        answer = moves.input("Delete %s docs? y/n" % len(result))

        if answer == 'y':
            count = 0
            for doc in result:
                if doc.doc_type != model_name:
                    print("Deleted %s docs" % count)
                    raise ValueError("Expected %s, got %s" % (model_name, doc.doc_type))

                doc.delete()
                count += 1

            print("Deleted %s docs" % count)

    def handle(self, **options):
        self.delete_docs(
            'FRIMessageBankMessage',
            FRIMessageBankMessage.view('fri/message_bank', include_docs=True).all()
        )

        self.delete_docs(
            'FRIRandomizedMessage',
            FRIRandomizedMessage.view('fri/randomized_message', include_docs=True).all()
        )

        self.delete_docs(
            'FRIExtraMessage',
            FRIExtraMessage.view('fri/extra_message', include_docs=True).all()
        )

Update fri dump script to delete docs
Update fri dump script to delete docs

  from __future__ import absolute_import
+ from __future__ import print_function
  from __future__ import unicode_literals
- from couchexport.export import export_raw
  from custom.fri.models import FRIMessageBankMessage, FRIRandomizedMessage, FRIExtraMessage
  from django.core.management.base import BaseCommand
- from io import open
+ from six import moves
  
  
  class Command(BaseCommand):
  
+     def delete_docs(self, model_name, result):
+         print("\nHandling %s" % model_name)
-     def write_result_to_file(self, model_name, result, fields):
-         with open('%s.xlsx' % model_name, 'wb') as f:
-             headers = fields
-             excel_data = []
  
-             for obj in result:
-                 excel_data.append((getattr(obj, field) for field in fields))
+         result = list(result)
+         answer = moves.input("Delete %s docs? y/n" % len(result))
  
-             export_raw(
-                 ((model_name, headers), ),
-                 ((model_name, excel_data), ),
-                 f
-             )
+         if answer == 'y':
+             count = 0
+             for doc in result:
+                 if doc.doc_type != model_name:
+                     print("Deleted %s docs" % count)
+                     raise ValueError("Expected %s, got %s" % (model_name, doc.doc_type))
+ 
+                 doc.delete()
+                 count += 1
+ 
+             print("Deleted %s docs" % count)
  
      def handle(self, **options):
-         self.write_result_to_file(
+         self.delete_docs(
              'FRIMessageBankMessage',
-             FRIMessageBankMessage.view('fri/message_bank', include_docs=True).all(),
+             FRIMessageBankMessage.view('fri/message_bank', include_docs=True).all()
-             ('_id', 'domain', 'risk_profile', 'message', 'fri_id')
          )
  
-         self.write_result_to_file(
+         self.delete_docs(
              'FRIRandomizedMessage',
-             FRIRandomizedMessage.view('fri/randomized_message', include_docs=True).all(),
+             FRIRandomizedMessage.view('fri/randomized_message', include_docs=True).all()
-             ('_id', 'domain', 'case_id', 'message_bank_message_id', 'order')
          )
  
-         self.write_result_to_file(
+         self.delete_docs(
              'FRIExtraMessage',
-             FRIExtraMessage.view('fri/extra_message', include_docs=True).all(),
+             FRIExtraMessage.view('fri/extra_message', include_docs=True).all()
-             ('_id', 'domain', 'message_id', 'message')
          )
  
Update fri dump script to delete docs
## Code Before:
from __future__ import absolute_import
from __future__ import unicode_literals
from couchexport.export import export_raw
from custom.fri.models import FRIMessageBankMessage, FRIRandomizedMessage, FRIExtraMessage
from django.core.management.base import BaseCommand
from io import open


class Command(BaseCommand):

    def write_result_to_file(self, model_name, result, fields):
        with open('%s.xlsx' % model_name, 'wb') as f:
            headers = fields
            excel_data = []

            for obj in result:
                excel_data.append((getattr(obj, field) for field in fields))

            export_raw(
                ((model_name, headers), ),
                ((model_name, excel_data), ),
                f
            )

    def handle(self, **options):
        self.write_result_to_file(
            'FRIMessageBankMessage',
            FRIMessageBankMessage.view('fri/message_bank', include_docs=True).all(),
            ('_id', 'domain', 'risk_profile', 'message', 'fri_id')
        )

        self.write_result_to_file(
            'FRIRandomizedMessage',
            FRIRandomizedMessage.view('fri/randomized_message', include_docs=True).all(),
            ('_id', 'domain', 'case_id', 'message_bank_message_id', 'order')
        )

        self.write_result_to_file(
            'FRIExtraMessage',
            FRIExtraMessage.view('fri/extra_message', include_docs=True).all(),
            ('_id', 'domain', 'message_id', 'message')
        )

## Instruction:
Update fri dump script to delete docs
## Code After:
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from custom.fri.models import FRIMessageBankMessage, FRIRandomizedMessage, FRIExtraMessage
from django.core.management.base import BaseCommand
from six import moves


class Command(BaseCommand):

    def delete_docs(self, model_name, result):
        print("\nHandling %s" % model_name)

        result = list(result)
        answer = moves.input("Delete %s docs? y/n" % len(result))

        if answer == 'y':
            count = 0
            for doc in result:
                if doc.doc_type != model_name:
                    print("Deleted %s docs" % count)
                    raise ValueError("Expected %s, got %s" % (model_name, doc.doc_type))

                doc.delete()
                count += 1

            print("Deleted %s docs" % count)

    def handle(self, **options):
        self.delete_docs(
            'FRIMessageBankMessage',
            FRIMessageBankMessage.view('fri/message_bank', include_docs=True).all()
        )

        self.delete_docs(
            'FRIRandomizedMessage',
            FRIRandomizedMessage.view('fri/randomized_message', include_docs=True).all()
        )

        self.delete_docs(
            'FRIExtraMessage',
            FRIExtraMessage.view('fri/extra_message', include_docs=True).all()
        )
