dashboard: add uniform star/unstar mechanism (#17313)

This commit is contained in:
Frédéric Péters 2017-05-22 23:02:27 +02:00
parent 271c46fd13
commit 5f34724597
3 changed files with 51 additions and 7 deletions

View File

@ -1,13 +1,16 @@
{% load i18n %}
{% load i18n dashboard %}
{% if user.is_authenticated %}
<span class="dashboard-cell-icons">
{% if not in_dashboard %}
<a class="add-to-dashboard" href="{% url 'combo-dashboard-add-tile' cell_reference=cell.get_reference %}"></a>
{% with tile=cell|as_dashboard_cell:request.user %}
{% if tile %}
<a class="remove-from-dashboard" href="{% url 'combo-dashboard-remove-tile' cell_reference=tile.cell.get_reference %}"></a>
{% else %}
<a class="add-to-dashboard" href="{% url 'combo-dashboard-add-tile' cell_reference=cell.get_reference %}"></a>
{% endif %}
{% endwith %}
{% else %}
<a class="dashboard-cell-menu"></a>
<ul class="menu closed">
<li><a href="{% url 'combo-dashboard-remove-tile' cell_reference=cell.get_reference %}">{% trans 'Remove from favorites' %}</a></li>
</ul>
{% endif %}
<a class="remove-from-dashboard" href="{% url 'combo-dashboard-remove-tile' cell_reference=cell.get_reference %}"></a>
{% endif %}
</span>
{% endif %}

View File

@ -0,0 +1,41 @@
# combo - content management system
# Copyright (C) 2014-2017 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django import template
from django.core import serializers
from ..models import Tile
register = template.Library()
def get_cell_data(cell):
# return a dictionary with cell parameters relevant for tile comparison
if cell is None:
return {}
cell_data = serializers.serialize('python', [cell])[0]
del cell_data['pk']
for key in ('restricted_to_unlogged', 'groups', 'last_update_timestamp',
'order', 'placeholder', 'public', 'page'):
del cell_data['fields'][key]
return cell_data
@register.filter
def as_dashboard_cell(cell, user):
cell_data = get_cell_data(cell)
for tile in Tile.objects.filter(user=user):
if get_cell_data(tile.cell) == cell_data:
return tile
return None