Example #1
0
def logframe_view(request, program):
    """
    Logframe view
    """
    serialized_program = LogframeProgramSerializer.load(program)
    context = {'js_context': serialized_program.data}
    return render(request, 'indicators/logframe/logframe.html', context)
 def test_values(self):
     serialized = LogframeProgramSerializer.load(pk=self.program.pk).data
     self.assertEqual(serialized['name'], "Test program name")
     self.assertEqual(serialized['levels'][0]['display_name'], "Test Tier 1: Test Goal Level")
     self.assertEqual(len(serialized['levels'][0]['indicators']), 0)
     self.assertEqual(len(serialized['levels'][0]['child_levels']), 1)
     self.assertEqual(len(serialized['levels'][1]['indicators']), 2)
     self.assertEqual(len(serialized['unassigned_indicators']), 1)
     self.assertEqual(serialized['rf_chain_sort_label'], "by Test Tier 2 chain")
Example #3
0
def logframe_excel_view(request, program):
    """
    Logframe Excel Export view
    """
    program = LogframeProgramSerializer.load(program).data
    wb = openpyxl.Workbook()
    wb.remove(wb.active)
    ws = wb.create_sheet(ugettext('Logframe'))
    add_title_cell(ws, 1, 1, clean_unicode(program['name']))
    ws.merge_cells(start_row=1, end_row=1, start_column=1, end_column=4)
    add_title_cell(ws, 2, 1, ugettext('Logframe'))
    ws.merge_cells(start_row=2, end_row=2, start_column=1, end_column=4)
    for col, name in enumerate([
            ugettext('Result level'),
            ugettext('Indicators'),
            ugettext('Means of verification'),
            ugettext('Assumptions')
    ]):
        add_header_cell(ws, 3, col + 1, name)
        ws.column_dimensions[openpyxl.utils.get_column_letter(col +
                                                              1)].width = 50
    levels = program['levels']
    if request.GET.get('groupby') == "2":
        sorted_levels = sorted(levels,
                               key=itemgetter('level_depth', 'ontology'))
    else:
        levels_by_pk = {l['pk']: l for l in levels}
        sorted_levels = []
        for level in [
                l for l in sorted(levels, key=itemgetter('ontology'))
                if l['level_depth'] == 1
        ]:
            sorted_levels += get_child_levels(level, levels_by_pk)
        levels = sorted_levels
    row = 4
    for level in sorted_levels:
        merge_start = row
        cell = ws.cell(row=row, column=1)
        cell.value = clean_unicode(level['display_name'])
        cell.alignment = TOP_LEFT_ALIGN_WRAP
        cell.fill = LEVEL_ROW_FILL
        cell = ws.cell(row=row, column=4)
        cell.value = clean_unicode(level['assumptions'])
        cell.alignment = TOP_LEFT_ALIGN_WRAP
        for indicator in sorted(level['indicators'],
                                key=itemgetter('level_order')):
            cell = ws.cell(row=row, column=2)
            value = ugettext('Indicator')
            if program['manual_numbering']:
                value += u' {}'.format(
                    indicator['number']) if indicator['number'] else u''
            elif indicator['level_order_display'] or level['display_ontology']:
                value += u' {}{}'.format(level['display_ontology'],
                                         indicator['level_order_display'])
            value += u': {}'.format(clean_unicode(indicator['name']))
            cell.value = value
            cell.alignment = TOP_LEFT_ALIGN_WRAP
            cell = ws.cell(row=row, column=3)
            cell.value = clean_unicode(indicator['means_of_verification'])
            cell.alignment = TOP_LEFT_ALIGN_WRAP
            row += 1
        if merge_start == row:
            row += 1
        else:
            ws.merge_cells(start_row=merge_start,
                           end_row=row - 1,
                           start_column=1,
                           end_column=1)
            ws.merge_cells(start_row=merge_start,
                           end_row=row - 1,
                           start_column=4,
                           end_column=4)
        cell = ws.cell(row=merge_start, column=1)
        cell.border = BORDER_TOP
        cell = ws.cell(row=merge_start, column=2)
        cell.border = BORDER_TOP
        cell = ws.cell(row=merge_start, column=3)
        cell.border = BORDER_TOP
        cell = ws.cell(row=merge_start, column=4)
        cell.border = BORDER_TOP
    if program['unassigned_indicators']:
        merge_start = row
        cell = ws.cell(row=row, column=1)
        cell.value = ugettext(
            'Indicators unassigned to a results framework level')
        cell.alignment = TOP_LEFT_ALIGN_WRAP
        cell.fill = LEVEL_ROW_FILL
        for indicator in program['unassigned_indicators']:
            cell = ws.cell(row=row, column=2)
            value = ugettext('Indicator')
            if program['manual_numbering']:
                value += u' {}'.format(
                    indicator['number']) if indicator['number'] else u''
            cell.value = u'{}: {}'.format(value,
                                          clean_unicode(indicator['name']))
            cell.alignment = TOP_LEFT_ALIGN_WRAP
            cell = ws.cell(row=row, column=3)
            cell.value = clean_unicode(indicator['means_of_verification'])
            cell.alignment = TOP_LEFT_ALIGN_WRAP
            row += 1
        if merge_start == row:
            row += 1
        else:
            ws.merge_cells(start_row=merge_start,
                           end_row=row - 1,
                           start_column=1,
                           end_column=1)
            ws.merge_cells(start_row=merge_start,
                           end_row=row - 1,
                           start_column=4,
                           end_column=4)
        cell = ws.cell(row=merge_start, column=1)
        cell.border = BORDER_TOP
        cell = ws.cell(row=merge_start, column=2)
        cell.border = BORDER_TOP
        cell = ws.cell(row=merge_start, column=3)
        cell.border = BORDER_TOP
        cell = ws.cell(row=merge_start, column=4)
        cell.border = BORDER_TOP
    response = HttpResponse(content_type="application/ms-excel")
    response['Content-Disposition'] = u'attachment; filename="{}"'.format(
        u'{} - {}.xlsx'.format(program['name'], ugettext('Logframe')))
    wb.save(response)
    return response