Skip to content

Commit

Permalink
[IMP] : add myaccount controller and improve ticket filtering based o…
Browse files Browse the repository at this point in the history
…n property permissions
  • Loading branch information
IrluiDev committed Sep 20, 2024
1 parent 75a82a7 commit 2184f6d
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 55 deletions.
1 change: 1 addition & 0 deletions pms_helpdesk_mgmt/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"data": [
"views/helpdesk_ticket_views.xml",
"views/helpdesk_ticket_templates.xml",
"securitty/helpdesk_security_pms.xml",
],
"demo": ["demo/helpdesk_demo.xml"],
"assets": {
Expand Down
2 changes: 2 additions & 0 deletions pms_helpdesk_mgmt/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from . import main
from . import myaccount

25 changes: 19 additions & 6 deletions pms_helpdesk_mgmt/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@

import odoo.http as http
from odoo.http import request

from odoo.exceptions import UserError, AccessError
from odoo.addons.helpdesk_mgmt.controllers.main import HelpdeskTicketController

_logger = logging.getLogger(__name__)


class CustomHelpdeskTicketController(HelpdeskTicketController):
@http.route("/get_companies", type="json", auth="user")
def get_companies(self):
user = request.env.user
allowed_pms_property_ids = user.get_active_property_ids()

Check warning on line 19 in pms_helpdesk_mgmt/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/main.py#L18-L19

Added lines #L18 - L19 were not covered by tests
if allowed_pms_property_ids:
properties = request.env['pms.property'].sudo().browse(allowed_pms_property_ids)
companies = properties.mapped('company_id')

Check warning on line 22 in pms_helpdesk_mgmt/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/main.py#L21-L22

Added lines #L21 - L22 were not covered by tests
else:
companies = request.env['res.company'].sudo().search([]) # Si no hay propiedades activas, lista todas las compañías

Check warning on line 24 in pms_helpdesk_mgmt/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/main.py#L24

Added line #L24 was not covered by tests

companies_data = [{"id": company.id, "name": company.name} for company in companies]
return {"companies": companies_data}

Check warning on line 27 in pms_helpdesk_mgmt/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/main.py#L27

Added line #L27 was not covered by tests
@http.route("/get_properties", type="json", auth="user")
def get_properties(self, company_id):
properties = (

Check warning on line 30 in pms_helpdesk_mgmt/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/main.py#L30

Added line #L30 was not covered by tests
Expand Down Expand Up @@ -44,10 +56,9 @@ def create_new_ticket(self, **kw):
company = request.env.company
tag_model = http.request.env["helpdesk.ticket.tag"]
tags = tag_model.with_company(company.id).search([("active", "=", True)])
property_model = http.request.env["pms.property"]
properties = property_model.sudo().search(
[("user_ids", "in", [http.request.env.user.id])]
)
allowed_pms_property_ids = user.get_active_property_ids()
properties = request.env['pms.property'].sudo().browse(allowed_pms_property_ids)
companies = properties.mapped('company_id')

Check warning on line 61 in pms_helpdesk_mgmt/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/main.py#L55-L61

Added lines #L55 - L61 were not covered by tests
all_room_ids = [
room_id for property in properties for room_id in property.room_ids.ids
]
Expand All @@ -56,7 +67,7 @@ def create_new_ticket(self, **kw):
response.qcontext.update(

Check warning on line 67 in pms_helpdesk_mgmt/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/main.py#L65-L67

Added lines #L65 - L67 were not covered by tests
{
"user_belongs_to_group": user_belongs_to_group,
"companies": assigned_companies,
"companies": companies,
"properties": properties,
"rooms": rooms,
"tags": tags,
Expand All @@ -82,3 +93,5 @@ def _prepare_submit_ticket_vals(self, **kw):
}
)
return vals

Check warning on line 95 in pms_helpdesk_mgmt/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/main.py#L95

Added line #L95 was not covered by tests


89 changes: 89 additions & 0 deletions pms_helpdesk_mgmt/controllers/myaccount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from odoo.http import request
from odoo.addons.helpdesk_mgmt.controllers.myaccount import CustomerPortalHelpdesk
from odoo.osv.expression import AND, OR
from odoo.addons.portal.controllers.portal import CustomerPortal, pager as portal_pager
from collections import OrderedDict
from operator import itemgetter
from odoo import _, http
from odoo.exceptions import AccessError, MissingError
from odoo.tools import groupby as groupbyelem


class CustomCustomerPortalHelpdesk(CustomerPortalHelpdesk):
@http.route(
["/my/tickets", "/my/tickets/page/<int:page>"],
type="http",
auth="user",
website=True,
)
def portal_my_tickets(self, **kw):
response = super(CustomCustomerPortalHelpdesk, self).portal_my_tickets(**kw)
page = response.qcontext.get('page', 1)
HelpdeskTicket = request.env["helpdesk.ticket"].sudo()

Check warning on line 22 in pms_helpdesk_mgmt/controllers/myaccount.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/myaccount.py#L20-L22

Added lines #L20 - L22 were not covered by tests

# Llamamos al método del padre para obtener los valores de la vista
values = super(CustomCustomerPortalHelpdesk, self)._prepare_portal_layout_values()

Check warning on line 25 in pms_helpdesk_mgmt/controllers/myaccount.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/myaccount.py#L25

Added line #L25 was not covered by tests
if not HelpdeskTicket.check_access_rights("read", raise_exception=False):
return request.redirect("/my")

Check warning on line 27 in pms_helpdesk_mgmt/controllers/myaccount.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/myaccount.py#L27

Added line #L27 was not covered by tests

domain = response.qcontext.get('domain', [])
order = response.qcontext.get('order', '')
sortby = response.qcontext.get('sortby', {})
groupby = response.qcontext.get('groupby', '')

Check warning on line 32 in pms_helpdesk_mgmt/controllers/myaccount.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/myaccount.py#L29-L32

Added lines #L29 - L32 were not covered by tests

active_property_ids = request.env.user.get_active_property_ids()

Check warning on line 34 in pms_helpdesk_mgmt/controllers/myaccount.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/myaccount.py#L34

Added line #L34 was not covered by tests
if active_property_ids:
domain.append(("pms_property_id", "in", active_property_ids))

Check warning on line 36 in pms_helpdesk_mgmt/controllers/myaccount.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/myaccount.py#L36

Added line #L36 was not covered by tests

HelpdeskTicket = request.env["helpdesk.ticket"].sudo()
ticket_count = HelpdeskTicket.search_count(domain)
pager = portal_pager(

Check warning on line 40 in pms_helpdesk_mgmt/controllers/myaccount.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/myaccount.py#L38-L40

Added lines #L38 - L40 were not covered by tests
url="/my/tickets",
url_args={
"date_begin": response.qcontext.get('date_begin', {}),
"date_end": response.qcontext.get('date_end', {}),
"sortby": sortby,
"filterby": response.qcontext.get('filterby', ''),
"groupby": groupby,
"search": response.qcontext.get('search', ''),
"search_in": response.qcontext.get('search_in', ''),
},
total=ticket_count,
page=page,
step=self._items_per_page,
)
tickets = HelpdeskTicket.search(

Check warning on line 55 in pms_helpdesk_mgmt/controllers/myaccount.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/myaccount.py#L55

Added line #L55 was not covered by tests
domain,
order=order,
limit=self._items_per_page,
offset=pager["offset"],
)
request.session["my_tickets_history"] = tickets.ids[:100]

Check warning on line 61 in pms_helpdesk_mgmt/controllers/myaccount.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/myaccount.py#L61

Added line #L61 was not covered by tests

group = response.qcontext.get('group', None)

Check warning on line 63 in pms_helpdesk_mgmt/controllers/myaccount.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/myaccount.py#L63

Added line #L63 was not covered by tests
grouped_tickets = [tickets] if not group else [
request.env["helpdesk.ticket"].concat(*g)
for k, g in groupbyelem(tickets, itemgetter(group))
]
values.update(

Check warning on line 68 in pms_helpdesk_mgmt/controllers/myaccount.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/myaccount.py#L68

Added line #L68 was not covered by tests
{
"date": response.qcontext.get('date_begin', {}),
"date_end": response.qcontext.get('date_end', {}),
"grouped_tickets": grouped_tickets,
"page_name": "ticket",
"default_url": "/my/tickets",
"pager": pager,
"searchbar_sortings": response.qcontext.get('searchbar_sortings', []),
"searchbar_groupby": response.qcontext.get('searchbar_groupby', []),
"searchbar_inputs": response.qcontext.get('searchbar_inputs', []),
"search_in": response.qcontext.get('search_in', ''),
"search": response.qcontext.get('search', ''),
"sortby": sortby,
"groupby": groupby,
"searchbar_filters": OrderedDict(sorted(response.qcontext.get('searchbar_filters', {}).items())),
"filterby": response.qcontext.get('filterby', ''),
}
)
return request.render("helpdesk_mgmt.portal_my_tickets", values)

Check warning on line 87 in pms_helpdesk_mgmt/controllers/myaccount.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/controllers/myaccount.py#L87

Added line #L87 was not covered by tests


47 changes: 28 additions & 19 deletions pms_helpdesk_mgmt/models/helpdesk_ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class PmsHelpdeskTicket(models.Model):
_inherit = "helpdesk.ticket"

pms_property_id = fields.Many2one(
comodel_name="pms.property",
string="Property",
domain="[('company_id', '=', company_id)]",
help="The property associated with this ticket",
'pms.property',
string="PMS Property",
help="The property linked to this ticket.",
required=True,
)
room_id = fields.Many2one(
comodel_name="pms.room",
Expand All @@ -24,30 +24,33 @@ class PmsHelpdeskTicket(models.Model):
widget="many2one_tags",
)

@api.model
def _get_default_pms_property(self):
user = self.env.user
active_property_ids = user.get_active_property_ids()

Check warning on line 30 in pms_helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/models/helpdesk_ticket.py#L29-L30

Added lines #L29 - L30 were not covered by tests
if active_property_ids:
return active_property_ids[0]
return None

Check warning on line 33 in pms_helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/models/helpdesk_ticket.py#L32-L33

Added lines #L32 - L33 were not covered by tests

@api.onchange("company_id")
def _onchange_company_id(self):
if self.company_id:
allowed_pms_property_ids = self.env.user.get_active_property_ids()

Check warning on line 38 in pms_helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/models/helpdesk_ticket.py#L38

Added line #L38 was not covered by tests
if self.pms_property_id and self.pms_property_id.company_id != self.company_id:
self.pms_property_id = False

Check warning on line 40 in pms_helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/models/helpdesk_ticket.py#L40

Added line #L40 was not covered by tests

return {

Check warning on line 42 in pms_helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/models/helpdesk_ticket.py#L42

Added line #L42 was not covered by tests
"domain": {"pms_property_id": [("id", "in", allowed_pms_property_ids)]}
"domain": {
"pms_property_id": [
("id", "in", allowed_pms_property_ids),
("company_id", "=", self.company_id.id),
]
}
}
else:
self.pms_property_id = False # Reinicia la propiedad si no hay compañía seleccionada
return {"domain": {"pms_property_id": []}}

Check warning on line 52 in pms_helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/models/helpdesk_ticket.py#L51-L52

Added lines #L51 - L52 were not covered by tests

@api.constrains("company_id", "pms_property_id")
def _check_property_company_consistency(self):
for record in self:
if (
record.pms_property_id
and record.pms_property_id.company_id != record.company_id
):
raise UserError(
_(
"The selected property does not belong to the selected company. "
"Please select a property that belongs to the correct company."
)
)

@api.onchange("pms_property_id")
def _onchange_property_id(self):
if self.pms_property_id:
Expand All @@ -60,3 +63,9 @@ def _onchange_property_id(self):
return {"domain": {"room_id": [("id", "in", room_ids)]}}

Check warning on line 63 in pms_helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/models/helpdesk_ticket.py#L62-L63

Added lines #L62 - L63 were not covered by tests
else:
return {"domain": {"room_id": []}}

Check warning on line 65 in pms_helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

pms_helpdesk_mgmt/models/helpdesk_ticket.py#L65

Added line #L65 was not covered by tests


def write(self, vals):
if 'partner_id' not in vals or not vals.get('partner_id'):
vals['partner_id'] = self.env.uid
return super(PmsHelpdeskTicket, self).write(vals)
25 changes: 25 additions & 0 deletions pms_helpdesk_mgmt/securitty/helpdesk_security_pms.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<data noupdate="0">
<record id="helpdesk_ticket_property_rule" model="ir.rule">
<field name="name">Helpdesk Tickets by Property</field>
<field ref="model_helpdesk_ticket" name="model_id" />
<field name="domain_force">[
'|',
('pms_property_id', '=', False),
('pms_property_id', 'in', user.get_active_property_ids())
]</field>
<field name="groups" eval="[(4, ref('base.group_user'))]" />
</record>
<record id="helpdesk_ticket_property_rule_portal" model="ir.rule">
<field name="name">Helpdesk Tickets by Property</field>
<field ref="model_helpdesk_ticket" name="model_id" />
<field name="domain_force">[
'|',
('pms_property_id', '=', False),
('pms_property_id', 'in', user.get_active_property_ids())
]</field>
<field name="groups" eval="[(4, ref('base.group_portal'))]" />
</record>
</data>
</odoo>
8 changes: 0 additions & 8 deletions pms_helpdesk_mgmt/static/src/js/hotel_on_company.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@ odoo.define("pms_helpdesk_mgmt.hotel_on_company", function (require) {
var $propertySelect = this.$('select[name="pms_property_id"]');

if (companyId) {
// Hacer una llamada AJAX para obtener las propiedades de la compañía seleccionada
this._rpc({
route: "/get_properties",
params: {
company_id: companyId,
},
}).then(function (result) {
// Limpiar el select de propiedades
$propertySelect.empty();
$propertySelect.append(
'<option value="">Select a Property</option>'
);

// Agregar las propiedades obtenidas al select
_.each(result.properties, function (property) {
$propertySelect.append(
'<option value="' +
Expand All @@ -42,7 +39,6 @@ odoo.define("pms_helpdesk_mgmt.hotel_on_company", function (require) {
});
});
} else {
// Limpiar el select de propiedades si no hay compañía seleccionada
$propertySelect.empty();
$propertySelect.append('<option value="">Select a Property</option>');
}
Expand All @@ -54,26 +50,22 @@ odoo.define("pms_helpdesk_mgmt.hotel_on_company", function (require) {
var $roomSelect = this.$('select[name="room_id"]');

if (propertyId) {
// Hacer una llamada AJAX para obtener las habitaciones de la propiedad seleccionada
this._rpc({
route: "/get_rooms",
params: {
property_id: propertyId,
},
}).then(function (result) {
// Limpiar el select de habitaciones
$roomSelect.empty();
$roomSelect.append('<option value="">Select a Room</option>');

// Agregar las habitaciones obtenidas al select
_.each(result.rooms, function (room) {
$roomSelect.append(
'<option value="' + room.id + '">' + room.name + "</option>"
);
});
});
} else {
// Limpiar el select de habitaciones si no hay propiedad seleccionada
$roomSelect.empty();
$roomSelect.append('<option value="">Select a Room</option>');
}
Expand Down
Loading

0 comments on commit 2184f6d

Please sign in to comment.