[FIX] pms: remove seq and scrap gc

This commit is contained in:
miguelpadin
2021-05-25 11:05:29 +02:00
parent aca9ea60a9
commit 9d1d2c5638
14 changed files with 127 additions and 145 deletions

View File

@@ -1,3 +1,2 @@
from . import models
from . import wizards
from .hooks import post_init_hook

View File

@@ -16,9 +16,12 @@
"partner_contact_gender",
"partner_contact_birthdate",
"partner_contact_nationality",
"queue_job",
],
"data": [
"data/cron_jobs.xml",
"data/queue_data.xml",
"data/queue_job_function_data.xml",
"data/pms_sequence.xml",
"security/ir.model.access.csv",
"views/pms_checkin_partner_views.xml",
@@ -27,5 +30,4 @@
"wizards/traveller_report.xml",
],
"installable": True,
"post_init_hook": "post_init_hook",
}

View File

@@ -15,7 +15,7 @@
name="nextcall"
eval="datetime.now(pytz.timezone('UTC')).strftime('%Y-%m-%d 03:00:00')"
/>
<field name="code">model.send_file_gc()</field>
<field name="code">model.send_file_gc_async()</field>
</record>
</data>
</odoo>

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<data noupdate="1">
<record id="channel_gc_file_send" model="queue.job.channel">
<field name="name">gc_file_send</field>
<field name="parent_id" ref="queue_job.channel_root" />
</record>
</data>
</odoo>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record
id="traveller_report_send_file_async_job_function"
model="queue.job.function"
>
<field name="model_id" ref="pms_l10n_es.model_traveller_report_wizard" />
<field name="method">send_file_async</field>
<field name="channel_id" ref="pms_l10n_es.channel_gc_file_send" />
<field name="retry_pattern" eval="{1: 10, 5: 30, 10: 60, 15: 300}" />
</record>
</odoo>

View File

@@ -1,16 +0,0 @@
from odoo import SUPERUSER_ID, api
def post_init_hook(cr, registry):
with api.Environment.manage():
env = api.Environment(cr, SUPERUSER_ID, {})
pms_properties = env["pms.property"].search([])
for pms_property in pms_properties:
if not pms_property.sequence_id:
pms_property.sequence_id = env["ir.sequence"].create(
{
"name": "sequence for property: " + pms_property.name,
"code": "property." + str(pms_property.id),
"padding": 3,
}
)

View File

@@ -1,4 +1,3 @@
from . import res_partner
from . import pms_checkin_partner
from . import pms_property
from . import ir_sequence

View File

@@ -1,16 +0,0 @@
from odoo import models
class IrSequence(models.Model):
_inherit = "ir.sequence"
def next_by_id(self, sequence_date=None):
seq = self.env['ir.sequence'].browse(self.id)
if seq.number_next == 1000:
seq.number_next = 1
# seq._set_number_next_actual()
result = super(IrSequence, self).next_by_id()
return result

View File

@@ -28,29 +28,3 @@ class PmsProperty(models.Model):
help="Password provided by institution to send the data.",
)
sequence_id = fields.Many2one("ir.sequence")
# ORM Overrides
@api.model
def create(self, vals):
result = super(PmsProperty, self).create(vals)
result["sequence_id"] = self.env["ir.sequence"].create(
{
"name": "sequence for property: " + result["name"],
"code": "property." + str(result.id),
"padding": 3,
}
)
return result
def write(self, vals):
result = super(PmsProperty, self).write(vals)
for record in self:
if not record.sequence_id:
record.sequence_id = self.env["ir.sequence"].create(
{
"name": "sequence for property: " + result["name"],
"code": "property." + str(result.id),
"padding": 3,
}
)
return result

View File

@@ -102,4 +102,3 @@ Ixjp6o7RTUaN8Tvkasq6+yO3m/qZASlaWFot4/nUbQ4mrcFuNLwy+AwF+mWj2zs3gyLp1txyM/1d
5mqwujGSQkBqvjrTcuFqN1W8rB2Vt2lh8kORdOag0wokRqEIr9baRRmW1FMdW4R58MD3R++Lj8UG
rp1MYp3/RgT408m2ECVAdf4WqslKYIYvuu8wd+RU4riEmViAqhOLUTpPSPaLtrM=
-----END CERTIFICATE-----

View File

@@ -1,2 +1 @@
from . import test_partner
from . import test_ir_sequence

View File

@@ -1,8 +0,0 @@
from odoo.tests import common
class TestIrSequence(common.SavepointCase):
def test_sequence_property(self):
pms_property = self.env["pms.property"].search([])[0]
value = pms_property.sequence_id.next_by_id()
print("test", value)

View File

@@ -1,11 +1,12 @@
import base64
import datetime
import io
from datetime import date
import os
import requests
from bs4 import BeautifulSoup as bs
from odoo import _, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.modules.module import get_module_resource
@@ -14,9 +15,13 @@ class TravellerReport(models.TransientModel):
_name = "traveller.report.wizard"
_description = "Traveller Report"
txt_filename = fields.Char()
txt_binary = fields.Binary()
txt_message = fields.Char()
txt_filename = fields.Text()
txt_binary = fields.Binary(
string="File Download"
)
txt_message = fields.Char(
string="File Preview"
)
def generate_file(self):
@@ -28,21 +33,17 @@ class TravellerReport(models.TransientModel):
# build content
content = self.generate_checkin_list(pms_property.id)
# get next sequence
sequence_num = self.env["ir.sequence"].next_by_code("traveller.report.wizard")
# file creation
txt_binary = self.env["traveller.report.wizard"].create(
{
"txt_filename": pms_property.institution_property_id
+ "."
+ sequence_num,
+ ".999",
"txt_binary": base64.b64encode(str.encode(content)),
"txt_message": content,
}
)
return {
"name": _("Download File"),
"name": _("Preview & Send File"),
"res_id": txt_binary.id,
"res_model": "traveller.report.wizard",
"target": "new",
@@ -133,41 +134,68 @@ class TravellerReport(models.TransientModel):
return content
def send_file_gc(self):
# get the active property
pms_property = self.env["pms.property"].search(
[("id", "=", self.env.user.get_active_property_ids()[0])]
)
def send_file_gc(self, pms_property=False):
url = "https://hospederias.guardiacivil.es/"
login_route = "/hospederias/login.do"
upload_file_route = "/hospederias/cargaFichero.do"
called_from_user = False
if not pms_property:
called_from_user = True
# get the active property
pms_property = self.env["pms.property"].search(
[("id", "=", self.env.user.get_active_property_ids()[0])]
)
# get next sequence to send
sequence_num = self.env["ir.sequence"].next_by_code("traveller.report.wizard")
# generate content to send
f = io.StringIO(self.generate_checkin_list(pms_property.id))
session = requests.Session()
# send info to GC
# response = \
session.post(
url="https://"
+ pms_property.institution_user
+ ":"
+ pms_property.institution_password
+ "@hospederias.guardiacivil.es/hospederias/servlet/"
"ControlRecepcionFichero",
files={
"file": (
pms_property.institution_user + "." + sequence_num,
f,
"application/octet-stream",
)
},
# TODO: review download file cert.pem
headers = {
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 "
"Build/MRA58N) AppleWebKit/537.36 (KHTML, like "
"Gecko) Chrome/90.0.4430.93 Mobile Safari/537.36",
}
s = requests.session()
login_payload = {
"usuario": pms_property.institution_user,
"pswd": pms_property.institution_password,
}
s.post(
url + login_route,
headers=headers,
data=login_payload,
verify=get_module_resource("pms_l10n_es", "static", "cert.pem"),
)
)
# TODO: review save log queue (oca/queue)
# print(response.content)
# if response.content != b'CORRECTO\r\n':
# raise ValidationError(response.content.decode())
pwd = get_module_resource("pms_l10n_es", "wizards", "")
checkin_list_file = open(pwd + pms_property.institution_user + ".999", "w+")
checkin_list_file.write(self.generate_checkin_list(pms_property.id))
checkin_list_file.close()
files = {"fichero": open(pwd + pms_property.institution_user + ".999", "rb")}
response_file_sent = s.post(
url + upload_file_route,
data={"autoSeq": "on"},
files=files,
verify=get_module_resource("pms_l10n_es", "static", "cert.pem"),
)
os.remove(pwd + pms_property.institution_user + ".999")
s.close()
soup = bs(response_file_sent.text, "html.parser")
errors = soup.select("#errores > tbody > tr > td > a")
if errors:
raise ValidationError(errors[2].text)
else:
if called_from_user:
message = {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': _('Sent succesfully!'),
'message': _('Successful file sending'),
'sticky': False,
}
}
return message
@api.model
def send_file_gc_async(self):
for prop in self.env["pms.property"].search([]):
self.with_delay().send_file_gc(prop)

View File

@@ -5,36 +5,37 @@
<field name="model">traveller.report.wizard</field>
<field name="arch" type="xml">
<form>
<sheet>
<div>
<field name="txt_filename" invisible="1" />
<div class="row">
<div class="col-12"
attrs="{'invisible': [('txt_message','=',False)]}"
>
<group>
<field name="txt_message" readonly="1" />
<field name="txt_binary" filename="txt_filename" readonly="1"/>
</group>
</div>
</div>
<div class="row ">
<div class="col-3">
<button
name="generate_file"
class="btn btn-primary btn-sm"
type="object"
string="Generate Police file"
/>
<field name="txt_filename" invisible="1" />
<field
name="txt_binary"
filename="txt_filename"
readonly="1"
style="margin-left: 20px;"
/>
<button
name="send_file_gc"
class="btn btn-primary btn-sm"
type="object"
string="Send file"
/>
name="generate_file"
class="btn btn-primary btn-sm"
type="object"
string="Preview file"
/>
</div>
<group colspan="1">
<div>
<p>
<field name="txt_message" readonly="1" />
</p>
<div class="col-3">
<button
name="send_file_gc"
class="btn btn-primary btn-sm"
type="object"
string="Send file"
/>
</div>
</group>
</sheet>
</div>
<footer/>
</form>
</field>
</record>