[IMP] partner_profiles_portal: refactoring of user information

Merge the main and public information edition in /my/account portal
page.
This commit is contained in:
Stéphan Sainléger
2023-04-18 18:10:06 +02:00
committed by Stéphan Sainléger
parent 4401cfab79
commit 5c16620fe6
5 changed files with 385 additions and 5 deletions

View File

@@ -3,7 +3,7 @@
{
"name": "partner_profiles_portal",
"version": "12.0.1.0.2",
"version": "12.0.2.0.0",
"author": "Elabore",
"website": "https://elabore.coop",
"maintainer": "Stéphan Sainléger",
@@ -28,6 +28,7 @@
"views/portal_home_template.xml",
"views/portal_my_profiles_template.xml",
"views/portal_partner_profile_template.xml",
"views/portal_my_account.xml",
"views/res_partner_view.xml",
],
# only loaded in demonstration mode

View File

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

View File

@@ -0,0 +1,142 @@
# -*- coding: utf-8 -*-
import base64
from odoo import tools
from odoo.http import request, route
from odoo.addons.portal.controllers.portal import CustomerPortal
class CustomerPortalMyProfile(CustomerPortal):
def _get_mandatory_main_fields(self):
return ["main_name", "main_email"]
def _get_optional_main_fields(self):
return ["main_street", "main_street2", "main_city", "main_country_id", "main_phone", "main_mobile", "main_zip", "main_state_id", "main_website"]
def _get_mandatory_public_fields(self):
return ["public_name"]
def _get_optional_public_fields(self):
return ["public_email", "public_street", "public_street2", "public_city", "public_phone", "public_mobile", "public_zip", "public_website"]
def _get_special_fields(self):
return ["main_logo"]
def _transform_in_partner_fields(self, kw, profile_fields, prefix=""):
'''Transforms kw's values in res_partner fields and values'''
return {key[len(prefix):]: kw[key] for key in profile_fields if key in kw}
def _retrieve_main_values(self, data):
main_fields = self._get_mandatory_main_fields() + self._get_optional_main_fields()
values = self._transform_in_partner_fields(data, main_fields, "main_")
if 'main_logo' in data:
image = data.get('main_logo')
if image:
image = image.read()
image = base64.b64encode(image)
values.update({
'image': image
})
return values
def _retrieve_public_values(self, data):
public_fields = self._get_mandatory_public_fields() + self._get_optional_public_fields()
values = self._transform_in_partner_fields(data, public_fields, "public_")
return values
def _get_page_opening_values(self):
# Just retrieve the values to display for Selection fields
countries = request.env["res.country"].sudo().search([])
states = request.env['res.country.state'].sudo().search([])
values = {
"countries": countries,
'states': states,
}
return values
@route(["/my/account"], type="http", auth="user", website=True)
def account(self, redirect=None, **post):
values = self._prepare_portal_layout_values()
user = request.env.user
partner = user.partner_id
public_partner = partner.public_profile_id
values.update({
'error': {},
'error_message': [],
})
if post and request.httprequest.method == 'POST':
error, error_message = self.details_form_validate(post)
values.update({'error': error, 'error_message': error_message})
values.update(post)
if not error:
# Save main profile values
values = self._retrieve_main_values(post)
partner.sudo().write(values)
# Save public profile values
public_values = self._retrieve_public_values(post)
if len(public_values) > 0:
public_partner.sudo().write(public_values)
# Email change generates a change of user's login
if post.get("main_email", user.login) != user.login:
user.login = post["main_email"]
return request.redirect("/web/session/logout")
if redirect:
return request.redirect(redirect)
return request.redirect('/my/home')
values.update(self._get_page_opening_values())
values.update({
'partner': partner,
'public_partner': public_partner,
'has_check_vat': hasattr(request.env['res.partner'], 'check_vat'),
'redirect': "/my/account?success=True",
'page_name': 'my_details',
})
response = request.render("portal.portal_my_details", values)
response.headers['X-Frame-Options'] = 'DENY'
return response
def details_form_validate(self, data):
error = dict()
error_message = []
# Validation
for field_name in self._get_mandatory_main_fields() + self._get_mandatory_public_fields():
if not data.get(field_name):
error[field_name] = 'missing'
# email validation
if data.get('main_email') and not tools.single_email_re.match(data.get('main_email')):
error["main_email"] = 'error'
error_message.append(_('Invalid Email! Please enter a valid email address.'))
if data.get('public_email') and not tools.single_email_re.match(data.get('public_email')):
error["public_email"] = 'error'
error_message.append(_('Invalid Public Email! Please enter a valid public email address.'))
# public name uniqueness
if data.get("public_name") and request.env["res.partner"].sudo().search(
[
("name", "=", data.get("public_name")),
("is_public_profile", "=", True),
("contact_id", "!=", request.env.user.partner_id.id),
]
):
error["public_name"] = "error"
error_message.append(
_("This public name is already used, please find an other idea.")
)
# error message for empty required fields
if [err for err in error.values() if err == 'missing']:
error_message.append('Some required fields are empty.')
unknown = [k for k in data if k not in self._get_mandatory_main_fields() + self._get_optional_main_fields() + self._get_mandatory_public_fields() + self._get_optional_public_fields() + self._get_special_fields()]
if unknown:
error['common'] = 'Unknown field'
error_message.append("Unknown field '%s'" % ','.join(unknown))
return error, error_message

View File

@@ -1,15 +1,17 @@
<?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">
<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 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
My information
</button>
</a>
<a t-attf-href="/my/profiles">

View File

@@ -0,0 +1,234 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="portal_my_details_profiles" inherit_id="portal.portal_my_details"
name="User profiles details">
<xpath expr="//form" position="replace">
<form action="/my/account" method="post" enctype="multipart/form-data">
<input type="hidden" name="csrf_token" t-att-value="request.csrf_token()" />
<div class="row o_portal_details">
<div class="col-lg-12">
<div id="messages" class="col-lg-12">
<div t-if="success" class="alert alert-success">
<i class="fa fa-fw fa-check-circle" /> Data saved! </div>
<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 class="row" id="name">
<div
t-attf-class="form-group #{error.get('main_name') and 'o_has_error' or ''} col-xl-12">
<label class="col-form-label" for="main_name">Your Name</label>
<input type="text" name="main_name" required="True"
t-attf-class="form-control #{error.get('main_name') and 'is-invalid' or ''}"
t-att-value="main_name or partner.name" />
</div>
</div>
<div id="logo">
<label class="col-form-label">Image</label>
<div class="row">
<div t-attf-class="col-xl-2">
<i>Current logo/picture:</i>
<div name="image" t-field="partner.image"
t-options="{&quot;widget&quot;: &quot;image&quot;, &quot;preview_image&quot;: &quot;image_512&quot;, &quot;class&quot;: &quot;d-block mx-auto mb16&quot;}" />
</div>
<div class="form-group form-field form-field-binary"
data-model-field="false" data-optional="true"
t-attf-class="col-xl-2">
<i>New logo/picture:</i>
<i>
<input type="file" name="main_logo" multiple="false"
data-show-upload="true" data-show-caption="true"
accept="image/*" />
</i>
</div>
</div>
</div>
<div id="contact">
<br />
<h3>
Contact information
</h3>
<div class="row" id="email">
<div
t-attf-class="form-group #{error.get('main_email') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="main_email">Email</label>
<small>- Modification causes log out. Sign in just after!</small>
<input type="email" name="main_email" required="True"
t-attf-class="form-control #{error.get('main_email') and 'is-invalid' or ''}"
t-att-value="main_email or partner.email" />
</div>
</div>
<div class="row" id="adress">
<div
t-attf-class="form-group #{error.get('main_street') and 'o_has_error' or ''} col-xl-12">
<label class="col-form-label" for="main_street">Street</label>
<input type="text" name="main_street"
t-attf-class="form-control #{error.get('main_street') and 'is-invalid' or ''}"
t-att-value="main_street or partner.street" />
<input type="text" name="main_street2"
t-attf-class="form-control #{error.get('main_street2') and 'is-invalid' or ''}"
t-att-value="main_street2 or partner.street2" />
</div>
<div
t-attf-class="form-group #{error.get('main_zip') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="main_zip">Zip / Postal Code</label>
<input type="text" name="main_zip"
t-attf-class="form-control #{error.get('zip') and 'is-invalid' or ''}"
t-att-value="main_zip or partner.zip" />
</div>
<div
t-attf-class="form-group #{error.get('main_city') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="main_city">City</label>
<input type="text" name="main_city"
t-attf-class="form-control #{error.get('main_city') and 'is-invalid' or ''}"
t-att-value="main_city or partner.city" />
</div>
<div
t-attf-class="form-group #{error.get('main_country_id') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="main_country_id">Country</label>
<select name="main_country_id" required="True"
t-attf-class="form-control #{error.get('main_country_id') and 'is-invalid' or ''}">
<option value="">Country...</option>
<t t-foreach="countries or []" t-as="country">
<option t-att-value="country.id"
t-att-selected="country.id == int(main_country_id) if main_country_id else country.id == partner.country_id.id">
<t t-esc="country.name" />
</option>
</t>
</select>
</div>
<div
t-attf-class="form-group #{error.get('main_state_id') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="main_state_id">State /
Province</label>
<select name="main_state_id"
t-attf-class="form-control #{error.get('state_id') and 'is-invalid' or ''}">
<option value="">select...</option>
<t t-foreach="states or []" t-as="state">
<option t-att-value="state.id" style="display:none;"
t-att-data-country_id="state.country_id.id"
t-att-selected="state.id == partner.state_id.id">
<t t-esc="state.name" />
</option>
</t>
</select>
</div>
</div>
<div class="row" id="other_contact_data">
<div
t-attf-class="form-group #{error.get('main_phone') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="main_phone">Phone</label>
<input type="tel" name="main_phone"
t-attf-class="form-control #{error.get('main_phone') and 'is-invalid' or ''}"
t-att-value="main_phone or partner.phone" />
</div>
<div
t-attf-class="form-group #{error.get('main_mobile') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="main_mobile">Mobile</label>
<input type="tel" name="main_mobile"
t-attf-class="form-control #{error.get('main_mobile') and 'is-invalid' or ''}"
t-att-value="main_mobile or partner.mobile" />
</div>
<div
t-attf-class="form-group #{error.get('main_website') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="main_website">Website</label>
<input type="text" name="main_website"
t-attf-class="form-control #{error.get('main_website') and 'is-invalid' or ''}"
t-att-value="main_website or partner.website" />
</div>
</div>
</div>
<br />
<div class="s_card card bg-white w-100" id="public">
<h3 class="card-header">
Public information
</h3>
<div class="card-body">
<p> The following information are public information that might be
used in tierce applications (annuary for instance).<br /> You
can customize them (and be anonymous for instance) to publicly
show whatever you need or want. </p>
<div class="row">
<div
t-attf-class="form-group #{error.get('public_name') and 'o_has_error' or ''} col-xl-12">
<label class="col-form-label" for="public_name">Name /
Nickname</label>
<input type="text" name="public_name" required="True"
t-attf-class="form-control #{error.get('public_name') and 'is-invalid' or ''}"
t-att-value="public_name or public_partner.name" />
</div>
<div
t-attf-class="form-group #{error.get('public_street') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="public_street">Street</label>
<input type="text" name="public_street"
t-attf-class="form-control #{error.get('public_street') and 'is-invalid' or ''}"
t-att-value="public_street or public_partner.street" />
<input type="text" name="public_street2"
t-attf-class="form-control #{error.get('public_street2') and 'is-invalid' or ''}"
t-att-value="public_street2 or public_partner.street2" />
</div>
<div
t-attf-class="form-group #{error.get('public_zip') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="public_zip">Zip / Postal
Code</label>
<input type="text" name="public_zip"
t-attf-class="form-control #{error.get('public_zip') and 'is-invalid' or ''}"
t-att-value="public_zip or public_partner.zip" />
</div>
<div
t-attf-class="form-group #{error.get('public_city') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="public_city">City</label>
<input type="text" name="public_city"
t-attf-class="form-control #{error.get('public_city') and 'is-invalid' or ''}"
t-att-value="public_city or public_partner.city" />
</div>
</div>
<div class="row">
<div
t-attf-class="form-group #{error.get('public_phone') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="public_phone">Phone</label>
<input type="tel" name="public_phone"
t-attf-class="form-control #{error.get('public_phone') and 'is-invalid' or ''}"
t-att-value="public_phone or public_partner.phone" />
</div>
<div
t-attf-class="form-group #{error.get('public_mobile') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="public_mobile">Mobile</label>
<input type="tel" name="public_mobile"
t-attf-class="form-control #{error.get('public_mobile') and 'is-invalid' or ''}"
t-att-value="public_mobile or public_partner.mobile" />
</div>
<div
t-attf-class="form-group #{error.get('public_email') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="public_email">Email</label>
<input type="email" name="public_email"
t-attf-class="form-control #{error.get('public_email') and 'is-invalid' or ''}"
t-att-value="public_email or public_partner.email" />
</div>
<div
t-attf-class="form-group #{error.get('public_website') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="public_website">Website</label>
<input type="text" name="public_website"
t-attf-class="form-control #{error.get('public_website') and 'is-invalid' or ''}"
t-att-value="public_website or public_partner.website" />
</div>
</div>
</div>
</div>
<input type="hidden" name="redirect" t-att-value="redirect" />
<div style="text-align:right;">
<button type="submit"
class="btn btn-primary ">Save
</button>
</div>
</div>
</div>
</form>
</xpath>
</template>
</odoo>