Skip to content

Commit

Permalink
Fixes zestedesavoir#2090: Utilisation d'une raw query
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceFox committed Jan 19, 2015
1 parent 4ddd509 commit f093da6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
8 changes: 5 additions & 3 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,10 @@
<div>
{% with topics=user|interventions_privatetopics %}
{% with unread_topics=topics.unread %}
{% with total_topics=topics.total %}
<a href="{% url "zds.mp.views.index" %}" class="ico-link">
{% if unread_topics|length > 0 %}
<span class="notif-count">{{ unread_topics|length }}</span>
{% if total_topics > 0 %}
<span class="notif-count">{{ total_topics }}</span>
{% endif %}
<span class="notif-text ico ico-messages">{% trans "Messagerie privée" %}</span>
</a>
Expand All @@ -285,7 +286,7 @@
{% endwith %}
{% endfor %}

{% if unread_topics|length = 0 %}
{% if total_topics = 0 %}
<li class="dropdown-empty-message">
{% trans "Aucun nouveau message" %}
</li>
Expand All @@ -297,6 +298,7 @@
</div>
{% endwith %}
{% endwith %}
{% endwith %}
</div>

{# NOTIFICATIONS #}
Expand Down
31 changes: 15 additions & 16 deletions zds/utils/templatetags/interventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,21 @@ def interventions_topics(user):
@register.filter('interventions_privatetopics')
def interventions_privatetopics(user):

topics_never_read = list(PrivateTopicRead.objects
.filter(user=user)
.filter(privatepost=F('privatetopic__last_message')).all())

tnrs = []
for tnr in topics_never_read:
tnrs.append(tnr.privatetopic.pk)

privatetopics_unread = PrivateTopic.objects\
.filter(Q(author=user) | Q(participants__in=[user]))\
.exclude(pk__in=tnrs)\
.select_related("privatetopic")\
.order_by("-pubdate")\
.distinct()

return {'unread': privatetopics_unread}
# Raw query because ORM doesn't seems to allow this kind of "left outer join" clauses.
# Parameters = list with 3x the same ID because SQLite backend doesn't allow map parameters.
privatetopics_unread = PrivateTopic.objects.raw(
'''
select distinct t.*
from mp_privatetopic t
inner join mp_privatetopic_participants p on p.privatetopic_id = t.id
left outer join mp_privatetopicread r on r.user_id = %s and r.privatepost_id = t.last_message_id
where (t.author_id = %s or p.user_id = %s)
and r.id is null
order by t.pubdate desc''',
[user.id, user.id, user.id])

# "total" re-do the query, but there is no other way to get the length as __len__ is not available on raw queries.
return {'unread': privatetopics_unread, 'total': len(list(privatetopics_unread))}


@register.filter(name='alerts_list')
Expand Down

0 comments on commit f093da6

Please sign in to comment.