From: saksham1115 Date: Sat, 13 Aug 2016 17:40:28 +0000 (+0000) Subject: Exceptional handling for customizing subitles X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=19b4f431665b8bcbf45e811d3fe58ca87659d4ca;p=mediagoblin.git Exceptional handling for customizing subitles --- diff --git a/mediagoblin/plugins/subtitles/tools.py b/mediagoblin/plugins/subtitles/tools.py index af32f270..735c89b2 100644 --- a/mediagoblin/plugins/subtitles/tools.py +++ b/mediagoblin/plugins/subtitles/tools.py @@ -18,14 +18,25 @@ from mediagoblin import mg_globals import os def open_subtitle(path): + status = True subtitle_public_filepath = path - with mg_globals.public_store.get_file( - subtitle_public_filepath, 'rb') as subtitle_public_file: - text = subtitle_public_file.read().decode('utf-8','ignore') - return text + try: + with mg_globals.public_store.get_file( + subtitle_public_filepath, 'rb') as subtitle_public_file: + text = subtitle_public_file.read().decode('utf-8','ignore') + return (text,status) + except: + status = False + return ('',status) def save_subtitle(path,text): + status = True subtitle_public_filepath = path - with mg_globals.public_store.get_file( - subtitle_public_filepath, 'wb') as subtitle_public_file: - subtitle_public_file.write(text.encode('utf-8','ignore')) \ No newline at end of file + try: + with mg_globals.public_store.get_file( + subtitle_public_filepath, 'wb') as subtitle_public_file: + subtitle_public_file.write(text.encode('utf-8','ignore')) + return status + except: + status = False + return (status) diff --git a/mediagoblin/plugins/subtitles/views.py b/mediagoblin/plugins/subtitles/views.py index a7f5cee5..338ba859 100644 --- a/mediagoblin/plugins/subtitles/views.py +++ b/mediagoblin/plugins/subtitles/views.py @@ -122,27 +122,52 @@ def custom_subtitles(request,media,id=None): for subtitle in media.subtitle_files: if subtitle["id"] == id: path = subtitle["filepath"] - text="" - text = open_subtitle(path) - form = forms.CustomizeSubtitlesForm(request.form, - subtitle=text) - if request.method == 'POST' and form.validate(): - subtitle_data = form.subtitle.data - save_subtitle(path,subtitle_data) + text = "" + value = open_subtitle(path) + text, status = value[0], value[1] + if status == True : + form = forms.CustomizeSubtitlesForm(request.form, + subtitle=text) + if request.method == 'POST' and form.validate(): + subtitle_data = form.subtitle.data + status = save_subtitle(path,subtitle_data) + if status == True: + messages.add_message( + request, + messages.SUCCESS, + ("Subtitle file changed!!!")) + return redirect(request, + location=media.url_for_self(request.urlgen)) + else : + messages.add_message( + request, + messages.ERROR, + ("Couldn't edit the subtitles!!!")) + return redirect(request, + location=media.url_for_self(request.urlgen)) + + return render_to_response( + request, + "mediagoblin/plugins/subtitles/custom_subtitles.html", + {"id": id, + "media": media, + "form": form }) + else: + index = 0 + for subtitle in media.subtitle_files: + if subtitle["id"] == id: + delete_container = index + media.subtitle_files.pop(delete_container) + media.save() + break + index += 1 messages.add_message( request, - messages.SUCCESS, - ("Subtitle file changed!!!")) + messages.ERROR, + ("File link broken! Upload the subtitle again")) return redirect(request, location=media.url_for_self(request.urlgen)) - return render_to_response( - request, - "mediagoblin/plugins/subtitles/custom_subtitles.html", - {"id": id, - "media": media, - "form": form }) - @require_active_login @get_media_entry_by_id @@ -167,4 +192,4 @@ def delete_subtitles(request,media): ("Subtitle file deleted!!!")) return redirect(request, - location=media.url_for_self(request.urlgen)) \ No newline at end of file + location=media.url_for_self(request.urlgen))