export_tools.py 4.88 KB
Newer Older
1 2
# -*- coding: utf-8
from __future__ import unicode_literals
Patrick's avatar
Patrick committed
3

4
from django.contrib.sites.models import Site
5
from django.utils.text import slugify
6 7 8 9 10 11
from django.utils.translation import ugettext_lazy as _
from openpyxl.cell import get_column_letter
from openpyxl.style import NumberFormat
from openpyxl.styles import Border
from openpyxl.workbook import Workbook

chris's avatar
chris committed
12
from .const import *
Patrick's avatar
Patrick committed
13
from repanier.const import EMPTY_STRING
Patrick's avatar
Patrick committed
14
from repanier.models.staff import Staff
15 16 17 18
from repanier.tools import cap


def worksheet_setup_a4(worksheet, title1, title2, add_print_title=True):
19 20
    title1 = "%s" % title1
    worksheet.title = cap(slugify(title1), 31)
21 22 23 24 25 26 27 28 29 30 31 32
    worksheet.page_setup.paperSize = worksheet.PAPERSIZE_A4
    worksheet.page_setup.fitToPage = True
    worksheet.page_setup.fitToHeight = 0
    worksheet.page_setup.fitToWidth = 1
    worksheet.print_gridlines = True
    if add_print_title:
        worksheet.add_print_title(1, rows_or_cols='rows')
        worksheet.freeze_panes = 'A2'.encode("utf8")
    worksheet.header_footer.left_header.text = Site.objects.get_current().name
    worksheet.header_footer.left_footer.text = "%s" % (title2)
    worksheet.header_footer.center_footer.text = title1
    worksheet.header_footer.right_footer.text = 'Page &[Page]/&[Pages]'
Patrick's avatar
Patrick committed
33 34 35
    orders_responsible = Staff.objects.filter(is_reply_to_order_email=True, is_active=True).order_by('?').first()
    invoices_responsible = Staff.objects.filter(is_reply_to_invoice_email=True, is_active=True).order_by('?').first()
    s1 = EMPTY_STRING
36 37 38 39
    if orders_responsible:
        c = orders_responsible.customer_responsible
        if c is not None:
            s1 = "%s: %s, %s" % (_("Orders"), c.long_basket_name, c.phone1)
Patrick's avatar
Patrick committed
40
    s2 = EMPTY_STRING
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    if invoices_responsible:
        c = invoices_responsible.customer_responsible
        if c is not None:
            s2 = "%s: %s, %s" % (_("Invoices"), c.long_basket_name, c.phone1)
    separator = chr(10) + " "
    worksheet.header_footer.right_header.text = separator.join((s1, s2))
    return worksheet


def worksheet_setup_portrait_a4(worksheet, title1, title2, add_print_title=True):
    worksheet = worksheet_setup_a4(worksheet, title1, title2, add_print_title)
    worksheet.page_setup.orientation = worksheet.ORIENTATION_PORTRAIT
    return worksheet


def new_portrait_a4_sheet(workbook, title1, title2, header=None, add_print_title=True):
    if workbook is None:
        workbook = Workbook()
        worksheet = workbook.get_active_sheet()
    else:
        worksheet = workbook.create_sheet()
    worksheet = worksheet_setup_portrait_a4(worksheet, title1, title2, add_print_title=add_print_title)
    if header is not None:
        worksheet_set_header(worksheet, header)
    return workbook, worksheet


def worksheet_setup_landscape_a4(worksheet, title1, title2, add_print_title=True):
    worksheet = worksheet_setup_a4(worksheet, title1, title2, add_print_title)
    worksheet.page_setup.orientation = worksheet.ORIENTATION_LANDSCAPE
    return worksheet


def new_landscape_a4_sheet(workbook, title1, title2, header=None, add_print_title=True):
    if workbook is None:
        workbook = Workbook()
        worksheet = workbook.get_active_sheet()
    else:
        worksheet = workbook.create_sheet()
    worksheet = worksheet_setup_landscape_a4(worksheet, title1, title2, add_print_title=add_print_title)
    if header is not None:
        worksheet_set_header(worksheet, header)
    return workbook, worksheet


def worksheet_set_header(worksheet, header):
Patrick's avatar
Patrick committed
87
    for col_num in range(len(header)):
88 89 90 91 92 93 94 95 96 97 98 99 100
        c = worksheet.cell(row=0, column=col_num)
        c.value = (header[col_num][ROW_TITLE]).encode("utf8")
        c.style.font.bold = True
        c.style.alignment.wrap_text = False
        c.style.borders.bottom.border_style = Border.BORDER_THIN
        worksheet.column_dimensions[get_column_letter(col_num + 1)].width = header[col_num][ROW_WIDTH]
        if header[col_num][ROW_TITLE] == _("Id"):
            worksheet.column_dimensions[get_column_letter(col_num + 1)].visible = False


def get_validation_formula(wb=None, valid_values=None):
    if valid_values:

Patrick's avatar
Patrick committed
101
        ws_dv_name = cap(slugify("%s" % (_("data validation"))), 31)
102 103 104
        ws_dv = wb.get_sheet_by_name(ws_dv_name)
        if ws_dv is None:
            ws_dv = wb.create_sheet(index=0)
Patrick's avatar
Patrick committed
105
            worksheet_setup_landscape_a4(ws_dv, ws_dv_name, EMPTY_STRING)
106 107
        col_dv = 0
        c = ws_dv.cell(row=0, column=col_dv)
Patrick's avatar
Patrick committed
108
        while (c.value is not None) and (col_dv < 20):
109 110 111 112 113 114 115 116 117 118 119 120
            col_dv += 1
            c = ws_dv.cell(row=0, column=col_dv)

        col_letter_dv = get_column_letter(col_dv + 1)
        row_num = 0
        for v in valid_values:
            c = ws_dv.cell(row=row_num, column=col_dv)
            c.value = "%s" % (v)
            c.style.number_format.format_code = NumberFormat.FORMAT_TEXT
            row_num += 1
        return "'%s'!$%s$1:$%s$%s" % (ws_dv_name, col_letter_dv, col_letter_dv, row_num + 1)
    else:
Patrick's avatar
Patrick committed
121
        return EMPTY_STRING