[IMP] partner_profiles_portal: add positions details in portal
Adds several portal views to consult all the positions of a structure, and their details.
This commit is contained in:
committed by
Stéphan Sainléger
parent
95ddef62c3
commit
43d21befbf
@@ -2,4 +2,6 @@
|
||||
|
||||
from . import portal_my_structures
|
||||
from . import portal_structure_profile
|
||||
from . import portal_my_positions
|
||||
from . import portal_position_profile
|
||||
from . import portal_my_account
|
80
partner_profiles_portal/controllers/portal_my_positions.py
Normal file
80
partner_profiles_portal/controllers/portal_my_positions.py
Normal file
@@ -0,0 +1,80 @@
|
||||
# Copyright 2020 Lokavaluto ()
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
from odoo import http, _
|
||||
from odoo.http import request
|
||||
from odoo.addons.portal.controllers.portal import CustomerPortal, pager as portal_pager
|
||||
|
||||
|
||||
class CustomerPortalMyPositions(CustomerPortal):
|
||||
|
||||
def _get_domain_my_positions(self, user):
|
||||
if user.partner_id.structure_position_ids:
|
||||
return [("id", "in", user.partner_id.structure_position_ids.ids),
|
||||
("is_company", "=", False),
|
||||
("is_position_profile", "=", True),
|
||||
]
|
||||
else:
|
||||
return None
|
||||
|
||||
def _prepare_portal_layout_values(self):
|
||||
values = super(CustomerPortalMyPositions, self)._prepare_portal_layout_values()
|
||||
domain = self._get_domain_my_structures(request.env.user)
|
||||
values["structure_count"] = request.env["res.partner"].search_count(domain) if domain else 0
|
||||
return values
|
||||
|
||||
@http.route(
|
||||
["/my/positions", "/my/positions/page/<int:page>"],
|
||||
type="http",
|
||||
auth="user",
|
||||
website=True,
|
||||
)
|
||||
def portal_my_positions(
|
||||
self, page=1, date_begin=None, date_end=None, sortby=None, **kw
|
||||
):
|
||||
values = self._prepare_portal_layout_values()
|
||||
position = request.env["res.partner"]
|
||||
domain = self._get_domain_my_positions(request.env.user)
|
||||
|
||||
searchbar_sortings = {
|
||||
"name": {"label": _("Name"), "order": "name"},
|
||||
"parent_id": {"label": _("Company"), "order": "parent_id"},
|
||||
}
|
||||
if not sortby:
|
||||
sortby = "name"
|
||||
order = searchbar_sortings[sortby]["order"]
|
||||
|
||||
# archive groups - Default Group By 'create_date'
|
||||
archive_groups = self._get_archive_groups("res.partner", domain)
|
||||
|
||||
# structures count
|
||||
position_count = position.search_count(domain) if domain else 0
|
||||
# pager
|
||||
pager = portal_pager(
|
||||
url="/my/positions",
|
||||
url_args={"sortby": sortby},
|
||||
total=position_count,
|
||||
page=page,
|
||||
step=self._items_per_page,
|
||||
)
|
||||
|
||||
# content according to pager and archive selected
|
||||
positions = position.search(
|
||||
domain,
|
||||
order=order,
|
||||
limit=self._items_per_page,
|
||||
offset=pager["offset"],
|
||||
) if domain else None
|
||||
request.session["my_positions_history"] = positions.ids[:100] if positions else None
|
||||
|
||||
values.update(
|
||||
{
|
||||
"positions": positions,
|
||||
"page_name": "position",
|
||||
"archive_groups": archive_groups,
|
||||
"default_url": "/my/positions",
|
||||
"pager": pager,
|
||||
"searchbar_sortings": searchbar_sortings,
|
||||
"sortby": sortby,
|
||||
}
|
||||
)
|
||||
return request.render("partner_profiles_portal.portal_my_positions", values)
|
120
partner_profiles_portal/controllers/portal_position_profile.py
Normal file
120
partner_profiles_portal/controllers/portal_position_profile.py
Normal file
@@ -0,0 +1,120 @@
|
||||
# Copyright 2020 Lokavaluto ()
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
import base64
|
||||
from odoo import http, tools, _
|
||||
from odoo.exceptions import AccessError, MissingError
|
||||
from odoo.http import request
|
||||
from odoo.addons.portal.controllers.portal import CustomerPortal
|
||||
|
||||
|
||||
class CustomerPortalPositionProfile(CustomerPortal):
|
||||
|
||||
def _position_get_page_view_values(self, position, access_token, **kwargs):
|
||||
values = {
|
||||
"page_name": "position",
|
||||
"position": position,
|
||||
}
|
||||
return self._get_page_view_values(
|
||||
position, access_token, values, "my_positions_history", False, **kwargs
|
||||
)
|
||||
|
||||
def _details_position_form_validate(self, data, position_id):
|
||||
error = dict()
|
||||
error_message = []
|
||||
# email validation
|
||||
if data.get("email") and not tools.single_email_re.match(data.get("email")):
|
||||
error["email"] = "error"
|
||||
error_message.append(
|
||||
_("Invalid Email! Please enter a valid email address.")
|
||||
)
|
||||
return error, error_message
|
||||
|
||||
def _get_position_profile_fields(self):
|
||||
'''Provides all the fields that must fill the structure's position profile of the user.
|
||||
All of them MUST start with "position_".'''
|
||||
fields = [
|
||||
"function",
|
||||
"phone",
|
||||
"email",
|
||||
"edit_structure_profiles",
|
||||
]
|
||||
return fields
|
||||
|
||||
def _get_position_boolean_fields(self):
|
||||
'''Provides the fields for which we must check the presence
|
||||
in form's kw to know the value to save in the partner field.'''
|
||||
fields = ["edit_structure_profiles"]
|
||||
return fields
|
||||
|
||||
def _transform_fields(self, kw, profile_fields):
|
||||
'''Transforms kw's values in res_partner fields and values'''
|
||||
return {key: kw[key] for key in profile_fields if key in kw}
|
||||
|
||||
def _get_page_saving_position_values(self, kw):
|
||||
profile_fields = self._get_position_profile_fields()
|
||||
values = self._transform_fields(kw, profile_fields)
|
||||
# Boolean fields are not returned in "kw" if their value in the form is False.
|
||||
# Then we have to check their presence to determine which value to save in the partner.
|
||||
boolean_fields = self._get_position_boolean_fields()
|
||||
for key in boolean_fields:
|
||||
values.update(
|
||||
{
|
||||
key: kw.get(key, "off") == "on"
|
||||
}
|
||||
)
|
||||
return values
|
||||
|
||||
@http.route(
|
||||
["/my/position/<int:position_id>", "/my/position/save"],
|
||||
type="http",
|
||||
auth="user",
|
||||
website=True,
|
||||
)
|
||||
def portal_my_position(
|
||||
self,position_id=None, access_token=None, redirect=None, **kw
|
||||
):
|
||||
# The following condition is to transform profile_id to an int, as it is sent as a string from the templace "portal_my_profile"
|
||||
# TODO: find a better way to retrieve the profile_id at form submit step
|
||||
if not isinstance(position_id, int):
|
||||
position_id = int(position_id)
|
||||
|
||||
# Check that the user has the right to see this profile
|
||||
try:
|
||||
position_sudo = self._document_check_access(
|
||||
"res.partner", position_id, access_token
|
||||
)
|
||||
except (AccessError, MissingError):
|
||||
return request.redirect("/my/positions")
|
||||
|
||||
position_profile = request.env["res.partner"].browse(position_id)
|
||||
|
||||
values = self._position_get_page_view_values(position_sudo, access_token, **kw)
|
||||
values.update(
|
||||
{
|
||||
"error": {},
|
||||
"error_message": [],
|
||||
}
|
||||
)
|
||||
if kw and request.httprequest.method == "POST":
|
||||
# the user has clicked in the Save button to save new data
|
||||
error, error_message = self._details_position_form_validate(kw, position_id)
|
||||
values.update({"error": error, "error_message": error_message})
|
||||
values.update(kw)
|
||||
if not error:
|
||||
# Update position profile
|
||||
new_values = self._get_page_saving_position_values(kw)
|
||||
position_profile.sudo().write(new_values)
|
||||
# End of updates
|
||||
if redirect:
|
||||
return request.redirect(redirect)
|
||||
return request.redirect("/my/positions")
|
||||
|
||||
# This is just the form page opening. We send all the data needed for the form fields
|
||||
values.update(
|
||||
{
|
||||
"position_id": position_id, # Sent in order to retrieve it at submit time
|
||||
"position": position_profile,
|
||||
"redirect": "/my/position/" + str(position_id) + "?success=True"
|
||||
}
|
||||
)
|
||||
return request.render("partner_profiles_portal.portal_position", values)
|
Reference in New Issue
Block a user