Commits

Sergiy shchypa committed c41117e

add create poll from poll (not admin interface)

Comments (0)

Files changed (3)

cmsplugin_poll/templates/cmsplugin_poll/create.html

+{% load i18n poll_tags %}
+
+<style>
+  .cmsplugin_poll
+  {
+    padding: 5px;
+    border: 1px solid grey;
+    border-radius: 5px;
+    background-color: white;
+    position: relative;
+  }
+
+  .cmsplugin_poll .messages .error
+  {
+    color: red;
+  }
+
+  .cmsplugin_poll .info
+  {
+    font-size: 75%;
+    color: grey;
+  }
+
+  .cmsplugin_poll ul
+  {
+    list-style-position: inside;
+    padding-left: 0.5em;
+  }
+</style>
+
+<div class="cmsplugin_poll">
+  <form method="POST" action="">
+  {% csrf_token %}
+      <table>
+      {{ poll.as_table }}
+      {{ formset.as_table }}
+<tr>
+        <th></th>
+        <td><input type="submit" name="submit" value="Submit" class="button"></td>
+      </tr>
+    </table>
+  <!--
+  {% for field in  poll %}
+  {{field.error}}
+    <label>{{field.label}}</label>:{{field}}<br/>
+  {% endfor %}
+
+{{ formset.non_form_errors.as_ul }}
+<table id="formset" class="form">
+{% for form in formset.forms %}
+  {% if forloop.first %}
+  <thead><tr>
+    {% for field in form.visible_fields %}
+    <th>{{ field.label|capfirst }}</th>
+    {% endfor %}
+  </tr></thead>
+  {% endif %}
+  <tr class="{% cycle row1,row2 %}">
+  {% for field in form.visible_fields %}
+    <td>
+    {# Include the hidden fields in the form #}
+    {% if forloop.first %}
+      {% for hidden in form.hidden_fields %}
+      {{ hidden }}
+      {% endfor %}
+    {% endif %}
+      {{ field.errors.as_ul }}
+      {{ field }}
+    </td>
+  {% endfor %}
+  </tr>
+{% endfor %}
+</table>
+<input type='submit' value="Sublmit"/>
+
+-->
+</form>
+</div>

cmsplugin_poll/urls.py

     (r'^(?P<poll_id>\d+)/results/$', 'results'),
     (r'^(?P<poll_id>\d+)/results/json/$', 'results_json'),
     (r'^(?P<poll_id>\d+)/vote/$', 'vote'),
+    (r'^create/$', 'create'),
 )

cmsplugin_poll/views.py

 from django.utils import simplejson
 from django.http import HttpResponse
 from django.core import serializers
+from django.forms import ModelForm
 
 
 def index(request):
     return render_to_response('cmsplugin_poll/latest_polls.html', c)
 
 
+class UserSubmittedPollForm(ModelForm):
+    class Meta:
+        model = Poll
+
+
+from django.forms.models import inlineformset_factory
+# from datetime import date
+
+ChoicesFormSet = inlineformset_factory(Poll, Choice, extra=2)
+
+
+def create(request):
+    if request.method == 'POST':  # If the form has been submitted...
+        # form = ChoicesFormSet(request.POST) # A form bound to the POST data
+
+        form = UserSubmittedPollForm(request.POST)
+        if form.is_valid():
+            print 123
+            poll = form.save(commit=False)
+            choices_formset = ChoicesFormSet(request.POST, instance=poll)
+            if choices_formset.is_valid():
+                poll.save()
+                choices_formset.save()
+                return HttpResponseRedirect(reverse('cmsplugin_poll.views.results', args=(poll.id,)))
+        else:
+            choices_formset = ChoicesFormSet(instance=Poll())
+
+    else:
+        # poll = Poll(question=_('Enter your quiestion here'))
+        form = UserSubmittedPollForm()
+        choices_formset = ChoicesFormSet(instance=Poll())
+
+    c = RequestContext(request, {'formset': choices_formset, 'poll': form})
+    return render_to_response('cmsplugin_poll/create.html', c)
+
+
 def detail(request, poll_id):
     poll = get_object_or_404(Poll, pk=poll_id)
     c = RequestContext(request, {'poll': poll})