src/hp/forms.py
author ymh <ymh.work@gmail.com>
Wed, 28 Nov 2012 02:12:51 +0100
changeset 95 c63f2508c34d
parent 91 eb2aa2a469e2
permissions -rw-r--r--
improve on kc rel form to avoid having all the contents in the drop down list, only those available.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
'''
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
Created on Nov 14, 2012
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
@author: ymh
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
'''
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
45
316a0101512e add pagination new ldt version
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
     8
from . import settings
41
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
from .models import VideoKCRel
45
316a0101512e add pagination new ldt version
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
    10
from .utils import get_all_objects
316a0101512e add pagination new ldt version
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
    11
from django import forms
316a0101512e add pagination new ldt version
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
    12
import logging
91
eb2aa2a469e2 add content title to relations
ymh <ymh.work@gmail.com>
parents: 83
diff changeset
    13
import requests
45
316a0101512e add pagination new ldt version
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
    14
316a0101512e add pagination new ldt version
ymh <ymh.work@gmail.com>
parents: 41
diff changeset
    15
logger = logging.getLogger(__name__)
41
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
class VideoKCRelForm(forms.ModelForm):
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    
65
6289931858a7 add a project property to override the front project.
ymh <ymh.work@gmail.com>
parents: 45
diff changeset
    19
    iri_id = forms.CharField(max_length=255, widget=forms.widgets.Select())
91
eb2aa2a469e2 add content title to relations
ymh <ymh.work@gmail.com>
parents: 83
diff changeset
    20
    project_id =  forms.CharField(max_length=255, widget=forms.widgets.Select())    
41
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    def __init__(self, *args, **kwargs):
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
        super(VideoKCRelForm, self).__init__(*args, **kwargs)
95
c63f2508c34d improve on kc rel form to avoid having all the contents in the drop down list, only those available.
ymh <ymh.work@gmail.com>
parents: 91
diff changeset
    24
                
83
07c9aa7de765 update ldt + add search on videos
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    25
        url = settings.LDT_API_URL + "contents/"
07c9aa7de765 update ldt + add search on videos
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    26
        contents = get_all_objects(url, {'limit':settings.LDT_MAX_FETCH}, 'front_project')
95
c63f2508c34d improve on kc rel form to avoid having all the contents in the drop down list, only those available.
ymh <ymh.work@gmail.com>
parents: 91
diff changeset
    27
        
c63f2508c34d improve on kc rel form to avoid having all the contents in the drop down list, only those available.
ymh <ymh.work@gmail.com>
parents: 91
diff changeset
    28
        #remove contents that already are associated        
c63f2508c34d improve on kc rel form to avoid having all the contents in the drop down list, only those available.
ymh <ymh.work@gmail.com>
parents: 91
diff changeset
    29
        associated_rel = dict([(rel.iri_id, rel) for rel in VideoKCRel.objects.all()])
c63f2508c34d improve on kc rel form to avoid having all the contents in the drop down list, only those available.
ymh <ymh.work@gmail.com>
parents: 91
diff changeset
    30
        
c63f2508c34d improve on kc rel form to avoid having all the contents in the drop down list, only those available.
ymh <ymh.work@gmail.com>
parents: 91
diff changeset
    31
        self.fields['iri_id'].widget.choices = [(content['iri_id'], content['title']) for content in contents if (content['iri_id'] not in associated_rel) or (content['iri_id'] == self['iri_id'].value())]
41
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        
83
07c9aa7de765 update ldt + add search on videos
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    33
        url = settings.LDT_API_URL + "projects/"
07c9aa7de765 update ldt + add search on videos
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    34
        projects = get_all_objects(url, {'state': 2, 'limit': settings.LDT_MAX_FETCH}, None)
65
6289931858a7 add a project property to override the front project.
ymh <ymh.work@gmail.com>
parents: 45
diff changeset
    35
        values = [(None, "-----")]
6289931858a7 add a project property to override the front project.
ymh <ymh.work@gmail.com>
parents: 45
diff changeset
    36
        values.extend([(project['ldt_id'], project['title']) for project in projects])
6289931858a7 add a project property to override the front project.
ymh <ymh.work@gmail.com>
parents: 45
diff changeset
    37
        
6289931858a7 add a project property to override the front project.
ymh <ymh.work@gmail.com>
parents: 45
diff changeset
    38
        self.fields['project_id'].widget.choices = values
91
eb2aa2a469e2 add content title to relations
ymh <ymh.work@gmail.com>
parents: 83
diff changeset
    39
        
eb2aa2a469e2 add content title to relations
ymh <ymh.work@gmail.com>
parents: 83
diff changeset
    40
        self.fields['content_title'].widget.attrs['readonly'] = True
65
6289931858a7 add a project property to override the front project.
ymh <ymh.work@gmail.com>
parents: 45
diff changeset
    41
91
eb2aa2a469e2 add content title to relations
ymh <ymh.work@gmail.com>
parents: 83
diff changeset
    42
    def clean_content_title(self):
eb2aa2a469e2 add content title to relations
ymh <ymh.work@gmail.com>
parents: 83
diff changeset
    43
        iri_id = self.cleaned_data['iri_id']
eb2aa2a469e2 add content title to relations
ymh <ymh.work@gmail.com>
parents: 83
diff changeset
    44
        url = settings.LDT_API_URL + "contents/%s/"  % iri_id        
eb2aa2a469e2 add content title to relations
ymh <ymh.work@gmail.com>
parents: 83
diff changeset
    45
        
eb2aa2a469e2 add content title to relations
ymh <ymh.work@gmail.com>
parents: 83
diff changeset
    46
        r = requests.get(url)
eb2aa2a469e2 add content title to relations
ymh <ymh.work@gmail.com>
parents: 83
diff changeset
    47
        if r.status_code != requests.codes.ok: #@UndefinedVariable
eb2aa2a469e2 add content title to relations
ymh <ymh.work@gmail.com>
parents: 83
diff changeset
    48
            logger.error("Error when requesting contents " + repr(r.status_code) + " : " + repr(r.text))
eb2aa2a469e2 add content title to relations
ymh <ymh.work@gmail.com>
parents: 83
diff changeset
    49
            raise forms.ValidationError("Unable to request content %s" % url)
eb2aa2a469e2 add content title to relations
ymh <ymh.work@gmail.com>
parents: 83
diff changeset
    50
        return r.json.get('title', None)
eb2aa2a469e2 add content title to relations
ymh <ymh.work@gmail.com>
parents: 83
diff changeset
    51
        
65
6289931858a7 add a project property to override the front project.
ymh <ymh.work@gmail.com>
parents: 45
diff changeset
    52
41
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    class Meta:
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        model = VideoKCRel
b6010b3d6ea8 add proxy, model, and correct embed_player
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55