use pdbid to load pdb and set example input
[django_unres.git] / django_simple / todo / forms.py
index 7ca4ce3..c8d2671 100644 (file)
@@ -5,6 +5,8 @@ from .models import MD_START
 from .models import MD_LANG
 from .models import FF_CHOICE
 import json
+import urllib
+
 
 class MultiWidgetBasic(forms.MultiWidget):
     def __init__(self, count, attrs=None):
@@ -43,9 +45,28 @@ class TaskForm(forms.Form):
 
 class TaskForm_min(forms.Form):
      name = forms.CharField(max_length=20)
-     file1 = forms.FileField(label='Upload a PDB file',
-     help_text='continuous (without breaks) protein chains,use TER to divide chains')
-                        
+     file1 = forms.FileField(label='Upload a PDB file',required=False,
+      help_text='continuous (without breaks) protein chains,use TER to divide chains')
+     pdbid = forms.CharField(min_length=4,max_length=4,required=False,
+      widget=forms.TextInput(attrs={'size':4, 'maxlength':4, 'title':'PDB code'}),
+      label='or PDB code')                   
+
+     def clean(self):
+             cleaned_data = super(TaskForm_min, self).clean()
+
+             pdbid = cleaned_data.get("pdbid") 
+             file1 = cleaned_data.get("file1")
+              
+             if not pdbid and not file1:
+                msg = 'provide pdb file or pdb code'
+                self.add_error('file1', msg)
+              
+             if pdbid:
+               test=urllib.urlopen('http://files.rcsb.org/download/'+pdbid+'.pdb')     
+               if test.code != 200:
+                 msg = 'wrong pdb code'
+                 self.add_error('pdbid', msg)
+               test.close()
             
 class TaskForm_min_a(forms.Form):
      name = forms.CharField(max_length=20)