Skip to content
Snippets Groups Projects
export.py 2.86 KiB
Newer Older
# coding=utf-8
import csv

from django.contrib.auth.decorators import login_required
from django.http import HttpResponse

from notifications import get_today_midnight_date
from . import e500_error, wrap_response
from ..models import Subject, Appointment


@login_required
Piotr Gawron's avatar
Piotr Gawron committed
def export_to_csv(request, data_type="subjects"):
    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(content_type='text/csv')
Piotr Gawron's avatar
Piotr Gawron committed
    response['Content-Disposition'] = 'attachment; filename="' + data_type + '-' + get_today_midnight_date().strftime(
        "%Y-%m-%d") + '.csv"'

Piotr Gawron's avatar
Piotr Gawron committed
    if data_type == "subjects":
        data = get_subjects_as_array()
    elif data_type == "appointments":
        data = get_appointments_as_array()
    else:
        return e500_error(request)
Piotr Gawron's avatar
Piotr Gawron committed
    writer = csv.writer(response, quotechar=str(u'"'), quoting=csv.QUOTE_ALL)
    for row in data:
        writer.writerow(row)

Piotr Gawron's avatar
Piotr Gawron committed
def get_subjects_as_array():
    result = []
    subject_fields = []
    for field in Subject._meta.fields:
        if field.name != "ID":
            subject_fields.append(field)

    field_names = []
    for field in subject_fields:
        field_names.append(field.verbose_name)

Piotr Gawron's avatar
Piotr Gawron committed
    result.append(field_names)

    subjects = Subject.objects.order_by('-last_name')
    for subject in subjects:
        row = []
        for field in subject_fields:
            row.append(getattr(subject, field.name))
Piotr Gawron's avatar
Piotr Gawron committed
        result.append([unicode(s).replace("\n", ";").replace("\r", ";").encode("utf-8") for s in row])
    return result
Piotr Gawron's avatar
Piotr Gawron committed
def get_appointments_as_array():
    result = []
    appointments_fields = []
    for field in Appointment._meta.fields:
Piotr Gawron's avatar
Piotr Gawron committed
        if field.name != "visit" and field.name != "id" and \
                        field.name != "worker_assigned" and field.name != "appointment_types" and \
                        field.name != "room" and field.name != "flying_team":
            appointments_fields.append(field)

    field_names = ['ND number', 'Family name', 'Name', 'Visit']
    for field in appointments_fields:
        field_names.append(field.verbose_name)

Piotr Gawron's avatar
Piotr Gawron committed
    result.append(field_names)

    appointments = Appointment.objects.order_by('-datetime_when')

    for appointment in appointments:
        row = [appointment.visit.subject.nd_number, appointment.visit.subject.last_name,
               appointment.visit.subject.first_name, appointment.visit.follow_up_title()]
        for field in appointments_fields:
            row.append(getattr(appointment, field.name))
        type_string = ""
Piotr Gawron's avatar
Piotr Gawron committed
        for appointment_type in appointment.appointment_types.all():
            type_string += appointment_type.code + ","
        row.append(type_string)
Piotr Gawron's avatar
Piotr Gawron committed
        result.append([unicode(s).replace("\n", ";").replace("\r", ";").encode("utf-8") for s in row])
    return result


def export(request):
    return wrap_response(request, 'export/index.html', {})