From 80dde3381e8d188f092c4c1ee9eb01758e0a46c1 Mon Sep 17 00:00:00 2001
From: Andrew <andrew.waterhouse@unibas.ch>
Date: Wed, 24 Jun 2015 17:54:19 +0200
Subject: [PATCH] Update data using ModelForm

---
 LigandWorld/annotator/models.py              | 16 ++++++++++++++++
 LigandWorld/annotator/templates/biounit.html | 13 +++++++++++++
 LigandWorld/annotator/views.py               |  9 +++++++--
 LigandWorld/static/css/tutorial.css          | 11 ++++++++++-
 4 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/LigandWorld/annotator/models.py b/LigandWorld/annotator/models.py
index fdba084..8bff1a6 100755
--- a/LigandWorld/annotator/models.py
+++ b/LigandWorld/annotator/models.py
@@ -1,5 +1,7 @@
 from django.db import models
 
+from django import forms
+from django.forms import ModelForm 
 # Create your models here.
 
 class Biounit(models.Model):
@@ -35,3 +37,17 @@ class Ligand(models.Model):
 									('natural','Natural, non covalently bound'),
 									('synthetic','Synthetic, non covalently bound'))
                    				 )
+
+class BiounitForm(ModelForm):
+	class Meta:
+		model = Biounit
+		fields = ['title','resolution']
+
+ 	def clean_resolution(self):
+		res = self.cleaned_data['resolution']
+		if res<1 or res>3:
+			raise forms.ValidationError("Resolution must be between 1 and 3!")
+
+		# Always return the cleaned data, whether you have changed it or
+		# not.
+		return res
\ No newline at end of file
diff --git a/LigandWorld/annotator/templates/biounit.html b/LigandWorld/annotator/templates/biounit.html
index 4be9c81..6a8b4c2 100755
--- a/LigandWorld/annotator/templates/biounit.html
+++ b/LigandWorld/annotator/templates/biounit.html
@@ -10,5 +10,18 @@
 
 <img src="http://swissmodel.expasy.org/templates/{{ biounit.smtl_id }}_s500.png">
 
+<div class="panel panel-primary">
+  <div class="panel-heading">
+    <h3 class="panel-title">Edit Title and Resolution</h3>
+  </div>
+  <div class="panel-body">
+	<form action="" method="post">
+		{% csrf_token %}
+		{{ form }}
+		<button type="submit" class="btn btn-primary">Submit</button>
+	</form>
+  </div>
+</div>
+
 
 {% endblock %}
\ No newline at end of file
diff --git a/LigandWorld/annotator/views.py b/LigandWorld/annotator/views.py
index 1371ca6..8475206 100755
--- a/LigandWorld/annotator/views.py
+++ b/LigandWorld/annotator/views.py
@@ -1,6 +1,6 @@
 from django.shortcuts import render
 
-from models import Biounit, Ligand
+from models import Biounit, Ligand, BiounitForm
 
 # Create your views here.
 
@@ -15,4 +15,9 @@ def single_biounit(request, smtl_id):
 
 	biounit = Biounit.objects.get(smtl_id=smtl_id)
 
-	return render(request, 'biounit.html', {'biounit':biounit})
+	form = BiounitForm(request.POST or None, instance=biounit)
+
+	if form.is_valid():
+		form.save()
+
+	return render(request, 'biounit.html', {'biounit':biounit, 'form':form})
diff --git a/LigandWorld/static/css/tutorial.css b/LigandWorld/static/css/tutorial.css
index ca33745..cae6352 100755
--- a/LigandWorld/static/css/tutorial.css
+++ b/LigandWorld/static/css/tutorial.css
@@ -6,4 +6,13 @@ body,html{
 	background-color:  #0c4b33;padding:16px;
 	color:white;
 }
-img { max-width:100%;}
\ No newline at end of file
+img { max-width:100%;}
+
+form label{
+	display:block;
+}
+
+.errorlist{
+	color:red;
+	font-weight:bold;
+}
\ No newline at end of file
-- 
GitLab