add space and other flake8 cleanups

This commit is contained in:
j 2016-05-28 11:14:34 +02:00
parent f21e8413fb
commit 225259e521

View file

@ -54,7 +54,7 @@ def get_id(info):
for value in info[key]: for value in info[key]:
q = q.filter(**{k: key, v: value}) q = q.filter(**{k: key, v: value})
else: else:
q = q.filter(**{k:key, v:info[key]}) q = q.filter(**{k: key, v: info[key]})
if q.count() == 1: if q.count() == 1:
return q[0].public_id return q[0].public_id
if settings.DATA_SERVICE: if settings.DATA_SERVICE:
@ -78,11 +78,9 @@ def get_item(info, user=None, async=False):
if filter(lambda k: k['id'] == 'year', settings.CONFIG['itemKeys']): if filter(lambda k: k['id'] == 'year', settings.CONFIG['itemKeys']):
item_data['year'] = info.get('year', '') or '' item_data['year'] = info.get('year', '') or ''
#add additional item metadata parsed from path # add additional item metadata parsed from path
for key in [i for i in info for key in [i for i in info if i in set([k['id'] for k in settings.CONFIG['itemKeys']])
if i in set([k['id'] for k in settings.CONFIG['itemKeys']]) \ and i not in ('language', ) and i not in item_data]:
and i not in ('language', ) \
and i not in item_data]:
item_data[key] = info[key] item_data[key] = info[key]
for key in ('episodeTitle', 'episodeDirector', 'episodeYear', for key in ('episodeTitle', 'episodeDirector', 'episodeYear',
@ -195,9 +193,9 @@ class Item(models.Model):
return self.external_data[key] return self.external_data[key]
item_key = utils.get_by_id(settings.CONFIG['itemKeys'], key) item_key = utils.get_by_id(settings.CONFIG['itemKeys'], key)
if item_key and 'value' in item_key \ if item_key and 'value' in item_key \
and isinstance(item_key['value'], dict) \ and isinstance(item_key['value'], dict) \
and item_key['value'].get('type') == 'map' \ and item_key['value'].get('type') == 'map' \
and self.get(item_key['value']['key']): and self.get(item_key['value']['key']):
value = re.compile(item_key['value']['map']).findall(self.get(item_key['value']['key'])) value = re.compile(item_key['value']['map']).findall(self.get(item_key['value']['key']))
return value[0] if value else default return value[0] if value else default
return default return default
@ -220,7 +218,7 @@ class Item(models.Model):
def editable(self, user): def editable(self, user):
if user.is_anonymous(): if user.is_anonymous():
return False return False
if user.profile.capability('canEditMetadata') == True or \ if user.profile.capability('canEditMetadata') or \
user.is_staff or \ user.is_staff or \
self.user == user or \ self.user == user or \
self.groups.filter(id__in=user.groups.all()).count() > 0: self.groups.filter(id__in=user.groups.all()).count() > 0:
@ -263,7 +261,7 @@ class Item(models.Model):
d.description = ox.sanitize_html(description) d.description = ox.sanitize_html(description)
d.save() d.save()
for key in data: for key in data:
if data[key] == None: if data[key] is None:
if key in self.data: if key in self.data:
del self.data[key] del self.data[key]
else: else:
@ -316,16 +314,16 @@ class Item(models.Model):
for t in c.keys(): for t in c.keys():
if c[t]: if c[t]:
if isinstance(c[t][0], basestring): if isinstance(c[t][0], basestring):
c[t]= [{'id': i, 'title': None} for i in c[t]] c[t] = [{'id': i, 'title': None} for i in c[t]]
ids = [i['id'] for i in c[t]] ids = [i['id'] for i in c[t]]
known = {} known = {}
for l in Item.objects.filter(public_id__in=ids): for l in Item.objects.filter(public_id__in=ids):
known[l.public_id] = l.get('title') known[l.public_id] = l.get('title')
for i in c[t]: for i in c[t]:
if i['id'] in known: if i['id'] in known:
i['item'] = i['id'] i['item'] = i['id']
i['title'] = known[i['id']] i['title'] = known[i['id']]
c[t]= filter(lambda x: x['title'], c[t]) c[t] = filter(lambda x: x['title'], c[t])
if not c[t]: if not c[t]:
del c[t] del c[t]
return c return c
@ -336,7 +334,7 @@ class Item(models.Model):
string = u'%s (%s)' % (ox.decode_html(self.get('title', 'Untitled')), self.get('year')) string = u'%s (%s)' % (ox.decode_html(self.get('title', 'Untitled')), self.get('year'))
else: else:
string = self.get('title', u'Untitled') string = self.get('title', u'Untitled')
return u'[%s] %s' % (self.public_id,string) return u'[%s] %s' % (self.public_id, string)
def get_absolute_url(self): def get_absolute_url(self):
return '/%s' % self.public_id return '/%s' % self.public_id
@ -508,7 +506,7 @@ class Item(models.Model):
'width': 640, 'width': 640,
'height': 1024, 'height': 1024,
'source': settings.URL, 'source': settings.URL,
'selected': url == None, 'selected': url is None,
'index': index.index(settings.URL) 'index': index.index(settings.URL)
}) })
@ -535,7 +533,7 @@ class Item(models.Model):
'index': p, 'index': p,
'position': f['position'], 'position': f['position'],
'selected': p == pos, 'selected': p == pos,
'url': '/%s/posterframe%d.jpg' %(self.public_id, p), 'url': '/%s/posterframe%d.jpg' % (self.public_id, p),
'height': f['height'], 'height': f['height'],
'width': f['width'] 'width': f['width']
}) })
@ -592,7 +590,6 @@ class Item(models.Model):
if not keys or 'posterRatio' in keys: if not keys or 'posterRatio' in keys:
i['posterRatio'] = self.poster_width / self.poster_height i['posterRatio'] = self.poster_width / self.poster_height
streams = self.streams() streams = self.streams()
i['durations'] = [s.duration for s in streams] i['durations'] = [s.duration for s in streams]
i['duration'] = sum(i['durations']) i['duration'] = sum(i['durations'])
@ -608,7 +605,7 @@ class Item(models.Model):
del i['videoRatio'] del i['videoRatio']
else: else:
i['duration'] = self.files.filter( i['duration'] = self.files.filter(
Q(selected=True)|Q(wanted=True) Q(selected=True) | Q(wanted=True)
).aggregate(Sum('duration'))['duration__sum'] ).aggregate(Sum('duration'))['duration__sum']
videos = self.files.filter(selected=True, is_video=True) videos = self.files.filter(selected=True, is_video=True)
if i['duration'] and videos.count(): if i['duration'] and videos.count():
@ -635,27 +632,26 @@ class Item(models.Model):
i['posterFrame'] = self.poster_frame i['posterFrame'] = self.poster_frame
dkeys = [k['id'] for k in dkeys = [k['id'] for k in
filter(lambda i: i.get('description'), settings.CONFIG['itemKeys'])] filter(lambda i: i.get('description'), settings.CONFIG['itemKeys'])]
if keys: if keys:
dkeys = filter(lambda k: k in keys, dkeys) dkeys = filter(lambda k: k in keys, dkeys)
for key in dkeys: for key in dkeys:
k = filter(lambda i: i['id'] == key, settings.CONFIG['itemKeys']) k = filter(lambda i: i['id'] == key, settings.CONFIG['itemKeys'])
if isinstance((k and k[0].get('type') or ''), list): if isinstance((k and k[0].get('type') or ''), list):
i['%sdescription'%key] = {} i['%sdescription' % key] = {}
if key == 'name': if key == 'name':
values = [] values = []
for ikey in filter(lambda i: i.get('sortType') == 'person', for ikey in filter(lambda i: i.get('sortType') == 'person', settings.CONFIG['itemKeys']):
settings.CONFIG['itemKeys']):
values += i.get(ikey['id'], []) values += i.get(ikey['id'], [])
values = list(set(values)) values = list(set(values))
else: else:
values = self.get(key) values = self.get(key)
if values: if values:
for d in Description.objects.filter(key=key, value__in=values): for d in Description.objects.filter(key=key, value__in=values):
i['%sdescription'%key][d.value] = d.description i['%sdescription' % key][d.value] = d.description
else: else:
qs = Description.objects.filter(key=key, value=self.get(key, '')) qs = Description.objects.filter(key=key, value=self.get(key, ''))
i['%sdescription'%key] = '' if qs.count() == 0 else qs[0].description i['%sdescription' % key] = '' if qs.count() == 0 else qs[0].description
if keys: if keys:
info = {} info = {}
for key in keys: for key in keys:
@ -766,7 +762,6 @@ class Item(models.Model):
titles += [a[0] for a in at] titles += [a[0] for a in at]
return titles return titles
with transaction.atomic(): with transaction.atomic():
for key in settings.CONFIG['itemKeys']: for key in settings.CONFIG['itemKeys']:
i = key['id'] i = key['id']
@ -818,7 +813,7 @@ class Item(models.Model):
isSeries = self.get('series', isSeries = self.get('series',
self.get('episodeTitle', self.get('episodeTitle',
self.get('episode', self.get('episode',
self.get('seriesTitle')))) != None self.get('seriesTitle')))) is not None
save('series', isSeries) save('series', isSeries)
def update_sort(self): def update_sort(self):
@ -901,7 +896,7 @@ class Item(models.Model):
s.clips = self.clips.count() s.clips = self.clips.count()
s.numberoffiles = self.files.all().count() s.numberoffiles = self.files.all().count()
videos = self.files.filter(selected=True).filter(Q(is_video=True)|Q(is_audio=True)) videos = self.files.filter(selected=True).filter(Q(is_video=True) | Q(is_audio=True))
if videos.count() > 0: if videos.count() > 0:
#s.duration = sum([v.duration for v in videos]) #s.duration = sum([v.duration for v in videos])
s.duration = sum([v.duration for v in self.streams()]) s.duration = sum([v.duration for v in self.streams()])
@ -1051,7 +1046,7 @@ class Item(models.Model):
def update_layer_facet(self, key): def update_layer_facet(self, key):
from entity.models import Entity from entity.models import Entity
current_values = [a['value'] current_values = [a['value']
for a in self.annotations.filter(layer=key).distinct().values('value')] for a in self.annotations.filter(layer=key).distinct().values('value')]
layer = utils.get_by_id(settings.CONFIG['layers'], key) layer = utils.get_by_id(settings.CONFIG['layers'], key)
if layer.get('type') == 'entity': if layer.get('type') == 'entity':
current_values = [a['name'] current_values = [a['name']
@ -1067,7 +1062,7 @@ class Item(models.Model):
if removed_values: if removed_values:
q = Q() q = Q()
for v in removed_values: for v in removed_values:
q |=Q(value__iexact=v) q |= Q(value__iexact=v)
Facet.objects.filter(item=self, key=key).filter(q).delete() Facet.objects.filter(item=self, key=key).filter(q).delete()
for value in current_values: for value in current_values:
@ -1112,7 +1107,7 @@ class Item(models.Model):
else: else:
height = min(height, stream.resolution) height = min(height, stream.resolution)
path = os.path.join(settings.MEDIA_ROOT, stream.path(), path = os.path.join(settings.MEDIA_ROOT, stream.path(),
'frames', "%dp"%height, "%s.jpg"%position) 'frames', "%dp" % height, "%s.jpg" % position)
if not os.path.exists(path) and stream.media: if not os.path.exists(path) and stream.media:
extract.frame(stream.media.path, path, position, height, info=stream.info) extract.frame(stream.media.path, path, position, height, info=stream.info)
if not os.path.exists(path): if not os.path.exists(path):
@ -1174,7 +1169,7 @@ class Item(models.Model):
def update_selected(self, update_timeline=True): def update_selected(self, update_timeline=True):
sets = self.sets() sets = self.sets()
for s in sets: for s in sets:
if s.filter(Q(is_video=True)|Q(is_audio=True)).filter(available=False).count() == 0: if s.filter(Q(is_video=True) | Q(is_audio=True)).filter(available=False).count() == 0:
update = False update = False
self.files.exclude(id__in=s).exclude(part=None).update(part=None) self.files.exclude(id__in=s).exclude(part=None).update(part=None)
deselect = self.files.filter(selected=True).exclude(id__in=s) deselect = self.files.filter(selected=True).exclude(id__in=s)
@ -1199,14 +1194,14 @@ class Item(models.Model):
if self.torrent: if self.torrent:
self.torrent.seek(0) self.torrent.seek(0)
data = ox.torrent.bdecode(self.torrent.read()) data = ox.torrent.bdecode(self.torrent.read())
url = request.build_absolute_uri("%s/torrent/"%self.get_absolute_url()) url = request.build_absolute_uri("%s/torrent/" % self.get_absolute_url())
if url.startswith('https://'): if url.startswith('https://'):
url = 'http' + url[5:] url = 'http' + url[5:]
data['url-list'] = ['%s%s' % (url, u.split('torrent/')[1]) for u in data['url-list']] data['url-list'] = ['%s%s' % (url, u.split('torrent/')[1]) for u in data['url-list']]
return ox.torrent.bencode(data) return ox.torrent.bencode(data)
def make_torrent(self): def make_torrent(self):
if settings.CONFIG['video'].get('torrent') == False: if not settings.CONFIG['video'].get('torrent'):
return return
streams = self.streams() streams = self.streams()
if streams.count() == 0: if streams.count() == 0:
@ -1228,7 +1223,7 @@ class Item(models.Model):
v = streams[0] v = streams[0]
media_path = v.media.path media_path = v.media.path
extension = media_path.split('.')[-1] extension = media_path.split('.')[-1]
url = "%s/torrent/%s.%s" % (self.get_absolute_url(), url = "%s/torrent/%s.%s" % (self.get_absolute_url(),
quote(filename.encode('utf-8')), quote(filename.encode('utf-8')),
extension) extension)
video = "%s.%s" % (base, extension) video = "%s.%s" % (base, extension)
@ -1241,7 +1236,7 @@ class Item(models.Model):
size = v.media.size size = v.media.size
duration = v.duration duration = v.duration
else: else:
url = "%s/torrent/" % self.get_absolute_url() url = "%s/torrent/" % self.get_absolute_url()
part = 1 part = 1
ox.makedirs(base) ox.makedirs(base)
for v in streams: for v in streams:
@ -1280,14 +1275,16 @@ class Item(models.Model):
self.save() self.save()
def audio_tracks(self): def audio_tracks(self):
tracks = [f['language'] for f in self.files.filter(selected=True).filter(Q(is_video=True)|Q(is_audio=True)).values('language') if f['language']] tracks = [f['language']
for f in self.files.filter(selected=True).filter(Q(is_video=True) | Q(is_audio=True)).values('language')
if f['language']]
return sorted(set(tracks)) return sorted(set(tracks))
def streams(self, track=None): def streams(self, track=None):
qs = archive.models.Stream.objects.filter( qs = archive.models.Stream.objects.filter(
source=None, available=True, file__item=self, file__selected=True source=None, available=True, file__item=self, file__selected=True
).filter( ).filter(
Q(file__is_audio=True)|Q(file__is_video=True) Q(file__is_audio=True) | Q(file__is_video=True)
) )
if not track: if not track:
tracks = self.audio_tracks() tracks = self.audio_tracks()
@ -1316,7 +1313,7 @@ class Item(models.Model):
n = streams.count() n = streams.count()
for s in streams: for s in streams:
self.data['volume'] += s.volume * s.duration self.data['volume'] += s.volume * s.duration
color = map(lambda a,b: (a+b)/n, color, ox.image.getRGB(s.color or [0.0] * 3)) color = map(lambda a, b: (a+b)/n, color, ox.image.getRGB(s.color or [0.0] * 3))
offset += s.duration offset += s.duration
self.data['hue'], self.data['saturation'], self.data['lightness'] = ox.image.getHSL(color) self.data['hue'], self.data['saturation'], self.data['lightness'] = ox.image.getHSL(color)
if offset: if offset:
@ -1376,7 +1373,7 @@ class Item(models.Model):
os.unlink(f) os.unlink(f)
def make_poster(self, force=False): def make_poster(self, force=False):
ox.makedirs(os.path.join(settings.MEDIA_ROOT,self.path())) ox.makedirs(os.path.join(settings.MEDIA_ROOT, self.path()))
if not self.poster or force: if not self.poster or force:
poster = self.make_siteposter() poster = self.make_siteposter()
url = self.prefered_poster_url() url = self.prefered_poster_url()
@ -1407,17 +1404,14 @@ class Item(models.Model):
if not os.path.exists(timeline) and os.path.exists(audio_timeline): if not os.path.exists(timeline) and os.path.exists(audio_timeline):
timeline = audio_timeline timeline = audio_timeline
cmd = [settings.ITEM_POSTER, cmd = [settings.ITEM_POSTER, '-d', '-', '-p', poster]
'-d', '-',
'-p', poster
]
data = self.json.copy() data = self.json.copy()
if frame: if frame:
data['frame'] = frame data['frame'] = frame
if os.path.exists(timeline): if os.path.exists(timeline):
data['timeline'] = timeline data['timeline'] = timeline
data['oxdbId'] = self.oxdbId or self.oxdb_id() or self.public_id data['oxdbId'] = self.oxdbId or self.oxdb_id() or self.public_id
ox.makedirs(os.path.join(settings.MEDIA_ROOT,self.path())) ox.makedirs(os.path.join(settings.MEDIA_ROOT, self.path()))
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, close_fds=True) p = subprocess.Popen(cmd, stdin=subprocess.PIPE, close_fds=True)
p.communicate(json.dumps(data, default=fields.to_json)) p.communicate(json.dumps(data, default=fields.to_json))
for f in glob(poster.replace('.jpg', '*.jpg')): for f in glob(poster.replace('.jpg', '*.jpg')):
@ -1488,13 +1482,11 @@ class Item(models.Model):
audio_timeline = '%stimelineaudio64p.jpg' % self.timeline_prefix audio_timeline = '%stimelineaudio64p.jpg' % self.timeline_prefix
if not os.path.exists(timeline) and os.path.exists(audio_timeline): if not os.path.exists(timeline) and os.path.exists(audio_timeline):
timeline = audio_timeline timeline = audio_timeline
cmd = [settings.ITEM_ICON, cmd = [settings.ITEM_ICON, '-i', self.icon.path]
'-i', self.icon.path
]
if os.path.exists(timeline): if os.path.exists(timeline):
cmd += ['-l', timeline] cmd += ['-l', timeline]
if frame: if frame:
cmd += ['-f', frame] cmd += ['-f', frame]
p = subprocess.Popen(cmd, close_fds=True) p = subprocess.Popen(cmd, close_fds=True)
p.wait() p.wait()
#remove cached versions #remove cached versions
@ -1565,7 +1557,7 @@ class Item(models.Model):
language = languages[0] language = languages[0]
#loop over all videos #loop over all videos
for f in self.files.filter(Q(is_audio=True)|Q(is_video=True)) \ for f in self.files.filter(Q(is_audio=True) | Q(is_video=True)) \
.filter(selected=True).order_by('sort_path'): .filter(selected=True).order_by('sort_path'):
subtitles_added = False subtitles_added = False
prefix = os.path.splitext(f.path)[0] prefix = os.path.splitext(f.path)[0]
@ -1588,15 +1580,15 @@ class Item(models.Model):
annotation = Annotation( annotation = Annotation(
item=self, item=self,
layer=layer, layer=layer,
start=float('%0.03f'%data['in']), start=float('%0.03f' % data['in']),
end=float('%0.03f'%data['out']), end=float('%0.03f' % data['out']),
value=value, value=value,
user=user user=user
) )
annotation.save() annotation.save()
#otherwise add empty 5 seconds annotation every minute #otherwise add empty 5 seconds annotation every minute
if not subtitles_added: if not subtitles_added:
start = offset and int (offset / 60) * 60 + 60 or 0 start = offset and int(offset / 60) * 60 + 60 or 0
for i in range(start, for i in range(start,
int(offset + f.duration) - 5, int(offset + f.duration) - 5,
60): 60):
@ -1644,16 +1636,16 @@ Item.facet_keys = []
Item.layer_facet_keys = [] Item.layer_facet_keys = []
Item.poster_keys = [] Item.poster_keys = []
for key in settings.CONFIG['itemKeys']: for key in settings.CONFIG['itemKeys']:
if 'autocomplete' in key and not 'autocompleteSortKey' in key or \ if 'autocomplete' in key and 'autocompleteSortKey' not in key or \
key.get('filter'): key.get('filter'):
Item.facet_keys.append(key['id']) Item.facet_keys.append(key['id'])
elif key.get('type') == 'layer' and \ elif key.get('type') == 'layer' and \
utils.get_by_id(settings.CONFIG['layers'], key['id']).get('type') == 'string': utils.get_by_id(settings.CONFIG['layers'], key['id']).get('type') == 'string':
Item.facet_keys.append(key['id']) Item.facet_keys.append(key['id'])
if key['id'] in ('title', 'director', 'year') or key.get('poster'): if key['id'] in ('title', 'director', 'year') or key.get('poster'):
Item.poster_keys.append(key['id']) Item.poster_keys.append(key['id'])
if key.get('type') == 'layer' and ( if key.get('type') == 'layer' and (
key.get('filter') or \ key.get('filter') or
utils.get_by_id(settings.CONFIG['layers'], key['id']).get('type') == 'string' utils.get_by_id(settings.CONFIG['layers'], key['id']).get('type') == 'string'
): ):
Item.layer_facet_keys.append(key['id']) Item.layer_facet_keys.append(key['id'])
@ -1787,7 +1779,7 @@ class AnnotationSequence(models.Model):
def reset(cls, item): def reset(cls, item):
s, created = cls.objects.get_or_create(item=item) s, created = cls.objects.get_or_create(item=item)
ids = [ox.fromAZ(a['public_id'].split('/')[1]) ids = [ox.fromAZ(a['public_id'].split('/')[1])
for a in item.annotations.exclude(public_id=None).values('public_id')] for a in item.annotations.exclude(public_id=None).values('public_id')]
s.value = max(ids) if ids else 0 s.value = max(ids) if ids else 0
s.save() s.save()