Exemplo n.º 1
0
def load_messages(request, talk):
    context = {
        'request': request,
    }

    user = request.user
    favorites = request.GET.get('favorite', False)
    mines = request.GET.get('mine', False)
    showing = request.GET.get('showing', '')
    n_views = 0

    if not favorites:
        if mines:
            messages = TalkMessages.objects.filter(talk__id=talk, user=user)
        else:
            messages = TalkMessages.objects.filter(talk__id=talk)
    else:
        if mines:
            messages = TalkMessages.objects.filter(
                talk__id=talk,
                chat_favorites_message__isnull=False,
                chat_favorites_message__user=user,
                user=user)
        else:
            messages = TalkMessages.objects.filter(
                talk__id=talk,
                chat_favorites_message__isnull=False,
                chat_favorites_message__user=user)

    if showing:  #Exclude ajax creation messages results
        showing = showing.split(',')
        messages = messages.exclude(id__in=showing)

    has_page = request.GET.get('page', None)

    paginator = Paginator(messages.order_by("-create_date"), 20)

    try:
        page_number = int(request.GET.get('page', 1))
    except ValueError:
        raise Http404

    try:
        page_obj = paginator.page(page_number)
    except EmptyPage:
        raise Http404

    context['messages'] = page_obj.object_list

    response = render_to_string("chat/_list_messages.html", context, request)

    return JsonResponse({
        "messages": response,
        "count": messages.count(),
        "num_pages": paginator.num_pages,
        "num_page": page_obj.number
    })
Exemplo n.º 2
0
def get_patient_stats_detail_context(report_date, patient_id):
    """

    """
    context = {}
    days = get_day_of_month(report_date.year, report_date.month, -1).day
    num_days_in_report_month = days
    datetime_end_month = datetime.datetime(report_date.year, report_date.month,
                                           num_days_in_report_month, 23, 59)
    datetime_start_month = datetime.datetime(report_date.year,
                                             report_date.month, 01, 0, 0)

    patient = get_object_or_404(Patient, id=patient_id)
    context["daily_doses"] = patient.daily_doses

    context["patient"] = patient
    if not report_date:
        report_date = datetime.now()

    wp_usage_rows = []
    for day in range(1, days + 1):
        row_date = datetime.date(report_date.year, report_date.month, day)
        if is_patient_out_of_date_range(patient, row_date):
            continue

        row = []
        row.append(row_date)
        msg_count = patient.wisepill_messages.filter(
            timestamp__year=row_date.year,
            timestamp__month=row_date.month,
            timestamp__day=row_date.day).count()
        row.append(msg_count)
        wp_usage_rows.append(row)

    pill_count_data = {}
    pills_missed_data = {}
    pill_count_data["patient_id"] = patient.subject_number
    pills_missed = patient.pillsmissed_set.filter(
        date__range=(datetime_start_month,
                     datetime_end_month)).order_by('-date')[:4]
    weeks = []
    for pm in pills_missed:
        week_start = pm.date - datetime.timedelta(days=7)
        week_end = pm.date
        num_missed = pm.num_missed
        weeks.append({
            'pills_missed': num_missed,
            'week_start': week_start,
            'week_end': week_end,
            'received_on': pm.date
        })

    pills_missed_data[patient] = weeks

    pm_weekly_aggregate = []
    num_weeks_in_report_month = num_days_in_report_month / 7
    for week in range(0, num_weeks_in_report_month + 1):
        week_start = (datetime.date(report_date.year, report_date.month, 1) +
                      datetime.timedelta(days=week * 7))
        week_end = week_start + datetime.timedelta(days=7)
        pm_week_data = {}
        if is_patient_out_of_date_range(patient, week_start, week_end):
            continue

        pills_missed_set = patient.pillsmissed_set.filter(
            date__gt=week_start, date__lt=week_end,
            source=1)  # source = 1 means SMS
        if len(pills_missed_set) > 0:
            pills_missed_week = pills_missed_set.aggregate(
                Sum('num_missed'))["num_missed__sum"]
        else:
            pills_missed_week = "No Response/No Query"

        pm_week_data["sum"] = pills_missed_week
        pm_week_data["week_start"] = week_start
        pm_week_data["week_end"] = week_end
        pm_weekly_aggregate.append(pm_week_data)

    patient_connections = patient.contact.connection_set.all()
    messages = Message.objects.filter(connection__in=patient_connections)

    messages = messages.exclude(text__contains='start tree').exclude(
        text__contains='TimeOut')  #exclude internal messages
    messages = messages.filter(
        date__range=(datetime_start_month, datetime_end_month)).order_by(
            '-date')  #stick to the report month

    reminders = Reminder.objects.all()

    context["pat_messages"] = messages
    context["wp_usage_rows"] = wp_usage_rows
    context["pm_weeks"] = pills_missed_data
    context["pm_weekly"] = pm_weekly_aggregate
    return context
Exemplo n.º 3
0
def get_patient_stats_detail_context(report_date, patient_id):
    """

    """
    context = {}
    days = get_day_of_month(report_date.year,report_date.month,-1).day
    num_days_in_report_month = days
    datetime_end_month = datetime.datetime(report_date.year,report_date.month,num_days_in_report_month,23,59)
    datetime_start_month = datetime.datetime(report_date.year, report_date.month,01,0,0)

    patient = get_object_or_404(Patient, id=patient_id)
    context["daily_doses"] = patient.daily_doses

    context["patient"] = patient
    if not report_date:
        report_date = datetime.now()

    wp_usage_rows = []
    for day in range(1,days+1):
        row_date = datetime.date(report_date.year,report_date.month, day)
        if is_patient_out_of_date_range(patient, row_date):
            continue
        
        row = []
        row.append(row_date)
        msg_count = patient.wisepill_messages.filter(timestamp__year=row_date.year,timestamp__month=row_date.month,timestamp__day=row_date.day).count()
        row.append(msg_count)
        wp_usage_rows.append(row)

    pill_count_data = {}
    pills_missed_data = {}
    pill_count_data["patient_id"] = patient.subject_number
    pills_missed = patient.pillsmissed_set.filter(date__range=(datetime_start_month,datetime_end_month)).order_by('-date')[:4]
    weeks = []
    for pm in pills_missed:
        week_start = pm.date-datetime.timedelta(days=7)
        week_end = pm.date
        num_missed = pm.num_missed
        weeks.append({
            'pills_missed': num_missed,
            'week_start': week_start,
            'week_end': week_end,
            'received_on': pm.date
        })

    pills_missed_data[patient] = weeks

    pm_weekly_aggregate = []
    num_weeks_in_report_month = num_days_in_report_month / 7
    for week in range(0,num_weeks_in_report_month + 1):
        week_start = (datetime.date(report_date.year,report_date.month,1) + datetime.timedelta(days=week*7))
        week_end = week_start + datetime.timedelta(days=7)
        pm_week_data = {}
        if is_patient_out_of_date_range(patient,week_start,week_end):
            continue

        pills_missed_set = patient.pillsmissed_set.filter(date__gt=week_start, date__lt=week_end, source=1) # source = 1 means SMS
        if len(pills_missed_set) > 0:
            pills_missed_week = pills_missed_set.aggregate(Sum('num_missed'))["num_missed__sum"]
        else:
            pills_missed_week = "No Response/No Query"


        pm_week_data["sum"] = pills_missed_week
        pm_week_data["week_start"] = week_start
        pm_week_data["week_end"] = week_end
        pm_weekly_aggregate.append(pm_week_data)



    patient_connections = patient.contact.connection_set.all()
    messages = Message.objects.filter(connection__in=patient_connections)

    messages = messages.exclude(text__contains='start tree').exclude(text__contains='TimeOut') #exclude internal messages
    messages = messages.filter(date__range=(datetime_start_month, datetime_end_month)).order_by('-date') #stick to the report month

    reminders = Reminder.objects.all()

    context["pat_messages"] = messages
    context["wp_usage_rows"] = wp_usage_rows
    context["pm_weeks"] = pills_missed_data
    context["pm_weekly"] = pm_weekly_aggregate
    return context