Renamed plugin from custom_subtitles to subtitles

This commit is contained in:
saksham1115
2016-07-30 18:48:24 +00:00
parent 05a3958319
commit 067b1b071a
8 changed files with 20 additions and 20 deletions

View File

@@ -0,0 +1,50 @@
# 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.tools import pluginapi
import os
PLUGIN_DIR = os.path.dirname(__file__)
def setup_plugin():
config = pluginapi.get_config('mediagoblin.plugins.subtitles')
routes = [
('mediagoblin.plugins.subtitles.customize',
'/u/<string:user>/m/<int:media_id>/customize/<string:path>',
'mediagoblin.plugins.subtitles.views:custom_subtitles'),
('mediagoblin.plugins.subtitles.subtitles',
'/u/<string:user>/m/<int:media_id>/subtitles/',
'mediagoblin.plugins.subtitles.views:edit_subtitles'),
('mediagoblin.plugins.subtitles.delete_subtitles',
'/u/<string:user>/m/<int:media_id>/delete/<string:path>',
'mediagoblin.plugins.subtitles.views:delete_subtitles')]
pluginapi.register_routes(routes)
# Register the template path.
pluginapi.register_template_path(os.path.join(PLUGIN_DIR, 'templates'))
pluginapi.register_template_hooks(
{"customize_subtitles": "mediagoblin/plugins/subtitles/custom_subtitles.html",
"add_subtitles": "mediagoblin/plugins/subtitles/subtitles.html",
"subtitle_sidebar": "mediagoblin/plugins/subtitles/subtitle_media_block.html"})
hooks = {
'setup': setup_plugin
}

View File

@@ -0,0 +1,29 @@
# 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/>.
import wtforms
class CustomizeSubtitlesForm(wtforms.Form):
subtitle = wtforms.TextAreaField(
('Subtitle'),
[wtforms.validators.Optional()],
description=(""))
class EditSubtitlesForm(wtforms.Form):
subtitle_language = wtforms.StringField(
'Language')
subtitle_file = wtforms.FileField(
'File')

View File

@@ -0,0 +1,49 @@
# 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 sqlalchemy import Column, Integer, Unicode, ForeignKey
from sqlalchemy.orm import relationship
from mediagoblin.db.models import User
from mediagoblin.db.base import Base,MediaEntry
class MediaSubtitleFile(Base):
__tablename__ = "core__subtitle_files"
id = Column(Integer, primary_key=True)
media_entry = Column(
Integer, ForeignKey(MediaEntry.id),
nullable=False)
name = Column(Unicode, nullable=False)
filepath = Column(PathTupleWithSlashes)
created = Column(DateTime, nullable=False, default=datetime.datetime.utcnow)
@property
def dict_view(self):
"""A dict like view on this object"""
return DictReadAttrProxy(self)
subtitle_files_helper = relationship("MediaSubtitleFile",
cascade="all, delete-orphan",
order_by="MediaSubtitleFile.created"
)
subtitle_files = association_proxy("subtitle_files_helper", "dict_view",
creator=lambda v: MediaSubtitleFile(
name=v["name"], filepath=v["filepath"])
)
MODELS = [
MediaSubtitleFile
]

View File

@@ -0,0 +1,48 @@
{#
# 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/>.
#}
{% extends "mediagoblin/base.html" %}
{% import "/mediagoblin/utils/wtforms.html" as wtforms_util %}
{% block title -%}
{%- endblock %}
{% block mediagoblin_content %}
<link href="{{
request.staticdirect('/css/subtitles.css') }}"
rel="stylesheet">
<form action="{{ request.urlgen('mediagoblin.plugins.subtitles.customize',
user=media.get_actor.username,
media_id=media.id,
path=path) }}" method="POST" enctype="multipart/form-data">
<div class="form_box edit_box">
{{ wtforms_util.render_divs(form) }}
<div class="form_submit_buttons">
{% set delete_url = request.urlgen('mediagoblin.plugins.subtitles.delete_subtitles',
user= media.get_actor.username,
media_id=media.id,
path=path) %}
<a class="button_action button_warning" href="{{ delete_url }}">{% trans %}Delete Subtitle{% endtrans %}</a>
<input type="submit" value="{% trans %}Save changes{% endtrans %}" class="button_form" />
{{ csrf_token }}
</div>
</div>
</form>
{% endblock %}

View File

@@ -0,0 +1,50 @@
{#
# 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/>.
#}
{% block subtitle_block %}
{% if "video.html" in media.media_manager.display_template or "audio.html" in media.media_manager.display_template %}
{%- if media.subtitle_files|count %}
<h3>{% trans %}Subtitles{% endtrans %}</h3>
<ul>
{%- for subtitle in media.subtitle_files %}
<li>
<a href="{{ request.urlgen('mediagoblin.plugins.subtitles.customize',
user=media.get_actor.username,
media_id=media.id,
path=subtitle.filepath) }}">
{{- subtitle.name -}}
</li>
{%- endfor %}
</ul>
{%- endif %}
{%- if request.user
and (media.actor == request.user.id
or request.user.has_privilege('admin')) %}
{%- if not media.subtitle_files|count %}
<h3>{% trans %}Subtitles{% endtrans %}</h3>
{%- endif %}
<p>
<a href="{{ request.urlgen('mediagoblin.plugins.subtitles.subtitles',
user=media.get_actor.username,
media_id=media.id) }}">
{%- trans %}Add subtitle {% endtrans -%}
</a>
</p>
{%- endif %}
{% endif %}
{% endblock %}

View File

@@ -0,0 +1,69 @@
{#
# 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/>.
#}
{%- extends "mediagoblin/base.html" %}
{% import "/mediagoblin/utils/wtforms.html" as wtforms_util %}
{% block title -%}
{% trans media_title=media.title -%}
Editing subtitles for {{ media_title }}
{%- endtrans %} &mdash; {{ super() }}
{%- endblock %}
{% block mediagoblin_content %}
<form action="{{ request.urlgen('mediagoblin.plugins.subtitles.subtitles',
user= media.get_actor.username,
media_id=media.id) }}"
method="POST" enctype="multipart/form-data">
<div class="form_box">
<h1>
{%- trans media_title=media.title -%}
Editing subtitles for {{ media_title }}
{%- endtrans -%}
</h1>
<div style="text-align: center;" >
<img src="{{ media.thumb_url }}" />
</div>
{% if media.subtitle_files|count %}
<h2>{% trans %}subtitles{% endtrans %}</h2>
<ul>
{%- for subtitle in media.subtitle_files %}
<li>
<a target="_blank" href="{{ request.app.public_store.file_url(
subtitle['filepath']) }}">
{{- subtitle.name -}}
</a>
</li>
{%- endfor %}
</ul>
{% endif %}
<h2>{% trans %}Add subtitle{% endtrans %}</h2>
{{- wtforms_util.render_divs(form) }}
<div class="form_submit_buttons">
<a class="button_action" href="{{ media.url_for_self(request.urlgen) }}">
{%- trans %}Cancel{% endtrans -%}
</a>
<input type="submit" value="{% trans %}Save changes{% endtrans %}"
class="button_form" />
{{ csrf_token }}
</div>
</div>
</form>
{% endblock %}

View File

@@ -0,0 +1,35 @@
# 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):
path = eval(path) # Converting string to a tuple
return path
def open_subtitle(path):
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
def save_subtitle(path,text):
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)

View File

@@ -0,0 +1,166 @@
# 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/>.
import six
from datetime import datetime
from itsdangerous import BadSignature
from werkzeug.exceptions import Forbidden
from werkzeug.utils import secure_filename
from mediagoblin import messages
from mediagoblin import mg_globals
from mediagoblin.plugins.subtitles import forms
from mediagoblin.decorators import (require_active_login, active_user_from_url,
get_media_entry_by_id, user_may_delete_media)
from mediagoblin.tools.metadata import (compact_and_validate, DEFAULT_CHECKER,
DEFAULT_SCHEMA)
from mediagoblin.tools.response import (render_to_response,
redirect, redirect_obj, render_404)
import mimetypes
from mediagoblin.plugins.subtitles.tools import open_subtitle,save_subtitle,get_path
UNSAFE_MIMETYPES = [
'text/html',
'text/svg+xml']
@get_media_entry_by_id
@user_may_delete_media
@require_active_login
def edit_subtitles(request, media):
allowed_extensions = ['aqt','gsub','jss','sub','ttxt','pjs','psb',
'rt','smi','stl','ssf','srt','ssa','ass','usf','vtt','lrc']
form = forms.EditSubtitlesForm(request.form)
# Add any subtitles
if 'subtitle_file' in request.files \
and request.files['subtitle_file']:
if mimetypes.guess_type(
request.files['subtitle_file'].filename)[0] in \
UNSAFE_MIMETYPES:
public_filename = secure_filename('{0}.notsafe'.format(
request.files['subtitle_file'].filename))
else:
public_filename = secure_filename(
request.files['subtitle_file'].filename)
filepath = request.files['subtitle_file'].filename
if filepath.count('.') != 1: # Not allowing double extensions or no extensions
messages.add_message(
request,
messages.ERROR,
("Check the filename"))
return redirect(request,
location=media.url_for_self(request.urlgen))
elif filepath.split('.')[:-1] not in allowed_extensions :
messages.add_message(
request,
messages.ERROR,
("Invalid subtitle file"))
return redirect(request,
location=media.url_for_self(request.urlgen))
subtitle_public_filepath \
= mg_globals.public_store.get_unique_filepath(
['media_entries', six.text_type(media.id), 'subtitle',
public_filename])
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())
request.files['subtitle_file'].stream.close()
media.subtitle_files.append(dict(
name=form.subtitle_language.data \
or request.files['subtitle_file'].filename,
filepath=subtitle_public_filepath,
created=datetime.utcnow(),
))
media.save()
messages.add_message(
request,
messages.SUCCESS,
("You added the subtitle %s!") %
(form.subtitle_language.data or
request.files['subtitle_file'].filename))
return redirect(request,
location=media.url_for_self(request.urlgen))
return render_to_response(
request,
'mediagoblin/plugins/subtitles/subtitles.html',
{'media': media,
'form': form})
@require_active_login
@get_media_entry_by_id
@user_may_delete_media
def custom_subtitles(request,media,path=None):
path = request.matchdict['path']
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)
messages.add_message(
request,
messages.SUCCESS,
("Subtitle file changed!!!"))
return redirect(request,
location=media.url_for_self(request.urlgen))
return render_to_response(
request,
"mediagoblin/plugins/subtitles/custom_subtitles.html",
{"path": path,
"media": media,
"form": form })
@require_active_login
@get_media_entry_by_id
@user_may_delete_media
def delete_subtitles(request,media):
path = request.matchdict['path']
path = get_path(path)
mg_globals.public_store.delete_file(path)
delete_container = None
index = 0
for subtitle in media.subtitle_files:
if str(subtitle["filepath"]) == str(path):
delete_container = index
index += 1
media.subtitle_files.pop(delete_container)
media.save()
break
messages.add_message(
request,
messages.SUCCESS,
("Subtitle file deleted!!!"))
return redirect(request,
location=media.url_for_self(request.urlgen))