diff options
author | saksham1115 <saksham115@gmail.com> | 2016-07-25 19:23:31 +0000 |
---|---|---|
committer | saksham1115 <saksham115@gmail.com> | 2016-07-25 19:23:31 +0000 |
commit | 13422829838bbf3c5a33e88ba90e47d91f55e455 (patch) | |
tree | 81cee92caac3d5e66bc9aedd04d2372e7c5071da /mediagoblin/plugins | |
parent | c36c6833046b6d1f6aa1b0f274585a1b23b9a5ad (diff) | |
download | mediagoblin-13422829838bbf3c5a33e88ba90e47d91f55e455.tar.lz mediagoblin-13422829838bbf3c5a33e88ba90e47d91f55e455.tar.xz mediagoblin-13422829838bbf3c5a33e88ba90e47d91f55e455.zip |
Using with statement for editing files
Diffstat (limited to 'mediagoblin/plugins')
-rw-r--r-- | mediagoblin/plugins/custom_subtitles/tools.py | 24 | ||||
-rw-r--r-- | mediagoblin/plugins/custom_subtitles/views.py | 19 |
2 files changed, 15 insertions, 28 deletions
diff --git a/mediagoblin/plugins/custom_subtitles/tools.py b/mediagoblin/plugins/custom_subtitles/tools.py index 2cc5157f..f947ba92 100644 --- a/mediagoblin/plugins/custom_subtitles/tools.py +++ b/mediagoblin/plugins/custom_subtitles/tools.py @@ -18,24 +18,18 @@ from mediagoblin import mg_globals import os def get_path(path): - path = eval(path) # Converting string to a tuple - return path + path = eval(path) # Converting string to a tuple + return path def open_subtitle(path): - subtitle_public_filepath = get_path(path) - subtitle_public_file = mg_globals.public_store.get_file( - subtitle_public_filepath, 'rb') - try: + subtitle_public_filepath = get_path(path) + with mg_globals.public_store.get_file( + subtitle_public_filepath, 'rb') as subtitle_public_file: text = subtitle_public_file.read().decode('utf-8') return text - finally: - subtitle_public_file.close() def save_subtitle(path,text): - 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 + subtitle_public_filepath = get_path(path) + with mg_globals.public_store.get_file( + subtitle_public_filepath, 'wb') as subtitle_public_file: + subtitle_public_file.write(text)
\ No newline at end of file diff --git a/mediagoblin/plugins/custom_subtitles/views.py b/mediagoblin/plugins/custom_subtitles/views.py index 5ea9cf35..3d75b0ae 100644 --- a/mediagoblin/plugins/custom_subtitles/views.py +++ b/mediagoblin/plugins/custom_subtitles/views.py @@ -64,14 +64,11 @@ def edit_subtitles(request, media): ['media_entries', six.text_type(media.id), 'subtitle', public_filename]) - subtitle_public_file = mg_globals.public_store.get_file( - subtitle_public_filepath, 'wb') - - try: + with mg_globals.public_store.get_file( + subtitle_public_filepath, 'wb') as subtitle_public_file: subtitle_public_file.write( request.files['subtitle_file'].stream.read()) - finally: - request.files['subtitle_file'].stream.close() + request.files['subtitle_file'].stream.close() media.subtitle_files.append(dict( name=form.subtitle_language.data \ @@ -85,7 +82,7 @@ def edit_subtitles(request, media): messages.add_message( request, messages.SUCCESS, - ("You added the subttile %s!") % + ("You added the subtitle %s!") % (form.subtitle_language.data or request.files['subtitle_file'].filename)) @@ -114,12 +111,8 @@ def custom_subtitles(request,media,path=None): request, messages.SUCCESS, ("Subtitle file changed!!!")) - return render_to_response( - request, - "mediagoblin/plugins/custom_subtitles/custom_subtitles.html", - {"path": path, - "media": media, - "form": form }) + return redirect(request, + location=media.url_for_self(request.urlgen)) return render_to_response( request, |