Esempio n. 1
0
def generate_preview(document_id):
    """ Get preview from previously built document """
    uid = str(session['uid'])
    pdf = PDFGenerator()
    """ We have a document with this id """
    if uid in _document_list_by_uid and document_id in _document_list_by_uid[
            uid]:
        dte_object, _ = _document_list_by_uid[uid][document_id]
        _, binary_pdf = pdf.generate_binary(dte_object)

        response = make_response(binary_pdf)
        response.headers['Content-Type'] = 'application/pdf'
        response.headers['Content-Disposition'] = \
        'attachment; filename=%s.pdf' % document_id
        return response
    else:
        return "No document " + str(document_id) + " found", 404
Esempio n. 2
0
def generate_test_pdf(type):
    uid = str(session['uid'])
    """ Get parameters, build PDF and return file """
    pdf = PDFGenerator()
    """ Dump test XML """
    sender_parameters = {}
    receiver_parameters = {}
    specific_header_parameters = {}
    item_list = {}
    """ Read test files """
    with open('test/data/sender.json') as json_file:
        sender_parameters = json.load(json_file)
    with open('test/data/receiver.json') as json_file:
        receiver_parameters = json.load(json_file)
    with open('test/data/items.json') as json_file:
        item_list = json.load(json_file)
    with open('test/data/specifics.json') as json_file:
        specific_header_parameters = json.load(json_file)

    caf = DTECAF(parameters={}, signature='', private_key='')
    caf.load_from_XML_string(_caf_by_uid[uid])

    builder = DTEBuidler()
    """ Bind user information """
    specific_header_parameters['User'] = {}
    specific_header_parameters['User']['Resolution'] = session['RES']
    specific_header_parameters['User']['ResolutionDate'] = session['RES_Date']
    specific_header_parameters['User']['RUT'] = session['RUT']

    _, pretty_dte, dte_object = builder.build(type, sender_parameters,
                                              receiver_parameters,
                                              specific_header_parameters,
                                              item_list, caf)
    pdfFilename, binary_pdf = pdf.generate_binary(dte_object)

    response = make_response(binary_pdf)
    response.headers['Content-Type'] = 'application/pdf'
    response.headers['Content-Disposition'] = \
    'attachment; filename=%s.pdf' % pdfFilename
    return response
Esempio n. 3
0
def set_dte():
	""" Format : """
	"""
	{
	'DocumentNumber': '', 'SII': '',
	'Header': {
		'Specifics': {
			'ShippingPort': '',
			'LandingPort': '',
			'MovementType': '',
			'ExpeditionType': ''
			}
		},
	'Date': '',
	'Receiver': {
		'Name': '', 'Address': '', 'Activity': '', 'RUT': '', 'City': '', 'Phone': ''
	},
	'Details':
		{
			'1': {'Code': '', 'Name': '', 'Quantity': '', 'Unit': '', 'UnitPrice': ''},
			'2': {'Code': '', 'Name': '', 'Quantity': '', 'Unit': '', 'UnitPrice': ''},
			'3': {'Code': '', 'Name': '', 'Quantity': '', 'Unit': '', 'UnitPrice': ''},
			'4': {'Code': '', 'Name': '', 'Quantity': '', 'Unit': '', 'UnitPrice': ''},
			'5': {'Code': '', 'Name': '', 'Quantity': '', 'Unit': '', 'UnitPrice': ''},
			'6': {'Code': '', 'Name': '', 'Quantity': '', 'Unit': '', 'UnitPrice': ''},
			'7': {'Code': '', 'Name': '', 'Quantity': '', 'Unit': '', 'UnitPrice': ''}
		},
	'Comment': '',
	'IVA': ''
	}
	"""
	""" Get POSTed parameters, build PDF and return file """
	uid = str(session['uid'])
	pdf = PDFGenerator()
	form_parameters = request.get_json(force=True)

	sender_parameters = {}
	receiver_parameters = form_parameters['Receiver']

	specific_header_parameters = form_parameters['Header']['Specifics']
	specific_header_parameters['DocumentNumber'] = form_parameters['DocumentNumber']
	specific_header_parameters['Comment'] = form_parameters['Comment']

	""" Mocked """
	specific_header_parameters['PaymentType'] = ''

	item_list = form_parameters['Details']

	""" Read sender file """
	with open('test/data/sender.json') as json_file:
		sender_parameters = json.load(json_file)

	builder = DTEBuidler()
	""" Bind user information """
	specific_header_parameters['User'] = {}
	specific_header_parameters['User']['Resolution'] = session['RES']
	specific_header_parameters['User']['ResolutionDate'] = session['RES_Date']
	specific_header_parameters['User']['RUT'] = session['RUT']

	type = int(form_parameters["DocumentType"])

	caf = DTECAF(parameters={}, signature='', private_key='')
	caf.load_from_XML_string(_caf_by_uid[uid])

	tree, pretty_dte, dte_object = builder.build(type, sender_parameters, receiver_parameters, specific_header_parameters, item_list, caf)
	pdfFilename, binary_pdf = pdf.generate_binary(dte_object)


	if uid not in _document_list_by_uid:
		_document_list_by_uid[uid] = {}

	_document_list_by_uid[uid][pdfFilename] = binary_pdf;

	return pdfFilename, 200
Esempio n. 4
0
        """ Extract key """
        cert = CertificateService(pfx_file_path=sys.argv[2],
                                  pfx_password=sys.argv[3])
        cert.generate_certificate_and_key()
        auth.set_key_and_certificate(cert.key, cert.certificate)
        token_string = auth.get_token(seed)
        token = Token(token_string)
        print("Token : " + token.get_token())
    if command == "generate_cert":
        """ Generate certificate only """
        logging.warning('Certificate generation test.')
        cert = CertificateService(pfx_file_path=sys.argv[2],
                                  pfx_password=sys.argv[3])
        cert.generate_certificate_and_key()
    if command == "generate_pdf_from_xml":
        pdf = PDFGenerator()
        builder = DTEBuidler()
        _, _, dte_object = builder.from_file(sys.argv[2])
        pdf.generate(dte_object)
    if command == "generate_pdf":
        pdf = PDFGenerator()
        """ Dump test XML """
        type = int(sys.argv[2])

        sender_parameters = {}
        receiver_parameters = {}
        specific_header_parameters = {}
        item_list = {}
        """ Read test files """
        with open('test/data/sender.json') as json_file:
            sender_parameters = json.load(json_file)
Esempio n. 5
0
    except:
        print("save_last_seen_uid:: Error")


def load_last_seen_uid():
    try:
        with open(JSON_LAST_SEEN_PATH, ) as read_file:
            context = json.load(read_file)
            return context.last_seen_uid
    except:
        print("load_last_seen_uid:: Error")
        return 0


last_seen_uid = load_last_seen_uid()
pdf = PDFGenerator()
builder = DTEBuidler()

# get all attachments for each email from INBOX folder
with MailBox(SII_INBOX_EMAIL_SERVER).login(
        SII_INBOX_EMAIL_ACCOUNT, SII_INBOX_EMAIL_PASSWORD) as mailbox:
    """ Filter out analysis result from SII """
    for msg in mailbox.fetch(SII_INBOX_IMAP_CRITERIA, mark_seen=False):
        if int(msg.uid) > int(last_seen_uid):
            last_seen_uid = msg.uid
            for att in msg.attachments:
                """ Only XML """
                if att.filename.upper().endswith(".XML"):
                    print("Getting " + att.filename, att.content_type)
                    try:
                        _, _, dte_object = builder.from_string(att.payload)