From c537ae6f06622adeec794d4fbf9c36ee4a07b572 Mon Sep 17 00:00:00 2001
From: Rachel TORCHET <racheltorchet@users.noreply.github.com>
Date: Mon, 6 Nov 2017 14:45:14 +0100
Subject: [PATCH] Update views.py

Former-commit-id: f4067dda556067ae1ac78af600b05263ce9f9030
---
 ippisite/ippidb/views.py | 41 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)

diff --git a/ippisite/ippidb/views.py b/ippisite/ippidb/views.py
index 668943c0..b4d4983b 100644
--- a/ippisite/ippidb/views.py
+++ b/ippisite/ippidb/views.py
@@ -89,7 +89,46 @@ class IppiWizard(NamedUrlSessionWizardView):
             initial['pdb_id'] = self.storage.get_step_data('PDBForm').get('PDBForm-pdb_id')
 
         return self.initial_dict.get(step, initial)
-  
+    
+    def process_step(self, form):
+        """
+        This method overrides the one used to postprocess the form data.
+        The added code just sets the form model for use in later forms
+        when appropriate
+        """
+        data = super(IppiWizard, self).process_step(form).copy()
+        if self.steps.current == 'Bibliography':
+            form.instance.autofill()
+        if self.steps.current == 'Bibliography':
+            form.instance.save()
+            data['pk'] = form.instance.id
+        if self.steps.current == 'PDBForm':
+            pdb_ids = [form['pdb_id'] for form in form.cleaned_data]
+            uniprot_ids = []
+            protein_ids = []
+            for pdb_id in pdb_ids:
+                uniprot_ids += get_pdb_uniprot_mapping(pdb_id)
+            for uniprot_id in uniprot_ids:
+                try:
+                    p = Protein.objects.get(uniprot_id=uniprot_id)
+                except Protein.DoesNotExist:
+                    p = Protein()
+                    p.uniprot_id = uniprot_ids[0]
+                    p.autofill()
+                    p.save()
+                protein_ids.append(p.id)
+            data['pks'] = protein_ids
+        return data
+        
+    def get_form_instance(self, step):
+        if self.steps.current == 'PDBForm':
+            pk = self.storage.get_step_data('BibliographyForm').get('pk')
+            return Bibliography.objects.get(pk=pk)
+        if self.steps.current == 'ProteinForm':
+            pks = self.storage.get_step_data('PDBForm').get('pks')
+            print(Protein.objects.filter(id__in=pks))
+            return Protein.objects.filter(id__in=pks)
+
     def get_template_names(self):
         return [TEMPLATES[self.steps.current]]
 
-- 
GitLab