-
Notifications
You must be signed in to change notification settings - Fork 6
/
video.py
308 lines (279 loc) · 11.2 KB
/
video.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import urllib.error
import urllib.parse
from flask import current_app, url_for
from markupsafe import Markup
from sqlalchemy.ext.associationproxy import association_proxy
from werkzeug.utils import cached_property
from ..models import (
PLAYLIST_AUTO_TYPE,
BaseIdNameMixin,
Model,
TimestampMixin,
backref,
relationship,
sa,
sa_orm,
)
from .tag import tags_videos
__all__ = ['PlaylistVideo', 'Video']
class PlaylistVideo(TimestampMixin, Model):
__tablename__ = 'playlist_video'
playlist_id = sa_orm.mapped_column(
sa.Integer, sa.ForeignKey('playlist.id'), primary_key=True
)
video_id = sa_orm.mapped_column(
sa.Integer, sa.ForeignKey('video.id'), primary_key=True
)
video = relationship(
'Video', backref=backref('_playlists', cascade='all, delete-orphan')
)
seq = sa_orm.mapped_column(sa.Integer, nullable=False)
description = sa_orm.mapped_column(sa.UnicodeText, nullable=False, default=True)
class Video(BaseIdNameMixin, Model):
__tablename__ = 'video'
playlist_id = sa_orm.mapped_column(
sa.Integer, sa.ForeignKey('playlist.id'), nullable=False
)
playlist = relationship(
'Playlist', backref=backref('primary_videos', cascade='all, delete-orphan')
)
channel = association_proxy('playlist', 'channel')
description = sa_orm.mapped_column(sa.UnicodeText, nullable=False, default='')
video_url = sa_orm.mapped_column(sa.Unicode(250), nullable=False)
slides_url = sa_orm.mapped_column(sa.Unicode(250), nullable=False, default='')
thumbnail_path = sa_orm.mapped_column(sa.Unicode(250), nullable=True, default='')
video_source = sa_orm.mapped_column(sa.Unicode(80), nullable=False, default='')
video_sourceid = sa_orm.mapped_column(sa.Unicode(80), nullable=False, default='')
slides_source = sa_orm.mapped_column(sa.Unicode(80), nullable=False, default='')
slides_sourceid = sa_orm.mapped_column(sa.Unicode(80), nullable=False, default='')
video_slides_mapping = sa_orm.mapped_column(
sa.UnicodeText, nullable=True, default=''
)
video_slides_mapping_json = sa_orm.mapped_column(
sa.UnicodeText, nullable=True, default=''
)
playlists = association_proxy(
'_playlists', 'playlist', creator=lambda x: PlaylistVideo(playlist=x)
)
tags = relationship('Tag', secondary=tags_videos, backref=backref('videos'))
__roles__ = {
'all': {
'read': {
'title',
'name',
'description',
'url',
'url_name',
'thumbnail',
'speaker_names',
'video_source',
'slides_source',
'video_iframe',
'slides_html',
},
},
'auth': {'read': {'url_action', 'current_action_permissions'}},
'channel_admin': {'read': {'url_user_playlists', 'url_action'}},
'speaker': {'read': {'url_user_playlists', 'url_action'}},
}
def __repr__(self):
return '<Video %s>' % self.url_name
# ====================
# RoleMixin properties
# ====================
@property
def url(self):
"""
URL to this video. For use with RoleMixin, in ``__roles__``.
"""
return self.url_for(_external=True)
@property
def url_action(self):
return self.url_for('action')
@property
def url_user_playlists(self):
return url_for('user_playlists', video=self.url_name)
@property
def thumbnail(self):
return url_for('static', filename='thumbnails/' + self.thumbnail_path)
@property
def speaker_names(self):
"""
For use with RoleMixin above, in ``__roles__``.
"""
return [speaker.pickername for speaker in self.speakers]
@property
def current_action_permissions(self):
"""
Returns all the valid action permissions provided by the model based on user
role. This is needed for JSON endpoints when they return current_access(), and
front-end has to know what actions the user can perform on the given model
object.
"""
return list({'delete', 'edit'}.intersection(self.current_permissions))
@property
def video_iframe(self):
return self.embed_video_for(current_app.config.get('VIDEO_VIEW_MODE', 'view'))
@property
def slides_html(self):
return self.embed_slides_for()
@cached_property
def speakers(self):
from .channel import Playlist
return [
plv.playlist.channel
for plv in PlaylistVideo.query.join(Playlist).filter(
PlaylistVideo.video == self,
Playlist.auto_type == PLAYLIST_AUTO_TYPE.SPEAKING_IN,
)
]
def get_related_videos(self, playlist):
videos_dict = []
next_video = playlist.next(video=self)
if next_video:
videos_dict.append(dict(next_video.current_access()))
prev_video = playlist.prev(video=self)
if prev_video:
videos_dict.append(dict(prev_video.current_access()))
return videos_dict
def permissions(self, user, inherited=None):
perms = super().permissions(user, inherited)
perms.add('view')
if user and self.channel.userid in user.user_organizations_owned_ids():
perms.add('edit')
perms.add('delete')
else:
perms.discard('edit')
perms.discard('delete')
# Allow speakers to edit
if user:
pl = user.channel.playlist_for_speaking_in()
if pl and self in pl.videos:
perms.add('edit')
return perms
def roles_for(self, actor=None, anchors=()):
roles = super().roles_for(actor, anchors)
roles.update(self.playlist.roles_for(actor, anchors))
if actor is not None:
# whether user is speaker
pl = actor.channel.playlist_for_speaking_in()
if pl and self in pl.videos:
roles.add('speaker')
return roles
def url_for(self, action='view', channel=None, playlist=None, _external=False):
channel = channel or self.channel
playlist = playlist or self.playlist
if playlist.channel != channel or playlist not in self.playlists:
return
if action == 'view':
return url_for(
'video_view',
videopath=f'{channel.name}/{playlist.name}/{self.url_name}',
_external=_external,
)
elif action == 'remove-video':
return url_for(
'video_remove',
channel=channel.name,
playlist=playlist.name,
video=self.url_name,
_external=_external,
)
# Edit and Delete can only be from the source playlist
elif action == 'edit':
return url_for(
'video_edit',
channel=self.channel.name,
playlist=self.playlist.name,
video=self.url_name,
_external=_external,
)
elif action == 'delete':
return url_for(
'video_delete',
channel=self.channel.name,
playlist=self.playlist.name,
video=self.url_name,
_external=_external,
)
elif action == 'add-speaker':
return url_for(
'video_add_speaker',
channel=channel.name,
playlist=playlist.name,
video=self.url_name,
_external=_external,
)
elif action == 'autocomplete-speaker':
return url_for(
'video_autocomplete_speaker',
channel=channel.name,
playlist=playlist.name,
video=self.url_name,
_external=_external,
)
elif action == 'remove-speaker':
return url_for(
'video_remove_speaker',
channel=channel.name,
playlist=playlist.name,
video=self.url_name,
_external=_external,
)
elif action == 'action':
return url_for(
'video_action',
channel=channel.name,
playlist=playlist.name,
video=self.url_name,
_external=_external,
)
def embed_video_for(self, action='view'):
if self.video_source == 'youtube':
if action == 'edit':
return Markup(
'<iframe id="youtube_player" src="//www.youtube.com/embed/%s?wmode=transparent&showinfo=0&rel=0&autohide=0&autoplay=0&enablejsapi=1&version=3" frameborder="0" allowfullscreen></iframe>'
% self.video_sourceid
)
elif action == 'view':
return Markup(
'<iframe id="youtube_player" src="//videoken.com/embed/?videoID=%s&wmode=transparent&showinfo=0&rel=0&autohide=0&autoplay=1&enablejsapi=1&version=3" frameborder="0" allowfullscreen></iframe>'
% self.video_sourceid
)
elif self.video_source == 'vimeo':
if action == 'edit':
return Markup(
'<iframe id="vimeo_player" src="//player.vimeo.com/video/%s?api=1&player_id=vimeoplayer" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'
% self.video_sourceid
)
elif action == 'view':
return Markup(
'<iframe id="vimeo_player" src="//player.vimeo.com/video/%s?api=1&autoplay=1" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'
% self.video_sourceid
)
elif self.video_source == 'ustream':
if action == 'edit':
return Markup(
'<iframe id="ustream_player" src="//www.ustream.tv/embed/%s?v=3&wmode=direct" scrolling="no" frameborder="0" style="border: 0px none transparent;"> </iframe>'
% self.video_sourceid
)
elif action == 'view':
return Markup(
'<iframe id="ustream_player" src="//www.ustream.tv/embed/%s?v=3&wmode=direct" scrolling="no" frameborder="0" style="border: 0px none transparent;"> </iframe>'
% self.video_sourceid
)
return ''
def embed_slides_for(self, action=None):
if self.slides_source == 'speakerdeck':
html = (
'<iframe src="//www.speakerdeck.com/embed/%s" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>'
% urllib.parse.quote(self.slides_sourceid)
)
return Markup(html)
elif self.slides_source == 'slideshare':
html = (
'<iframe id="slideshare" src="//www.slideshare.net/slideshow/embed_code/%s" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>'
% urllib.parse.quote(self.slides_sourceid)
)
return Markup(html)
return ''