2021-01-14 16:56:21 +01:00
|
|
|
import os
|
|
|
|
import base64
|
2020-04-19 17:54:08 +02:00
|
|
|
import requests
|
2021-01-14 16:56:21 +01:00
|
|
|
import tempfile
|
2020-04-19 17:54:08 +02:00
|
|
|
from datetime import date
|
|
|
|
import simplejson as json
|
|
|
|
|
2021-01-14 16:56:21 +01:00
|
|
|
try:
|
|
|
|
from app.models import MODELS
|
|
|
|
except:
|
|
|
|
from models import MODELS
|
|
|
|
|
|
|
|
try:
|
|
|
|
from app.commons.common import slugify, file_open
|
|
|
|
except:
|
|
|
|
from commons.common import slugify, file_open
|
2020-12-20 12:14:56 +01:00
|
|
|
|
2020-04-19 17:54:08 +02:00
|
|
|
|
|
|
|
def encoder(obj):
|
|
|
|
# FIXME: add datetime, buffer, bytes
|
|
|
|
if isinstance(obj, date):
|
|
|
|
return {
|
|
|
|
'__class__': 'date',
|
|
|
|
'year': obj.year,
|
|
|
|
'month': obj.month,
|
|
|
|
'day': obj.day,
|
|
|
|
}
|
|
|
|
raise TypeError(repr(obj) + " is not JSON serializable")
|
|
|
|
|
|
|
|
|
|
|
|
class FastModel(object):
|
|
|
|
|
2020-12-20 12:14:56 +01:00
|
|
|
def __init__(self, model, ctx, fields=None):
|
2020-04-19 17:54:08 +02:00
|
|
|
self.model = model
|
|
|
|
self.ctx = ctx
|
2020-04-22 02:26:19 +02:00
|
|
|
api_url = ctx['params']['api_url']
|
|
|
|
db = ctx['params']['database']
|
|
|
|
self.api = '/'.join(['http:/', api_url, db])
|
2020-12-20 12:14:56 +01:00
|
|
|
_model = MODELS.get(model)
|
|
|
|
self.fields = None
|
|
|
|
if fields:
|
|
|
|
self.fields = fields
|
|
|
|
elif _model.get('fields'):
|
|
|
|
self.fields = _model['fields']
|
2020-04-19 17:54:08 +02:00
|
|
|
|
|
|
|
def __getattr__(self, name, *args):
|
|
|
|
'Return attribute value'
|
|
|
|
self.method = name
|
|
|
|
return self
|
|
|
|
|
2020-12-15 04:51:28 +01:00
|
|
|
def find(self, domain, order=None, limit=1000, ctx=None, fields=None):
|
2020-04-19 17:54:08 +02:00
|
|
|
if ctx:
|
|
|
|
self.ctx.update(ctx)
|
|
|
|
route = self.get_route('search')
|
2020-12-20 12:14:56 +01:00
|
|
|
if not fields:
|
|
|
|
fields = self.fields
|
2020-04-19 17:54:08 +02:00
|
|
|
args_ = {
|
|
|
|
'model': self.model,
|
|
|
|
'domain': domain,
|
|
|
|
'order': order,
|
|
|
|
'limit': limit,
|
2020-12-15 04:51:28 +01:00
|
|
|
'fields': fields,
|
2020-04-19 17:54:08 +02:00
|
|
|
'context': self.ctx,
|
|
|
|
}
|
|
|
|
data = json.dumps(args_, default=encoder)
|
2020-06-12 04:36:27 +02:00
|
|
|
res = requests.post(route, data=data)
|
2020-04-19 17:54:08 +02:00
|
|
|
return res.json()
|
|
|
|
|
2021-03-01 06:04:54 +01:00
|
|
|
def write_many(self, ids, values, fields=None):
|
|
|
|
route = self.get_route('save_many')
|
|
|
|
if not fields:
|
|
|
|
fields = self.fields
|
|
|
|
args_ = {
|
|
|
|
'model': self.model,
|
|
|
|
'ids': ids,
|
|
|
|
'values': values,
|
|
|
|
'fields': fields,
|
|
|
|
'context': self.ctx,
|
|
|
|
}
|
|
|
|
data = json.dumps(args_, default=encoder)
|
|
|
|
res = requests.put(route, data=data)
|
|
|
|
return res.json()
|
|
|
|
|
2020-12-20 17:05:04 +01:00
|
|
|
def write(self, ids, values, fields=None):
|
2020-04-19 17:54:08 +02:00
|
|
|
route = self.get_route('save')
|
2021-03-01 06:04:54 +01:00
|
|
|
|
2020-12-20 17:05:04 +01:00
|
|
|
if not fields:
|
|
|
|
fields = self.fields
|
2021-08-21 13:42:22 +02:00
|
|
|
if values.get('rec_name'):
|
|
|
|
_ = values.pop('rec_name')
|
2020-04-19 17:54:08 +02:00
|
|
|
args_ = {
|
|
|
|
'model': self.model,
|
|
|
|
'id': ids[0],
|
2021-03-01 06:04:54 +01:00
|
|
|
'ids': ids,
|
2020-04-19 17:54:08 +02:00
|
|
|
'record_data': values,
|
2020-12-20 17:05:04 +01:00
|
|
|
'fields': fields,
|
2020-04-19 17:54:08 +02:00
|
|
|
'context': self.ctx,
|
|
|
|
}
|
|
|
|
data = json.dumps(args_, default=encoder)
|
|
|
|
res = requests.put(route, data=data)
|
|
|
|
return res.json()
|
|
|
|
|
2021-01-23 14:16:49 +01:00
|
|
|
def create(self, values):
|
|
|
|
route = self.get_route('create')
|
2021-08-21 13:42:22 +02:00
|
|
|
if values.get('rec_name'):
|
|
|
|
_ = values.pop('rec_name')
|
2021-01-23 14:16:49 +01:00
|
|
|
args_ = {
|
|
|
|
'model': self.model,
|
|
|
|
'record': values,
|
|
|
|
'context': self.ctx,
|
|
|
|
}
|
|
|
|
data = json.dumps(args_, default=encoder)
|
|
|
|
res = requests.post(route, data=data)
|
|
|
|
return res.json()
|
|
|
|
|
2020-04-19 17:54:08 +02:00
|
|
|
def delete(self, ids):
|
|
|
|
route = self.get_route('delete')
|
|
|
|
args_ = {
|
|
|
|
'model': self.model,
|
|
|
|
'ids': ids,
|
|
|
|
'context': self.ctx,
|
|
|
|
}
|
|
|
|
data = json.dumps(args_, default=encoder)
|
|
|
|
res = requests.delete(route, data=data)
|
|
|
|
return res.json()
|
|
|
|
|
|
|
|
def get_route(self, target):
|
|
|
|
route = self.api + '/' + target
|
|
|
|
return route
|
|
|
|
|
2022-05-03 00:25:05 +02:00
|
|
|
def __call__(self, values=None, ctx=None):
|
|
|
|
if ctx:
|
|
|
|
self.ctx.update(ctx)
|
2020-04-19 17:54:08 +02:00
|
|
|
args_ = {
|
|
|
|
'model': self.model,
|
|
|
|
'method': self.method,
|
2021-01-12 16:32:04 +01:00
|
|
|
'args': values,
|
2020-04-19 17:54:08 +02:00
|
|
|
'context': self.ctx,
|
|
|
|
}
|
|
|
|
route = self.get_route('model_method')
|
|
|
|
data = json.dumps(args_, default=encoder)
|
|
|
|
res = requests.post(route, data=data)
|
2020-12-20 02:23:30 +01:00
|
|
|
response = None
|
2020-08-15 00:11:46 +02:00
|
|
|
try:
|
2020-12-20 02:23:30 +01:00
|
|
|
response = res.json()
|
2020-08-15 00:11:46 +02:00
|
|
|
except ValueError:
|
2020-12-30 17:29:31 +01:00
|
|
|
pass
|
2020-12-20 02:23:30 +01:00
|
|
|
return response
|
2020-04-19 17:54:08 +02:00
|
|
|
|
2021-01-13 23:14:57 +01:00
|
|
|
|
|
|
|
class FastReport(object):
|
|
|
|
|
2021-01-14 16:56:21 +01:00
|
|
|
def __init__(self, ctx):
|
2021-01-13 23:14:57 +01:00
|
|
|
self.ctx = ctx
|
|
|
|
api_url = ctx['params']['api_url']
|
|
|
|
db = ctx['params']['database']
|
|
|
|
self.api = '/'.join(['http:/', api_url, db])
|
|
|
|
|
2021-01-14 16:56:21 +01:00
|
|
|
def get_route(self, target):
|
|
|
|
route = self.api + '/' + target
|
|
|
|
return route
|
|
|
|
|
|
|
|
def get(self, values):
|
2021-01-13 23:14:57 +01:00
|
|
|
route = self.get_route('report')
|
|
|
|
args_ = {
|
|
|
|
'report': values['report_name'],
|
|
|
|
'args': values['args'],
|
2021-01-14 16:56:21 +01:00
|
|
|
'context': self.ctx['params'],
|
2021-01-13 23:14:57 +01:00
|
|
|
}
|
|
|
|
data = json.dumps(args_, default=encoder)
|
|
|
|
res = requests.post(route, data=data)
|
|
|
|
return res.json()
|
|
|
|
|
2021-01-14 16:56:21 +01:00
|
|
|
def open(self, args):
|
|
|
|
oext = args['oext']
|
|
|
|
content = args['content']
|
|
|
|
direct_print = args['direct_print']
|
|
|
|
name = args['name']
|
|
|
|
|
|
|
|
dtemp = tempfile.mkdtemp(prefix='tryton_')
|
2021-03-17 18:15:01 +01:00
|
|
|
fp_name = os.path.join(
|
|
|
|
dtemp, slugify(name) + os.extsep + slugify(oext)
|
|
|
|
)
|
|
|
|
print('archivo :', dtemp, fp_name)
|
|
|
|
|
2021-01-14 16:56:21 +01:00
|
|
|
content_data = base64.b64decode(content)
|
2021-03-17 18:15:01 +01:00
|
|
|
with open(fp_name, 'wb') as file_d:
|
|
|
|
file_d.write(content_data)
|
|
|
|
file_open(fp_name, type, direct_print=direct_print)
|
2021-01-14 16:56:21 +01:00
|
|
|
|
2020-04-19 17:54:08 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-01-13 23:14:57 +01:00
|
|
|
test_model = False
|
|
|
|
test_report = True
|
2021-01-14 16:56:21 +01:00
|
|
|
ctx = {
|
|
|
|
'company': 1,
|
|
|
|
'user': 1,
|
|
|
|
'params': {'api_url': 'localhost:5070', 'database': 'DEMO50'}
|
|
|
|
}
|
2021-01-13 23:14:57 +01:00
|
|
|
if test_model:
|
|
|
|
model = {'model': 'sale.sale'}
|
2021-01-14 16:56:21 +01:00
|
|
|
test_model = FastModel(model, ctx)
|
2021-01-13 23:14:57 +01:00
|
|
|
id = 180
|
|
|
|
data = {
|
|
|
|
'reference': 'OC-02874',
|
|
|
|
}
|
|
|
|
res = test_model.write([id], data)
|
2021-01-14 16:56:21 +01:00
|
|
|
|
2021-01-13 23:14:57 +01:00
|
|
|
if test_report:
|
|
|
|
data = {
|
|
|
|
'report_name': 'sale_pos_frontend.sale_square_box_report',
|
2021-01-14 16:56:21 +01:00
|
|
|
'args': {
|
|
|
|
'date': '2021-01-12',
|
|
|
|
'turn': 1,
|
|
|
|
'company': 1,
|
|
|
|
'shop': 1,
|
|
|
|
},
|
2021-01-13 23:14:57 +01:00
|
|
|
}
|
2021-01-14 16:56:21 +01:00
|
|
|
report = FastReport(ctx)
|
|
|
|
res = report.get(data)
|
|
|
|
report.open(res)
|