Skip to content

Commit

Permalink
[MIG] connector_cbl: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
KNVx committed Jan 9, 2024
1 parent 2b0c78b commit 6b18923
Show file tree
Hide file tree
Showing 16 changed files with 560 additions and 115 deletions.
57 changes: 46 additions & 11 deletions connector_cbl/README.rst
Original file line number Diff line number Diff line change
@@ -1,29 +1,64 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

=============
CBL Connector
=============

* Connector to get tracking info from CBL
..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:993a1737f298bceacbac525aabe94919c03ee42ed98a7e1a7ba4b44d85aa213c
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
:target: https://odoo-community.org/page/development-status
:alt: Production/Stable
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-nuobit%2Fodoo--addons-lightgray.png?logo=github
:target: https://github.com/nuobit/odoo-addons/tree/16.0/connector_cbl
:alt: nuobit/odoo-addons

|badge1| |badge2| |badge3|

Connector to get tracking info from CBL

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/nuobit/odoo-addons/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smashing it by providing a detailed and welcomed feedback.
Bugs are tracked on `GitHub Issues <https://github.com/nuobit/odoo-addons/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/nuobit/odoo-addons/issues/new?body=module:%20connector_cbl%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* NuoBiT Solutions SL

Contributors
------------
~~~~~~~~~~~~

* `NuoBiT <https://www.nuobit.com>`__:

* Eric Antones <[email protected]>
* Eric Antones <[email protected]>
* Kilian Niubo <[email protected]>


Maintainers
~~~~~~~~~~~

This module is part of the `nuobit/odoo-addons <https://github.com/nuobit/odoo-addons/tree/16.0/connector_cbl>`_ project on GitHub.

You are welcome to contribute.
4 changes: 0 additions & 4 deletions connector_cbl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
# Copyright NuoBiT Solutions, S.L. (<https://www.nuobit.com>)
# Eric Antones <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from . import controllers
from . import models
10 changes: 4 additions & 6 deletions connector_cbl/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
# Copyright NuoBiT Solutions, S.L. (<https://www.nuobit.com>)
# Eric Antones <[email protected]>
# Copyright NuoBiT Solutions - Eric Antones <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

{
"name": "CBL Connector",
"description": "Connector to get tracking info from CBL",
"version": "11.0.0.1.2",
"author": "NuoBiT Solutions, S.L., Eric Antones",
"version": "16.0.1.0.0",
"author": "NuoBiT Solutions SL",
"license": "AGPL-3",
"category": "Connector",
"website": "https://github.com/nuobit/odoo-addons",
"external_dependencies": {
"python": [
"requests",
"lxml",
"json",
],
},
"depends": [
Expand All @@ -26,5 +23,6 @@
"templates/cbl_template.xml",
"security/ir.model.access.csv",
],
"development_status": "Production/Stable",
"installable": True,
}
4 changes: 0 additions & 4 deletions connector_cbl/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
# Copyright NuoBiT Solutions, S.L. (<https://www.nuobit.com>)
# Eric Antones <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from . import main
8 changes: 4 additions & 4 deletions connector_cbl/controllers/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Copyright NuoBiT Solutions - Eric Antones <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

import logging

import werkzeug.exceptions

from odoo import http

from odoo.addons.connector_cbl.models.cbl import CBL
from ..models.cbl import CBL

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -45,9 +48,6 @@ def tracking_data(self, tracking_number=None):
return werkzeug.exceptions.NotFound(
"There's no data with tracking number '%s'" % tracking_number
)

# if not er.logout():
# return werkzeug.exceptions.InternalServerError("Logout not successful")
_logger.info(
"CBL shipment %s successfully retrieved from %s."
% (tracking_number, remote_ip)
Expand Down
4 changes: 0 additions & 4 deletions connector_cbl/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
# Copyright NuoBiT Solutions, S.L. (<https://www.nuobit.com>)
# Eric Antones <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from . import backend
from . import cbl
50 changes: 27 additions & 23 deletions connector_cbl/models/backend.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright NuoBiT Solutions, S.L. (<https://www.nuobit.com>)
# Eric Antones <[email protected]>
# Copyright NuoBiT Solutions - Eric Antones <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

import logging

from odoo import api, exceptions, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError

from . import cbl

Expand All @@ -25,37 +25,41 @@ def _select_state(self):
("production", "In Production"),
]

name = fields.Char("Name", required=True)
name = fields.Char(
required=True,
)
sequence = fields.Integer(
required=True,
default=1,
)
username = fields.Char(
required=True,
)
password = fields.Char(
required=True,
)
output = fields.Text(
readonly=True,
)
active = fields.Boolean(
default=True,
)
state = fields.Selection(
selection="_select_state",
default="draft",
)

sequence = fields.Integer("Sequence", required=True, default=1)

username = fields.Char("Username", required=True)
password = fields.Char("Password", required=True)

output = fields.Text("Output", readonly=True)

active = fields.Boolean(string="Active", default=True)
state = fields.Selection(selection="_select_state", string="State", default="draft")

@api.multi
def button_reset_to_draft(self):
self.ensure_one()
self.write({"state": "draft", "output": None})

@api.multi
def _check_connection(self):
self.ensure_one()
er = cbl.CBL(username=self.username, password=self.password)

if not er.login():
raise exceptions.ValidationError("Error on logging in")

# if not er.logout():
# raise exceptions.ValidationError("Error on logging out")

raise ValidationError(_("Error on logging in"))
self.output = "OK"

@api.multi
def button_check_connection(self):
self._check_connection()
self.write({"state": "checked"})
66 changes: 41 additions & 25 deletions connector_cbl/models/cbl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Copyright NuoBiT Solutions, S.L. (<https://www.nuobit.com>)
# Eric Antones <[email protected]>
# Copyright NuoBiT Solutions - Eric Antones <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

import datetime
Expand All @@ -10,13 +9,29 @@
import requests
from lxml import etree

from odoo import _
from odoo.exceptions import ValidationError

_logger = logging.getLogger(__name__)


def xpath1(tree, xpath):
tag_l = tree.xpath(xpath)
if len(tag_l) == 0:
raise ValidationError(_("No elements found on xpath: %s" % xpath))
if len(tag_l) != 1:
raise Exception("Expected 1 element")
raise ValidationError(
_(
"Expected 1 element, %(elements)s found on "
"xpath: %(xpath)s"
"Elements:%(tags)s"
% {
"elements": len(tag_l),
"xpath": xpath,
"tags": tag_l,
}
)
)

return tag_l[0]

Expand All @@ -30,18 +45,17 @@ def __init__(self, username, password, debug=False):
self.username = username
self.password = password
self.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/78.0.3904.108 Safari/537.36",
}
self.debug = debug

def login(self):
url = "%s/login.aspx" % self._base_url

## obtenim les dades del viewstate
res = self.session.get(url, headers=self.headers)
self._update_viewstate(res)

## fem el login
# login
form_data = {
"ScriptManager1": "UpdatePanel1|Login1$LoginButton",
"Login1$UserName": self.username,
Expand All @@ -51,13 +65,11 @@ def login(self):
form_data.update(self.viewstate)
res = self.session.post(url, data=form_data, headers=self.headers)
self._update_viewstate(res)

## comprovem que el login ha estat ok
# Check login
return not self.is_login_page(res)

def filter_by_refcte(self, refcte):
url = "%s/Consultas/envios.aspx" % self._base_url

res = self.session.get(url, headers=self.headers)
self._update_viewstate(res)

Expand All @@ -72,15 +84,15 @@ def filter_by_refcte(self, refcte):
for ut in userid_tags:
ut_tag = xpath1(tree, "//form[@id='aspnetForm']//input[@id='%s']" % ut)
if ut_tag is None:
raise Exception("Expected value")
raise ValidationError(_("Expected value"))
if not userid:
userid = ut_tag.attrib["value"]
else:
if userid != ut_tag.attrib["value"]:
raise Exception("Diferent userid values")
raise ValidationError(_("Different userid values"))

if not userid:
raise Exception("userid not found")
raise ValidationError(_("Userid not found"))

form_data = {
"ctl00$AJAXScriptManager": "ctl00$UpdatePanel1|ctl00$TOPCONTENEDOR$WebCUI_buscar",
Expand Down Expand Up @@ -116,22 +128,22 @@ def filter_by_refcte(self, refcte):
if len(elem_table_l) == 0:
return []
elif len(elem_table_l) > 1:
raise Exception("Unexpected content CONSENV_RES")
raise ValidationError(_("Unexpected content CONSENV_RES"))
elem_table = elem_table_l[0]

result_ld = []
input_detail_l = elem_table.xpath(
"//tr/td/input[contains(@onclick, 'MuestraDetalleConsulta')]"
)
for input in input_detail_l:
for input_d in input_detail_l:
m = re.match(
r"^.+MuestraDetalleConsulta\('([^']+)'\)", input.attrib["onclick"]
r"^.+MuestraDetalleConsulta\('([^']+)'\)", input_d.attrib["onclick"]
)
if not m:
raise Exception("Unexpected content on MuestraDetalleConsulta")
raise ValidationError(_("Unexpected content on MuestraDetalleConsulta"))
nexpedicion = m.group(1)

### detalle expedicion
# Expedition detail
url = "%s/api/Comun/DetalleEnvio/QueryDatosExpedicion" % self._base_url
form_data = {
"expedicion": nexpedicion,
Expand All @@ -141,7 +153,7 @@ def filter_by_refcte(self, refcte):
data = res.json()["data"]
expedition_d = data

### tracking
# tracking
url = "%s/api/Comun/DetalleEnvio/QueryTracking" % self._base_url
form_data = {
"jtStartIndex": "1",
Expand Down Expand Up @@ -200,16 +212,20 @@ def _update_viewstate(self, res):
tag_viewstate = tree.xpath("//*[@id='%s']" % vsf)
for t in tag_viewstate:
if "value" not in t.attrib:
raise Exception(
"Unexpected, ViewState element iff found must have value attribute"
raise ValidationError(
_(
"Unexpected, ViewState element iff found must have value attribute"
)
)
if vsf not in self.viewstate:
self.viewstate[vsf] = t.attrib["value"] or None
else:
if self.viewstate[vsf] != t.attrib["value"]:
raise Exception(
"Unexpected! all the ViewState must have the same 'value'"
raise ValidationError(
_(
"Unexpected! all the ViewState must have the same 'value'"
)
)

if not self.viewstate:
raise Exception("ViewState not found")
raise ValidationError(_("ViewState not found"))
5 changes: 5 additions & 0 deletions connector_cbl/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
* `NuoBiT <https://www.nuobit.com>`__:

* Eric Antones <[email protected]>
* Kilian Niubo <[email protected]>

1 change: 1 addition & 0 deletions connector_cbl/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Connector to get tracking info from CBL
Loading

0 comments on commit 6b18923

Please sign in to comment.