from rapidsms.apps.base import AppBase
from script.models import Script, ScriptProgress
from django.conf import settings
from unregister.models import Blacklist
from .models import EmisReporter

class App (AppBase):

    def handle (self, message):
        import pdb; pdb.set_trace()
        if message.text.strip().lower() in [i.lower() for i in getattr(settings, 'OPT_OUT_WORDS', ['quit'])]:
            Blacklist.objects.create(connection=message.connection)
            if (message.connection.contact):
                reporter = EmisReporter.objects.get(connection=message.connection)
                message.connection.contact.active = False
                message.connection.contact.save()
                reporter.active = False
                reporter.save()
            message.respond(getattr(settings, 'OPT_OUT_CONFIRMATION', 'Thank you for your contribution as a education monitoring reporter, to rejoin the system send JOIN to 6200'))
            return True
        elif message.text.strip().lower() in [i.lower() for i in getattr(settings, 'OPT_IN_WORDS', ['join'])]:
            if Blacklist.objects.filter(connection=message.connection).exists():
                Blacklist.objects.filter(connection=message.connection).delete()

                if not ScriptProgress.objects.filter(script__slug='edtrac_autoreg', connection=message.connection).exists():
                    ScriptProgress.objects.create(script=Script.objects.get(slug="edtrac_autoreg"),\
                        connection=message.connection, language="en")
                else:
                    message.respond("You are already in the system and do not need to 'Join' again.")

            if not message.connection.contact:
                ScriptProgress.objects.get_or_create(script=Script.objects.get(slug="edtrac_autoreg"), \
                    connection=message.connection, language="en")

            return True
        elif Blacklist.objects.filter(connection=message.connection).count():
            return True
        return False
from rapidsms.apps.base import AppBase
from script.models import Script, ScriptProgress
from django.conf import settings
from unregister.models import Blacklist
from .models import EmisReporter

class App (AppBase):

    def handle (self, message):
        #import pdb; pdb.set_trace()
        if message.text.strip().lower() in [i.lower() for i in getattr(settings, 'OPT_OUT_WORDS', ['quit'])]:
            Blacklist.objects.create(connection=message.connection)
            if (message.connection.contact):
                reporter = EmisReporter.objects.get(connection=message.connection)
                message.connection.contact.active = False
                message.connection.contact.save()
                reporter.active = False
                reporter.save()
            message.respond(getattr(settings, 'OPT_OUT_CONFIRMATION', 'Thank you for your contribution as a education monitoring reporter, to rejoin the system send JOIN to 6200'))
            return True
        elif message.text.strip().lower() in [i.lower() for i in getattr(settings, 'OPT_IN_WORDS', ['join'])]:
            if Blacklist.objects.filter(connection=message.connection).exists():
                Blacklist.objects.filter(connection=message.connection).delete()

                if not ScriptProgress.objects.filter(script__slug='edtrac_autoreg', connection=message.connection).exists():
                    ScriptProgress.objects.create(script=Script.objects.get(slug="edtrac_autoreg"),\
                        connection=message.connection, language="en")
                else:
                    message.respond("You are already in the system and do not need to 'Join' again.")

            if not message.connection.contact:
                ScriptProgress.objects.get_or_create(script=Script.objects.get(slug="edtrac_autoreg"), \
                    connection=message.connection, language="en")

            return True
        elif Blacklist.objects.filter(connection=message.connection).count():
            return True
        return False
remove pdb