Skip to content

Commit

Permalink
Merge pull request #720 from crdoconnor/master
Browse files Browse the repository at this point in the history
BUG 1196293 : Remove rating stars from edit review reply
  • Loading branch information
magopian committed Oct 1, 2015
2 parents 2295fee + a43d0c4 commit bd8e427
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 74 deletions.
36 changes: 33 additions & 3 deletions apps/reviews/forms.py
Expand Up @@ -18,8 +18,21 @@


class ReviewReplyForm(forms.Form):
title = forms.CharField(required=False)
body = forms.CharField(widget=forms.Textarea(attrs={'rows': 3}))
form_id = "review-reply-edit"

title = forms.CharField(
required=False,
label=_lazy(u"Title"),
widget=forms.TextInput(
attrs={'id': 'id_review_reply_title', },
),
)
body = forms.CharField(
widget=forms.Textarea(
attrs={'rows': 3, 'id': 'id_review_reply_body', },
),
label="Review",
)

def clean_body(self):
body = self.cleaned_data.get('body', '')
Expand All @@ -30,7 +43,24 @@ def clean_body(self):


class ReviewForm(ReviewReplyForm):
rating = forms.ChoiceField(zip(range(1, 6), range(1, 6)))
form_id = "review-edit"

title = forms.CharField(
required=False,
label=_lazy(u"Title"),
widget=forms.TextInput(
attrs={'id': 'id_review_title', },
),
)
body = forms.CharField(
widget=forms.Textarea(
attrs={'rows': 3, 'id': 'id_review_body', },
),
label="Review",
)
rating = forms.ChoiceField(
zip(range(1, 6), range(1, 6)), label=_lazy(u"Rating")
)
flags = re.I | re.L | re.U | re.M
# This matches the following three types of patterns:
# http://... or https://..., generic domain names, and IPv4
Expand Down
8 changes: 8 additions & 0 deletions apps/reviews/helpers.py
Expand Up @@ -61,6 +61,14 @@ def edit_review_form(context):
return c


@jingo.register.inclusion_tag('reviews/edit_review.html')
@jinja2.contextfunction
def edit_review_reply_form(context):
c = dict(context.items())
c.update(form=forms.ReviewReplyForm())
return c


def user_can_delete_review(request, review):
"""Return whether or not the request.user can delete reviews.
Expand Down
12 changes: 6 additions & 6 deletions apps/reviews/templates/reviews/edit_review.html
@@ -1,19 +1,19 @@
{% from "includes/forms.html" import pretty_field, required_note %}
<div class="hidden">
<form method="post" id="review-edit-form" action="#"
<form method="post" id="{{ form.form_id }}-form" action="#"
class="review article review-form prettyform">
{{ csrf() }}
<fieldset>
<ul>
{{ pretty_field(form.title, _('Title')) }}
{{ pretty_field(form.rating, _('Rating')) }}
{{ pretty_field(form.body, _('Review')) }}
{% for field in form %}
{{ pretty_field(field, field.label) }}
{% endfor %}
</ul>
</fieldset>
<footer>
{{ required_note() }}
<input type="submit" value="{{ _('Submit review') }}">
{{ _('or') }} <a href="#" id="review-edit-cancel">{{ _('Cancel') }}</a>
<input id="{{ form.form_id }}-submit" type="submit" value="{{ _('Submit review') }}">
{{ _('or') }} <a href="#" id="{{ form.form_id }}-cancel">{{ _('Cancel') }}</a>
</footer>
</form>
</div>
2 changes: 1 addition & 1 deletion apps/reviews/templates/reviews/review.html
Expand Up @@ -85,7 +85,7 @@ <h3 class="addon-name"><a href="{{ addon.get_url_path() }}">{{ addon.name }}</a>
{% if review.user_id == request.user.id %}
{% if is_reply %}
<li>
<a class="review-edit" href="#">
<a class="review-reply-edit" href="#">
{{ _('Edit reply') }}</a>
</li>
{% else %}
Expand Down
1 change: 1 addition & 0 deletions apps/reviews/templates/reviews/review_list.html
Expand Up @@ -95,6 +95,7 @@ <h1>{{ _('No reviews found.') }}</h1>
{{ reviews|impala_paginator }}
{% endblock review_list %}
{{ edit_review_form() }}
{{ edit_review_reply_form() }}
</div>
{{ report_review_popup() }}
{% endblock content %}
4 changes: 2 additions & 2 deletions apps/reviews/tests/test_views.py
Expand Up @@ -135,7 +135,7 @@ def test_list_item_actions(self):
actions = item.find('.item-actions')
eq_(actions.length, 1)
classes = sorted(c.get('class') for c in actions.find('li a'))
eq_(classes, ['delete-review', 'review-edit'])
eq_(classes, ['delete-review', 'review-reply-edit'])

def test_cant_view_unlisted_addon_reviews(self):
"""An unlisted addon doesn't have reviews."""
Expand Down Expand Up @@ -420,7 +420,7 @@ def test_edit_reply(self):
response = self.client.get(helpers.url('addons.reviews.list',
self.addon.slug))
doc = pq(response.content)
assert doc('#review-218468 .review-edit').text() == 'Edit reply'
assert doc('#review-218468 .review-reply-edit').text() == 'Edit reply'


class TestTranslate(ReviewTest):
Expand Down
143 changes: 81 additions & 62 deletions static/js/impala/reviews.js
Expand Up @@ -56,75 +56,94 @@ $(document).ready(function() {
}
});

$('.primary').delegate('.review-edit', 'click', function(e) {
e.preventDefault();
var $form = $('#review-edit-form'),
$review = $(this).closest('.review'),
rating = $review.attr('data-rating'),
edit_url = $('a.permalink', $review).attr('href') + 'edit',
$cancel = $('#review-edit-cancel'),
title_selector;

clearErrors($form);
$form.unbind().hide();
$('.review').not($review).show();
$form.detach().insertAfter($review);

if ($review.find('h4').length) {
$form.find('fieldset h3').remove();
title_selector = 'h4 > b';
$form.find('fieldset').prepend($review.find('h3').clone());
} else {
title_selector = 'h3 > b';
}

$form.find('#id_title').val($review.find(title_selector).text());
$form.find('.ratingwidget input:radio[value=' + rating + ']').click();
$form.find('#id_body').val($review.children('p.description').html().replace(/<br>/g, '\n'));
$review.hide();
$form.show();
$window.resize();
location.hash = '#review-edit-form';
// A review comment can either be a review or a review reply
function review_comment_edit_click(comment_form_id, comment_title_widget_id, comment_body_widget_id, comment_cancel_btn_id) {
return function(e) {
e.preventDefault();
var $form = $('#' + comment_form_id),
$review = $(this).closest('.review'),
edit_url = $('a.permalink', $review).attr('href') + 'edit',
$cancel = $('#' + comment_cancel_btn_id),
title_selector;

function done_edit() {
clearErrors($form);
$form.unbind().hide();
$review.show();
$cancel.unbind();
$('.review').not($review).show();
$form.detach().insertAfter($review);

if ($review.find('h4').length) {
$form.find('fieldset h3').remove();
title_selector = 'h4 > b';
$form.find('fieldset').prepend($review.find('h3').clone());
} else {
title_selector = 'h3 > b';
}

$form.find('#' + comment_title_widget_id).val($review.find(title_selector).text());
$form.find('#' + comment_body_widget_id).val($review.children('p.description').html().replace(/<br>/g, '\n'));
$review.hide();
$form.show();
$window.resize();
}
location.hash = '#' + comment_form_id;

$cancel.click(_pd(done_edit));
function done_edit() {
clearErrors($form);
$form.unbind().hide();
$review.show();
$cancel.unbind();
$window.resize();
}

$form.submit(function (e) {
e.preventDefault();
$.ajax({type: 'POST',
url: edit_url,
data: $form.serialize(),
success: function(response, status) {
clearErrors($form);
$review.find(title_selector).text($form.find('#id_title').val());
var rating = $form.find('.ratingwidget input:radio:checked').val();
$('.stars', $review).removeClass('stars-0 stars-1 stars-2 stars-3 stars-4 stars-5').addClass('stars-' + rating);
rating = $review.attr('data-rating', rating);
$review.children('p.description').html(
$form.find('#id_body').val()
.replace(/&/g,'&amp;')
.replace(/</g,'&lt;')
.replace(/>/g,'&gt;')
.replace(/\n/g, '<br>'));
done_edit();
},
error: function(xhr) {
var errors = $.parseJSON(xhr.responseText);
populateErrors($form, errors);
},
dataType: 'json'
});
return false;
});
});
$cancel.click(_pd(done_edit));

$form.submit(function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: edit_url,
data: $form.serialize(),
success: function(response, status) {
clearErrors($form);
$review.find(title_selector).text($form.find('#' + comment_title_widget_id).val());
var rating = $form.find('.ratingwidget input:radio:checked').val();
$('.stars', $review).removeClass('stars-0 stars-1 stars-2 stars-3 stars-4 stars-5').addClass('stars-' + rating);
rating = $review.attr('data-rating', rating);
$review.children('p.description').html(
$form.find('#' + comment_body_widget_id).val()
.replace(/&/g,'&amp;')
.replace(/</g,'&lt;')
.replace(/>/g,'&gt;')
.replace(/\n/g, '<br>'));
done_edit();
},
error: function(xhr) {
var errors = $.parseJSON(xhr.responseText);
populateErrors($form, errors);
},
dataType: 'json'
});
return false;
});
}
}

$('.primary').delegate('.review-reply-edit', 'click',
review_comment_edit_click(
'review-reply-edit-form',
'id_review_reply_title',
'id_review_reply_body',
'review-reply-edit-cancel'
)
);

$('.primary').delegate('.review-edit', 'click',
review_comment_edit_click(
'review-edit-form',
'id_review_title',
'id_review_body',
'review-edit-cancel'
)
);

$('.delete-review').click(function(e) {
e.preventDefault();
Expand Down

0 comments on commit bd8e427

Please sign in to comment.