[ADD] partner_profiles_portal: create add-on

This commit is contained in:
Stéphan Sainléger
2022-11-10 00:30:28 +01:00
parent c651ac4355
commit a4dc58a2b1
15 changed files with 724 additions and 0 deletions

2
partner_profiles_portal/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
*.*~
*pyc

View File

@@ -0,0 +1,43 @@
===============
partner_profiles_portal
===============
Provide portal pages and forms to manage partner's profiles from portal home space.
Installation
============
Use Odoo normal module installation procedure to install
``partner_profiles_portal``.
Known issues / Roadmap
======================
None yet.
Bug Tracker
===========
Bugs are tracked on `our issues website <https://github.com/elabore-coop/partner-tools/issues>`_. In case of
trouble, please check there if your issue has already been
reported. If you spotted it first, help us smashing it by providing a
detailed and welcomed feedback.
Credits
=======
Contributors
------------
* Stéphan Sainléger
Funders
-------
The development of this module has been financially supported by:
* Elabore (https://elabore.coop)
Maintainer
----------
This module is maintained by Elabore.

View File

@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
from . import models
from . import controllers

View File

@@ -0,0 +1,41 @@
# Copyright 2022 Stéphan Sainléger (Elabore)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "partner_profiles_portal",
"version": "12.0.1.0.0",
"author": "Elabore",
"website": "https://elabore.coop",
"maintainer": "Stéphan Sainléger",
"license": "AGPL-3",
"category": "Tools",
"summary": "Provide portal pages and forms to manage partner's profiles from portal home space.",
# any module necessary for this one to work correctly
"depends": [
"base",
"partner_profiles",
"portal",
"website",
],
"qweb": [],
"external_dependencies": {
"python": [],
},
# always loaded
"data": [
"security/members_security.xml",
"views/portal_home_template.xml",
"views/portal_my_profiles_template.xml",
"views/portal_partner_profile_template.xml",
"views/res_partner_view.xml",
],
# only loaded in demonstration mode
"demo": [],
"js": [],
"css": [],
"installable": True,
# Install this module automatically if all dependency have been previously
# and independently installed. Used for synergetic or glue modules.
"auto_install": False,
"application": False,
}

View File

@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
from . import portal_my_profiles
from . import portal_partner_profile

View File

@@ -0,0 +1,91 @@
# 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 CustomerPortalMyProfiles(CustomerPortal):
def _get_domain_my_profiles(self, user):
if user.partner_id.other_contact_ids:
main_profile_ids = user.partner_id.other_contact_ids.filtered(
"edit_structure_main_profile"
).mapped("parent_id")
public_profile_ids = user.partner_id.other_contact_ids.filtered(
"edit_structure_public_profile"
).mapped("parent_id.public_profile_id")
return [
"|",
"|",
("contact_id", "=", user.partner_id.id),
("id", "in", main_profile_ids.ids),
("id", "in", public_profile_ids.ids),
]
else:
return [("contact_id", "=", user.partner_id.id)]
def _prepare_portal_layout_values(self):
values = super(CustomerPortalMyProfiles, self)._prepare_portal_layout_values()
values["profile_count"] = request.env["res.partner"].search_count(
self._get_domain_my_profiles(request.env.user)
)
return values
@http.route(
["/my/profiles", "/my/profiles/page/<int:page>"],
type="http",
auth="user",
website=True,
)
def portal_my_profiles(
self, page=1, date_begin=None, date_end=None, sortby=None, **kw
):
values = self._prepare_portal_layout_values()
profile = request.env["res.partner"]
domain = self._get_domain_my_profiles(request.env.user)
searchbar_sortings = {
"name": {"label": _("Name"), "order": "name"},
"partner_profile": {"label": _("Profile Type"), "order": "partner_profile"},
"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)
# profiles count
profile_count = profile.search_count(domain)
# pager
pager = portal_pager(
url="/my/profiles",
url_args={"sortby": sortby},
total=profile_count,
page=page,
step=self._items_per_page,
)
# content according to pager and archive selected
profiles = profile.search(
domain,
order=order,
limit=self._items_per_page,
offset=pager["offset"],
)
request.session["my_profiles_history"] = profiles.ids[:100]
values.update(
{
"profiles": profiles,
"page_name": "profile",
"archive_groups": archive_groups,
"default_url": "/my/profiles",
"pager": pager,
"searchbar_sortings": searchbar_sortings,
"sortby": sortby,
}
)
return request.render("partner_profiles_portal.portal_my_profiles", values)

View File

@@ -0,0 +1,130 @@
# Copyright 2020 Lokavaluto ()
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
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 CustomerPortalPartnerProfile(CustomerPortal):
def _profile_get_page_view_values(self, profile, access_token, **kwargs):
values = {
"page_name": "profile",
"profile": profile,
}
return self._get_page_view_values(
profile, access_token, values, "my_profiles_history", False, **kwargs
)
def _details_profile_form_validate(self, data, profile_id):
error = dict()
error_message = []
# nickname uniqueness
if data.get("nickname") and request.env["res.partner"].sudo().search(
[
("name", "=", data.get("nickname")),
("partner_profile.ref", "=", "partner_profile_public"),
("id", "!=", profile_id),
]
):
error["nickname"] = "error"
error_message.append(
_("This nickname is already used, please find an other idea.")
)
# 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_profile_fields(self):
fields = [
"nickname",
"function",
"phone",
"mobile",
"email",
"website_url",
"street",
"street2",
"city",
"country_id",
"zipcode",
]
return fields
def _get_page_saving_values(self, profile, kw):
profile_fields = self._get_profile_fields()
values = {key: kw[key] for key in profile_fields if key in kw}
values.update(
{
"name": values.pop("nickname", profile.name),
"zip": values.pop("zipcode", ""),
"website": values.pop("website_url", ""),
}
)
return values
def _get_page_opening_values(self):
# Just retrieve the values to display for Selection fields
countries = request.env["res.country"].sudo().search([])
values = {
"countries": countries,
}
return values
@http.route(
["/my/profile/<int:profile_id>", "/my/profile/save"],
type="http",
auth="user",
website=True,
)
def portal_my_profile(
self, profile_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(profile_id, int):
profile_id = int(profile_id)
# Check that the user has the right to see this profile
try:
profile_sudo = self._document_check_access(
"res.partner", profile_id, access_token
)
except (AccessError, MissingError):
return request.redirect("/my/profiles")
values = self._profile_get_page_view_values(profile_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_profile_form_validate(kw, profile_id)
values.update({"error": error, "error_message": error_message})
values.update(kw)
if not error:
profile = request.env["res.partner"].browse(profile_id)
values = self._get_page_saving_values(profile, kw)
profile.sudo().write(values)
if redirect:
return request.redirect(redirect)
return request.redirect("/my/profiles")
# This is just the form page opening. We send all the data needed for the form fields
values.update(self._get_page_opening_values())
values.update(
{
"profile_id": profile_id, # Sent in order to retrieve it at submit time
"redirect": redirect
}
)
return request.render("partner_profiles_portal.portal_my_profile", values)

View File

@@ -0,0 +1 @@
This directory should contain the *.po for Odoo translation.

View File

@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
from . import res_partner

View File

@@ -0,0 +1,50 @@
# Copyright 2022 Elabore (https://elabore.coop)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
from odoo import _, api, fields, models
_logger = logging.getLogger(__name__)
class res_partner(models.Model):
_inherit = "res.partner"
edit_structure_main_profile = fields.Boolean(
string=_("Manage structure's main profile")
)
edit_structure_public_profile = fields.Boolean(
string=_("Manage structure's public profile")
)
can_edit_main_profile_ids = fields.Many2many(
"res.partner",
relation="res_partner_main_profile_rel",
column1="partner_id",
column2="profile_id",
store=True,
compute="_compute_can_edit",
string="Can edit main profile",
)
can_edit_public_profile_ids = fields.Many2many(
"res.partner",
relation="res_partner_public_profile_rel",
column1="partner_id",
column2="profile_id",
store=True,
compute="_compute_can_edit",
string="Can edit public profile",
)
@api.depends(
"other_contact_ids",
"other_contact_ids.edit_structure_main_profile",
"other_contact_ids.edit_structure_public_profile",
)
def _compute_can_edit(self):
for partner in self:
partner.can_edit_main_profile_ids = partner.child_ids.filtered(
"edit_structure_main_profile"
).mapped("contact_id")
partner.can_edit_public_profile_ids = partner.child_ids.filtered(
"edit_structure_public_profile"
).mapped("contact_id")

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record model="ir.rule" id="res_partner_portal_members_rule">
<field name="name">res_partner: portal: read/write access on my profiles</field>
<field name="model_id" ref="base.model_res_partner" />
<field name="domain_force">['|','|',('contact_id', '=', user.partner_id.id),
('can_edit_main_profile_ids', 'in', [user.partner_id.id]),
('can_edit_public_profile_ids', 'in', [user.partner_id.id])]</field>
<field name="groups" eval="[(4, ref('base.group_portal'))]" />
<field name="perm_create" eval="False" />
<field name="perm_unlink" eval="False" />
</record>
</odoo>

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="portal_my_home_profile_menu" name="Portal My Home: Profile Menu" inherit_id="portal.portal_layout" priority="40">
<xpath expr="//div[hasclass('o_portal_my_details')]" position="replace">
<div class="o_portal_my_details">
<h4>Your Details </h4>
<hr class="mt-1 mb-0" />
<div class="mb8" t-field="user_id.partner_id" t-options="{&quot;widget&quot;: &quot;contact&quot;, &quot;fields&quot;: [&quot;email&quot;, &quot;phone&quot;, &quot;address&quot;, &quot;name&quot;]}" />
<div name="profiles_management">
<a t-attf-href="/my/account">
<button class="btn btn-primary mb8">
Modify my account
</button>
</a>
<a t-attf-href="/my/profiles">
<button class="btn btn-primary mb8">
Consult my profiles
</button>
</a>
</div>
</div>
</xpath>
</template>
</odoo>

View File

@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="portal_my_profiles" name="My Profiles">
<t t-call="portal.portal_layout">
<t t-set="breadcrumbs_searchbar" t-value="True" />
<t t-call="portal.portal_searchbar">
<t t-set="title">Profiles</t>
</t>
<div class="oe_structure" id="oe_structure_portal_my_profiles_1" />
<t t-if="not profiles">
<div class="alert alert-warning mt8" role="alert">
There are no profiles.
</div>
</t>
<h3>
<br />
My profiles
</h3>
<t t-if="profiles" t-call="portal.portal_table">
<thead>
<tr class="active">
<th>
<span class='d-none d-md-inline'>Profile name</span>
</th>
<th class="text-right">Profile type</th>
<th class="text-right">Position</th>
<th class="text-right">Company</th>
</tr>
</thead>
<tbody>
<tr t-foreach="profiles" t-as="profile">
<t t-if="not profile.is_company">
<td>
<a t-attf-href="/my/profile/#{profile.id}?{{ keep_query() }}">
<span t-field="profile.name" />
</a>
</td>
<td class="text-right">
<span t-field="profile.partner_profile" />
</td>
<td class="text-right">
<span t-field="profile.function" />
</td>
<td class="text-right">
<span t-field="profile.parent_id" />
</td>
</t>
</tr>
</tbody>
</t>
<p style="font-style:italic; font-size:smaller">
<b>Public profile :</b>
profiles that might be available to tierce applications (annuary for instance).
<br />
<b>Position profiles :</b>
profiles that indicate your belonging to an organization, and the role you have in.
</p>
<h3>
<br />
My organizations' profiles
</h3>
<t t-if="profiles" t-call="portal.portal_table">
<thead>
<tr class="active">
<th>
<span class='d-none d-md-inline'>Profile name</span>
</th>
<th class="text-right">Profile type</th>
</tr>
</thead>
<tbody>
<tr t-foreach="profiles" t-as="profile">
<t t-if="profile.is_company">
<td>
<a t-attf-href="/my/profile/#{profile.id}?{{ keep_query() }}">
<span t-field="profile.name" />
</a>
</td>
<td class="text-right">
<span t-field="profile.partner_profile" />
</td>
</t>
</tr>
</tbody>
</t>
<p style="font-style:italic; font-size:smaller">
<b>Main profiles :</b>
internal and private profiles, used for membership management and internal communication.
<br />
<b>Public profiles :</b>
profiles that might be available to tierce applications (annuary for instance).
</p>
<div class="oe_structure" id="oe_structure_portal_my_profiles_2" />
</t>
</template>
</odoo>

View File

@@ -0,0 +1,189 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="portal_my_profile" name="My Profile">
<t t-call="portal.portal_layout">
<t t-set="o_portal_fullwidth_alert" groups="profile.group_profile_user">
<t t-call="portal.portal_back_in_edit_mode">
<t t-set="backend_url" t-value="'/web#return_label=Website&amp;model=res.partner&amp;id=%s&amp;view_type=form' % (profile.id)" />
</t>
</t>
<t t-call="portal.portal_record_layout">
<t t-set="card_header">
<h5 class="mb-0">
<strong>
<span t-field="profile.name" />
</strong>
<small class="text-muted">
-
<span t-field="profile.partner_profile" />
</small>
</h5>
<div t-if="profile.is_position_profile">
<h5 class="mb-0">
<span t-field="profile.parent_id" />
</h5>
<br />
<ul class="col-12 col-md-6 pb-2" style="list-style-type:none">
<li>
<span t-field="profile.street" />
</li>
<li>
<span t-field="profile.zip" />
</li>
<li>
<span t-field="profile.city" />
</li>
<li>
<span t-field="profile.country_id" />
</li>
</ul>
</div>
</t>
<t t-set="card_body">
<div class="oe_structure" id="oe_structure_portal_my_profile_1" />
<!-- Body for Position partner profiles-->
<div t-if="profile.is_position_profile">
<form action="/my/profile/save" method="post">
<input type="hidden" name="csrf_token" t-att-value="request.csrf_token()" />
<div class="row o_portal_details">
<div class="col-lg-12">
<div class="col-lg-12">
<div t-if="error_message" class="alert alert-danger" role="alert">
<t t-foreach="error_message" t-as="err">
<t t-esc="err" />
<br />
</t>
</div>
</div>
<h3>
<br />
Job position
</h3>
<div class="row">
<div t-attf-class="form-group #{error.get('function') and 'o_has_error' or ''} col-xl-12">
<input type="text" name="function" t-attf-class="form-control #{error.get('function') and 'is-invalid' or ''}" t-att-value="function or profile.function" />
</div>
</div>
<h3>
<br />
Contact information
</h3>
<div class="row">
<div t-attf-class="form-group #{error.get('phone') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="phone">Phone</label>
<input type="tel" name="phone" t-attf-class="form-control #{error.get('phone') and 'is-invalid' or ''}" t-att-value="phone or profile.phone" />
</div>
<div t-attf-class="form-group #{error.get('mobile') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="mobile">Mobile</label>
<input type="tel" name="mobile" t-attf-class="form-control #{error.get('mobile') and 'is-invalid' or ''}" t-att-value="mobile or profile.mobile" />
</div>
<div t-attf-class="form-group #{error.get('email') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="email">Email</label>
<input type="email" name="email" t-attf-class="form-control #{error.get('email') and 'is-invalid' or ''}" t-att-value="email or profile.email" />
</div>
<div t-attf-class="form-group #{error.get('website_url') and 'o_has_error' or ''}col-xl-6">
<label class="col-form-label" for="website_url">Website</label>
<input type="text" name="website_url" t-attf-class="form-control #{error.get('website') and 'is-invalid' or ''}" t-att-value="website_url or profile.website" />
</div>
</div>
<input type="hidden" name="profile_id" t-att-value="profile_id" />
<input type="hidden" name="redirect" t-att-value="redirect" />
<div class="clearfix">
<button type="submit" class="btn btn-primary float-right mb32 ">
Save
<span class="fa fa-long-arrow-right" />
</button>
</div>
</div>
</div>
</form>
</div>
<!-- Body for Public partner profiles-->
<div t-if="profile.is_main_profile or profile.is_public_profile">
<form action="/my/profile/save" method="post">
<input type="hidden" name="csrf_token" t-att-value="request.csrf_token()" />
<div class="row o_portal_details">
<div class="col-lg-12">
<div name="errors" class="col-lg-12">
<div t-if="error_message" class="alert alert-danger" role="alert">
<t t-foreach="error_message" t-as="err">
<t t-esc="err" />
<br />
</t>
</div>
</div>
<div name="nickname" class="row">
<div t-attf-class="form-group #{error.get('nickname') and 'o_has_error' or ''} col-xl-12">
<label class="col-form-label" for="nickname">Name / Nickname</label>
<input type="text" name="nickname" t-attf-class="form-control #{error.get('nickname') and 'is-invalid' or ''}" t-att-value="nickname or profile.name" />
</div>
</div>
<h3>
<br />
Contact information
</h3>
<div name="contact_info_1" class="row">
<div name="street" t-attf-class="form-group #{error.get('street') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="street">Street</label>
<input type="text" name="street" t-attf-class="form-control #{error.get('street') and 'is-invalid' or ''}" t-att-value="street or profile.street" />
</div>
<div name="street2" t-attf-class="form-group #{error.get('street2') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="street2">Street 2</label>
<input type="text" name="street2" t-attf-class="form-control #{error.get('street2') and 'is-invalid' or ''}" t-att-value="street2 or profile.street2" />
</div>
<div name="zip" t-attf-class="form-group #{error.get('zipcode') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="zipcode">Zip / Postal Code</label>
<input type="text" name="zipcode" t-attf-class="form-control #{error.get('zipcode') and 'is-invalid' or ''}" t-att-value="zipcode or profile.zip" />
</div>
<div name="city" t-attf-class="form-group #{error.get('city') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="city">City</label>
<input type="text" name="city" t-attf-class="form-control #{error.get('city') and 'is-invalid' or ''}" t-att-value="city or profile.city" />
</div>
<div name="country" t-attf-class="form-group #{error.get('country_id') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="country_id">Country</label>
<select name="country_id" t-attf-class="form-control">
<option value="">Country...</option>
<t t-foreach="countries or []" t-as="country">
<option t-att-value="country.id" t-att-selected="country.id == int(country_id) if country_id else country.id == profile.country_id.id">
<t t-esc="country.name" />
</option>
</t>
</select>
</div>
</div>
<div name="contact_info_2" class="row">
<div name="phone" t-attf-class="form-group #{error.get('phone') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="phone">Phone</label>
<input type="tel" name="phone" t-attf-class="form-control #{error.get('phone') and 'is-invalid' or ''}" t-att-value="phone or profile.phone" />
</div>
<div name="mobile" t-attf-class="form-group #{error.get('mobile') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="mobile">Mobile</label>
<input type="tel" name="mobile" t-attf-class="form-control #{error.get('mobile') and 'is-invalid' or ''}" t-att-value="mobile or profile.mobile" />
</div>
<div name="email" t-attf-class="form-group #{error.get('email') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="email">Email</label>
<input type="email" name="email" t-attf-class="form-control #{error.get('email') and 'is-invalid' or ''}" t-att-value="email or profile.email" />
</div>
<div name="website" t-attf-class="form-group #{error.get('website_url') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="website_url">Website</label>
<input type="text" name="website_url" t-attf-class="form-control #{error.get('website_url') and 'is-invalid' or ''}" t-att-value="website_url or profile.website" />
</div>
</div>
<input type="hidden" name="profile_id" t-att-value="profile_id" />
<input type="hidden" name="redirect" t-att-value="redirect" />
<div class="clearfix">
<button type="submit" class="btn btn-primary float-right mb32 ">
Save
<span class="fa fa-long-arrow-right" />
</button>
</div>
</div>
</div>
</form>
</div>
<div class="oe_structure" id="oe_structure_portal_my_profile_2" />
</t>
</t>
</t>
</template>
</odoo>

View File

@@ -0,0 +1,32 @@
<?xml version="1.0"?>
<odoo>
<data>
<record id="partner_profiles_form_view" model="ir.ui.view">
<field name="name">Partner Profiles Form View</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form" />
<field name="sequence">99</field>
<field name="arch" type="xml">
<!-- Main display -->
<xpath expr="//group[@name='profile_status']" position="after">
<group name="structure_access_rights" attrs="{'invisible': ['|', ('is_position_profile','=',False), ('parent_id','=',False)]}">
<field name="edit_structure_main_profile" />
<field name="edit_structure_public_profile" />
</group>
</xpath>
<!-- page Contacts & Adresses -->
<xpath expr="//field[@name='child_ids']/form/sheet/group/group/field[@name='comment']" position="before">
<field name="edit_structure_main_profile" attrs="{'invisible': [('is_position_profile','=',False)]}" />
<field name="edit_structure_public_profile" attrs="{'invisible': [('is_position_profile','=',False)]}" />
</xpath>
<!-- page Other Positions -->
<xpath expr="//field[@name='other_contact_ids']/form/sheet/group/group/field[@name='parent_id']" position="after">
<field name="edit_structure_main_profile" attrs="{'invisible': [('is_position_profile','=',False)]}" />
<field name="edit_structure_public_profile" attrs="{'invisible': [('is_position_profile','=',False)]}" />
</xpath>
</field>
</record>
</data>
</odoo>