2021-09-28 13:10:22 +00:00
|
|
|
import logging
|
2021-10-10 15:06:43 +00:00
|
|
|
import json
|
2021-09-28 13:10:22 +00:00
|
|
|
|
2021-10-10 15:06:43 +00:00
|
|
|
from django.conf import settings
|
2021-09-28 13:10:22 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.db import models
|
2021-10-23 10:11:42 +00:00
|
|
|
import ox
|
2021-09-28 13:10:22 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
User = get_user_model()
|
|
|
|
|
2021-10-23 10:11:42 +00:00
|
|
|
DEFAULT_LAYER = 'descriptions'
|
|
|
|
|
2021-09-28 13:10:22 +00:00
|
|
|
class Page(models.Model):
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
modified = models.DateTimeField(auto_now=True)
|
|
|
|
|
2021-10-11 12:55:45 +00:00
|
|
|
slug = models.SlugField(blank=True, unique=True)
|
2021-09-28 13:10:22 +00:00
|
|
|
public = models.BooleanField(default=False)
|
|
|
|
|
2021-10-11 12:26:51 +00:00
|
|
|
title = models.TextField(blank=True)
|
|
|
|
body = models.TextField(blank=True)
|
|
|
|
data = models.JSONField(default=dict, blank=True)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '%s (%s)' % (self.title, self.slug)
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
if self.slug:
|
|
|
|
return '/' + settings.URL_PREFIX + '' + self.slug
|
|
|
|
else:
|
|
|
|
return '/' + settings.URL_PREFIX[:-1]
|
|
|
|
|
2021-09-28 13:10:22 +00:00
|
|
|
|
2021-10-28 10:12:34 +00:00
|
|
|
LANGUAGE_CHOICES = (
|
|
|
|
('en', 'EN'),
|
|
|
|
('zh', 'ZH'),
|
|
|
|
)
|
|
|
|
|
2021-10-10 15:06:43 +00:00
|
|
|
class Text(models.Model):
|
2021-09-28 13:10:22 +00:00
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
modified = models.DateTimeField(auto_now=True)
|
2021-10-28 10:12:34 +00:00
|
|
|
language = models.CharField(choices=LANGUAGE_CHOICES, max_length=8, default='en')
|
2021-09-28 13:10:22 +00:00
|
|
|
slug = models.SlugField()
|
|
|
|
public = models.BooleanField(default=False)
|
2021-10-11 12:55:45 +00:00
|
|
|
position = models.IntegerField(default=0)
|
2021-10-28 10:12:34 +00:00
|
|
|
|
2021-10-10 15:06:43 +00:00
|
|
|
title = models.TextField()
|
2021-10-11 12:55:45 +00:00
|
|
|
byline = models.TextField(default="", blank=True)
|
2021-10-10 15:06:43 +00:00
|
|
|
body = models.TextField(default="", blank=True)
|
|
|
|
|
2021-09-28 13:10:22 +00:00
|
|
|
data = models.JSONField(default=dict)
|
2021-10-23 10:11:42 +00:00
|
|
|
annotations = models.JSONField(default=dict, blank=True, editable=False)
|
2021-09-28 13:10:22 +00:00
|
|
|
|
2021-10-10 15:06:43 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
return '/' + settings.URL_PREFIX + 'assemblies/' + self.slug
|
|
|
|
|
2021-10-23 10:11:42 +00:00
|
|
|
def get_annotations(self):
|
|
|
|
api = ox.api.signin(settings.DEFAULT_PANDORA_API)
|
|
|
|
if 'item' in self.data:
|
|
|
|
return self.get_item_annotations(api)
|
|
|
|
elif 'edit' in self.data:
|
|
|
|
return self.get_edit_annotations(api)
|
|
|
|
else:
|
2021-10-23 14:36:13 +00:00
|
|
|
return []
|
2021-10-23 10:11:42 +00:00
|
|
|
|
|
|
|
def get_item_annotations(self, api):
|
|
|
|
query = {
|
|
|
|
'id': self.data['item'],
|
|
|
|
'keys': [
|
|
|
|
'layers'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
layer = self.data.get('layer', None) or DEFAULT_LAYER
|
|
|
|
annotations = api.get(**query)['data']['layers'][layer]
|
|
|
|
user = self.data.get('user', None)
|
|
|
|
if user:
|
|
|
|
annotations = list(filter(lambda annot: annot['user'] == user, annotations))
|
2021-10-29 14:04:34 +00:00
|
|
|
if self.data.get('only_e'):
|
|
|
|
annotations = [e for e in annotations if e['value'].startswith('E:')]
|
|
|
|
for e in annotations:
|
|
|
|
e['value'] = e['value'][2:].strip()
|
|
|
|
else:
|
|
|
|
annotations = [e for e in annotations if not e['value'].startswith('E:')]
|
2021-10-23 10:11:42 +00:00
|
|
|
return self.get_clips(api, annotations)
|
|
|
|
|
|
|
|
def get_edit_annotations(self, api):
|
|
|
|
query = {
|
|
|
|
'id': self.data['edit'],
|
|
|
|
'keys': []
|
|
|
|
}
|
2021-10-29 13:45:21 +00:00
|
|
|
r = api.getEdit(**query)
|
|
|
|
if 'data' not in r:
|
|
|
|
return []
|
|
|
|
clips = r['data']['clips']
|
2021-10-23 10:11:42 +00:00
|
|
|
annotations = []
|
2021-11-01 10:44:09 +00:00
|
|
|
cited = {}
|
2021-10-23 10:11:42 +00:00
|
|
|
layer = self.data.get('layer', None) or DEFAULT_LAYER
|
|
|
|
for clip in clips:
|
2021-11-01 10:44:09 +00:00
|
|
|
item_id = clip['item']
|
|
|
|
cited[item_id] = {
|
|
|
|
'title': clip['title'],
|
|
|
|
'director': clip['director'],
|
|
|
|
'id': item_id,
|
|
|
|
}
|
2021-10-23 10:11:42 +00:00
|
|
|
for annotation in clip['layers'][layer]:
|
|
|
|
if 'user' in self.data and annotation['user'] == self.data['user']:
|
|
|
|
continue
|
2021-11-01 10:44:09 +00:00
|
|
|
for key in ('title', 'director', 'date'):
|
|
|
|
if key in clip:
|
|
|
|
annotation[key] = clip[key]
|
2021-10-29 13:45:21 +00:00
|
|
|
if self.data.get('only_e'):
|
|
|
|
if annotation['value'].startswith('E:'):
|
|
|
|
annotation['value'] = annotation['value'][2:].strip()
|
|
|
|
annotations.append(annotation)
|
2021-10-29 14:04:34 +00:00
|
|
|
elif not annotation['value'].startswith('E:'):
|
2021-10-29 13:45:21 +00:00
|
|
|
annotations.append(annotation)
|
2021-11-01 10:44:09 +00:00
|
|
|
self.data['cited'] = list(cited.values())
|
2021-10-23 10:11:42 +00:00
|
|
|
return self.get_clips(api, annotations)
|
|
|
|
|
|
|
|
def get_clips(self, api, annotations):
|
2021-10-24 14:24:46 +00:00
|
|
|
item_ids = list({annot['id'].split('/')[0] for annot in annotations})
|
2021-10-23 10:11:42 +00:00
|
|
|
query = {
|
|
|
|
'itemsQuery': {
|
|
|
|
'conditions': [{
|
|
|
|
'key': 'id',
|
|
|
|
'operator': '&',
|
|
|
|
'value': item_ids
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
'range': [0, 10000],
|
|
|
|
'keys': [
|
|
|
|
'id',
|
|
|
|
'hue',
|
|
|
|
'saturation',
|
|
|
|
'lightness'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
items = api.findClips(**query)['data']['items']
|
|
|
|
colors = {}
|
|
|
|
for item in items:
|
|
|
|
colors[item['id']] = item
|
|
|
|
previous = None
|
|
|
|
for annot in annotations:
|
|
|
|
annot_id = annot['id'].split('/')[0]
|
|
|
|
annot_in = '{:.3f}'.format(annot['in'])
|
|
|
|
annot_out = '{:.3f}'.format(annot['out'])
|
|
|
|
clip_id = f'{annot_id}/{annot_in}-{annot_out}'
|
|
|
|
annot['color1'] = colors[clip_id]
|
|
|
|
if previous:
|
|
|
|
previous['color2'] = annot['color1']
|
|
|
|
previous = annot
|
|
|
|
if len(annotations) > 0:
|
|
|
|
annotations[len(annotations) - 1]['color2'] = annotations[0]['color1']
|
|
|
|
return annotations
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
annotations = self.get_annotations()
|
|
|
|
self.annotations = annotations
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2021-10-10 15:06:43 +00:00
|
|
|
def json(self):
|
2021-10-26 10:47:33 +00:00
|
|
|
from ..video.models import Film
|
2021-10-10 15:06:43 +00:00
|
|
|
data = {}
|
|
|
|
data['title'] = self.title
|
2021-10-11 13:50:19 +00:00
|
|
|
data['byline'] = self.byline
|
|
|
|
data['body'] = self.body
|
2021-11-01 10:44:09 +00:00
|
|
|
data['language'] = self.language
|
2021-10-26 10:47:33 +00:00
|
|
|
item_id = self.data.get('related')
|
|
|
|
if not item_id:
|
|
|
|
item_id = self.data.get('item')
|
|
|
|
if item_id:
|
|
|
|
item = Film.objects.filter(padma_id=item_id).first()
|
|
|
|
if item:
|
2021-10-26 11:09:16 +00:00
|
|
|
for key in ('title', 'title_zh', 'director'):
|
2021-10-26 10:47:33 +00:00
|
|
|
data['item_' + key] = item.data[key]
|
|
|
|
data['item_url'] = item.get_absolute_url()
|
2021-10-23 10:15:25 +00:00
|
|
|
if isinstance(self.annotations, list) and len(self.annotations) > 0:
|
|
|
|
data['annotations'] = self.annotations
|
2021-10-10 15:06:43 +00:00
|
|
|
data.update(self.data)
|
|
|
|
return json.dumps(data)
|