Пример #1
0
def reports(request):
    clinics = Provider.objects.values('clinic__id','clinic__name').distinct()
    
    zones = Case.objects.order_by("zone").values('zone', 'zone__name').distinct()
    p = Case.objects.order_by("zone").values('provider', 'provider__user__first_name', 'provider__user__last_name', 'zone').distinct()
    providers = []
    for provider in p:
        tmp = {}
        tmp['id'] = provider['provider']
        tmp['name'] = provider['provider__user__last_name'] + " " + provider['provider__user__first_name']
        tmp['zone'] = provider['zone']
        providers.append(tmp)  
        
    now = datetime.today()
    first   = Case.objects.order_by('created_at')[:1][0]
    date    = first.created_at

    months=[]
    while ((now - date).days > 0):
        months.append({'id': date.strftime("%m%Y"), 'label': date.strftime("%B %Y"), 'date': date})
        date = next_month(date)
    
    context = {
        "app": app,
        "clinics": clinics,
        "providers": providers,
        "zones": zones,
        "months": months
    }
    return as_html(request, "reports/reports.html", context)
Пример #2
0
def http_view(request):
   
    has_provider = True
    context = {
		"app": app,
    }
    
    try:
        mobile = request.user.provider.mobile
        if request.method == "POST":
            messageform = MessageForm(request.POST)
            if messageform.is_valid():
                result = message_users(mobile, **messageform.cleaned_data)
                context["msg"] = result
        else:
            messageform = MessageForm()
    except ObjectDoesNotExist:
        has_provider = False
        messageform = None

    context.update({
			"app": app,
            "message_form": messageform,
            "has_provider": has_provider,
        })

    return as_html(request, "httpview.html", context)
Пример #3
0
def chwstatus_view(request, output="html"):
    if output == "pdf":
        return chwstatus_pdf(request)

    now = datetime.today()
    first = Case.objects.order_by('created_at')[:1][0]
    date = first.created_at

    months = []
    while ((now - date).days > 0):
        months.append({
            'id': date.strftime("%m%Y"),
            'label': date.strftime("%B %Y"),
            'date': date
        })
        date = next_month(date)

    #YO
    providers = Provider.objects.order_by("clinic").all()

    duration_start = datetime.today().replace(day=1, month=7)
    duration_end = datetime.now()
    # providers array
    ps = []

    for provider in providers:
        # create provider dict
        p = {}
        p['provider'] = provider
        cases = Case.objects.filter(provider=provider)
        p['num_cases'] = cases.count()
        p['num_malaria_reports'] = ReportMalaria.objects.filter(
            entered_at__lte=duration_end,
            entered_at__gte=duration_start).filter(provider=provider).count()
        p['num_muac_reports'] = ReportMalnutrition.objects.filter(
            entered_at__lte=duration_end,
            entered_at__gte=duration_start).filter(provider=provider).count()
        p['sms_sent'] = MessageLog.objects.filter(
            created_at__lte=duration_end,
            created_at__gte=duration_start).filter(
                sent_by=provider.user_id).count()
        p['sms_processed'] = MessageLog.objects.filter(
            created_at__lte=duration_end,
            created_at__gte=duration_start).filter(sent_by=provider.user_id,
                                                   was_handled=True).count()
        p['sms_refused'] = MessageLog.objects.filter(
            created_at__lte=duration_end,
            created_at__gte=duration_start).filter(sent_by=provider.user_id,
                                                   was_handled=False).count()
        if p['sms_sent'] != 0:
            p['sms_rate'] = round(
                float(p['sms_processed']) / float(p['sms_sent']) * 100)
        else:
            p['sms_rate'] = 0
        ps.append(p)

    context = {"app": app, "providers": ps, "months": months}

    return as_html(request, "chwstatusview.html", context)
Пример #4
0
def provider_list(request):
    nonhtml, tables = get(request, (["provider", Q()],))
    if nonhtml:
        return nonhtml
    context = {
		"app": app,
        "provider": tables[0],
    }
    return as_html(request, "providerlist.html", context)
Пример #5
0
def provider_list(request):
    nonhtml, tables = get(request, (["provider", Q()], ))
    if nonhtml:
        return nonhtml
    context = {
        "app": app,
        "provider": tables[0],
    }
    return as_html(request, "providerlist.html", context)
Пример #6
0
def search_view(request):
    term = request.GET.get("q")
    query = Q(id__icontains=term) | \
            Q(first_name__icontains=term) | \
            Q(last_name__icontains=term)

    nonhtml, tables = get(request, [ ["case", query], ])
    if nonhtml: 
        return nonhtml

    return as_html(request, "searchview.html", { "search": tables[0], })
Пример #7
0
def district_view(request):
    district = request.GET.get("d")
    context = {
		"app": app,
        "districts": Zone.objects.all(),
    }
    if district:
        nonhtml, tables = get(request, (["case", Q(zone=district)],))
        if nonhtml:
            return nonhtml
        context["cases"] = tables[0]

    return as_html(request, "districtview.html", context)
Пример #8
0
def district_view(request):
    district = request.GET.get("d")
    context = {
        "app": app,
        "districts": Zone.objects.all(),
    }
    if district:
        nonhtml, tables = get(request, (["case", Q(zone=district)], ))
        if nonhtml:
            return nonhtml
        context["cases"] = tables[0]

    return as_html(request, "districtview.html", context)
Пример #9
0
def search_view(request):
    term = request.GET.get("q")
    query = Q(id__icontains=term) | \
            Q(first_name__icontains=term) | \
            Q(last_name__icontains=term)

    nonhtml, tables = get(request, [
        ["case", query],
    ])
    if nonhtml:
        return nonhtml

    return as_html(request, "searchview.html", {
        "search": tables[0],
    })
Пример #10
0
def provider_view(request, object_id):
    provider = get_object_or_404(Provider, id=object_id)
    nonhtml, tables = get(request, (["case", Q(provider=provider)], [
        "message", Q(sent_by=provider.user)
    ], ["event", Q(content_type="provider", object_id=provider.pk)]))
    if nonhtml:
        return nonhtml
    context = {
        "app": app,
        "object": provider,
        "cases": tables[0],
        "messages": tables[1],
        "event": tables[2]
    }
    return as_html(request, "providerview.html", context)
Пример #11
0
def globalreports_view(request):

    now = datetime.today()
    first   = Case.objects.order_by('created_at')[:1][0]
    date    = first.created_at

    months=[]
    while ((now - date).days > 0):
        months.append({'id': date.strftime("%m%Y"), 'label': date.strftime("%B %Y"), 'date': date})
        date = next_month(date)

    context = {
		"app": app,
	    "providers": Provider.objects.filter(active=True),
	    "months": months
    }
    return as_html(request, "globalreportsview.html", context)
Пример #12
0
def provider_view(request, object_id):
    provider = get_object_or_404(Provider, id=object_id)
    nonhtml, tables = get(request, (
        ["case", Q(provider=provider)],
        ["message", Q(sent_by=provider.user)],
        ["event", Q(content_type="provider", object_id=provider.pk)]
        ))
    if nonhtml:
        return nonhtml
    context = {
		"app": app,
        "object": provider,
        "cases": tables[0],
        "messages": tables[1],
        "event": tables[2]
    }
    return as_html(request, "providerview.html", context)
Пример #13
0
def chwstatus_view(request, output="html"):
    if output == "pdf":
        return chwstatus_pdf(request)
    
    now = datetime.today()
    first   = Case.objects.order_by('created_at')[:1][0]
    date    = first.created_at

    months=[]
    while ((now - date).days > 0):
        months.append({'id': date.strftime("%m%Y"), 'label': date.strftime("%B %Y"), 'date': date})
        date = next_month(date)

    #YO
    providers = Provider.objects.order_by("clinic").all()

    duration_start = datetime.today().replace(day=1,month=7)
    duration_end = datetime.now()
    # providers array
    ps = []

    for provider in providers:
        # create provider dict
        p = {}
        p['provider'] = provider
        cases   = Case.objects.filter(provider=provider)
        p['num_cases'] = cases.count()
        p['num_malaria_reports'] = ReportMalaria.objects.filter(entered_at__lte=duration_end, entered_at__gte=duration_start).filter(provider=provider).count()
        p['num_muac_reports'] = ReportMalnutrition.objects.filter(entered_at__lte=duration_end, entered_at__gte=duration_start).filter(provider=provider).count()
        p['sms_sent'] = MessageLog.objects.filter(created_at__lte=duration_end, created_at__gte=duration_start).filter(sent_by=provider.user_id).count()
        p['sms_processed'] = MessageLog.objects.filter(created_at__lte=duration_end, created_at__gte=duration_start).filter(sent_by=provider.user_id, was_handled=True).count()
        p['sms_refused'] = MessageLog.objects.filter(created_at__lte=duration_end, created_at__gte=duration_start).filter(sent_by=provider.user_id,was_handled=False).count()
        if p['sms_sent'] != 0:
            p['sms_rate'] = round(float(p['sms_processed'])/float(p['sms_sent'])*100)
        else:
            p['sms_rate'] = 0
        ps.append(p)
            
    context = {
        "app": app,
        "providers": ps,
        "months": months
    }

    return as_html(request, "chwstatusview.html", context)
Пример #14
0
def case_view(request, object_id):
    case = get_object_or_404(Case, id=object_id)
    nonhtml, tables = get(request, [
        ["malnutrition", Q(case=case)],
        ["diagnosis", Q(case=case)],
        ["malaria", Q(case=case)],
        ["event", Q(content_type="case", object_id=object_id)],
        ])

    if nonhtml:
        return nonhtml

    results = ReportMalnutrition.objects\
        .filter(Q(case=case)).exclude(muac=None)\
        .order_by("-entered_at")\
        .values_list("muac", "entered_at")
    results1 = [ [ time.mktime(r[1].timetuple()) * 1000,  r[0] ] for r in results ]

    muac_data = [{'data': results1, 'label': "Muac"}]
    
    g = FlotGraph()
    g.set_data(muac_data)
    g.set_xaxis_mode("time")
    g.set_key_position(0)
    g.set_display_title("Muac over time")
    g.set_width(400)
    g.set_height(200)
    g.set_title("muac")
    g.set_zoomable(1)
    g.set_key_position(1)
    g.set_xaxis_min_ticksize("[1, \"day\"]")
    g.set_time_format("%b %d")
    g.generate_javascript()

    context = {
		"app": app,
        "object": case,
        "malnutrition": tables[0],
        "diagnosis": tables[1],
        "malaria": tables[2],
        "event": tables[3],
        "graph": g,
        "raw_data": results1,
    }
    return as_html(request, "caseview.html", context)
Пример #15
0
def case_view(request, object_id):
    case = get_object_or_404(Case, id=object_id)
    nonhtml, tables = get(request, [
        ["malnutrition", Q(case=case)],
        ["diagnosis", Q(case=case)],
        ["malaria", Q(case=case)],
        ["event", Q(content_type="case", object_id=object_id)],
        ])

    if nonhtml:
        return nonhtml

    context = {
		"app": app,
        "object": case,
        "malnutrition": tables[0],
        "diagnosis": tables[1],
        "malaria": tables[2],
        "event": tables[3],
    }
    return as_html(request, "caseview.html", context)
Пример #16
0
def globalreports_view(request):

    now = datetime.today()
    first = Case.objects.order_by('created_at')[:1][0]
    date = first.created_at

    months = []
    while ((now - date).days > 0):
        months.append({
            'id': date.strftime("%m%Y"),
            'label': date.strftime("%B %Y"),
            'date': date
        })
        date = next_month(date)

    context = {
        "app": app,
        "providers": Provider.objects.filter(active=True),
        "months": months
    }
    return as_html(request, "globalreportsview.html", context)
Пример #17
0
def login(request):
    context = {}
    if request.GET.has_key("msg"):
        msg = messages.get(request.GET["msg"], "")
        if msg:
            context["msg"] = msg

    if request.method == "POST" and not request.user.is_authenticated():
        form = LoginForm(request.POST)
        if form.is_valid():
            user = auth.authenticate(username=form.cleaned_data["username"],
                                     password=form.cleaned_data["password"])
            if user:
                if user.is_active and user.is_staff:
                    auth.login(request, user)
                    log(user, "user_logged_in")
                    return HttpResponseRedirect("/")
            return HttpResponseRedirect("/accounts/login/?msg=login_failed")
    else:
        form = LoginForm(request.POST)
    context["form"] = form
    return as_html(request, "login.html", context)
Пример #18
0
def dashboard(request):
    nonhtml, tables = get(request, [
        ["case", Q()],
        ["event", Q()],
        ["message", Q()],
    ])
    if nonhtml:
        return nonhtml

    has_provider = True
    context = {
		"app": app,
        "case_table": tables[0],
        "event_table": tables[1],
        "message_table": tables[2]
    }    

    try:
        mobile = request.user.provider.mobile
        if request.method == "POST":
            messageform = MessageForm(request.POST)
            if messageform.is_valid():
                result = message_users(mobile, **messageform.cleaned_data)
                context["msg"] = result
        else:
            messageform = MessageForm()
    except ObjectDoesNotExist:
        has_provider = False
        messageform = None

    context.update({
			"app": app,
            "message_form": messageform,
            "has_provider": has_provider,
            "summary": get_summary(),
            "graph": get_graph()
        })

    return as_html(request, "dashboard.html", context)
Пример #19
0
def dashboard(request):
    nonhtml, tables = get(request, [
        ["case", Q()],
        ["event", Q()],
        ["message", Q()],
    ])
    if nonhtml:
        return nonhtml

    has_provider = True
    context = {
        "app": app,
        "case_table": tables[0],
        "event_table": tables[1],
        "message_table": tables[2]
    }

    try:
        mobile = request.user.provider.mobile
        if request.method == "POST":
            messageform = MessageForm(request.POST)
            if messageform.is_valid():
                result = message_users(mobile, **messageform.cleaned_data)
                context["msg"] = result
        else:
            messageform = MessageForm()
    except ObjectDoesNotExist:
        has_provider = False
        messageform = None

    context.update({
        "app": app,
        "message_form": messageform,
        "has_provider": has_provider,
        "summary": get_summary(),
        "graph": get_graph()
    })

    return as_html(request, "dashboard.html", context)
Пример #20
0
def login(request):
    context = {}
    if request.GET.has_key("msg"):
        msg = messages.get(request.GET["msg"], "")
        if msg:
            context["msg"] = msg
    
    if request.method == "POST" and not request.user.is_authenticated():
        form = LoginForm(request.POST)
        if form.is_valid():
            user = auth.authenticate(
                username=form.cleaned_data["username"], 
                password=form.cleaned_data["password"])
            if user:
                if user.is_active and user.is_staff:
                    auth.login(request, user)
                    log(user, "user_logged_in")
                    return HttpResponseRedirect("/")
            return HttpResponseRedirect("/accounts/login/?msg=login_failed")
    else:
        form = LoginForm(request.POST)
    context["form"] = form
    return as_html(request, "login.html", context)
Пример #21
0
        ["diarrhea", Q(case=case)],
    ])

    if nonhtml:
        return nonhtml

    context = {
        "app": app,
        "object": case,
        "malnutrition": tables[0],
        "diagnosis": tables[1],
        "malaria": tables[2],
        "event": tables[3],
        "diarrhea": tables[4],
    }
    return as_html(request, "caseview.html", context)


@login_required
def district_view(request):
    district = request.GET.get("d")
    context = {
        "app": app,
        "districts": Zone.objects.all(),
    }
    if district:
        nonhtml, tables = get(request, (["case", Q(zone=district)], ))
        if nonhtml:
            return nonhtml
        context["cases"] = tables[0]