Skip to content
Snippets Groups Projects
Commit b5cc8dcb authored by Erin Mullaney's avatar Erin Mullaney
Browse files

#960 x out the widget in favor of a simpler html form with buttons

parent 06256ca8
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,6 @@ from opentech.apply.review.options import NA
from opentech.apply.stream_forms.forms import StreamBaseForm
from .models import Review, ReviewOpinion
from .widgets import ButtonsAsSelectWidget
class MixedMetaClass(type(StreamBaseForm), type(forms.ModelForm)):
......@@ -91,8 +90,4 @@ class ReviewOpinionForm(forms.ModelForm):
class Meta:
model = ReviewOpinion
fields = ('opinion',)
widgets = {
'opinion': ButtonsAsSelectWidget(),
}
fields = ()
......@@ -36,7 +36,13 @@
<form method="post">
{% csrf_token %}
{{ form }}
{% for choice in opinion_choices %}
<button name="opinion" type="submit" value="{{ choice.value }}"
class="button button--primary {{ choice.disabled_class }}" {% if choice.disabled %}disabled{% endif %}>
{{ choice.label }}
</button>
{% endfor %}
</form>
{% endblock %}
{% for group_name, group_choices, group_index in widget.optgroups %}
{% if group_index %} {# Do not include the first blank option as a button #}
<button name="opinion" type="submit" value="{{ group_choices.0.value }}" class="button button--primary">
{{ group_choices.0.label }}
</button>
{% endif %}
{% endfor %}
\ No newline at end of file
......@@ -18,6 +18,7 @@ from opentech.apply.users.decorators import staff_required
from opentech.apply.utils.views import CreateOrUpdateView
from .models import Review, ReviewOpinion
from .options import OPINION_CHOICES
class ReviewContextMixin:
......@@ -105,7 +106,23 @@ class ReviewDisplay(DetailView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = ReviewOpinionForm()
if self.get_object().author != self.request.user:
existing_opinion = ReviewOpinion.objects.filter(
author=self.request.user, review=self.get_object()).first()
opinion_choices = []
for value, label in OPINION_CHOICES:
button_dict = {
'value': value,
'label': label,
'disabled': False,
}
if existing_opinion and existing_opinion.opinion == value:
button_dict['disabled'] = True
button_dict['disabled_class'] = 'is-disabled'
opinion_choices.append(button_dict)
context['opinion_choices'] = opinion_choices
context['form'] = ReviewOpinionForm()
return context
def dispatch(self, request, *args, **kwargs):
......@@ -129,12 +146,17 @@ class ReviewOpinionFormView(SingleObjectMixin, FormView):
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
review_opinion = form.save(commit=False)
review_opinion.author = self.request.user
review_opinion.review = self.object
review_opinion.save()
opinion = int(request.POST.get('opinion'))
# TODO: update_or_create is failing here for me, so I'm doing this the long way ...
existing_review = ReviewOpinion.objects.filter(author=self.request.user, review=self.object).first()
if existing_review:
existing_review.opinion = opinion
existing_review.save()
else:
ReviewOpinion.objects.create(
opinion=opinion,
author=self.request.user,
review=self.object)
return super().post(request, *args, **kwargs)
......
from django.forms.widgets import Select
class ButtonsAsSelectWidget(Select):
template_name = 'review/widgets/buttons_select.html'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment