from django.core.management.commands import reset

class Command(reset.Command):
    help = ("Executes ``sqlreset`` for the proposal system in the "
            "current database.")
    args = ''

    output_transaction = True

    def handle(self, *args, **options):
        from django.db import models
        try:
            app = models.get_app('propmgr')
        except (ImproperlyConfigured, ImportError), e:
            raise CommandError(
                "%s. Are you sure your INSTALLED_APPS setting is correct?" % e)

        if options.get('interactive'):
            confirm = raw_input("""
You have requested a database reset.
This will IRREVERSIBLY DESTROY any data for
the Proposal System application in the database "%s".
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: """ % (settings.DATABASE_NAME))
            options['interactive']=False # to prevent second prompt
            if confirm != 'yes':
                print "Reset cancelled."
                return
        self.purge_change_history(app, **options)
        return self.handle_app(app, **options)

    def purge_change_history(self, app, **options):
        print "Clearing log entries"
        from django.contrib.contenttypes.models import ContentType
        if not app:
            print "No Models to Clean."
            return
        ids = [ContentType.objects.get_for_model(klass).id
               for klass in app_models]
        if not ids:
            print "No Content Types to Clean."
            return
        from django.contrib.admin.models import LogEntry
        query = LogEntry.objects.filter(content_type__in=ids)
        count = query.count()
        if not count:
            print "No log entries to delete."
        query.delete()
        print "Removed", count, "Log Entries"
