refactoring reputation

This commit is contained in:
slav0nic 2009-05-24 22:51:42 +03:00
parent 2dfcc591c0
commit bb74c80bf7
6 changed files with 53 additions and 48 deletions

View file

@ -134,7 +134,7 @@ class Topic(models.Model):
@property
def head(self):
return self.posts.all().order_by('created').select_related()[0]
'None'#return self.posts.all().order_by('created').select_related()[0]
@property
def reply_count(self):
@ -260,18 +260,13 @@ class Profile(models.Model):
return posts[0].created
else:
return None
def reply_total(self):
total = 0
for reputation in Reputation.objects.filter(to_user=self.user).select_related():
total += reputation.sign
return total
def reply_count_minus(self):
return Reputation.objects.filter(to_user=self.user, sign=-1).select_related().count()
return Reputation.objects.filter(to_user=self.user, sign=-1).count()
def reply_count_plus(self):
return Reputation.objects.filter(to_user=self.user, sign=1).select_related().count()
return Reputation.objects.filter(to_user=self.user, sign=1).count()
class Read(models.Model):
"""
@ -284,7 +279,7 @@ class Read(models.Model):
time = models.DateTimeField(_('Time'), blank=True)
class Meta:
unique_together = ['user', 'topic']
unique_together = [('user', 'topic')]
verbose_name = _('Read')
verbose_name_plural = _('Reads')

View file

@ -10,12 +10,15 @@ def post_saved(instance, **kwargs):
if created:
updated_time = datetime.now()
post.topic.updated = updated_time
post.topic.last_post = post
post.topic.post_count = Post.objects.filter(topic=post.topic).count()
post.topic.save(force_update=True)
post.topic.forum.updated = updated_time
post.topic.forum.post_count = Post.objects.filter(topic__forum=post.topic.forum).count()
post.topic.forum.last_post = post
post.topic.forum.save(force_update=True)
notify_topic_subscribers(post)
def post_deleted(instance, **kwargs):

View file

@ -18,7 +18,6 @@
<b><span>{{ forum }}</span></b>
</h2>
<div class="box">
<div class="inbox">
<table cellspacing="0">
<thead>
@ -26,30 +25,28 @@
<th class="tcl" scope="col">{% trans "Topic" %}</th>
<th class="tc2" scope="col">{% trans "Replies" %}</th>
<th class="tc3" scope="col">{% trans "Views" %}</th>
<th class="tcr" scope="col">{% trans "Last post" %}</th>
</tr>
</thead>
<tbody>
{% if topics %}
{% for topic in topics|forum_unreads:user %}
<tr >
<tr>
<td class="tcl">
<div class="intd">
<div {% if topic|has_unreads:user %}class="inew"{% else %}class="icon"{% endif %}><div class="nosize"><!-- --></div></div>
<div class="tclcon">
{% if topic|has_unreads:user %}
<strong>{% link topic %} <span class="byuser">{% trans "by" %} {{ topic.user }}</span></strong>
<strong>{% link topic %} <span class="byuser">{% trans "by" %} {{ topic.user.username }}</span></strong>
{% else %}
{% link topic %} <span class="byuser">{% trans "by" %} {{ topic.user }}</span>
{% link topic %} <span class="byuser">{% trans "by" %} {{ topic.user.username }}</span>
{% endif %}
</div>
</div>
</td>
<td class="tc2">{{ topic.reply_count }}</td>
<td class="tc3">{{ topic.views }}</td>
<td class="tcr"><a href="{{ topic.get_absolute_url }}">{% forum_time topic.updated %}</a> <span class="byuser">{% trans "by" %} {{ topic.last_post.user }}</span></td>
<td class="tcr"><a href="{{ topic.get_absolute_url }}">{% forum_time topic.updated %}</a> <span class="byuser">{% trans "by" %} {{ topic.last_post.user.username }}</span></td>
</tr>
{% endfor %}
{% else %}
@ -60,17 +57,14 @@
</div>
</div>
</div>
<div class="linkst">
<div class="inbox">
<p class="pagelink conl">{% pagination %}</p>
<p class="postlink conr"><a href="{% url add_topic forum.id %}">{% trans "New topic" %}</a></p>
<ul><li><a href="{% url index %}">{% trans "Root" %}</a> </li><li>&raquo; {{ forum }}</li></ul>
<div class="clearer"></div>
</div>
</div>
{% if "LOFI_SUPPORT"|forum_setting %}
<div id="brdfooter" class="block">
<div class="box">
@ -81,9 +75,7 @@
</div>
</div>
{% endif %}
{% endblock %}
{% block controls %}
<div class="conl">
<form id="qjump" method="get" action="forum">
@ -100,7 +92,6 @@
</select>
<input type="button" onclick="window.location=('/forum/'+getElementById('forum_id').value)" value=" {% trans "Go" %} " accesskey="g" />
</label></div>
</form>
{% if moderator %}
<p id="modcontrols"><a href="{% url moderate forum.id %}">{% trans "Moderate forum" %}</a></p>

View file

@ -1,11 +1,12 @@
{% load forum_extras %}
{% load cache %}
{% load i18n %}
{% if forum.last_post.topic %}
<tr {% if forum.last_post.topic|has_unreads:user %}class="inew"{% endif %}>
{% else %}
<tr>
{% endif %}
{% endif %}
<td class="tcl">
<div class="intd">
<div class="icon">
@ -16,9 +17,11 @@
{{ forum.description|safe }}
<p>
{% if forum.moderators.count %}
{% include 'forum/moderators.html' %}
{% endif %}
{% cache 6000 moderators forum.id %}
{% if forum.moderators.count %}
{% include 'forum/moderators.html' %}
{% endif %}
{% endcache %}
</p>
</div>

View file

@ -10,7 +10,7 @@
<p class="postlink conr"><a href="{% url add_post topic.id %}">{% trans "Reply" %}</a></p>
{% endif %}
<ul><li><a href="{% url index %}">{% trans "Root" %} </a></li><li>&raquo; {% link topic.forum %} </li><li>&raquo; {{ topic }}
<a href="{% url forum_feed "topic" %}{{ topic.id }}/"><img src="{{ MEDIA_URL }}/forum/img/feed-icon-small.png" alt="[RSS Feed]" title="[RSS Feed]" class="breadcrumb_rss" /></a>
<a href="{% url forum_feed "topic" %}{{ topic.id }}/"><img src="{{ MEDIA_URL }}/forum/img/feed-icon-small.png" alt="[RSS Feed]" title="[RSS Feed]" class="breadcrumb_rss" /></a>
</li></ul>
<div class="clearer"></div>
@ -53,17 +53,17 @@
<dd>{% trans "IP:" %} {{ post.user_ip }}</dd>
{% endif %}
{% if "REPUTATION_SUPPORT"|forum_setting %}
{% ifnotequal request.user post.user %}
<dd><a href="{% url reputation post.user %}">{% trans "Reputation" %}</a> : <a href="{% url reputation post.user %}?action=plus&topic_id={{ post.topic.id }}"><img src="{{ MEDIA_URL }}forum/img/reputation/warn_add.gif" alt="+" border="0"></a>&nbsp;&nbsp;<strong>{{ post.user.forum_profile.reply_total }}&nbsp;&nbsp;</strong><a href="{% url reputation post.user %}?action=minus&topic_id={{ post.topic.id }}"><img src="{{ MEDIA_URL }}forum/img/reputation/warn_minus.gif" alt="-" border="0"></a></dd>
{% endifnotequal %}
{% ifnotequal request.user post.user %}
<dd><a href="{% url reputation post.user %}">{% trans "Reputation" %}</a> : <a href="{% url reputation post.user %}?action=plus&topic_id={{ post.topic.id }}"><img src="{{ MEDIA_URL }}forum/img/reputation/warn_add.gif" alt="+" border="0"></a>&nbsp;&nbsp;<strong>{{ post.user.forum_profile.reply_total }}&nbsp;&nbsp;</strong><a href="{% url reputation post.user %}?action=minus&topic_id={{ post.topic.id }}"><img src="{{ MEDIA_URL }}forum/img/reputation/warn_minus.gif" alt="-" border="0"></a></dd>
{% endifnotequal %}
{% endif %}
<dd class="usercontacts"><a href="{% url forum_profile post.user %}">{% trans "Profile" %}</a>&nbsp;&nbsp;
{% ifequal post.user.forum_profile.privacy_permission 0 %}
<a href="mailto:{{ post.user.email }}">{% trans "E-mail" %}</a>&nbsp;&nbsp;
{% else %}
{% ifequal post.user.forum_profile.privacy_permission 1 %}
<a href="{% url misc %}?mail_to={{ post.user }}">{% trans "Send e-mail" %}</a>&nbsp;&nbsp;
{% endifequal %}
<a href="mailto:{{ post.user.email }}">{% trans "E-mail" %}</a>&nbsp;&nbsp;
{% else %}
{% ifequal post.user.forum_profile.privacy_permission 1 %}
<a href="{% url misc %}?mail_to={{ post.user }}">{% trans "Send e-mail" %}</a>&nbsp;&nbsp;
{% endifequal %}
{% endifequal %}
{% if "PM_SUPPORT"|forum_setting %}
{% if user.is_authenticated %}

View file

@ -11,7 +11,7 @@ from django.core.urlresolvers import reverse
from django.db import connection
from django.core.cache import cache
from django.utils import translation
from django.db.models import Q
from django.db.models import Q, Sum
from forum.util import render_to, paged, build_form, paginate, set_language
from forum.models import Category, Forum, Topic, Post, Profile, Read,\
@ -39,7 +39,7 @@ def index(request, full=True):
user_groups = request.user.groups.all()
if request.user.is_anonymous(): # in django 1.1 EmptyQuerySet raise exception
user_groups = []
for forum in Forum.objects.filter(Q(category__groups__in=user_groups) | Q(category__groups__isnull=True)).select_related('category'):
for forum in Forum.objects.filter(Q(category__groups__in=user_groups) | Q(category__groups__isnull=True)).select_related('last_post__topic','last_post__user', 'category'):
cat = cats.setdefault(forum.category.id,
{'cat': forum.category, 'forums': []})
cat['forums'].append(forum)
@ -257,6 +257,7 @@ def misc(request):
'user': user,
}, 'forum/mail_to.html'
@render_to('forum/forum.html')
@paged('topics', forum_settings.FORUM_PAGE_SIZE)
def show_forum(request, forum_id, full=True):
@ -271,15 +272,15 @@ def show_forum(request, forum_id, full=True):
'forum': forum,
'topics': topics,
'paged_qs': topics,
'posts': forum.posts.count(),
'topics': forum.topics.count(),
'posts': forum.post_count,
'topics': forum.topic_count,
'moderator': moderator,
}
else:
pages, paginator, paged_list_name = paginate(topics, request, forum_settings.FORUM_PAGE_SIZE)
return {'categories': Category.objects.all(),
'forum': forum,
'posts': forum.posts.count(),
'posts': forum.post_count,
'moderator': moderator,
'pages': pages,
'paginator': paginator,
@ -287,7 +288,6 @@ def show_forum(request, forum_id, full=True):
}, 'forum/lofi/forum.html'
@render_to('forum/topic.html')
@paged('posts', forum_settings.TOPIC_PAGE_SIZE)
def show_topic(request, topic_id, full=True):
@ -297,19 +297,32 @@ def show_topic(request, topic_id, full=True):
topic.views += 1
topic.save()
last_post = topic.posts.order_by('-created')[0]
last_post = topic.last_post
if request.user.is_authenticated():
topic.update_read(request.user)
posts = topic.posts.all().select_related()
profiles = Profile.objects.filter(user__pk__in=set(x.user.id for x in posts))
profiles = dict((x.user_id, x) for x in profiles)
users= set(post.user.id for post in posts)
profiles = Profile.objects.filter(user__pk__in=users)
profiles = dict((profile.user_id, profile) for profile in profiles)
for post in posts:
post.user.forum_profile = profiles[post.user.id]
if forum_settings.REPUTATION_SUPPORT:
replies_list = Reputation.objects.filter(to_user__pk__in=users).values('to_user_id').annotate(sign=Sum('sign')) #values_list buggy?
if replies_list:
replies = {}
for r in replies_list:
replies[r['to_user_id']] = r['sign']
for post in posts:
post.user.forum_profile.reply_total = replies.get(post.user.id, 0)
print replies
initial = {}
if request.user.is_authenticated():
initial = {'markup': request.user.forum_profile.markup}