Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MIG] website_portal_contact: Migration to 12.0 #701

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions website_portal_contact/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
.. image:: https://img.shields.io/badge/licence-LGPL--3-blue.svg
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3

=================================
Contact Manager In Website Portal
=================================

This module extends the functionality of the website portal to allow your
users to manage their contacts.

Usage
=====

To use this module, you need to:

#. Log in as a portal user.
#. Go to `your portal home </my/home>`_.
#. Go to `your contacts manager </my/contacts>`_.
#. Read, create, edit or delete (disable) any contacts you want!

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/186/9.0

Known issues / Roadmap
======================

* This uses the backported version of the website portal from v10. When
migrating to v10, we should adapt to any changes there.
* Remove workarounds for https://github.com/odoo/odoo/issues/12961 in JS tour
when fixed.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/website/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
------------

* Jairo Llopis <[email protected]>

Maintainer
----------

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

To contribute to this module, please visit https://odoo-community.org.
4 changes: 4 additions & 0 deletions website_portal_contact/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2016 Jairo Llopis <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from . import controllers
25 changes: 25 additions & 0 deletions website_portal_contact/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2016 Jairo Llopis <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
{
"name": "Contact Manager In Website Portal",
"summary": "Allows logged in portal users to manage their contacts",
"version": "12.0.1.0.0",
"category": "Portal",
"website": "https://tecnativa.com/",
"author": "Tecnativa, Odoo Community Association (OCA)",
"license": "LGPL-3",
"application": False,
"installable": True,
"depends": [
"portal",
"website"
],
"data": [
"security/ir.model.access.csv",
"security/ir.rule.csv",
"views/assets.xml",
"views/contact_form.xml",
"views/contact_tree.xml",
"views/layout.xml",
],
}
4 changes: 4 additions & 0 deletions website_portal_contact/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2016 Jairo Llopis <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from . import main
207 changes: 207 additions & 0 deletions website_portal_contact/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
# Copyright 2016 Jairo Llopis <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import logging

from odoo import _
from odoo.addons.portal.controllers.portal import CustomerPortal
from odoo.exceptions import ValidationError
from odoo.http import local_redirect, request, route

_logger = logging.getLogger(__name__)


class WebsiteAccount(CustomerPortal):
def _contacts_domain(self, search=""):
"""Get user's contacts domain."""
# domain = request.env.ref(
# "website_portal_contact.rule_edit_own_contacts"
# ).domain_force
# TODO manage permission, permit to show own contacts
domain = []

# To edit yourself you have /my/account
domain += [("id", "!=", request.env.user.partner_id.id)]

# Add search query
for term in search.split():
domain += [
"|",
"|",
("name", "ilike", term),
("mobile", "ilike", term),
("email", "ilike", term),
]

return domain

def _prepare_portal_layout_values(self, contact=None):
values = super(WebsiteAccount, self)._prepare_portal_layout_values()
partner_counts = request.env["res.partner"].search_count(
self._contacts_domain()
)
values['contact_count'] = partner_counts
return values

def _prepare_contacts_values(
self, page=1, date_begin=None, date_end=None, search="", sortby=None
):
"""Prepare the rendering context for the contacts list."""
values = self._prepare_portal_layout_values()
Partner = request.env["res.partner"]
base_url = "/my/contacts"

searchbar_sortings = {
'date': {'label': _('Newest'), 'order': 'create_date desc'},
'name': {'label': _('Name'), 'order': 'name'},
}
if not sortby:
sortby = 'date'
order = searchbar_sortings[sortby]['order']

# Get the required domains
domain = self._contacts_domain(search)
archive_groups = self._get_archive_groups("res.partner", domain)

if date_begin and date_end:
domain += [
("create_date", ">=", date_begin),
("create_date", "<", date_end),
]

# Make pager
pager = request.website.pager(
url=base_url,
url_args={"date_begin": date_begin, "date_end": date_end, "sortby": sortby},
total=Partner.search_count(domain),
page=page,
step=self._items_per_page,
)

# Current records to display
contacts = Partner.search(
domain, order=order, limit=self._items_per_page, offset=pager["offset"]
)
request.session['my_contacts_history'] = contacts.ids[:100]

values.update(
{
"date": date_begin,
"date_end": date_end,
"contacts": contacts,
"page_name": 'contact',
"pager": pager,
"archive_groups": archive_groups,
"default_url": base_url,
"search": search,
'searchbar_sortings': searchbar_sortings,
'sortby': sortby
}
)

return values

def _contacts_fields(self):
"""Fields to display in the form."""
return [
"name",
"phone",
"mobile",
"email",
]

def _contacts_fields_check(self, received):
"""Check received fields match those available."""
disallowed = set(received) - set(self._contacts_fields())
if disallowed:
raise ValidationError(
_("Fields not available: %s") % ", ".join(disallowed)
)

def _contacts_clean_values(self, values, contact=False):
"""Set values to a write-compatible format"""
result = {k: v or False for k, v in values.items()}
result.setdefault("type", "contact")
if not contact or contact.id != request.env.user.commercial_partner_id.id:
result.setdefault(
"parent_id", request.env.user.commercial_partner_id.id
)
return result

@route(
["/my/contacts", "/my/contacts/page/<int:page>"],
type="http",
auth="user",
website=True,
)
def portal_my_contacts(
self, page=1, date_begin=None, date_end=None, sortby=None, search="", **kw
):
"""List all of your contacts."""
values = self._prepare_contacts_values(page, date_begin, date_end, search,
sortby)
return request.render("website_portal_contact.portal_my_contacts", values)

@route("/my/contacts/new", auth="user", website=True)
def portal_my_contacts_new(self):
"""Form to create a contact."""
return self.portal_my_contacts_read(request.env["res.partner"].new())

@route("/my/contacts/create", auth="user", website=True)
def portal_my_contacts_create(self, redirect="/my/contacts/{}", **kwargs):
"""Create a contact."""
self._contacts_fields_check(kwargs.keys())
values = self._contacts_clean_values(kwargs)
_logger.debug("Creating contact with: %s", values)
contact = request.env["res.partner"].create(values)
return local_redirect(redirect.format(contact.id))

def _contact_get_page_view_values(self, contact, access_token, **kwargs):
values = {
"contact": contact,
"fields": self._contacts_fields(),
'page_name': 'contact',
'user': request.env.user
}

return self._get_page_view_values(contact, access_token, values,
'my_contact_history', False, **kwargs)

@route(
["/my/contacts/<model('res.partner'):contact>"],
type="http",
auth="user",
website=True,
)
def portal_my_contacts_read(self, contact, access_token=None, **kw):
"""Read a contact form."""
values = self._contact_get_page_view_values(contact, access_token, **kw)
return request.render(
"website_portal_contact.contacts_followup", values
)

@route(
"/my/contacts/<model('res.partner'):contact>/update",
auth="user",
website=True,
)
def portal_my_contacts_update(
self, contact, redirect="/my/contacts/{}", **kwargs
):
"""Update a contact."""
self._contacts_fields_check(kwargs.keys())
values = self._contacts_clean_values(kwargs, contact=contact)
_logger.debug("Updating %r with: %s", contact, values)
contact.write(values)
return local_redirect(redirect.format(contact.id))

@route(
"/my/contacts/<model('res.partner'):contact>/disable",
auth="user",
website=True,
)
def portal_my_contacts_disable(self, contact, redirect="/my/contacts"):
"""Disable a contact."""
_logger.debug("Disabling %r", contact)
contact.sudo().active = False
return local_redirect(redirect)
Loading