-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from simahawk/add-cms_delete_content
[ADD] cms_delete_content
- Loading branch information
Showing
15 changed files
with
528 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg | ||
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html | ||
:alt: License: AGPL-3 | ||
|
||
|
||
CMS delete content | ||
================== | ||
|
||
Basic features for deleting content via frontend. | ||
|
||
Features | ||
-------- | ||
|
||
- register your own custom delete confirmation view per-model | ||
- use ``cms_status_message`` to show confirmation message for deletion | ||
- generic template for asking delete confirmation | ||
- new fields and parameters on ``website.published.mixin`` to handle | ||
delete links and redirects | ||
|
||
Usage | ||
----- | ||
|
||
Delete button and behavior | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
To add a delete button: | ||
|
||
.. code:: html | ||
|
||
<a class="btn btn-danger cms_delete_confirm" t-att-href="object.cms_delete_confirm_url">Delete</a> | ||
|
||
When you click on a confirmation dialog pops up. | ||
|
||
If you hit ``cancel`` the popup is closed. If you hit submit the item is | ||
deleted and you get redirected to your model's ``cms_after_delete_url``. | ||
By default is ``/``. | ||
|
||
Customization | ||
------------- | ||
|
||
Custom per-model delete messge | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. code:: python | ||
class MyModel(models.Model): | ||
_inherit = "my.model" | ||
@api.multi | ||
def msg_content_delete_confirm(self): | ||
self.ensure_one() | ||
return _('Are you sure you want to delete "%s"?.') % self.name | ||
Custom "after delete URL" | ||
~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
When you are viewing a content and you delete it you want to be | ||
redirected to some other place. | ||
|
||
By default you get redirected to the root of the website. | ||
|
||
To change this behavior just override the attribute in your model | ||
declaration: | ||
|
||
.. code:: python | ||
class MyModel(models.Model): | ||
_inherit = "my.model" | ||
cms_after_delete_url = '/foo' | ||
Note: if you want to customize it on demand for particular pages, or you | ||
are deleting an item from another page (like a management page) you can | ||
pass ``?redirect=`` in the url, like: | ||
|
||
.. code:: html | ||
|
||
<a class="btn btn-danger cms_delete_confirm" t-attf-href="#{object.cms_delete_confirm_url}?redirect=">Delete</a> | ||
|
||
Custom global delete confirm message appeareance | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. code:: xml | ||
<template id="delete_confirm" inherit_id="cms_delete_content.delete_confirm"> | ||
<xpath expr="//h4[@id='delete_confirm']" position="replace"> | ||
<h1 t-esc="main_object.msg_content_delete_confirm()">I want it bigger!</h1> | ||
</xpath> | ||
</template> | ||
Bug Tracker | ||
=========== | ||
|
||
Bugs are tracked on `GitHub Issues <https://github.com/OCA/website-cms/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 | ||
------------ | ||
|
||
- Simone Orsi simone.orsi@camptocamp.com | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import controllers | ||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2017 Camptocamp | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
{ | ||
'name': 'CMS delete content', | ||
'summary': """ | ||
Basic features for handling content deletion via frontend.""", | ||
'version': '9.0.1.0.0', | ||
'license': 'AGPL-3', | ||
'author': 'Camptocamp,Odoo Community Association (OCA)', | ||
'depends': [ | ||
'website', | ||
'cms_status_message', | ||
], | ||
'data': [ | ||
'templates/assets.xml', | ||
'templates/delete_confirm.xml', | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2017 Camptocamp | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
||
from openerp import http, _ | ||
from openerp.http import request | ||
import json | ||
import werkzeug | ||
|
||
|
||
class DeleteMixin(object): | ||
|
||
delete_confirm_template = 'cms_delete_content.delete_confirm' | ||
|
||
def get_main_object(self, model, model_id): | ||
main_object = request.env[model].browse(model_id) | ||
if main_object.exists(): | ||
return main_object | ||
raise werkzeug.exceptions.NotFound( | ||
_('model: %s id: %s') % (model, model_id) | ||
) | ||
|
||
def handle_delete_confirm(self, model, model_id, **kwargs): | ||
"""Render modal for delete confirmation. Called via JS. | ||
:param model: odoo model name | ||
:param model_id: odoo model id | ||
:param kwargs: extra request args, ``redirect`` only used ATM | ||
:return: rendered modal template | ||
""" | ||
main_object = self.get_main_object(model, model_id) | ||
return request.env.ref(self.delete_confirm_template).render({ | ||
'main_object': main_object, | ||
'delete_url': main_object.cms_delete_url, | ||
'redirect': kwargs.get( | ||
'redirect', main_object.cms_after_delete_url), | ||
}) | ||
|
||
def handle_delete(self, model, model_id, **kwargs): | ||
"""Delete a content. Called via JS. | ||
:param model: odoo model name | ||
:param model_id: odoo model id | ||
:param kwargs: extra request args, ``redirect`` only used ATM | ||
:return: json data to handle redirect and status message | ||
""" | ||
main_object = self.get_main_object(model, model_id) | ||
result = { | ||
'redirect': kwargs.get('redirect', ' ').strip(), | ||
'message': '', | ||
} | ||
msg = main_object.msg_content_deleted() | ||
main_object.unlink() | ||
if result['redirect']: | ||
# put message in session if redirecting | ||
request.env['website'].add_status_message(msg) | ||
else: | ||
# otherwise return it and handle it via JS | ||
result['message'] = msg | ||
return json.dumps(result) | ||
|
||
|
||
class DeleteController(http.Controller, DeleteMixin): | ||
"""Controller for handling model deletion.""" | ||
|
||
@http.route( | ||
'/cms/delete/<string:model>/<int:model_id>/confirm', | ||
type='http', auth="user", website=True) | ||
def delete_confirm(self, model, model_id, **kwargs): | ||
return self.handle_delete_confirm(model, model_id, **kwargs) | ||
|
||
# TODO: should we use `DELETE` method? | ||
@http.route( | ||
'/cms/delete/<string:model>/<int:model_id>', | ||
type='http', auth="user", website=True, methods=['POST']) | ||
def delete(self, model, model_id, **kwargs): | ||
return self.handle_delete(model, model_id, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Translation of Odoo Server. | ||
# This file contains the translation of the following modules: | ||
# * cms_delete_content | ||
# | ||
msgid "" | ||
msgstr "" | ||
"Project-Id-Version: Odoo Server 9.0c\n" | ||
"Report-Msgid-Bugs-To: \n" | ||
"POT-Creation-Date: 2017-01-10 16:41+0000\n" | ||
"PO-Revision-Date: 2017-01-10 17:43+0100\n" | ||
"Last-Translator: <>\n" | ||
"Language-Team: \n" | ||
"MIME-Version: 1.0\n" | ||
"Content-Type: text/plain; charset=UTF-8\n" | ||
"Content-Transfer-Encoding: 8bit\n" | ||
"Plural-Forms: \n" | ||
"Language: de\n" | ||
"X-Generator: Poedit 1.8.9\n" | ||
|
||
#. module: cms_delete_content | ||
#: code:addons/cms_delete_content/controllers.py:46 | ||
#: code:addons/external-src/website-cms/cms_delete_content/controllers.py:46 | ||
#, python-format | ||
msgid "%s deleted." | ||
msgstr "%s gelöscht." | ||
|
||
#. module: cms_delete_content | ||
#: model:ir.ui.view,arch_db:cms_delete_content.delete_confirm | ||
msgid "Are you sure you want to delete this item?" | ||
msgstr "Wollen Sie dieses Element wirklich löschen?" | ||
|
||
#. module: cms_delete_content | ||
#: model:ir.model.fields,field_description:cms_delete_content.field_website_published_mixin_cms_delete_url | ||
msgid "CMS delete URL" | ||
msgstr "CMS delete URL" | ||
|
||
#. module: cms_delete_content | ||
#: model:ir.model.fields,field_description:cms_delete_content.field_website_published_mixin_cms_delete_confirm_url | ||
msgid "CMS delete confirm URL" | ||
msgstr "CMS delete confirm URL" | ||
|
||
#. module: cms_delete_content | ||
#: model:ir.ui.view,arch_db:cms_delete_content.delete_confirm | ||
msgid "Cancel" | ||
msgstr "Abbrechen" | ||
|
||
#. module: cms_delete_content | ||
#: model:ir.ui.view,arch_db:cms_delete_content.delete_confirm | ||
msgid "Click here to delete this item" | ||
msgstr "Click here to delete this item" | ||
|
||
#. module: cms_delete_content | ||
#: model:ir.ui.view,arch_db:cms_delete_content.delete_confirm | ||
msgid "Confirm" | ||
msgstr "Bestätigen" | ||
|
||
#. module: cms_delete_content | ||
#: model:ir.model,name:cms_delete_content.model_website_published_mixin | ||
msgid "website.published.mixin" | ||
msgstr "website.published.mixin" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import website_published | ||
from . import test_models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from openerp import models, tools | ||
|
||
testing = tools.config.get('test_enable') | ||
|
||
if testing: | ||
class PublishResPartner(models.Model): | ||
_name = "res.partner" | ||
_inherit = ["res.partner", "website.published.mixin"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2017 Camptocamp | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
||
from openerp import api, fields, models, _ | ||
|
||
|
||
class WebsitePublishedMixin(models.AbstractModel): | ||
_inherit = "website.published.mixin" | ||
|
||
cms_delete_url = fields.Char( | ||
string='CMS delete URL', | ||
compute='_compute_cms_delete_url', | ||
readonly=True, | ||
) | ||
|
||
@api.multi | ||
def _compute_cms_delete_url(self): | ||
for item in self: | ||
item.cms_delete_url = \ | ||
u'/cms/delete/{}/{}'.format(item._name, item.id) | ||
|
||
cms_delete_confirm_url = fields.Char( | ||
string='CMS delete confirm URL', | ||
compute='_compute_cms_delete_confirm_url', | ||
readonly=True, | ||
) | ||
|
||
@api.multi | ||
def _compute_cms_delete_confirm_url(self): | ||
for item in self: | ||
item.cms_delete_confirm_url = \ | ||
u'/cms/delete/{}/{}/confirm'.format(item._name, item.id) | ||
|
||
@property | ||
def cms_after_delete_url(self): | ||
return '/' | ||
|
||
@api.multi | ||
def msg_content_delete_confirm(self): | ||
self.ensure_one() | ||
return _('Are you sure you want to delete this item?') | ||
|
||
@api.multi | ||
def msg_content_deleted(self): | ||
self.ensure_one() | ||
return _('%s deleted.') % self._description |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.