<commit_before>import base64
import StringIO
from datetime import datetime
import json
from django.contrib import messages
from django.core.urlresolvers import reverse, reverse_lazy
from django.db.models import Count, Q
from django.http.response import HttpResponseRedirect, Http404
from django.utils.decorators import method_decorator
from django.views.generic.base import TemplateView
from django.views.generic.edit import DeleteView
from django.views.generic.list import ListView
from corehq.apps.commtrack.models import StockState
from corehq.apps.products.models import SQLProduct
from corehq.apps.domain.views import BaseDomainView, DomainViewMixin
from corehq.apps.locations.models import SQLLocation
from corehq.apps.sms.mixin import VerifiedNumber
from corehq.apps.sms.models import SMSLog
from corehq.apps.sms.util import clean_phone_number
from corehq.apps.users.models import CommCareUser, WebUser, UserRole
from django.http import HttpResponse
from django.utils.translation import ugettext_noop
from django.views.decorators.http import require_POST
from corehq.apps.domain.decorators import domain_admin_required
from corehq.const import SERVER_DATETIME_FORMAT_NO_SEC
from custom.ilsgateway import DashboardReport
from custom.ilsgateway.comparison_reports import ProductAvailabilityReport
from custom.ilsgateway.forms import SupervisionDocumentForm
from custom.ilsgateway.stock_data import ILSStockDataSynchronization
from custom.ilsgateway.tanzania import make_url
from custom.ilsgateway.tanzania.reminders.delivery import send_delivery_reminder
from custom.ilsgateway.tanzania.reminders.randr import send_ror_reminder
from custom.ilsgateway.tanzania.reminders.stockonhand import send_soh_reminder
from custom.ilsgateway.tanzania.reminders.supervision import send_supervision_reminder
from custom.ilsgateway.tanzania.reports.delivery import DeliveryReport
from custom.ilsgateway.tanzania.reports.randr import RRreport
from custom.ilsgateway.tanzania.reports.stock_on_hand import StockOnHandReport
from custom.ilsgateway.tanzania.reports.supervision import SupervisionReport
from custom.ilsgateway.tasks import clear_report_data
from casexml.apps.stock.models import StockTransaction
from custom.logistics.models import StockDataCheckpoint
from custom.logistics.tasks import fix_groups_in_location_task, resync_web_users
from custom.ilsgateway.api import ILSGatewayAPI
from custom.logistics.tasks import stock_data_task
from custom.ilsgateway.api import ILSGatewayEndpoint
from custom.ilsgateway.models import ILSGatewayConfig, ReportRun, SupervisionDocument, DeliveryGroups, ILSNotes, \
    ProductAvailabilityData
from custom.ilsgateway.tasks import report_run, ils_clear_stock_data_task, \
    ils_bootstrap_domain_task
from custom.logistics.views import BaseConfigView, BaseRemindersTester


class GlobalStats(BaseDomainView):
    section_name = 'Global Stats'
    section_url = ""
    template_name = "ilsgateway/global_stats.html"
    show_supply_point_types = False
    root_name = 'MOHSW'

    @property
    def main_context(self):
        contacts = CommCareUser.by_domain(self.domain, reduce=True)
        web_users = WebUser.by_domain(self.domain)
        web_users_admins = web_users_read_only = 0
        facilities = SQLLocation.objects.filter(domain=self.domain, location_type__name__iexact='FACILITY')
        admin_role_list = UserRole.by_domain_and_name(self.domain, 'Administrator')
        if admin_role_list:
            admin_role = admin_role_list[0]
        else:
            admin_role = None
        for web_user in web_users:
            dm = web_user.get_domain_membership(self.domain)
            if admin_role and dm.role_id == admin_role.get_id:
                web_users_admins += 1
            else:
                web_users_read_only += 1

        main_context = super(GlobalStats, self).main_context
        entities_reported_stock = SQLLocation.objects.filter(
            domain=self.domain,
            location_type__administrative=False
        ).count()

        context = {
            'root_name': self.root_name,
            'country': SQLLocation.objects.filter(domain=self.domain,
                                                  location_type__name__iexact=self.root_name).count(),
            'region': SQLLocation.objects.filter(domain=self.domain, location_type__name__iexact='region').count(),
            'district': SQLLocation.objects.filter(
                domain=self.domain,
                location_type__name__iexact='district'
            ).count(),
            'entities_reported_stock': entities_reported_stock,
            'facilities': len(facilities),
            'contacts': contacts[0]['value'] if contacts else 0,
            'web_users': len(web_users),
            'web_users_admins': web_users_admins,
            'web_users_read_only': web_users_read_only,
            'products': SQLProduct.objects.filter(domain=self.domain, is_archived=False).count(),
            'product_stocks': StockState.objects.filter(sql_product__domain=self.domain).count(),
            'stock_transactions': StockTransaction.objects.filter(report__domain=self.domain).count(),
            'inbound_messages': SMSLog.count_incoming_by_domain(self.domain),
            'outbound_messages': SMSLog.count_outgoing_by_domain(self.domain)
        }

        if self.show_supply_point_types:
            counts = SQLLocation.objects.values('location_type__name').filter(domain=self.domain).annotate(
                Count('location_type')
            ).order_by('location_type__name')
            context['location_types'] = counts
        main_context.update(context)
        return main_context


class ILSConfigView(BaseConfigView):
    config = ILSGatewayConfig
    urlname = 'ils_config'
    sync_urlname = 'sync_ilsgateway'
    sync_stock_url = 'ils_sync_stock_data'
    clear_stock_url = 'ils_clear_stock_data'
    page_title = ugettext_noop("ILSGateway")
    template_name = 'ilsgateway/ilsconfig.html'
    source = 'ilsgateway'


class SupervisionDocumentListView(BaseDomainView):
    section_name = 'Supervision Documents'
    section_url = ""
    template_name = "ilsgateway/supervision_docs.html"
    urlname = 'supervision'

    def dispatch(self, request, *args, **kwargs):
        if not self.request.couch_user.is_web_user():
            raise Http404()
        return super(SupervisionDocumentListView, self).dispatch(request, *args, **kwargs)

    @method_decorator(domain_admin_required)
    def post(self, *args, **kwargs):
        request = args[0]
        form = SupervisionDocumentForm(request.POST, request.FILES)
        if form.is_valid():
            supervision_document = SupervisionDocument(
                name=form.cleaned_data['document'].name,
                document=base64.b64encode(form.cleaned_data['document'].read()),
                data_type=form.cleaned_data['document'].content_type,
                domain=self.domain
            )
            supervision_document.save()
        return HttpResponseRedirect(
            reverse(self.urlname, args=[self.domain])
        )

    def _ils_make_url(self, cls):
        params = '?location_id=%s&filter_by_program=%s&datespan_type=%s&datespan_first=%s&datespan_second=%s'
        return make_url(cls, self.domain, params, (
            self.request.GET.get('location_id', ''),
            self.request.GET.get('filter_by_program', ''),
            self.request.GET.get('datespan_type', ''),
            self.request.GET.get('datespan_first', ''),
            self.request.GET.get('datespan_second', ''),
        ))

    @property
    def report_links(self):
        return [
            ('Dashboard Report', self._ils_make_url(DashboardReport)),
            ('Stock On Hand', self._ils_make_url(StockOnHandReport)),
            ('R&R', self._ils_make_url(RRreport)),
            ('Delivery', self._ils_make_url(DeliveryReport)),
            ('Supervision', self._ils_make_url(SupervisionReport))
        ]

    @property
    def main_context(self):
        main_context = super(SupervisionDocumentListView, self).main_context
        main_context.update({
            'form': SupervisionDocumentForm(),
            'documents': SupervisionDocument.objects.filter(domain=self.domain),
            'is_user_domain_admin': self.request.couch_user.is_domain_admin(self.domain),
            'report_links': self.report_links<commit_msg>Tweak in supervisions url<commit_after>import base64
import StringIO
from datetime import datetime
import json
from django.contrib import messages
from django.core.urlresolvers import reverse, reverse_lazy
from django.db.models import Count, Q
from django.http.response import HttpResponseRedirect, Http404
from django.shortcuts import get_object_or_404
from django.utils.decorators import method_decorator
from django.views.generic.base import TemplateView
from django.views.generic.edit import DeleteView
from django.views.generic.list import ListView
from corehq.apps.commtrack.models import StockState
from corehq.apps.products.models import SQLProduct
from corehq.apps.domain.views import BaseDomainView, DomainViewMixin
from corehq.apps.locations.models import SQLLocation
from corehq.apps.sms.mixin import VerifiedNumber
from corehq.apps.sms.models import SMSLog
from corehq.apps.sms.util import clean_phone_number
from corehq.apps.users.models import CommCareUser, WebUser, UserRole
from django.http import HttpResponse
from django.utils.translation import ugettext_noop
from django.views.decorators.http import require_POST
from corehq.apps.domain.decorators import domain_admin_required
from corehq.const import SERVER_DATETIME_FORMAT_NO_SEC
from custom.ilsgateway import DashboardReport
from custom.ilsgateway.comparison_reports import ProductAvailabilityReport
from custom.ilsgateway.forms import SupervisionDocumentForm
from custom.ilsgateway.stock_data import ILSStockDataSynchronization
from custom.ilsgateway.tanzania import make_url
from custom.ilsgateway.tanzania.reminders.delivery import send_delivery_reminder
from custom.ilsgateway.tanzania.reminders.randr import send_ror_reminder
from custom.ilsgateway.tanzania.reminders.stockonhand import send_soh_reminder
from custom.ilsgateway.tanzania.reminders.supervision import send_supervision_reminder
from custom.ilsgateway.tanzania.reports.delivery import DeliveryReport
from custom.ilsgateway.tanzania.reports.randr import RRreport
from custom.ilsgateway.tanzania.reports.stock_on_hand import StockOnHandReport
from custom.ilsgateway.tanzania.reports.supervision import SupervisionReport
from custom.ilsgateway.tasks import clear_report_data
from casexml.apps.stock.models import StockTransaction
from custom.logistics.models import StockDataCheckpoint
from custom.logistics.tasks import fix_groups_in_location_task, resync_web_users
from custom.ilsgateway.api import ILSGatewayAPI
from custom.logistics.tasks import stock_data_task
from custom.ilsgateway.api import ILSGatewayEndpoint
from custom.ilsgateway.models import ILSGatewayConfig, ReportRun, SupervisionDocument, DeliveryGroups, ILSNotes, \
    ProductAvailabilityData
from custom.ilsgateway.tasks import report_run, ils_clear_stock_data_task, \
    ils_bootstrap_domain_task
from custom.logistics.views import BaseConfigView, BaseRemindersTester


class GlobalStats(BaseDomainView):
    section_name = 'Global Stats'
    section_url = ""
    template_name = "ilsgateway/global_stats.html"
    show_supply_point_types = False
    root_name = 'MOHSW'

    @property
    def main_context(self):
        contacts = CommCareUser.by_domain(self.domain, reduce=True)
        web_users = WebUser.by_domain(self.domain)
        web_users_admins = web_users_read_only = 0
        facilities = SQLLocation.objects.filter(domain=self.domain, location_type__name__iexact='FACILITY')
        admin_role_list = UserRole.by_domain_and_name(self.domain, 'Administrator')
        if admin_role_list:
            admin_role = admin_role_list[0]
        else:
            admin_role = None
        for web_user in web_users:
            dm = web_user.get_domain_membership(self.domain)
            if admin_role and dm.role_id == admin_role.get_id:
                web_users_admins += 1
            else:
                web_users_read_only += 1

        main_context = super(GlobalStats, self).main_context
        entities_reported_stock = SQLLocation.objects.filter(
            domain=self.domain,
            location_type__administrative=False
        ).count()

        context = {
            'root_name': self.root_name,
            'country': SQLLocation.objects.filter(domain=self.domain,
                                                  location_type__name__iexact=self.root_name).count(),
            'region': SQLLocation.objects.filter(domain=self.domain, location_type__name__iexact='region').count(),
            'district': SQLLocation.objects.filter(
                domain=self.domain,
                location_type__name__iexact='district'
            ).count(),
            'entities_reported_stock': entities_reported_stock,
            'facilities': len(facilities),
            'contacts': contacts[0]['value'] if contacts else 0,
            'web_users': len(web_users),
            'web_users_admins': web_users_admins,
            'web_users_read_only': web_users_read_only,
            'products': SQLProduct.objects.filter(domain=self.domain, is_archived=False).count(),
            'product_stocks': StockState.objects.filter(sql_product__domain=self.domain).count(),
            'stock_transactions': StockTransaction.objects.filter(report__domain=self.domain).count(),
            'inbound_messages': SMSLog.count_incoming_by_domain(self.domain),
            'outbound_messages': SMSLog.count_outgoing_by_domain(self.domain)
        }

        if self.show_supply_point_types:
            counts = SQLLocation.objects.values('location_type__name').filter(domain=self.domain).annotate(
                Count('location_type')
            ).order_by('location_type__name')
            context['location_types'] = counts
        main_context.update(context)
        return main_context


class ILSConfigView(BaseConfigView):
    config = ILSGatewayConfig
    urlname = 'ils_config'
    sync_urlname = 'sync_ilsgateway'
    sync_stock_url = 'ils_sync_stock_data'
    clear_stock_url = 'ils_clear_stock_data'
    page_title = ugettext_noop("ILSGateway")
    template_name = 'ilsgateway/ilsconfig.html'
    source = 'ilsgateway'


class SupervisionDocumentListView(BaseDomainView):
    section_name = 'Supervision Documents'
    section_url = ""
    template_name = "ilsgateway/supervision_docs.html"
    urlname = 'supervision'

    def dispatch(self, request, *args, **kwargs):
        if not self.request.couch_user.is_web_user():
            raise Http404()
        return super(SupervisionDocumentListView, self).dispatch(request, *args, **kwargs)

    @method_decorator(domain_admin_required)
    def post(self, *args, **kwargs):
        request = args[0]
        form = SupervisionDocumentForm(request.POST, request.FILES)
        if form.is_valid():
            supervision_document = SupervisionDocument(
                name=form.cleaned_data['document'].name,
                document=base64.b64encode(form.cleaned_data['document'].read()),
                data_type=form.cleaned_data['document'].content_type,
                domain=self.domain
            )
            supervision_document.save()
        return HttpResponseRedirect(
            reverse(self.urlname, args=[self.domain])
        )

    def _ils_make_url(self, cls):
        params = '?location_id=%s&filter_by_program=%s&datespan_type=%s&datespan_first=%s&datespan_second=%s'
        return make_url(cls, self.domain, params, (
            self.request.GET.get(
                'location_id', get_object_or_404(
                    SQLLocation, domain=self.domain, location_type__name='MOHSW'
                ).location_id
            ),
            self.request.GET.get('filter_by_program', ''),
            self.request.GET.get('datespan_type', ''),
            self.request.GET.get('datespan_first', ''),
            self.request.GET.get('datespan_second', ''),
        ))

    @property
    def report_links(self):
        return [
            ('Dashboard Report', self._ils_make_url(DashboardReport)),
            ('Stock On Hand', self._ils_make_url(StockOnHandReport)),
            ('R&R', self._ils_make_url(RRreport)),
            ('Delivery', self._ils_make_url(DeliveryReport)),
            ('Supervision', self._ils_make_url(SupervisionReport))
        ]

    @property
    def main_context(self):
        main_context = super(SupervisionDocumentListView, self).main_context
        main_context.update({
            'form': SupervisionDocumentForm(),
            'documents': SupervisionDocument.objects.filter(domain=self.domain),
            'is_user_domain_admin': self.request.couch_user.is_domain_admin(self.domain),
            'report_links': self.report_links
{'fraction_non_alphanumeric': 0.08684644371172869, 'fraction_numerical': 0.0025906735751295338, 'mean_word_length': 3.5843454790823213, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2613392', 'n_tokens_mistral': 5004, 'n_tokens_neox': 4869, 'n_words': 862}