Conjunto de cambios 17dcd3a en seiven para gestion_informacion


Ignorar:
Fecha y hora:
13/12/2016 10:32:58 (hace 7 años)
Autor:
Ing. Roldan D. Vargas G <rvargas@…>
Branches:
master, carga
Children:
3ccd153
Parents:
9ae3a72
Mensaje:

filtros y modificaciones para la carga y descarga de datos masivos

Fichero:
1 editado

Leyenda

No modificado
Añadido
Eliminado
  • gestion_informacion/ajax.py

    re9477bd r17dcd3a  
    1212# @copyright <a href='http://www.gnu.org/licenses/gpl-2.0.html'>GNU Public License versión 2 (GPLv2)</a>
    1313from __future__ import unicode_literals, absolute_import
     14
     15import os
    1416
    1517from django.http import HttpResponse
     
    2729import logging
    2830import json
    29 import pyexcel
    30 import csv
    3131import xlwt
    3232
     
    6868            modelo = apps.get_model(app, mod)
    6969            workbook = xlwt.Workbook()
    70             sheet = workbook.add_sheet("Datos")
     70            sheet = workbook.add_sheet("Datos", cell_overwrite_ok=True)
    7171            instance = modelo()
    72             datos = instance.gestion_init(**filter)
    73             font_bold = xlwt.easyxf('font: bold 1')
     72            contenido = instance.gestion_init(**filter)
    7473
    75             if datos['cabecera'][0]:
    76                 c = 0
     74            row = 0
     75            ## Loop que permite agregar datos al archivo a descargar
     76            for con in contenido['fields']:
     77                index_col, style = 0, 'align: horiz center;'
     78                for col in con:
     79                    if 'color' in col:
     80                        style += 'pattern: pattern solid, fore_colour %s;' % col['color']
     81                    if 'text_color' in col:
     82                        style += 'font: color %s, bold True;' % col['text_color']
     83                    custom_style = xlwt.easyxf(style)
     84                    sheet.write(row, index_col, col['tag'], custom_style)
     85                    if 'cabecera' in col:
     86                        sheet.col(index_col).width = int(333 * (len(col['tag']) + 1))
    7787
    78                 for cabecera in datos['cabecera'][0]:
    79                     style = font_bold
    80                     if cabecera['color'] and cabecera['text_color']:
    81                         style = xlwt.easyxf('pattern: pattern solid, fore_colour %s; font: color %s, bold True; align: horiz center;' % (cabecera['color'], cabecera['text_color']))
    82                     if cabecera['combine'] > 0:
    83                         count_merge = c + cabecera['combine']
    84                         sheet.write_merge(0, 0, c, count_merge, cabecera['tag'], style)
    85                         c = count_merge + 1
     88                    if 'combine' in col and col['combine'] > 0:
     89                        sheet.merge(row, row, index_col, (index_col + (col['combine']-1)), custom_style)
     90                        index_col = col['combine'] + index_col
    8691                    else:
    87                         sheet.write(0, c, cabecera['tag'], style)
    88                         sheet.col(c).width = int(256 * (len(cabecera['tag']) + 1))
    89                         c += 1
     92                        index_col += 1
     93                row += 1
    9094
    91             i = 0
    92             for cabecera in datos['cabecera'][1]:
    93                 sheet.write(1, i, cabecera['label'], font_bold)
    94                 sheet.col(i).width = int(256 * (len(cabecera['label']) + 1))
    95                 i += 1
     95            # Ruta y nombre del archivo a generar
     96            archivo = "%s/%s.xls" % (settings.GESTION_INFORMACION_FILES, contenido['output'])
    9697
    97             # Se obtiene la cantidad de datos
    98             cantidad = len(datos['data'])
    99             # Si existen datos se crean las filas requeridas
    100             if cantidad > 0:
    101                 for i in range(1, cantidad + 1):
    102                     row = len(datos['cabecera'])
    103                     for j in range(0, row):
    104                         sheet.write(i, j, datos['data'][i - 1][j])
    105 
    106             archivo = "%s/%s.xls" % (settings.GESTION_INFORMACION_FILES, datos['output'])
    107 
     98            # Guarda el contenido de la hoja de cálculo al archivo indicado
    10899            workbook.save(archivo)
    109100
    110             open_file = "%s.xls" % datos['output']
     101            # Nombre del archivo a utilizar para la descarga
     102            open_file = "%s.xls" % contenido['output']
    111103
    112104            return HttpResponse(json.dumps({
     
    158150            process = instance.gestion_process(archivo, request.user, **filter)
    159151
     152            # elimina el archivo después de cargar los datos
     153            os.unlink(ruta)
     154
    160155            if process['result']:
    161156                return HttpResponse(json.dumps({'result': True, 'message': str(MSG_UPLOAD_FILE_SUCCESS)}))
Nota: Vea TracChangeset para ayuda en el uso del visor de conjuntos de cambios.