Exceptional handling for customizing subitles
authorsaksham1115 <saksham115@gmail.com>
Sat, 13 Aug 2016 17:40:28 +0000 (17:40 +0000)
committersaksham1115 <saksham115@gmail.com>
Sat, 13 Aug 2016 17:40:28 +0000 (17:40 +0000)
mediagoblin/plugins/subtitles/tools.py
mediagoblin/plugins/subtitles/views.py

index af32f270823feeae8736afd4b29420e6ce0f6834..735c89b2f402a91fe7f0050014a8937d7cf1def1 100644 (file)
@@ -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)
index a7f5cee5e5732ead84e8324b249106dd2db3eaea..338ba859f7ab2451c56b4acb8e7a037bfbfbc469 100644 (file)
@@ -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))