Skip to content

Commit

Permalink
[IMP] fieldservice_fleet: black, isort, prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
brian10048 committed Nov 1, 2020
1 parent ac7d01e commit 9953a9e
Show file tree
Hide file tree
Showing 11 changed files with 881 additions and 578 deletions.
44 changes: 18 additions & 26 deletions fieldservice_fleet/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,23 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
'name': 'Field Service Fleet',
'summary': 'Link Field Service vehicles with Odoo Fleet',
'version': '12.0.1.0.1',
'category': 'Field Service',
'author':
'Brian McMaster, '
'Open Source Integrators, '
'Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/field-service',
'depends': [
'fieldservice_vehicle',
'fleet',
"name": "Field Service Fleet",
"summary": "Link Field Service vehicles with Odoo Fleet",
"version": "12.0.1.0.1",
"category": "Field Service",
"author": "Brian McMaster, "
"Open Source Integrators, "
"Odoo Community Association (OCA)",
"website": "https://github.com/OCA/field-service",
"depends": ["fieldservice_vehicle", "fleet"],
"data": [
"views/fsm_vehicle.xml",
"views/fleet_vehicle.xml",
"wizard/fsm_fleet_wizard.xml",
],
'data': [
'views/fsm_vehicle.xml',
'views/fleet_vehicle.xml',
'wizard/fsm_fleet_wizard.xml',
],
'license': 'AGPL-3',
'development_status': 'Beta',
'maintainers': [
'wolfhall',
'max3903',
'brian10048',
],
'pre_init_hook': 'pre_init_hook',
'installable': True,
"license": "AGPL-3",
"development_status": "Beta",
"maintainers": ["wolfhall", "max3903", "brian10048"],
"pre_init_hook": "pre_init_hook",
"installable": True,
}
37 changes: 18 additions & 19 deletions fieldservice_fleet/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@

def pre_init_hook(cr):
# Check for existing fsm vehicles
cr.execute('SELECT * FROM fsm_vehicle')
cr.execute("SELECT * FROM fsm_vehicle")
vehicles = []
vehicles = cr.dictfetchall()
if vehicles:
# Add new columns to hold values
cr.execute('ALTER TABLE fsm_vehicle ADD fleet_vehicle_id INT;')
cr.execute('ALTER TABLE fleet_vehicle ADD is_fsm_vehicle BOOLEAN;')
cr.execute("ALTER TABLE fsm_vehicle ADD fleet_vehicle_id INT;")
cr.execute("ALTER TABLE fleet_vehicle ADD is_fsm_vehicle BOOLEAN;")

# Get a fleet vehicle model to set on the new Fleet vehicle(s)
env = api.Environment(cr, SUPERUSER_ID, {})
model_id = env['fleet.vehicle.model'].search([], limit=1).id
model_id = env["fleet.vehicle.model"].search([], limit=1).id

# Create a new Fleet vehicle for each FSM vehicle
for veh in vehicles:
# Get the FSM worker to set as the Fleet driver
fsm_person_id = veh.get('person_id', False)
fsm_person_id = veh.get("person_id", False)
driver_id = False
if fsm_person_id:
driver_id = env['fsm.person'].browse(
fsm_person_id).partner_id.id
driver_id = env["fsm.person"].browse(fsm_person_id).partner_id.id

cr.execute("""
cr.execute(
"""
INSERT INTO fleet_vehicle (
name,
model_id,
Expand All @@ -42,25 +42,24 @@ def pre_init_hook(cr):
True,
'kilometers',
True);""",
(
veh.get('name'),
model_id,
driver_id
)
)
(veh.get("name"), model_id, driver_id),
)

# Set this new Fleet vehicle on the existing FSM vehicle
cr.execute("""
cr.execute(
"""
SELECT id
FROM fleet_vehicle
ORDER BY id desc
LIMIT 1
""")
"""
)
fleet = cr.dictfetchone()

cr.execute("""
cr.execute(
"""
UPDATE fsm_vehicle
SET fleet_vehicle_id = %s
WHERE id = %s;""",
(fleet.get('id'), veh.get('id'))
)
(fleet.get("id"), veh.get("id")),
)
20 changes: 9 additions & 11 deletions fieldservice_fleet/models/fleet_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,25 @@


class FleetVehicle(models.Model):
_inherit = 'fleet.vehicle'
_inherit = "fleet.vehicle"

is_fsm_vehicle = fields.Boolean(
string='Is used for Field Service?'
)
is_fsm_vehicle = fields.Boolean(string="Is used for Field Service?")

def set_fsm_driver(self):
self.ensure_one()
if self.driver_id and self.is_fsm_vehicle:
driver_partner = self.driver_id
fsm_worker = self.env['fsm.person'].search(
[('partner_id', '=', driver_partner.id)]
fsm_worker = self.env["fsm.person"].search(
[("partner_id", "=", driver_partner.id)]
)
if not fsm_worker:
# Create FSM worker
fsm_worker = self.env['fsm.person'].create(
{'partner_id': driver_partner.id}
fsm_worker = self.env["fsm.person"].create(
{"partner_id": driver_partner.id}
)
driver_partner.write({'fsm_person': True})
fsm_vehicle = self.env['fsm.vehicle'].search(
[('fleet_vehicle_id', '=', self.id)]
driver_partner.write({"fsm_person": True})
fsm_vehicle = self.env["fsm.vehicle"].search(
[("fleet_vehicle_id", "=", self.id)]
)
# Assign the worker to the FSM vehicle
fsm_vehicle.person_id = fsm_worker
32 changes: 16 additions & 16 deletions fieldservice_fleet/models/fsm_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@


class FSMVehicle(models.Model):
_inherit = 'fsm.vehicle'
_inherits = {'fleet.vehicle': 'fleet_vehicle_id'}
_inherit = "fsm.vehicle"
_inherits = {"fleet.vehicle": "fleet_vehicle_id"}

fleet_vehicle_id = fields.Many2one(
'fleet.vehicle', string='Vehicle Details',
required=True, ondelete='restrict',
"fleet.vehicle", string="Vehicle Details", required=True, ondelete="restrict",
)

_sql_constraints = [
('fsm_vehicle_fleet_uniq',
'unique(id,fleet_vehicle_id)',
'FSM vehicle can only be linked to one fleet vehicle')
(
"fsm_vehicle_fleet_uniq",
"unique(id,fleet_vehicle_id)",
"FSM vehicle can only be linked to one fleet vehicle",
)
]

@api.model
def create(self, vals):
fleet_id = vals.get('fleet_vehicle_id')
fleet_id = vals.get("fleet_vehicle_id")
if fleet_id:
if vals.get('person_id', False):
vals['driver_id'] = vals.get('person_id')
vals['is_fsm_vehicle'] = True
if vals.get("person_id", False):
vals["driver_id"] = vals.get("person_id")
vals["is_fsm_vehicle"] = True
return super().create(vals)

@api.multi
def write(self, vals):
# update fsm.vehicle worker based on the fleet.vehicle driver
if 'driver_id' in vals:
if "driver_id" in vals:
for vehicle in self:
if vehicle.is_fsm_vehicle:
vehicle.set_fsm_driver()
# update fleet.vehicle driver based on the fsm.vehicle worker
fsm_worker_id = vals.get('person_id', False)
fsm_worker_id = vals.get("person_id", False)
if fsm_worker_id:
worker_partner = self.env['fsm.person'].browse(
fsm_worker_id).partner_id
vals.update({'driver_id': worker_partner.id})
worker_partner = self.env["fsm.person"].browse(fsm_worker_id).partner_id
vals.update({"driver_id": worker_partner.id})
return super().write(vals)
Loading

0 comments on commit 9953a9e

Please sign in to comment.