src/hp/forms.py
changeset 91 eb2aa2a469e2
parent 83 07c9aa7de765
child 95 c63f2508c34d
--- a/src/hp/forms.py	Mon Nov 26 23:58:04 2012 +0100
+++ b/src/hp/forms.py	Tue Nov 27 01:48:15 2012 +0100
@@ -10,13 +10,14 @@
 from .utils import get_all_objects
 from django import forms
 import logging
+import requests
 
 logger = logging.getLogger(__name__)
 
 class VideoKCRelForm(forms.ModelForm):
     
     iri_id = forms.CharField(max_length=255, widget=forms.widgets.Select())
-    project_id =  forms.CharField(max_length=255, widget=forms.widgets.Select())
+    project_id =  forms.CharField(max_length=255, widget=forms.widgets.Select())    
 
     def __init__(self, *args, **kwargs):
         super(VideoKCRelForm, self).__init__(*args, **kwargs)
@@ -32,7 +33,19 @@
         values.extend([(project['ldt_id'], project['title']) for project in projects])
         
         self.fields['project_id'].widget.choices = values
+        
+        self.fields['content_title'].widget.attrs['readonly'] = True
 
+    def clean_content_title(self):
+        iri_id = self.cleaned_data['iri_id']
+        url = settings.LDT_API_URL + "contents/%s/"  % iri_id        
+        
+        r = requests.get(url)
+        if r.status_code != requests.codes.ok: #@UndefinedVariable
+            logger.error("Error when requesting contents " + repr(r.status_code) + " : " + repr(r.text))
+            raise forms.ValidationError("Unable to request content %s" % url)
+        return r.json.get('title', None)
+        
 
     class Meta:
         model = VideoKCRel