| author | ymh <ymh.work@gmail.com> |
| Wed, 28 Nov 2012 02:12:51 +0100 | |
| changeset 95 | c63f2508c34d |
| parent 91 | eb2aa2a469e2 |
| permissions | -rw-r--r-- |
|
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 | 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 | 10 |
from .utils import get_all_objects |
11 |
from django import forms |
|
12 |
import logging |
|
| 91 | 13 |
import requests |
| 45 | 14 |
|
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 | 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 | 25 |
url = settings.LDT_API_URL + "contents/" |
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 | 33 |
url = settings.LDT_API_URL + "projects/" |
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 | 39 |
|
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 | 42 |
def clean_content_title(self): |
43 |
iri_id = self.cleaned_data['iri_id'] |
|
44 |
url = settings.LDT_API_URL + "contents/%s/" % iri_id |
|
45 |
||
46 |
r = requests.get(url) |
|
47 |
if r.status_code != requests.codes.ok: #@UndefinedVariable |
|
48 |
logger.error("Error when requesting contents " + repr(r.status_code) + " : " + repr(r.text)) |
|
49 |
raise forms.ValidationError("Unable to request content %s" % url) |
|
50 |
return r.json.get('title', None) |
|
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 |