closed #50, #61: Now user can't set reputation to youself and can vote for post only once. WARNING: Reputation model was changed, pls migrate it youself.

This commit is contained in:
slav0nic 2010-02-23 14:19:20 +02:00
parent 7ee8d7eff1
commit 3e08a0e613
7 changed files with 40 additions and 26 deletions

View file

@ -29,8 +29,8 @@ class ProfileAdmin(admin.ModelAdmin):
raw_id_fields = ['user']
class ReputationAdmin(admin.ModelAdmin):
list_display = ['from_user', 'to_user', 'topic', 'sign', 'time', 'reason']
raw_id_fields = ['from_user', 'to_user', 'topic']
list_display = ['from_user', 'to_user', 'post', 'sign', 'time', 'reason']
raw_id_fields = ['from_user', 'to_user', 'post']
class ReportAdmin(admin.ModelAdmin):
list_display = ['reported_by', 'post', 'zapped', 'zapped_by', 'created', 'reason']

View file

@ -293,15 +293,15 @@ class ReputationForm(forms.ModelForm):
class Meta:
model = Reputation
fields = ['reason', 'topic', 'sign']
fields = ['reason', 'post', 'sign']
def __init__(self, *args, **kwargs):
self.from_user = kwargs.pop('from_user', None)
self.to_user = kwargs.pop('to_user', None)
self.topic = kwargs.pop('topic', None)
self.post = kwargs.pop('post', None)
self.sign = kwargs.pop('sign', None)
super(ReputationForm, self).__init__(*args, **kwargs)
self.fields['topic'].widget = forms.HiddenInput()
self.fields['post'].widget = forms.HiddenInput()
self.fields['sign'].widget = forms.HiddenInput()
self.fields['reason'].widget = forms.Textarea(attrs={'class':'bbcode'})
@ -314,11 +314,18 @@ class ReputationForm(forms.ModelForm):
else:
return user
def clean(self):
try:
Reputation.objects.get(from_user=self.from_user, post=self.cleaned_data['post'])
except Reputation.DoesNotExist:
pass
else:
raise forms.ValidationError(_('You already voted for this post'))
def save(self, commit=True):
reputation = super(ReputationForm, self).save(commit=False)
reputation.from_user = self.from_user
reputation.to_user = self.to_user
reputation.time = datetime.now()
if commit:
reputation.save()
return reputation

View file

@ -244,17 +244,18 @@ class Post(models.Model):
class Reputation(models.Model):
from_user = models.ForeignKey(User, related_name='reputations_from', verbose_name=_('From'))
to_user = models.ForeignKey(User, related_name='reputations_to', verbose_name=_('To'))
topic = models.ForeignKey(Topic, related_name='topic', verbose_name=_('Topic'))
time = models.DateTimeField(_('Time'), blank=True)
post = models.ForeignKey(Post, related_name='post', verbose_name=_('Post'))
time = models.DateTimeField(_('Time'), auto_now_add=True)
sign = models.IntegerField(_('Sign'), choices=SIGN_CHOICES, default=0)
reason = models.TextField(_('Reason'), blank=True, default='', max_length=1000)
reason = models.TextField(_('Reason'), max_length=1000)
class Meta:
verbose_name = _('Reputation')
verbose_name_plural = _('Reputations')
unique_together = (('from_user', 'post'),)
def __unicode__(self):
return u'T[%d], FU[%d], TU[%d]: %s' % (self.topic.id, self.from_user.id, self.to_user.id, unicode(self.time))
return u'T[%d], FU[%d], TU[%d]: %s' % (self.post.id, self.from_user.id, self.to_user.id, unicode(self.time))
class Profile(models.Model):

View file

@ -19,7 +19,7 @@
<thead>
<tr>
<th class="tc3" style="width: 15%;">{% trans "From user" %}</th>
<th class="tc3" style="width: 15%;">{% trans "For topic" %}</th>
<th class="tc3" style="width: 15%;">{% trans "For post in topic" %}</th>
<th class="tc3" style="width: 35%;">{% trans "Reason" %}</th>
<th class="tc3" style="width: 10%; text-align: center;">{% trans "Estimation" %}</th>
<th class="tc3" style="width: 15%;">{% trans "Date" %}</th>
@ -32,7 +32,7 @@
{% for reputation in reputations %}
<tr>
<td><a href="{% url djangobb:reputation reputation.from_user %}">{{ reputation.from_user }}</a></td>
<td>{% link reputation.topic %}</td>
<td><a href="{{ reputation.post.get_absolute_url }}">{{ reputation.post.topic }}</a></td>
<td>{{ reputation.reason }}</td>
<td style="text-align: center;">
{% ifequal reputation.sign 1 %}

View file

@ -9,7 +9,8 @@
<div class="inbox">
<form action="{% url djangobb:reputation form.to_user %}" method="post">
<table cellspacing="0">
{{ form.topic }}
{{ form.errors.as_ul }}
{{ form.post }}
{{ form.sign }}
<tbody><tr>
<td class="tc4" width="30%">{% trans "Your name:" %}</td>
@ -18,10 +19,9 @@
<tr>
<td class="tc4" width="30%">{% trans "To whom we change a reputation:" %}</td>
<td class="tc4" width="70%">{{ form.to_user }}</td>
</tr>
<tr>
<td class="tc4" width="30%">{% trans "The reason of change of reputation:" %}</td>
<td class="tc4" width="30%"></td>
<td class="tc4" width="70%">{{ form.reason }}</td>
</tr>
<tr>
@ -33,7 +33,6 @@
{% trans "Reduction of a reputation" %}
{% endifequal %}
</td>
</tr>
</tbody></table>
<table cellspacing="0">

View file

@ -54,8 +54,9 @@
<dd>{% trans "IP:" %} {{ post.user_ip }}</dd>
{% endif %}
{% if forum_settings.REPUTATION_SUPPORT %}
{% ifnotequal request.user post.user.username %}
<dd><a href="{% url djangobb:reputation post.user %}">{% trans "Reputation" %}</a> : <a href="{% url djangobb: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 djangobb: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>
<dd><a href="{% url djangobb:reputation post.user %}">{% trans "Reputation" %}</a>
{% ifnotequal request.user post.user %} {# TODO: and user.is_authenticated #}
: <a href="{% url djangobb:reputation post.user %}?action=plus&post_id={{ post.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 djangobb:reputation post.user %}?action=minus&post_id={{ post.id }}"><img src="{{ MEDIA_URL }}forum/img/reputation/warn_minus.gif" alt="-" border="0"></a></dd>
{% endifnotequal %}
{% endif %}
<dd class="usercontacts"><a href="{% url djangobb:forum_profile post.user %}">{% trans "Profile" %}</a>&nbsp;&nbsp;

View file

@ -495,10 +495,14 @@ def reputation(request, username):
form = build_form(ReputationForm, request, from_user=request.user, to_user=user)
if 'action' in request.GET:
if 'topic_id' in request.GET:
if request.user == user:
return HttpResponseForbidden(u'You can not change the reputation of yourself')
if 'post_id' in request.GET:
sign = 0
topic_id = request.GET['topic_id']
form.fields['topic'].initial = topic_id
post_id = request.GET['post_id']
form.fields['post'].initial = post_id
print form.fields['post']
if request.GET['action'] == 'plus':
form.fields['sign'].initial = 1
elif request.GET['action'] == 'minus':
@ -510,7 +514,7 @@ def reputation(request, username):
raise Http404
elif request.method == 'POST':
if 'del_reputation' in request.POST:
if 'del_reputation' in request.POST and request.user.is_superuser:
reputation_list = request.POST.getlist('reputation_id')
for reputation_id in reputation_list:
reputation = get_object_or_404(Reputation, pk=reputation_id)
@ -518,11 +522,13 @@ def reputation(request, username):
return HttpResponseRedirect(reverse('djangobb:index'))
elif form.is_valid():
form.save()
topic_id = request.POST['topic']
topic = get_object_or_404(Topic, id=topic_id)
return HttpResponseRedirect(topic.get_absolute_url())
post_id = request.POST['post']
post = get_object_or_404(Post, id=post_id)
return HttpResponseRedirect(post.get_absolute_url())
else:
raise Http404
return {'form': form,
'TEMPLATE': 'forum/reputation_form.html'
}
else:
reputations = Reputation.objects.filter(to_user=user).order_by('-time').select_related()
return {'reputations': reputations,