drop thumbs-up & thumbs-down rating tables

This commit is contained in:
Chris Van 2012-06-08 17:25:30 -07:00
Родитель 16ca1d1021
Коммит 00d4eb4f37
3 изменённых файлов: 35 добавлений и 111 удалений

Просмотреть файл

@ -0,0 +1,16 @@
ALTER TABLE `ratings` DROP FOREIGN KEY `ratings_addon_id_fk`;
ALTER TABLE `ratings` DROP FOREIGN KEY `ratings_user_id_fk`;
ALTER TABLE `ratings` DROP FOREIGN KEY `ratings_body_fk`;
ALTER TABLE `ratings` DROP FOREIGN KEY `ratings_reply_to_fk3`;
ALTER TABLE `ratings_moderation_flags` DROP FOREIGN KEY `ratings_moderation_flags_rating_id_fk`;
ALTER TABLE `ratings_moderation_flags` DROP FOREIGN KEY `ratings_moderation_flags_user_id_fk`;
DROP INDEX `ratings_addon_id_idx` ON `ratings`;
DROP INDEX `ratings_user_id_idx` ON `ratings`;
DROP INDEX `ratings_moderation_flags_rating_id_idx` ON `ratings_moderation_flags`;
DROP INDEX `ratings_moderation_flags_user_id_idx` ON `ratings_moderation_flags`;
DROP TABLE `ratings`;
DROP TABLE `ratings_moderation_flags`;

Просмотреть файл

@ -1,105 +0,0 @@
from django import forms
from django.forms.models import modelformset_factory
import happyforms
from quieter_formset.formset import BaseModelFormSet
from tower import ugettext_lazy as _lazy
import amo
from amo.utils import raise_required
import reviews
from .models import RatingFlag, Rating
class RatingReplyForm(forms.Form):
body = forms.CharField(max_length=150,
widget=forms.Textarea(attrs={'rows': 2}))
def clean_body(self):
body = self.cleaned_data.get('body', '')
# Whitespace is not a review!
if not body.strip():
raise_required()
return body
class RatingForm(RatingReplyForm):
score = forms.ChoiceField(choices=([1, _lazy('Thumbs Up')],
[-1, _lazy('Thumbs Down')]))
def __init__(self, *args, **kw):
super(RatingForm, self).__init__(*args, **kw)
# Default to a blank value.
if self.fields['score'].choices[0][0]:
self.fields['score'].choices.insert(0, ('', ''))
class RatingFlagForm(forms.ModelForm):
class Meta:
model = RatingFlag
fields = ('flag', 'note', 'rating', 'user')
def clean(self):
data = super(RatingFlagForm, self).clean()
if data.get('note', '').strip():
data['flag'] = RatingFlag.OTHER
return data
class BaseRatingFlagFormSet(BaseModelFormSet):
def __init__(self, *args, **kwargs):
self.form = ModerateRatingFlagForm
super(BaseRatingFlagFormSet, self).__init__(*args, **kwargs)
def save(self):
for form in self.forms:
if form.cleaned_data:
action = int(form.cleaned_data['action'])
is_flagged = form.instance.reviewflag_set.count() > 0
if action != reviews.REVIEW_MODERATE_SKIP: # Delete flags.
for flag in form.instance.reviewflag_set.all():
flag.delete()
rating = form.instance
addon = rating.addon
if action in (reviews.REVIEW_MODERATE_KEEP,
reviews.REVIEW_MODERATE_KEEP):
if action == reviews.REVIEW_MODERATE_DELETE:
rating_addon, rating_id = rating.addon, rating.id
log_action = amo.LOG.DELETE_REVIEW
rating.delete()
elif action == reviews.REVIEW_MODERATE_KEEP:
rating_addon, rating_id = rating.addon, rating
rating.update(editorreview=False)
log_action = amo.LOG.APPROVE_REVIEW
amo.log(log_action, rating_addon, rating_id,
details=dict(body=unicode(rating.body),
addon_id=addon.id,
addon_title=unicode(addon.name),
is_flagged=is_flagged))
class ModerateRatingFlagForm(happyforms.ModelForm):
action_choices = [
(reviews.REVIEW_MODERATE_KEEP, _lazy('Keep review; remove flags')),
(reviews.REVIEW_MODERATE_SKIP, _lazy('Skip for now')),
(reviews.REVIEW_MODERATE_DELETE, _lazy('Delete review'))
]
action = forms.ChoiceField(choices=action_choices, required=False,
initial=0, widget=forms.RadioSelect())
class Meta:
model = Rating
fields = ('action',)
RatingFlagFormSet = modelformset_factory(Rating, extra=0,
form=ModerateRatingFlagForm,
formset=BaseRatingFlagFormSet)

Просмотреть файл

@ -11,6 +11,8 @@ import amo.log
from addons.decorators import addon_view_factory, has_purchased
from addons.models import Addon
from amo.decorators import json_view, login_required, post_required
from amo.helpers import absolutify
from reviews.models import Review
from reviews.helpers import user_can_delete_review
from reviews.views import get_flags
@ -110,13 +112,24 @@ def add(request, addon):
data = request.POST or None
form = forms.RatingForm(data)
if data and form.is_valid():
rating = Rating.objects.create(**_review_details(request, addon, form))
amo.log(amo.LOG.ADD_REVIEW, addon, rating)
log.debug('New rating: %s' % rating.id)
review = Review.objects.create(**_review_details(request, addon, form))
amo.log(amo.LOG.ADD_REVIEW, addon, review)
log.debug('New review: %s' % review.id)
messages.success(request, _('Your review was successfully added!'))
return redirect(addon.get_detail_url() + '#reviews')
# TODO: When rating list is done uncomment this (bug 755954).
#return redirect(addon.get_ratings_url('list'))
reply_url = addon.get_ratings_url('reply', args=[addon, review.id],
add_prefix=False)
data = {'name': addon.name,
'rating': '%s out of 5 stars' % details['rating'],
'review': details['body'],
'reply_url': absolutify(reply_url)}
# emails = addon.values_list('email', flat=True)
# send_mail('reviews/emails/add_review.ltxt',
# u'Mozilla Marketplace User Review: %s' % addon.name,
# emails, Context(data), 'new_review')
return redirect(addon.get_ratings_url('list'))
return jingo.render(request, 'ratings/add.html',
{'product': addon, 'form': form})