Removed dependence on absolute path for editing
authorsaksham1115 <saksham115@gmail.com>
Sat, 16 Jul 2016 09:44:21 +0000 (09:44 +0000)
committersaksham1115 <saksham115@gmail.com>
Tue, 19 Jul 2016 17:29:25 +0000 (17:29 +0000)
mediagoblin/plugins/custom_subtitles/tools.py
mediagoblin/plugins/custom_subtitles/views.py

index efafbeec809104c065e84ce774b1ec58c895a2c6..2cc5157ffa5af05242a35a8f8aef3a1b50125376 100644 (file)
@@ -1,20 +1,41 @@
+# GNU MediaGoblin -- federated, autonomous media hosting
+# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from mediagoblin import mg_globals
 import os
 
 def get_path(path):
-       temp = ['user_dev','media','public']
-       path = list(eval(path))
-       file_path = os.path.abspath(__file__).split('/') # Path of current file as dictionary
-       subtitle_path = file_path[:-3] + temp + path # Creating the absolute path for the subtitle file
-       subtitle_path = "/" + os.path.join(*subtitle_path)
-       return subtitle_path
+       path = eval(path) # Converting string to a tuple
+       return path
 
 def open_subtitle(path):
-       subtitle_path = get_path(path)
-       subtitle = open(subtitle_path,"r") # Opening the file using the absolute path
-       text = subtitle.read()
-       return text
+       subtitle_public_filepath = get_path(path)
+       subtitle_public_file = mg_globals.public_store.get_file(
+            subtitle_public_filepath, 'rb')
+       try:
+            text = subtitle_public_file.read().decode('utf-8')
+            return text
+        finally:
+            subtitle_public_file.close()
 
 def save_subtitle(path,text):
-       subtitle_path = get_path(path)
-       subtitle = open(subtitle_path,"w") # Opening the file using the absolute path
-       subtitle.write(text)
\ No newline at end of file
+       subtitle_public_filepath = get_path(path)
+       subtitle_public_file = mg_globals.public_store.get_file(
+            subtitle_public_filepath, 'wb')
+       try:
+            subtitle_public_file.write(text)
+        finally:
+            subtitle_public_file.close()
\ No newline at end of file
index ce540af0d59ec9cc055142eed958f38308615f34..e647df595a7dbacb7c2b30899480491b56107165 100644 (file)
@@ -104,12 +104,16 @@ def edit_subtitles(request, media):
 @path_subtitle
 def custom_subtitles(request,media,path=None):
     text=""
-#    text = open_subtitle(path)
+    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)
+        save_subtitle(path,subtitle_data)
+        messages.add_message(
+            request,
+            messages.SUCCESS,
+            ("Subtitle file changed!!!"))
         return render_to_response(
         request,
         "mediagoblin/plugins/custom_subtitles/custom_subtitles.html",
@@ -125,38 +129,10 @@ def custom_subtitles(request,media,path=None):
                     index += 1
                 print media.subtitle_files.pop(delete_container)
                 media.save()"""
-
+        
     return render_to_response(
         request,
         "mediagoblin/plugins/custom_subtitles/custom_subtitles.html",
         {"path": path,
          "media": media,
-         "form": form })
-
-@require_active_login
-@get_media_entry_by_id
-@user_may_delete_media
-@path_subtitle
-def delete_subtitles(request,media,path=None):
-    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)
-
-    """delete_container = None
-                index = 0
-                for subtitle in media.subtitle_files:
-                    if subtitle["name"] == "Two And A Half Men S02E02.srt":
-                        delete_container = index
-                    index += 1
-                print media.subtitle_files.pop(delete_container)
-                media.save()"""
-    return render_to_response(
-        request,
-        "mediagoblin/plugins/custom_subtitles/custom_subtitles.html",
-        {"path": path,
-         "media": media,
-         "form": form })
+         "form": form })
\ No newline at end of file