• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

EsupPortail / Esup-Pod / 8541404367

03 Apr 2024 03:28PM UTC coverage: 70.28%. First build
8541404367

Pull #1088

github

web-flow
Merge b9ebb336f into 2d37cfc11
Pull Request #1088: [DONE] Add a submit button on the add video page

0 of 1 new or added line in 1 file covered. (0.0%)

10031 of 14273 relevant lines covered (70.28%)

0.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/pod/video_encode_transcript/utils.py
1
"""Esup-Pod video encoding and transcripting utilities."""
2

3
import os
1✔
4
import bleach
1✔
5
import time
1✔
6

7
from django.urls import reverse
1✔
8
from django.conf import settings
1✔
9
from django.utils.translation import ugettext_lazy as _
1✔
10
from django.core.mail import mail_admins
1✔
11
from django.core.mail import send_mail
1✔
12
from django.core.mail import mail_managers
1✔
13
from django.core.mail import EmailMultiAlternatives
1✔
14
from pod.video.models import Video
1✔
15
from pod.progressive_web_app.utils import notify_user
1✔
16
from .models import EncodingStep
1✔
17
from .models import EncodingLog
1✔
18

19
DEBUG = getattr(settings, "DEBUG", True)
1✔
20

21
TEMPLATE_VISIBLE_SETTINGS = getattr(
1✔
22
    settings,
23
    "TEMPLATE_VISIBLE_SETTINGS",
24
    {
25
        "TITLE_SITE": "Pod",
26
        "TITLE_ETB": "University name",
27
        "LOGO_SITE": "img/logoPod.svg",
28
        "LOGO_ETB": "img/esup-pod.svg",
29
        "LOGO_PLAYER": "img/pod_favicon.svg",
30
        "LINK_PLAYER": "",
31
        "LINK_PLAYER_NAME": _("Home"),
32
        "FOOTER_TEXT": ("",),
33
        "FAVICON": "img/pod_favicon.svg",
34
        "CSS_OVERRIDE": "",
35
        "PRE_HEADER_TEMPLATE": "",
36
        "POST_FOOTER_TEMPLATE": "",
37
        "TRACKING_TEMPLATE": "",
38
    },
39
)
40

41
__TITLE_SITE__ = (
1✔
42
    TEMPLATE_VISIBLE_SETTINGS["TITLE_SITE"]
43
    if (TEMPLATE_VISIBLE_SETTINGS.get("TITLE_SITE"))
44
    else "Pod"
45
)
46

47
DEFAULT_FROM_EMAIL = getattr(settings, "DEFAULT_FROM_EMAIL", "noreply@univ.fr")
1✔
48

49
USE_ESTABLISHMENT_FIELD = getattr(settings, "USE_ESTABLISHMENT_FIELD", False)
1✔
50

51
MANAGERS = getattr(settings, "MANAGERS", {})
1✔
52

53
SECURE_SSL_REDIRECT = getattr(settings, "SECURE_SSL_REDIRECT", False)
1✔
54
VIDEOS_DIR = getattr(settings, "VIDEOS_DIR", "videos")
1✔
55

56

57
# ##########################################################################
58
# ENCODE VIDEO : GENERIC FUNCTIONS
59
# ##########################################################################
60

61

62
def change_encoding_step(video_id, num_step, desc):
1✔
63
    """Change encoding step."""
64
    encoding_step, created = EncodingStep.objects.get_or_create(
1✔
65
        video=Video.objects.get(id=video_id)
66
    )
67
    encoding_step.num_step = num_step
1✔
68
    encoding_step.desc_step = desc[:255]
1✔
69
    encoding_step.save()
1✔
70

71
    if DEBUG:
1✔
72
        print("step: %d - desc: %s" % (num_step, desc))
1✔
73

74

75
def add_encoding_log(video_id, log):
1✔
76
    """Add message in video_id encoding log."""
77
    encoding_log, created = EncodingLog.objects.get_or_create(
1✔
78
        video=Video.objects.get(id=video_id)
79
    )
80
    if created:
1✔
81
        encoding_log.log = log
1✔
82
    else:
83
        encoding_log.log += "\n\n%s" % log
×
84
    encoding_log.save()
1✔
85
    if DEBUG:
1✔
86
        print(log)
1✔
87

88

89
def check_file(path_file):
1✔
90
    """Check if path_file is accessible and is not null."""
91
    if os.access(path_file, os.F_OK) and os.stat(path_file).st_size > 0:
1✔
92
        return True
1✔
93
    return False
1✔
94

95

96
def create_outputdir(video_id, video_path):
1✔
97
    """ENCODE VIDEO: CREATE OUTPUT DIR."""
98
    dirname = os.path.dirname(video_path)
×
99
    output_dir = os.path.join(dirname, "%04d" % video_id)
×
100
    if not os.path.exists(output_dir):
×
101
        os.makedirs(output_dir)
×
102
    return output_dir
×
103

104

105
###############################################################
106
# EMAIL
107
###############################################################
108

109

110
def send_email_item(msg, item, item_id):
1✔
111
    """Send email notification when encoding fails for a specific item."""
112
    subject = "[" + __TITLE_SITE__ + "] Error Encoding %s id: %s" % (item, item_id)
1✔
113
    message = "Error Encoding %s id: %s\n%s" % (item, item_id, msg)
1✔
114
    html_message = "<p>Error Encoding %s id: %s</p><p>%s</p>" % (
1✔
115
        item,
116
        item_id,
117
        msg.replace("\n", "<br>"),
118
    )
119
    mail_admins(subject, message, fail_silently=False, html_message=html_message)
1✔
120

121

122
def send_email_recording(msg, recording_id):
1✔
123
    """Send email notification when recording encoding failed."""
124
    send_email_item(msg, "Recording", recording_id)
×
125

126

127
def send_email_encoding(video_to_encode):
1✔
128
    """Send email on encoding completion."""
129
    subject_prefix = _("Encoding")
1✔
130
    send_notification_email(video_to_encode, subject_prefix)
1✔
131

132

133
def send_notification_encoding(video_to_encode):
1✔
134
    """Send push notification on encoding completion."""
135
    subject_prefix = _("Encoding")
×
136
    send_notification(video_to_encode, subject_prefix)
×
137

138

139
def send_email(msg, video_id):
1✔
140
    """Send email notification when video encoding failed."""
141
    send_email_item(msg, "Video", video_id)
1✔
142

143

144
def send_email_transcript(video_to_encode):
1✔
145
    """Send email on transcripting completion."""
NEW
146
    subject_prefix = _("The transcripting of content")
×
147
    send_notification_email(video_to_encode, subject_prefix)
×
148

149

150
def send_notification_email(video_to_encode, subject_prefix):
1✔
151
    """Send email notification on video encoding or transcripting completion."""
152
    if DEBUG:
1✔
153
        print("SEND EMAIL ON %s COMPLETION" % subject_prefix.upper())
1✔
154
    url_scheme = "https" if SECURE_SSL_REDIRECT else "http"
1✔
155
    content_url = "%s:%s" % (url_scheme, video_to_encode.get_full_url())
1✔
156
    subject = "[%s] %s" % (
1✔
157
        __TITLE_SITE__,
158
        _("%(subject)s #%(content_id)s completed")
159
        % {"subject": subject_prefix, "content_id": video_to_encode.id},
160
    )
161

162
    html_message = (
1✔
163
        '<p>%s</p><p>%s</p><p>%s<br><a href="%s"><i>%s</i></a>\
164
                </p><p>%s</p>'
165
        % (
166
            _("Hello,"),
167
            _(
168
                "%(content_type)s ā€œ%(content_title)sā€ has been %(action)s"
169
                + ", and is now available on %(site_title)s."
170
            )
171
            % {
172
                "content_type": (
173
                    _("The content")
174
                    if subject_prefix == _("The transcripting of content")
175
                    else _("The video")
176
                ),
177
                "content_title": "<b>%s</b>" % video_to_encode.title,
178
                "action": (
179
                    _("automatically transcripted")
180
                    if (subject_prefix == _("The transcripting of content"))
181
                    else _("encoded to Web formats")
182
                ),
183
                "site_title": __TITLE_SITE__,
184
            },
185
            _("You will find it here:"),
186
            content_url,
187
            content_url,
188
            _("Regards."),
189
        )
190
    )
191

192
    full_html_message = html_message + "<br>%s%s<br>%s%s" % (
1✔
193
        _("Post by:"),
194
        video_to_encode.owner,
195
        _("the:"),
196
        video_to_encode.date_added,
197
    )
198

199
    message = bleach.clean(html_message, tags=[], strip=True)
1✔
200
    full_message = bleach.clean(full_html_message, tags=[], strip=True)
1✔
201

202
    from_email = DEFAULT_FROM_EMAIL
1✔
203
    to_email = []
1✔
204
    to_email.append(video_to_encode.owner.email)
1✔
205

206
    if (
1✔
207
        USE_ESTABLISHMENT_FIELD
208
        and MANAGERS
209
        and video_to_encode.owner.owner.establishment.lower() in dict(MANAGERS)
210
    ):
211
        bcc_email = []
×
212
        video_estab = video_to_encode.owner.owner.establishment.lower()
×
213
        manager = dict(MANAGERS)[video_estab]
×
214
        if isinstance(manager, (list, tuple)):
×
215
            bcc_email = manager
×
216
        elif isinstance(manager, str):
×
217
            bcc_email.append(manager)
×
218
        msg = EmailMultiAlternatives(
×
219
            subject, message, from_email, to_email, bcc=bcc_email
220
        )
221
        msg.attach_alternative(html_message, "text/html")
×
222
        msg.send()
×
223
    else:
224
        mail_managers(
1✔
225
            subject,
226
            full_message,
227
            fail_silently=False,
228
            html_message=full_html_message,
229
        )
230
        if not DEBUG:
1✔
231
            send_mail(
×
232
                subject,
233
                message,
234
                from_email,
235
                to_email,
236
                fail_silently=False,
237
                html_message=html_message,
238
            )
239

240

241
def send_notification(video_to_encode, subject_prefix):
1✔
242
    """Send push notification on video encoding or transcripting completion."""
243
    subject = "[%s] %s" % (
×
244
        __TITLE_SITE__,
245
        _("%(subject)s #%(content_id)s completed")
246
        % {"subject": subject_prefix, "content_id": video_to_encode.id},
247
    )
248
    message = _(
×
249
        "%(content_type)s ā€œ%(content_title)sā€ has been %(action)s"
250
        + ", and is now available on %(site_title)s."
251
    ) % {
252
        "content_type": (
253
            _("content") if subject_prefix == _("Transcripting") else _("video")
254
        ),
255
        "content_title": video_to_encode.title,
256
        "action": (
257
            _("automatically transcripted")
258
            if (subject_prefix == _("Transcripting"))
259
            else _("encoded to Web formats")
260
        ),
261
        "site_title": __TITLE_SITE__,
262
    }
263

264
    notify_user(
×
265
        video_to_encode.owner,
266
        subject,
267
        message,
268
        url=reverse("video:video", args=(video_to_encode.slug,)),
269
    )
270

271

272
def time_to_seconds(a_time):
1✔
273
    """Convert a time to seconds."""
274
    seconds = time.strptime(str(a_time), "%H:%M:%S")
1✔
275
    seconds = seconds.tm_sec + seconds.tm_min * 60 + seconds.tm_hour * 3600
1✔
276
    return seconds
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc