From a8eeff9ff8e27039572f8655e5a31f3b8db6db61 Mon Sep 17 00:00:00 2001
From: jpic <jamespic@gmail.com>
Date: Mon, 28 Dec 2015 08:49:47 +0100
Subject: [PATCH] Bugfix: Position form would only work in best case

---
 memopol/views.py                               |  3 ++-
 representatives_positions/forms.py             |  5 +++--
 .../representatives_positions/_form.haml       | 12 ++++++++++++
 .../position_form.haml                         |  7 +++++++
 representatives_positions/views.py             |  2 +-
 .../representatives/representative_detail.haml | 18 ++----------------
 6 files changed, 27 insertions(+), 20 deletions(-)
 create mode 100644 representatives_positions/templates/representatives_positions/_form.haml
 create mode 100644 representatives_positions/templates/representatives_positions/position_form.haml

diff --git a/memopol/views.py b/memopol/views.py
index 1de22214..952ec17b 100644
--- a/memopol/views.py
+++ b/memopol/views.py
@@ -28,7 +28,8 @@ class RepresentativeDetail(representatives_views.RepresentativeDetail):
 
     def get_context_data(self, **kwargs):
         c = super(RepresentativeDetail, self).get_context_data(**kwargs)
-        c['position_form'] = PositionForm()
+        c['position_form'] = PositionForm(
+            initial={'representative': self.object.pk})
         self.add_representative_country_and_main_mandate(c['object'])
         return c
 
diff --git a/representatives_positions/forms.py b/representatives_positions/forms.py
index 2e4bf9fe..89256d55 100644
--- a/representatives_positions/forms.py
+++ b/representatives_positions/forms.py
@@ -8,7 +8,7 @@ from .models import Position
 class PositionForm(forms.ModelForm):
     class Meta:
         model = Position
-        fields = ['tags', 'datetime', 'text', 'link']
+        fields = ['tags', 'datetime', 'text', 'link', 'representative']
         widgets = {
             # Use localization and bootstrap 3
             'datetime': DateWidget(
@@ -17,5 +17,6 @@ class PositionForm(forms.ModelForm):
                 },
                 usel10n=True,
                 bootstrap_version=3,
-            )
+            ),
+            'representative': forms.HiddenInput
         }
diff --git a/representatives_positions/templates/representatives_positions/_form.haml b/representatives_positions/templates/representatives_positions/_form.haml
new file mode 100644
index 00000000..2c11c8e3
--- /dev/null
+++ b/representatives_positions/templates/representatives_positions/_form.haml
@@ -0,0 +1,12 @@
+- load bootstrap3
+
+%form{:action => '{% url "representatives_positions:position-create" %}',
+      :method => 'post'}
+  - csrf_token
+
+  - bootstrap_form form
+
+  - buttons
+    %button{'type': 'submit', 'class': 'btn btn-primary'}
+      {% bootstrap_icon "star" %} Submit
+  - endbuttons
diff --git a/representatives_positions/templates/representatives_positions/position_form.haml b/representatives_positions/templates/representatives_positions/position_form.haml
new file mode 100644
index 00000000..deb4b78d
--- /dev/null
+++ b/representatives_positions/templates/representatives_positions/position_form.haml
@@ -0,0 +1,7 @@
+- extends 'base.html'
+
+- block head
+  {{ form.media }}
+
+- block content
+  - include 'representatives_positions/_form.html'
diff --git a/representatives_positions/views.py b/representatives_positions/views.py
index 69d74477..3b24ef11 100644
--- a/representatives_positions/views.py
+++ b/representatives_positions/views.py
@@ -10,7 +10,7 @@ from .forms import PositionForm
 
 class PositionCreate(generic.CreateView):
     model = Position
-    fields = PositionForm.Meta.fields + ['representative']
+    form_class = PositionForm
 
     def get_success_url(self):
         return self.object.representative.get_absolute_url()
diff --git a/templates/representatives/representative_detail.haml b/templates/representatives/representative_detail.haml
index 6c98c187..96f7c0e4 100644
--- a/templates/representatives/representative_detail.haml
+++ b/templates/representatives/representative_detail.haml
@@ -1,12 +1,9 @@
--# List representatives
-
 - extends 'base.html'
 
 - load humanize
 - load representatives_tags
 - load representatives_votes_tags
 - load representatives_recommendations_tags
-- load bootstrap3
 
 - block head
   {{ position_form.media }}
@@ -15,6 +12,7 @@
   .row
     .col-md-6
       - include 'representatives/_representative_block.html' with representative=representative
+
     .col-md-6
       %h2#votes Votes
 
@@ -72,16 +70,4 @@
             %a{:href => '{{position.link}}'}
               = position.link
 
-    %form{:action => '{% url "representatives_positions:position-create" %}',
-          :method => 'post'}
-      - csrf_token
-      %input{:type => 'hidden',
-             :name => 'representative',
-             :value => '{{ representative.pk }}'}
-
-      - bootstrap_form position_form
-
-      - buttons
-        %button{'type': 'submit', 'class': 'btn btn-primary'}
-          {% bootstrap_icon "star" %} Submit
-      - endbuttons
+    - include 'representatives_positions/_form.html' with form=position_form
-- 
GitLab