Exemple #1
0
def generate_return_act(request):
    """Генерация нового docx акта возврата картриджей. Вью возвращает Url с свежезгенерированным файлом. 
    """
    resp_dict = dict()
    # использование глобальных переменных не очень хороший приём
    # но он позволяет упростить программный код
    total_pages_count = 0

    def actions_decoder(actions_list):
        """Преобразует список мнемонических действий в человекочитаемые строки.
        """
        result = str()
        for elem in actions_list:
            if elem == 'regeneration':
                result += _('Regeneration')
            elif elem == 'filled':
                result += _('Filling and cleaning')
            elif elem == 'fotoreceptor':
                result += _('Replacing fotovala')
            elif elem == 'rakel':
                result += _('Replacement squeegee')
            elif elem == 'chip':
                result += _('Replacement chip')
            elif elem == 'magnit':
                result += _('Replacing the magnetic roller')
            else:
                result += _('Not implement')
            result += ', '

        return result

    doc_id = request.POST.get('doc_id', '')
    doc_id = str2int(doc_id)

    try:
        m1 = RefillingCart.objects.get(pk=doc_id)
    except RefillingCart.DoesNotExist:
        resp_dict['error'] = '1'
        resp_dict['text']  = _('The object with the ID is not found.')
        return JsonResponse(resp_dict, safe=False)

    jsontext = m1.json_content
    jsontext = json.loads(jsontext)

    pages_count = 0
    co = len(jsontext) # количество передаваемых картриджей на заправку

    if not os.path.exists(settings.STATIC_ROOT_DOCX):
        os.makedirs(settings.STATIC_ROOT_DOCX)

    # ротация файлов
    files = filter(os.path.isfile, glob.glob(settings.STATIC_ROOT_DOCX + '\*.docx'))
    files = list(files)
    files.sort(key=lambda x: os.path.getmtime(x))
    try:
        if len(files) > settings.MAX_COUNT_DOCX_FILES:
            os.remove(files[0])
    except:
        pass
    # производим инициализацию некоторых переменных начальными значениями    
    docx_full_file_name, docx_file_name = rotator_files(request, 'docx')
    # генерация печатной версии документа без группировки наименований
    document = Document()
    
    # добавляем шапку для документа
    doc_number = str(m1.number) + '/' + str(request.user.pk)
    act_number_string = _('The act of receiving cartridges # %(doc_number)s from  %(date_created)s') % {'doc_number': doc_number, 'date_created': localize_date(m1.date_created)}
    hh1 = document.add_heading(act_number_string, level=2)
    
    hh2 = document.add_heading(str(request.user.departament), level=2)
    
    document.add_paragraph("") # добавляем оступ сверху
    document.add_paragraph(_('Contractor:  %(firm)s') % {'firm': m1.firm})

    # рисуем таблицу на первой странице
    table = document.add_table(rows=1, cols=4, style='Table Grid')
    obj_styles = document.styles
    obj_charstyle = obj_styles.add_style('FontSize', WD_STYLE_TYPE.PARAGRAPH)
    obj_font = obj_charstyle.font
    obj_font.size = Pt(8)
    #obj_font.name = 'Times New Roman'
    sum_money = 0
    hdr_cells = table.rows[0].cells
    hdr_cells[0].text = _('Number')
    hdr_cells[1].text = _('Name')
    hdr_cells[2].text = _('Ongoing work')
    hdr_cells[3].text = '%s, %s' % (_('The price'), _('Currency'))
    
    for item in jsontext:
        row_cells = table.add_row().cells
        row_cells[0].text = str(item[0])
        row_cells[1].text = str(item[1])
        
        # https://github.com/python-openxml/python-docx/issues/141
        paragraph = row_cells[2].paragraphs[0]
        paragraph.text = actions_decoder(item[2])
        paragraph.style = 'FontSize'
        money = float(item[3][-1])
        sum_money += money
        row_cells[3].text = str(money)

    sender_full_name = ""
    recipient_full_name = request.user.fio
    # добавляем место для подписей принимающих и передающих
    document.add_paragraph("") # добавляем оступ сверху
    document.add_paragraph(_('Just made: %(co)s pieces') % {'co': co})
    document.add_paragraph(_('The total amount of: %(sum_money)s dollars') % {'sum_money': sum_money})
    document.add_paragraph("")
    document.add_paragraph("%s       _________________      %s" % (_('Resipient'), recipient_full_name,))
    document.add_paragraph("")
    document.add_paragraph("%s       _________________      %s" % (_('Sender'), sender_full_name,))
    document.add_paragraph("")
    document.add_paragraph("")
    document.add_paragraph('www.severcart.org' + ' '*120)
    document.save(docx_full_file_name)
    
    resp_dict['error'] = '0'
    resp_dict['text']  = _('Document %(doc_number)s_%(user_id)s_3.docx generated') % { 'doc_number': m1.number, 'user_id': request.user.pk}
    resp_dict['url'] = settings.STATIC_URL + 'docx/' + docx_file_name
    return JsonResponse(resp_dict)
Exemple #2
0
def generate_act(request):
    """Генерация нового docx документа. Вью возвращает Url с свежезгенерированным файлом. 
    """
    resp_dict = dict()
    # использование глобальных переменных не очень хороший приём
    # но он позволяет упростить программный код
    total_pages_count = 0

    def add_footer(page_num=1):
        # форматирование нижнего колонтитула копирайтом и количеством страниц
        for i in range(4):
            document.add_paragraph("")

        page_number_string = _('Page %(num)s from %(total)s') % {'num': page_num, 'total': total_pages_count}
        p = document.add_paragraph('www.severcart.org' + ' '*100 + page_number_string)
        
    doc_id = request.POST.get('doc_id', '')
    doc_action = request.POST.get('doc_action', '')

    pages_count = 0
    doc_id = str2int(doc_id)

    try:
        m1 = RefillingCart.objects.get(pk=doc_id)
    except RefillingCart.DoesNotExist:
        resp_dict['error'] = '1'
        resp_dict['text']  = _('The object with the ID is not found.')
        return JsonResponse(resp_dict, safe=False)

    jsontext = m1.json_content
    jsontext = json.loads(jsontext)

    co = len(jsontext) # количество передаваемых картриджей на заправку

    file_full_name, docx_file_name = rotator_files(request, file_type='docx')
    # производим инициализацию некоторых переменных начальными значениями
    sender_full_name    = request.user.fio
    recipient_full_name = ' '*50

    if not((doc_action == "docx_with_group") or (doc_action == "docx_without_group")):
        # если действие указано не верное, сообщаем об это и прекращаем работу 
        # скрипта
        resp_dict['error'] = '1'
        resp_dict['text']  = _('This action is not implemented.')
        return JsonResponse(resp_dict)
    

    if doc_action == 'docx_with_group':
        jsontext = group_names(jsontext)
        header_cell_one = _('The name of the cartridge')
        header_cell_two = _('Amount cartridges')
        
    if doc_action == 'docx_without_group':
        header_cell_one = _('Cartridge number')
        header_cell_two = _('The name of the cartridge')

    # генерация печатной версии документа без группировки наименований
    document = Document()
    
    # добавляем шапку для документа
    doc_number = str(m1.number) + '/' + str(request.user.pk)
    act_number_string = _('The act of transferring cartridges # %(doc_number)s from  %(date_created)s') % {'doc_number': doc_number, 'date_created': localize_date(m1.date_created)}
    hh1 = document.add_heading(act_number_string, level=2)
    
    hh2 = document.add_heading(str(request.user.departament), level=2)
    
    document.add_paragraph("") # добавляем оступ сверху
    document.add_paragraph("")

    # рисуем таблицу на первой странице
    table = document.add_table(rows=1, cols=2, style='Table Grid')
    hdr_cells = table.rows[0].cells
    hdr_cells[0].text = header_cell_one
    hdr_cells[1].text = header_cell_two
    if (len(jsontext) <= settings.MAX_TABLE_ROWS_FIRST_PAGE):
        for item in jsontext:
            row_cells = table.add_row().cells
            row_cells[0].text = str(item[0])
            row_cells[1].text = str(item[1])
        total_pages_count = 1

    if (len(jsontext) > settings.MAX_TABLE_ROWS_FIRST_PAGE):
        first_part  = jsontext[0:settings.MAX_TABLE_ROWS_FIRST_PAGE-1]
        second_part = jsontext[settings.MAX_TABLE_ROWS_FIRST_PAGE:]
        
        p = Paginator(second_part, settings.MAX_TABLE_ROWS)
        total_pages_count = 1 + p.num_pages

        for item in first_part:
            row_cells = table.add_row().cells
            row_cells[0].text = str(item[0])
            row_cells[1].text = str(item[1])

        pages_count += 1    
        add_footer(pages_count)
        document.add_page_break()
        # далее с каждой новой страницы рисуем новую таблицу с 
        # продолжением печати данных
        for pg in range(p.num_pages):
            stranica = p.page(pg + 1)
            # на каждой новой странице печатаем заново новый заголовок
            table = document.add_table(rows=1, cols=2, style='Table Grid')
            hdr_cells = table.rows[0].cells
            hdr_cells[0].text = header_cell_one
            hdr_cells[1].text = header_cell_two
            for item in stranica:
                row_cells = table.add_row().cells
                row_cells[0].text = str(item[0])
                row_cells[1].text = str(item[1])

            # если страница последняя то разрыв страницы не добавляем
            if  pg != (p.num_pages - 1):
                pages_count += 1
                document.add_page_break()
                add_footer(pages_count)

    # добавляем место для подписей принимающих и передающих
    document.add_paragraph("") # добавляем оступ сверху
    document.add_paragraph(_('Total count - %(co)s') % {'co': co})
    document.add_paragraph("")
    document.add_paragraph("%s        %s           ______________" % (_('Sender'), sender_full_name,))
    document.add_paragraph("")
    document.add_paragraph("%s        %s           ______________" % (_('Resipient'), recipient_full_name,))

    pages_count += 1
    add_footer(pages_count)

    document.save(file_full_name)
    
    resp_dict['error'] = '0'
    resp_dict['text']  = _('Document %(doc_number)s_%(user_id)s_0.docx generated') % { 'doc_number': m1.number, 'user_id': request.user.pk}
    #resp_dict['url'] = request.META.get('HTTP_ORIGIN') + settings.STATIC_URL + 'docx/' + docx_file_name
    resp_dict['url'] = settings.STATIC_URL + 'docx/' + docx_file_name
    
    return JsonResponse(resp_dict)