Permalink
Browse files

Merge pull request #132 from tiriplicamihai/master

Issue #86 - better answer management in qpool. Thanks MihaiT
  • Loading branch information...
2 parents 1f8e90a + 7c2bb31 commit 59e6c20630df4affa1ee3818dcfd381d79c7f8df @alexef alexef committed Oct 6, 2012
@@ -83,6 +83,22 @@ def save(self):
self.instance.save()
return self.instance
+
+class AnswerForm(forms.Form):
+ def __init__(self, data=None, instance=None):
+ super(AnswerForm, self).__init__(data)
+
+ self.fields['new_answer_text'] = forms.CharField(max_length=100,
+ widget=forms.Textarea, required=False)
+ self.fields['new_answer_correct'] = forms.BooleanField(required=False)
+
+ def save(self, id=None):
+ data = self.cleaned_data
+ a = Answer.objects.create(question=id)
+ a.text = data['new_answer_text']
+ a.correct = data['new_answer_correct']
+ a.save()
+
class TagsForm(forms.Form):
def __init__(self, data=None, instance=None, tags=[]):
super(TagsForm, self).__init__(data)
@@ -8,8 +8,10 @@
url(r'^qpool/$', 'wouso.interface.cpanel.views.qpool_home', name='qpool_home'),
url(r'^qpool/(?P<page>\d+)/$', 'wouso.interface.cpanel.views.qpool_home', name='qpool_home'),
url(r'^qpool/tag_questions/$', 'wouso.interface.cpanel.views.qpool_tag_questions', name='tag_questions'),
+ url(r'^qpool/edit/(?P<id>\d+)/add_answer', 'wouso.interface.cpanel.views.qpool_add_answer', name='add_answer'),
url(r'^qpool/edit/(?P<id>\d+)/$', 'wouso.interface.cpanel.views.qpool_edit', name='question_edit'),
url(r'^qpool/del/(?P<id>\d+)/$', 'wouso.interface.cpanel.views.qpool_delete', name='question_del'),
+ url(r'^qpool/del/(?P<question_id>\d+)/(?P<answer_id>\d+)/$', 'wouso.interface.cpanel.views.qpool_delete_answer', name='answer_del'),
url(r'^qpool/set_active_categories', 'wouso.interface.cpanel.views.qpool_set_active_categories', name='set_active_categories'),
url(r'^qpool/new/$', 'wouso.interface.cpanel.views.qpool_edit', name='question_new'),
url(r'^qpool/switch_active/(?P<id>\d+)/$', 'wouso.interface.cpanel.views.question_switch', name='switch_active'),
@@ -14,15 +14,15 @@
from wouso.core.decorators import staff_required
from wouso.core.user.models import Player, PlayerGroup, Race
from wouso.core.magic.models import Artifact, ArtifactGroup, Spell
-from wouso.core.qpool.models import Schedule, Question, Tag, Category
+from wouso.core.qpool.models import Schedule, Question, Tag, Category, Answer
from wouso.core.qpool import get_questions_with_category
from wouso.core.god import God
from wouso.core import scoring
from wouso.interface.activity.signals import addActivity
from wouso.interface.cpanel.models import Customization, Switchboard, GamesSwitchboard
from wouso.interface.apps.qproposal import QUEST_GOLD, CHALLENGE_GOLD, QOTD_GOLD
from wouso.utils.import_questions import import_from_file
-from forms import QuestionForm, TagsForm, UserForm, SpellForm, AddTagForm
+from forms import QuestionForm, TagsForm, UserForm, SpellForm, AddTagForm, AnswerForm
from django.contrib.auth.models import User
@@ -233,7 +233,7 @@ def qpool_new(request, cat=None):
form = QuestionForm()
categs = [(c.name.capitalize(), c.name) for c in Category.objects.all()]
if request.method == "POST":
- question = QuestionForm(data=request.POST)
+ question = QuestionForm(data = request.POST)
if question.is_valid():
newq = question.save()
return redirect('qpool_home', cat=newq.category.name)
@@ -243,7 +243,27 @@ def qpool_new(request, cat=None):
return render_to_response('cpanel/qpool_new.html',
{'form': form,
'module': 'qpool',
- 'categs':categs},
+ 'categs': categs},
+ context_instance=RequestContext(request)
+ )
+
+
+@permission_required('config.change_setting')
+def qpool_add_answer(request, id):
+ form = AnswerForm()
+ question = get_object_or_404(Question, pk=id)
+ if request.method == 'POST':
+ answer = AnswerForm(request.POST, instance=question)
+ if answer.is_valid():
+ answer.save(id=question)
+ return redirect('question_edit', id=question.id)
+ else:
+ form = answer
+
+ return render_to_response('cpanel/add_answer.html',
+ {'form': form,
+ 'question': question,
+ 'module': 'qpool'},
context_instance=RequestContext(request)
)
@@ -264,7 +284,7 @@ def qpool_edit(request, id=None):
if newq.endorsed_by is None:
newq.endorsed_by = request.user
newq.save()
- return HttpResponseRedirect(reverse('wouso.interface.cpanel.views.qpool_home', args = (newq.category.name,)))
+ return redirect('qpool_home', cat = newq.category.name)
else:
print "nevalid"
else:
@@ -343,6 +363,15 @@ def qpool_delete(request, id):
@permission_required('config.change_setting')
+def qpool_delete_answer(request, question_id, answer_id):
+ answer = get_object_or_404(Answer, pk=answer_id)
+
+ answer.delete()
+
+ return redirect('question_edit', id=question_id)
+
+
+@permission_required('config.change_setting')
def qotd_schedule(request):
Schedule.automatic()
return redirect('qpool_home', cat='qotd')
@@ -0,0 +1,42 @@
+{% extends 'cpanel/index.html' %}
+
+{% load user %}
+
+{% block sectiontitle %}Add Answer{% endblock %}
+
+{% block sectioncontent %}
+
+{% if form.errors %}
+ <p style="color: red;">
+ Please correct the error{{ form.errors|pluralize }} below.
+ </p>
+{% endif %}
+
+<form method="post" action="">
+
+ {{ form.errors }}
+
+<table>
+ <tr><td><label for="text">Text:</label></td>
+ <td><textarea id="text" name="text" rows="10" cols="50" readonly='readonly'>{{ question.text}}</textarea>
+ </tr>
+
+ </tr>
+ {% for a in question.answers %}
+ <tr><td><input disabled='disabled' type="checkbox" name="correct_{{a.id}}" {% if a.correct %}checked{% endif %}/></td>
+ <td><textarea name="answer_{{a.id}}" cols="50" readonly='readonly'>{{ a.text }}</textarea></td>
+ </tr>
+ {% endfor %}
+
+ <tr><td><input type="checkbox" name="new_answer_correct"/></td>
+ <td><textarea name="new_answer_text" cols="50"></textarea>
+ </tr>
+</table>
+
+<div class="actions">
+ <button onclick="history.back(); return false;">Cancel</button>
+ <button type="submit" class="default">Save</button>
+</div>
+{% csrf_token %}
+
+{% endblock %}
@@ -21,17 +21,17 @@
<tr><td><input type="checkbox" name="correct_{{a.id}}" {% if a.correct %}checked{% endif %}/></td>
<td><textarea name="answer_{{a.id}}" cols="50">{{ a.text }}</textarea>
{% if question %}
- <a href="{% url admin:qpool_question_change question.id %}">x</a>
+ <a href="{% url answer_del question.id a.id %}">x</a>
{% endif %}
</td>
</tr>
{% endfor %}
- {% if question.category.name != 'proposed' %}
- <tr><td colspan="2"><label for="active">Active: </label>
- <input id="active" type="checkbox" name="active" {% if question.active %}checked{% endif %} /></td>
+ <tr><td><div class='action'>
+ <a class="button" href="{% url add_answer id=question.id %}">Add answer</a>
+ </div>
+ </td>
</tr>
- {% endif %}
{% if question.category.name == 'qotd' %}
<tr><td><label for="schedule">Schedule (dd.mm.yyyy):</label></td>

0 comments on commit 59e6c20

Please sign in to comment.