Skip to content

Commit

Permalink
TUNE-72: use a ModelChoiceField, layout
Browse files Browse the repository at this point in the history
  • Loading branch information
maltem-za committed Apr 8, 2016
1 parent 6a706dd commit 88af45d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
23 changes: 9 additions & 14 deletions molo/commenting/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import forms
from django.db import OperationalError
from django.forms import ModelChoiceField
from django.utils.translation import ugettext_lazy as _
from django_comments.forms import CommentForm
from molo.commenting.models import MoloComment, CannedResponse
Expand Down Expand Up @@ -43,16 +43,6 @@ def get_comment_object(self):
return new


def get_canned_choices():
canned_choices = [('', '---------')]
try:
canned_choices += ([(canned.response, canned.response_header) for canned in CannedResponse.objects.all()])
except OperationalError:
canned_choices = []

return canned_choices


class MoloCommentReplyForm(MoloCommentForm):
parent = forms.ModelChoiceField(
queryset=MoloComment.objects.all(), widget=forms.HiddenInput,
Expand All @@ -66,8 +56,13 @@ class MoloCommentReplyForm(MoloCommentForm):
honeypot = forms.CharField(
required=False, widget=forms.HiddenInput)

canned_response = ModelChoiceField(queryset=CannedResponse.objects.all(),
label="Or add a canned response",
to_field_name="response",
required=False)

def __init__(self, *args, **kwargs):
parent = MoloComment.objects.get(pk=kwargs.pop('parent'))
super(MoloCommentReplyForm, self).__init__(parent.content_object, *args, **kwargs)
self.fields['canned_select'] = forms.ChoiceField(choices=get_canned_choices(),
label="Or add a Canned response", required=False)
super(MoloCommentReplyForm, self).__init__(
parent.content_object, *args, **kwargs
)
4 changes: 2 additions & 2 deletions molo/commenting/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class CannedResponse(models.Model):
date_added = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return self.response
return self.response_header

class Meta:
verbose_name_plural = 'Canned replies'
verbose_name_plural = 'Canned responses'
app_label = 'commenting'
ordering = ['response_header', 'response']
10 changes: 5 additions & 5 deletions molo/commenting/templates/admin/reply.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
{% block content %}
<form action="" method="post">
{% csrf_token %}
{{ form }}
{{ form.as_p }}
<input type="submit" value="Reply" />
</form>

<script>
var cannedSelect = document.getElementById("id_canned_select");
document.getElementsByTagName("form")[0].setAttribute("onchange", "commentReply()");
function commentReply() {
document.getElementById('id_comment').innerHTML = (cannedSelect[cannedSelect.selectedIndex].value);
var cannedSelect = document.getElementById("id_canned_response");
cannedSelect.setAttribute("onchange", "setCannedResponse()");
function setCannedResponse() {
document.getElementById('id_comment').innerHTML = cannedSelect[cannedSelect.selectedIndex].value;
}
</script>

Expand Down

0 comments on commit 88af45d

Please sign in to comment.