from django.contrib import messages
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext_lazy as _

from custom.icds import icds_toggles
from corehq.apps.hqwebapp.decorators import use_daterangepicker
from corehq.apps.sms.views import BaseMessagingSectionView
from custom.icds.forms import CustomSMSReportRequestForm
from custom.icds.tasks.sms import send_custom_sms_report
from custom.icds_core.const import SMSUsageReport_urlname


@method_decorator(icds_toggles.ICDS_CUSTOM_SMS_REPORT.required_decorator(), name='dispatch')
class SMSUsageReport(BaseMessagingSectionView):
    template_name = 'icds/sms/custom_sms_report.html'
    urlname = SMSUsageReport_urlname
    page_title = _('Custom SMS Usage Report')

    @use_daterangepicker
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

    @property
    def page_context(self):
        self.request_form = CustomSMSReportRequestForm()
        return {
            'request_form': self.request_form,
        }

    def post(self, request, *args, **kwargs):
        self.request_form = CustomSMSReportRequestForm(request.POST)
        user_email = request.user.email
        if not user_email:
            messages.error(self.request, _("Unable to find any email associated with your account"))
            return self.get(*args, **kwargs)
        if self.request_form.is_valid():
            data = self.request_form.cleaned_data
            start_date = data['start_date']
            end_date = data['end_date']
            send_custom_sms_report.delay(start_date, end_date, user_email)
            messages.success(self.request, _(
                "Report will we soon emailed to your email i.e {user_email}"
                .format(user_email=user_email))
            )
        else:
            for message_list in self.request_form.errors.values():
                for message in message_list:
                    messages.error(self.request, message)
        return self.get(*args, **kwargs)

@@ -149,26 +149,21 @@
 om c
-ustom.icds
+orehq
  import 
 icds
@@ -158,21 +158,16 @@
  import 
-icds_
 toggles%0A
@@ -481,21 +481,16 @@
 corator(
-icds_
 toggles.

use correct module