-
-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
537 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# -*- coding: utf-8 -*- | ||
# (c) 2017 Diagram Software S.L. | ||
# (c) 2017 Consultoría Informática Studio 73 S.L. | ||
# (c) 2019 Acysos S.L. | ||
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html | ||
|
||
from odoo import api, models, fields, _ | ||
|
||
|
||
class L10nEsAeatCertificate(models.Model): | ||
_name = 'l10n.es.aeat.certificate' | ||
|
||
name = fields.Char(string="Name") | ||
state = fields.Selection([ | ||
('draft', 'Draft'), | ||
('active', 'Active') | ||
], string="State", default="draft") | ||
file = fields.Binary(string="File", required=True) | ||
folder = fields.Char(string="Folder Name", required=True) | ||
date_start = fields.Date(string="Start Date") | ||
date_end = fields.Date(string="End Date") | ||
public_key = fields.Char(string="Public Key", readonly=True) | ||
private_key = fields.Char(string="Private Key", readonly=True) | ||
company_id = fields.Many2one( | ||
comodel_name="res.company", | ||
string="Compañía", | ||
required=True, | ||
default=lambda self: self.env.user.company_id.id | ||
) | ||
|
||
@api.multi | ||
def load_password_wizard(self): | ||
self.ensure_one() | ||
return { | ||
'type': 'ir.actions.act_window', | ||
'name': _('Insert Password'), | ||
'res_model': 'l10n.es.aeat.certificate.password', | ||
'view_mode': 'form', | ||
'view_type': 'form', | ||
'views': [(False, 'form')], | ||
'target': 'new', | ||
} | ||
|
||
@api.multi | ||
def action_active(self): | ||
self.ensure_one() | ||
other_configs = self.search([('id', '!=', self.id), | ||
('company_id', '=', self.company_id.id)]) | ||
for config_id in other_configs: | ||
config_id.state = 'draft' | ||
self.state = 'active' | ||
|
||
@api.multi | ||
def get_certificates(self, company = False): | ||
if not company: | ||
company = self.env.user.company_id | ||
today = fields.Date.today() | ||
aeat_certificate = self.search([ | ||
('company_id', '=', company.id), | ||
('public_key', '!=', False), | ||
('private_key', '!=', False), | ||
'|', ('date_start', '=', False), | ||
('date_start', '<=', today), | ||
'|', ('date_end', '=', False), | ||
('date_end', '>=', today), | ||
('state', '=', 'active') | ||
], limit=1) | ||
if aeat_certificate: | ||
public_crt = aeat_certificate.public_key | ||
private_key = aeat_certificate.private_key | ||
else: | ||
public_crt = self.env['ir.config_parameter'].get_param( | ||
'l10n_es_aeat_certificate.publicCrt', False) | ||
private_key = self.env['ir.config_parameter'].get_param( | ||
'l10n_es_aeat_certificate.privateKey', False) | ||
return public_crt, private_key | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<odoo> | ||
<data> | ||
<record id="l10n_es_aeat_certificate_form_view" model="ir.ui.view"> | ||
<field name="name">l10n.es.aeat.certificate.form</field> | ||
<field name="model">l10n.es.aeat.certificate</field> | ||
<field name="arch" type="xml"> | ||
<form string="Load Certificate"> | ||
<header> | ||
<button name="load_password_wizard" type="object" string="Obtain Keys"/> | ||
<button name="action_active" type="object" string="To Active"/> | ||
<field name="state" widget="statusbar" statusbar_visible="draft,active"/> | ||
</header> | ||
<group> | ||
<group> | ||
<field name="name"/> | ||
<field name="file"/> | ||
<field name="folder"/> | ||
</group> | ||
<group> | ||
<field name="company_id" groups="base.group_multi_company"/> | ||
<field name="date_start"/> | ||
<field name="date_end"/> | ||
</group> | ||
</group> | ||
<group> | ||
<field name="private_key"/> | ||
<field name="public_key"/> | ||
</group> | ||
</form> | ||
</field> | ||
</record> | ||
|
||
<record id="l10n_es_certificate_tree_view" model="ir.ui.view"> | ||
<field name="name">l10n.es.aeat.certificate.tree</field> | ||
<field name="model">l10n.es.aeat.certificate</field> | ||
<field name="arch" type="xml"> | ||
<tree> | ||
<field name="name"/> | ||
<field name="date_start"/> | ||
<field name="date_end"/> | ||
<field name="state"/> | ||
</tree> | ||
</field> | ||
</record> | ||
|
||
<record id="l10n_es_certificate_action" model="ir.actions.act_window"> | ||
<field name="name">Certificates</field> | ||
<field name="type">ir.actions.act_window</field> | ||
<field name="res_model">l10n.es.aeat.certificate</field> | ||
<field name="view_type">form</field> | ||
<field name="view_mode">tree,form</field> | ||
</record> | ||
|
||
<menuitem id="l10n_es_aeat_certificate_menu" name="Certificates" | ||
action="l10n_es_certificate_action" sequence="0" | ||
parent="l10n_es_aeat.menu_l10n_es_aeat_config" /> | ||
|
||
</data> | ||
</odoo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
|
||
from . import compare_boe_file | ||
from . import export_to_boe | ||
from . import aeat_certificate_password |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2017 Diagram Software S.L. | ||
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html | ||
|
||
|
||
from odoo import _, api, exceptions, fields, models | ||
from odoo.exceptions import ValidationError | ||
from odoo.tools import config | ||
from odoo import release | ||
import contextlib | ||
import os | ||
import tempfile | ||
import base64 | ||
import logging | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
try: | ||
import OpenSSL.crypto | ||
except (ImportError, IOError) as err: | ||
_logger.debug(err) | ||
|
||
if tuple(map(int, OpenSSL.__version__.split('.'))) < (0, 15): | ||
_logger.warning( | ||
'OpenSSL version is not supported. Upgrade to 0.15 or greater.') | ||
|
||
|
||
@contextlib.contextmanager | ||
def pfx_to_pem(file, pfx_password, directory=None): | ||
with tempfile.NamedTemporaryFile( | ||
prefix='private_', suffix='.pem', delete=False, | ||
dir=directory) as t_pem: | ||
f_pem = open(t_pem.name, 'wb') | ||
p12 = OpenSSL.crypto.load_pkcs12(file, pfx_password) | ||
f_pem.write(OpenSSL.crypto.dump_privatekey( | ||
OpenSSL.crypto.FILETYPE_PEM, p12.get_privatekey())) | ||
f_pem.close() | ||
yield t_pem.name | ||
|
||
|
||
@contextlib.contextmanager | ||
def pfx_to_crt(file, pfx_password, directory=None): | ||
with tempfile.NamedTemporaryFile( | ||
prefix='public_', suffix='.crt', delete=False, | ||
dir=directory) as t_crt: | ||
f_crt = open(t_crt.name, 'wb') | ||
p12 = OpenSSL.crypto.load_pkcs12(file, pfx_password) | ||
f_crt.write(OpenSSL.crypto.dump_certificate( | ||
OpenSSL.crypto.FILETYPE_PEM, p12.get_certificate())) | ||
f_crt.close() | ||
yield t_crt.name | ||
|
||
|
||
class L10nEsAeatCertificatePassword(models.TransientModel): | ||
_name = 'l10n.es.aeat.certificate.password' | ||
|
||
password = fields.Char(string="Password", required=True) | ||
|
||
@api.multi | ||
def get_keys(self): | ||
record = self.env['l10n.es.aeat.certificate'].browse( | ||
self.env.context.get('active_id')) | ||
directory = os.path.join( | ||
os.path.abspath(config['data_dir']), 'certificates', | ||
release.series, self.env.cr.dbname, record.folder) | ||
file = base64.decodestring(record.file) | ||
if tuple(map(int, OpenSSL.__version__.split('.'))) < (0, 15): | ||
raise exceptions.Warning( | ||
_('OpenSSL version is not supported. Upgrade to 0.15 ' | ||
'or greater.')) | ||
try: | ||
if directory and not os.path.exists(directory): | ||
os.makedirs(directory) | ||
with pfx_to_pem(file, self.password, directory) as private_key: | ||
record.private_key = private_key | ||
with pfx_to_crt(file, self.password, directory) as public_key: | ||
record.public_key = public_key | ||
except Exception as e: | ||
if e.args: | ||
args = list(e.args) | ||
raise ValidationError(args[-1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<odoo> | ||
<data> | ||
|
||
<record id="l10n_es_aeat_certificate_password_wizard_view" model="ir.ui.view"> | ||
<field name="name">l10n.es.aeat.certificate.password.wizard</field> | ||
<field name="model">l10n.es.aeat.certificate.password</field> | ||
<field name="arch" type="xml"> | ||
<form string="Insert Password"> | ||
<group> | ||
<field name="password" password="True"/> | ||
</group> | ||
<footer> | ||
<button name="get_keys" type="object" string="Obtain Keys" class="oe_highlight"/> | ||
or | ||
<button string="Cancel" class="oe_link" special="cancel"/> | ||
</footer> | ||
</form> | ||
</field> | ||
</record> | ||
|
||
</data> | ||
</odoo> |