From 0cbca17df58fbe1b7bf86563adc8daadf8fa0908 Mon Sep 17 00:00:00 2001 From: Ernesto Tejeda Date: Wed, 28 Oct 2020 15:32:43 -0400 Subject: [PATCH] [IMP] rma_sale: black, isort, prettier --- rma_sale/__manifest__.py | 5 +- rma_sale/controllers/rma_portal.py | 5 +- rma_sale/controllers/sale_portal.py | 38 ++-- rma_sale/models/rma.py | 67 +++--- rma_sale/models/sale.py | 133 ++++++------ rma_sale/tests/test_rma_sale.py | 42 ++-- rma_sale/views/assets.xml | 8 +- rma_sale/views/report_rma.xml | 4 +- rma_sale/views/rma_views.xml | 12 +- rma_sale/views/sale_portal_template.xml | 193 +++++++++++++----- rma_sale/views/sale_views.xml | 32 +-- rma_sale/wizard/sale_order_rma_wizard.py | 149 +++++++------- .../wizard/sale_order_rma_wizard_views.xml | 51 ++--- 13 files changed, 415 insertions(+), 324 deletions(-) diff --git a/rma_sale/__manifest__.py b/rma_sale/__manifest__.py index cc08a170..e39a5e20 100644 --- a/rma_sale/__manifest__.py +++ b/rma_sale/__manifest__.py @@ -10,10 +10,7 @@ "author": "Tecnativa, Odoo Community Association (OCA)", "maintainers": ["ernestotejeda"], "license": "AGPL-3", - "depends": [ - "rma", - "sale_stock", - ], + "depends": ["rma", "sale_stock",], "data": [ "views/assets.xml", "views/report_rma.xml", diff --git a/rma_sale/controllers/rma_portal.py b/rma_sale/controllers/rma_portal.py index 505ab5c4..c4e58156 100644 --- a/rma_sale/controllers/rma_portal.py +++ b/rma_sale/controllers/rma_portal.py @@ -5,9 +5,8 @@ from odoo.addons.rma.controllers.main import PortalRma class PortalRma(PortalRma): - def _get_filter_domain(self, kw): res = super()._get_filter_domain(kw) - if 'sale_id' in kw: - res.append(('order_id', '=', int(kw['sale_id']))) + if "sale_id" in kw: + res.append(("order_id", "=", int(kw["sale_id"]))) return res diff --git a/rma_sale/controllers/sale_portal.py b/rma_sale/controllers/sale_portal.py index 95c0460a..70403a80 100644 --- a/rma_sale/controllers/sale_portal.py +++ b/rma_sale/controllers/sale_portal.py @@ -1,42 +1,48 @@ # Copyright 2020 Tecnativa - Ernesto Tejeda # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import http, _ +from odoo import _, http from odoo.exceptions import AccessError, MissingError from odoo.http import request + from odoo.addons.sale.controllers.portal import CustomerPortal class CustomerPortal(CustomerPortal): - @http.route(['/my/orders//requestrma'], type='http', - auth="public", methods=['POST'], website=True) + @http.route( + ["/my/orders//requestrma"], + type="http", + auth="public", + methods=["POST"], + website=True, + ) def request_rma(self, order_id, access_token=None, **post): try: - order_sudo = self._document_check_access('sale.order', order_id, - access_token=access_token) + order_sudo = self._document_check_access( + "sale.order", order_id, access_token=access_token + ) except (AccessError, MissingError): - return request.redirect('/my') - order_obj = request.env['sale.order'] - wizard_obj = request.env['sale.order.rma.wizard'] + return request.redirect("/my") + order_obj = request.env["sale.order"] + wizard_obj = request.env["sale.order.rma.wizard"] # Set wizard line vals mapped_vals = {} for name, value in post.items(): - row, field_name = name.split('-', 1) + row, field_name = name.split("-", 1) mapped_vals.setdefault(row, {}).update({field_name: value}) # If no operation is filled, no RMA will be created line_vals = [ - (0, 0, vals) for vals in mapped_vals.values() - if vals.get("operation_id")] + (0, 0, vals) for vals in mapped_vals.values() if vals.get("operation_id") + ] # Create wizard an generate rmas order = order_obj.browse(order_id).sudo() location_id = order.warehouse_id.rma_loc_id.id - wizard = wizard_obj.with_context(active_id=order_id).create({ - 'line_ids': line_vals, - 'location_id': location_id - }) + wizard = wizard_obj.with_context(active_id=order_id).create( + {"line_ids": line_vals, "location_id": location_id} + ) rma = wizard.sudo().create_rma() for rec in rma: - rec.origin += _(' (Portal)') + rec.origin += _(" (Portal)") # Add the user as follower of the created RMAs so they can # later view them. rma.message_subscribe([request.env.user.partner_id.id]) diff --git a/rma_sale/models/rma.py b/rma_sale/models/rma.py index c3a947fc..e4b87f2f 100644 --- a/rma_sale/models/rma.py +++ b/rma_sale/models/rma.py @@ -8,69 +8,64 @@ class Rma(models.Model): _inherit = "rma" order_id = fields.Many2one( - comodel_name='sale.order', - string='Sale Order', + comodel_name="sale.order", + string="Sale Order", domain="[" - " ('partner_id', 'child_of', commercial_partner_id)," - " ('state', 'in', ['sale', 'done'])," - "]", + " ('partner_id', 'child_of', commercial_partner_id)," + " ('state', 'in', ['sale', 'done'])," + "]", readonly=True, - states={'draft': [('readonly', False)]}, + states={"draft": [("readonly", False)]}, ) allowed_picking_ids = fields.Many2many( - comodel_name='stock.picking', - compute="_compute_allowed_picking_ids", - ) - picking_id = fields.Many2one( - domain="[('id', 'in', allowed_picking_ids)]", + comodel_name="stock.picking", compute="_compute_allowed_picking_ids", ) + picking_id = fields.Many2one(domain="[('id', 'in', allowed_picking_ids)]",) allowed_move_ids = fields.Many2many( - comodel_name='sale.order.line', - compute="_compute_allowed_move_ids", - ) - move_id = fields.Many2one( - domain="[('id', 'in', allowed_move_ids)]", + comodel_name="sale.order.line", compute="_compute_allowed_move_ids", ) + move_id = fields.Many2one(domain="[('id', 'in', allowed_move_ids)]",) allowed_product_ids = fields.Many2many( - comodel_name='product.product', - compute="_compute_allowed_product_ids", - ) - product_id = fields.Many2one( - domain="[('id', 'in', allowed_product_ids)]", + comodel_name="product.product", compute="_compute_allowed_product_ids", ) + product_id = fields.Many2one(domain="[('id', 'in', allowed_product_ids)]",) - @api.depends('partner_id', 'order_id') + @api.depends("partner_id", "order_id") def _compute_allowed_picking_ids(self): - domain = [('state', '=', 'done'), - ('picking_type_id.code', '=', 'outgoing')] + domain = [("state", "=", "done"), ("picking_type_id.code", "=", "outgoing")] for rec in self: # if rec.partner_id: commercial_partner = rec.partner_id.commercial_partner_id - domain.append(('partner_id', 'child_of', commercial_partner.id)) + domain.append(("partner_id", "child_of", commercial_partner.id)) if rec.order_id: - domain.append(('sale_id', '=', rec.order_id.id)) - rec.allowed_picking_ids = self.env['stock.picking'].search(domain) + domain.append(("sale_id", "=", rec.order_id.id)) + rec.allowed_picking_ids = self.env["stock.picking"].search(domain) - @api.depends('order_id', 'picking_id') + @api.depends("order_id", "picking_id") def _compute_allowed_move_ids(self): for rec in self: if rec.order_id: - order_move = rec.order_id.order_line.mapped('move_ids') + order_move = rec.order_id.order_line.mapped("move_ids") rec.allowed_move_ids = order_move.filtered( - lambda r: r.picking_id == self.picking_id).ids + lambda r: r.picking_id == self.picking_id + ).ids else: rec.allowed_move_ids = self.picking_id.move_lines.ids - @api.depends('order_id') + @api.depends("order_id") def _compute_allowed_product_ids(self): for rec in self: if rec.order_id: - order_product = rec.order_id.order_line.mapped('product_id') + order_product = rec.order_id.order_line.mapped("product_id") rec.allowed_product_ids = order_product.filtered( - lambda r: r.type in ['consu', 'product']).ids + lambda r: r.type in ["consu", "product"] + ).ids else: - rec.allowed_product_ids = self.env['product.product'].search( - [('type', 'in', ['consu', 'product'])]).ids + rec.allowed_product_ids = ( + self.env["product.product"] + .search([("type", "in", ["consu", "product"])]) + .ids + ) @api.onchange("partner_id") def _onchange_partner_id(self): @@ -78,7 +73,7 @@ class Rma(models.Model): self.order_id = False return res - @api.onchange('order_id') + @api.onchange("order_id") def _onchange_order_id(self): self.product_id = self.picking_id = False diff --git a/rma_sale/models/sale.py b/rma_sale/models/sale.py index 54f59860..4346b501 100644 --- a/rma_sale/models/sale.py +++ b/rma_sale/models/sale.py @@ -10,64 +10,63 @@ class SaleOrder(models.Model): # RMAs that were created from a sale order rma_ids = fields.One2many( - comodel_name='rma', - inverse_name='order_id', - string='RMAs', - copy=False, - ) - rma_count = fields.Integer( - string='RMA count', - compute='_compute_rma_count', + comodel_name="rma", inverse_name="order_id", string="RMAs", copy=False, ) + rma_count = fields.Integer(string="RMA count", compute="_compute_rma_count",) def _compute_rma_count(self): - rma_data = self.env['rma'].read_group( - [('order_id', 'in', self.ids)], ['order_id'], ['order_id']) - mapped_data = dict( - [(r['order_id'][0], r['order_id_count']) for r in rma_data]) + rma_data = self.env["rma"].read_group( + [("order_id", "in", self.ids)], ["order_id"], ["order_id"] + ) + mapped_data = {r["order_id"][0]: r["order_id_count"] for r in rma_data} for record in self: record.rma_count = mapped_data.get(record.id, 0) def action_create_rma(self): self.ensure_one() - if self.state not in ['sale', 'done']: - raise ValidationError(_("You may only create RMAs from a " - "confirmed or done sale order.")) - wizard_obj = self.env['sale.order.rma.wizard'] - line_vals = [(0, 0, { - 'product_id': data['product'].id, - 'quantity': data['quantity'], - 'uom_id': data['uom'].id, - 'picking_id': data['picking'] and data['picking'].id, - }) for data in self.get_delivery_rma_data()] - wizard = wizard_obj.with_context(active_id=self.id).create({ - 'line_ids': line_vals, - 'location_id': self.warehouse_id.rma_loc_id.id - }) + if self.state not in ["sale", "done"]: + raise ValidationError( + _("You may only create RMAs from a " "confirmed or done sale order.") + ) + wizard_obj = self.env["sale.order.rma.wizard"] + line_vals = [ + ( + 0, + 0, + { + "product_id": data["product"].id, + "quantity": data["quantity"], + "uom_id": data["uom"].id, + "picking_id": data["picking"] and data["picking"].id, + }, + ) + for data in self.get_delivery_rma_data() + ] + wizard = wizard_obj.with_context(active_id=self.id).create( + {"line_ids": line_vals, "location_id": self.warehouse_id.rma_loc_id.id} + ) return { - 'name': _('Create RMA'), - 'type': 'ir.actions.act_window', - 'view_type': 'form', - 'view_mode': 'form', - 'res_model': 'sale.order.rma.wizard', - 'res_id': wizard.id, - 'target': 'new', + "name": _("Create RMA"), + "type": "ir.actions.act_window", + "view_type": "form", + "view_mode": "form", + "res_model": "sale.order.rma.wizard", + "res_id": wizard.id, + "target": "new", } def action_view_rma(self): self.ensure_one() - action = self.env.ref('rma.rma_action').read()[0] + action = self.env.ref("rma.rma_action").read()[0] rma = self.rma_ids if len(rma) == 1: action.update( - res_id=rma.id, - view_mode="form", - views=[], + res_id=rma.id, view_mode="form", views=[], ) else: - action['domain'] = [('id', 'in', rma.ids)] + action["domain"] = [("id", "in", rma.ids)] # reset context to show all related rma without default filters - action['context'] = {} + action["context"] = {} return action def get_delivery_rma_data(self): @@ -83,19 +82,23 @@ class SaleOrderLine(models.Model): def get_delivery_move(self): self.ensure_one() - return self.move_ids.filtered(lambda r: ( - self.product_id == r.product_id - and r.state == 'done' - and not r.scrapped - and r.location_dest_id.usage == "customer" - and (not r.origin_returned_move_id - or (r.origin_returned_move_id and r.to_refund)) - )) + return self.move_ids.filtered( + lambda r: ( + self.product_id == r.product_id + and r.state == "done" + and not r.scrapped + and r.location_dest_id.usage == "customer" + and ( + not r.origin_returned_move_id + or (r.origin_returned_move_id and r.to_refund) + ) + ) + ) def prepare_sale_rma_data(self): self.ensure_one() product = self.product_id - if self.product_id.type != 'product': + if self.product_id.type != "product": return {} moves = self.get_delivery_move() data = [] @@ -103,20 +106,24 @@ class SaleOrderLine(models.Model): for move in moves: qty = move.product_uom_qty move_dest = move.move_dest_ids.filtered( - lambda r: r.state in ['partially_available', - 'assigned', 'done']) - qty -= sum(move_dest.mapped('product_uom_qty')) - data.append({ - 'product': product, - 'quantity': qty, - 'uom': move.product_uom, - 'picking': move.picking_id, - }) + lambda r: r.state in ["partially_available", "assigned", "done"] + ) + qty -= sum(move_dest.mapped("product_uom_qty")) + data.append( + { + "product": product, + "quantity": qty, + "uom": move.product_uom, + "picking": move.picking_id, + } + ) else: - data.append({ - 'product': product, - 'quantity': self.qty_delivered, - 'uom': self.product_uom, - 'picking': False, - }) + data.append( + { + "product": product, + "quantity": self.qty_delivered, + "uom": self.product_uom, + "picking": False, + } + ) return data diff --git a/rma_sale/tests/test_rma_sale.py b/rma_sale/tests/test_rma_sale.py index ab0198be..bc4529bf 100644 --- a/rma_sale/tests/test_rma_sale.py +++ b/rma_sale/tests/test_rma_sale.py @@ -8,21 +8,17 @@ class TestRmaSale(SavepointCase): @classmethod def setUpClass(cls): super(TestRmaSale, cls).setUpClass() - cls.res_partner = cls.env['res.partner'] - cls.product_product = cls.env['product.product'] - cls.sale_order = cls.env['sale.order'] + cls.res_partner = cls.env["res.partner"] + cls.product_product = cls.env["product.product"] + cls.sale_order = cls.env["sale.order"] - cls.product_1 = cls.product_product.create({ - 'name': 'Product test 1', - 'type': 'product', - }) - cls.product_2 = cls.product_product.create({ - 'name': 'Product test 2', - 'type': 'product', - }) - cls.partner = cls.res_partner.create({ - 'name': 'Partner test', - }) + cls.product_1 = cls.product_product.create( + {"name": "Product test 1", "type": "product",} + ) + cls.product_2 = cls.product_product.create( + {"name": "Product test 2", "type": "product",} + ) + cls.partner = cls.res_partner.create({"name": "Partner test",}) order_form = Form(cls.sale_order) order_form.partner_id = cls.partner with order_form.order_line.new() as line_form: @@ -33,13 +29,14 @@ class TestRmaSale(SavepointCase): # Maybe other modules create additional lines in the create # method in sale.order model, so let's find the correct line. cls.order_line = cls.sale_order.order_line.filtered( - lambda r: r.product_id == cls.product_1) + lambda r: r.product_id == cls.product_1 + ) cls.order_out_picking = cls.sale_order.picking_ids cls.order_out_picking.move_lines.quantity_done = 5 cls.order_out_picking.button_validate() def test_create_rma_with_so(self): - rma_form = Form(self.env['rma']) + rma_form = Form(self.env["rma"]) rma_form.partner_id = self.partner rma_form.order_id = self.sale_order rma_form.product_id = self.product_1 @@ -51,9 +48,9 @@ class TestRmaSale(SavepointCase): def test_create_rma_from_so(self): order = self.sale_order - wizard_id = order.action_create_rma()['res_id'] - wizard = self.env['sale.order.rma.wizard'].browse(wizard_id) - rma = self.env['rma'].browse(wizard.create_and_open_rma()['res_id']) + wizard_id = order.action_create_rma()["res_id"] + wizard = self.env["sale.order.rma.wizard"].browse(wizard_id) + rma = self.env["rma"].browse(wizard.create_and_open_rma()["res_id"]) self.assertEqual(rma.partner_id, order.partner_id) self.assertEqual(rma.order_id, order) self.assertEqual(rma.picking_id, self.order_out_picking) @@ -61,7 +58,7 @@ class TestRmaSale(SavepointCase): self.assertEqual(rma.product_id, self.product_1) self.assertEqual(rma.product_uom_qty, self.order_line.product_uom_qty) self.assertEqual(rma.product_uom, self.order_line.product_uom) - self.assertEqual(rma.state, 'confirmed') + self.assertEqual(rma.state, "confirmed") self.assertEqual( rma.reception_move_id.origin_returned_move_id, self.order_out_picking.move_lines, @@ -72,10 +69,7 @@ class TestRmaSale(SavepointCase): ) # Refund the RMA user = self.env["res.users"].create( - { - "login": "test_refund_with_so", - "name": "Test", - } + {"login": "test_refund_with_so", "name": "Test",} ) order.user_id = user.id rma.action_confirm() diff --git a/rma_sale/views/assets.xml b/rma_sale/views/assets.xml index e0a8aee1..e946398f 100644 --- a/rma_sale/views/assets.xml +++ b/rma_sale/views/assets.xml @@ -1,8 +1,12 @@ - + diff --git a/rma_sale/views/report_rma.xml b/rma_sale/views/report_rma.xml index df29c25c..7bcc1688 100644 --- a/rma_sale/views/report_rma.xml +++ b/rma_sale/views/report_rma.xml @@ -4,13 +4,13 @@
Sale order: -

+

Requested operation: -

+

diff --git a/rma_sale/views/rma_views.xml b/rma_sale/views/rma_views.xml index f00ce87f..6247ed8d 100644 --- a/rma_sale/views/rma_views.xml +++ b/rma_sale/views/rma_views.xml @@ -1,19 +1,19 @@ - + rma.view.form rma - + - + - - - + + + diff --git a/rma_sale/views/sale_portal_template.xml b/rma_sale/views/sale_portal_template.xml index c361b63f..3063a429 100644 --- a/rma_sale/views/sale_portal_template.xml +++ b/rma_sale/views/sale_portal_template.xml @@ -1,37 +1,80 @@ -