Feature #482 - Media attachments -
* Moved attachment uploading to separate view * Support for multiple attachments!
This commit is contained in:
parent
2c4374938f
commit
3a8c3a3855
@ -30,10 +30,6 @@ class EditForm(wtforms.Form):
|
|||||||
tags = wtforms.TextField(
|
tags = wtforms.TextField(
|
||||||
'Tags',
|
'Tags',
|
||||||
[tag_length_validator])
|
[tag_length_validator])
|
||||||
attachment_name = wtforms.TextField(
|
|
||||||
'Attachment title')
|
|
||||||
attachment_delete = wtforms.BooleanField(
|
|
||||||
'Delete attachment')
|
|
||||||
|
|
||||||
class EditProfileForm(wtforms.Form):
|
class EditProfileForm(wtforms.Form):
|
||||||
bio = wtforms.TextAreaField('Bio',
|
bio = wtforms.TextAreaField('Bio',
|
||||||
@ -42,3 +38,9 @@ class EditProfileForm(wtforms.Form):
|
|||||||
'Website',
|
'Website',
|
||||||
[wtforms.validators.Optional(),
|
[wtforms.validators.Optional(),
|
||||||
wtforms.validators.URL(message='Improperly formed URL')])
|
wtforms.validators.URL(message='Improperly formed URL')])
|
||||||
|
|
||||||
|
class EditAttachmentsForm(wtforms.Form):
|
||||||
|
attachment_name = wtforms.TextField(
|
||||||
|
'Title')
|
||||||
|
attachment_file = wtforms.FileField(
|
||||||
|
'File')
|
||||||
|
@ -17,6 +17,10 @@
|
|||||||
|
|
||||||
from webob import exc
|
from webob import exc
|
||||||
from string import split
|
from string import split
|
||||||
|
from cgi import FieldStorage
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from werkzeug.utils import secure_filename
|
||||||
|
|
||||||
from mediagoblin import messages
|
from mediagoblin import messages
|
||||||
from mediagoblin import mg_globals
|
from mediagoblin import mg_globals
|
||||||
@ -34,7 +38,6 @@ def edit_media(request, media):
|
|||||||
if not may_edit_media(request, media):
|
if not may_edit_media(request, media):
|
||||||
return exc.HTTPForbidden()
|
return exc.HTTPForbidden()
|
||||||
|
|
||||||
|
|
||||||
defaults = dict(
|
defaults = dict(
|
||||||
title=media['title'],
|
title=media['title'],
|
||||||
slug=media['slug'],
|
slug=media['slug'],
|
||||||
@ -44,7 +47,6 @@ def edit_media(request, media):
|
|||||||
if len(media['attachment_files']):
|
if len(media['attachment_files']):
|
||||||
defaults['attachment_name'] = media['attachment_files'][0]['name']
|
defaults['attachment_name'] = media['attachment_files'][0]['name']
|
||||||
|
|
||||||
|
|
||||||
form = forms.EditForm(
|
form = forms.EditForm(
|
||||||
request.POST,
|
request.POST,
|
||||||
**defaults)
|
**defaults)
|
||||||
@ -70,9 +72,11 @@ def edit_media(request, media):
|
|||||||
media['description'])
|
media['description'])
|
||||||
|
|
||||||
if 'attachment_name' in request.POST:
|
if 'attachment_name' in request.POST:
|
||||||
media['attachment_files'][0]['name'] = request.POST['attachment_name']
|
media['attachment_files'][0]['name'] = \
|
||||||
|
request.POST['attachment_name']
|
||||||
|
|
||||||
if 'attachment_delete' in request.POST and 'y' == request.POST['attachment_delete']:
|
if 'attachment_delete' in request.POST \
|
||||||
|
and 'y' == request.POST['attachment_delete']:
|
||||||
del media['attachment_files'][0]
|
del media['attachment_files'][0]
|
||||||
|
|
||||||
media['slug'] = request.POST['slug']
|
media['slug'] = request.POST['slug']
|
||||||
@ -88,7 +92,6 @@ def edit_media(request, media):
|
|||||||
request, messages.WARNING,
|
request, messages.WARNING,
|
||||||
"You are editing another user's media. Proceed with caution.")
|
"You are editing another user's media. Proceed with caution.")
|
||||||
|
|
||||||
|
|
||||||
return render_to_response(
|
return render_to_response(
|
||||||
request,
|
request,
|
||||||
'mediagoblin/edit/edit.html',
|
'mediagoblin/edit/edit.html',
|
||||||
@ -96,9 +99,60 @@ def edit_media(request, media):
|
|||||||
'form': form})
|
'form': form})
|
||||||
|
|
||||||
|
|
||||||
|
@get_user_media_entry
|
||||||
|
@require_active_login
|
||||||
|
def edit_attachments(request, media):
|
||||||
|
if mg_globals.app_config['allow_attachments']:
|
||||||
|
form = forms.EditAttachmentsForm()
|
||||||
|
|
||||||
|
# Add any attachements
|
||||||
|
if ('attachment_file' in request.POST
|
||||||
|
and isinstance(request.POST['attachment_file'], FieldStorage)
|
||||||
|
and request.POST['attachment_file'].file):
|
||||||
|
|
||||||
|
attachment_public_filepath \
|
||||||
|
= mg_globals.public_store.get_unique_filepath(
|
||||||
|
['media_entries', unicode(media['_id']), 'attachment',
|
||||||
|
secure_filename(request.POST['attachment_file'].filename)])
|
||||||
|
|
||||||
|
attachment_public_file = mg_globals.public_store.get_file(
|
||||||
|
attachment_public_filepath, 'wb')
|
||||||
|
|
||||||
|
try:
|
||||||
|
attachment_public_file.write(
|
||||||
|
request.POST['attachment_file'].file.read())
|
||||||
|
finally:
|
||||||
|
request.POST['attachment_file'].file.close()
|
||||||
|
|
||||||
|
media['attachment_files'].append(dict(
|
||||||
|
name=request.POST['attachment_name'] \
|
||||||
|
or request.POST['attachment_file'].filename,
|
||||||
|
filepath=attachment_public_filepath,
|
||||||
|
created=datetime.utcnow()
|
||||||
|
))
|
||||||
|
|
||||||
|
media.save()
|
||||||
|
|
||||||
|
messages.add_message(
|
||||||
|
request, messages.SUCCESS,
|
||||||
|
"You added the attachment %s!" \
|
||||||
|
% (request.POST['attachment_name']
|
||||||
|
or request.POST['attachment_file'].filename))
|
||||||
|
|
||||||
|
return redirect(request, 'mediagoblin.user_pages.media_home',
|
||||||
|
user=media.uploader()['username'],
|
||||||
|
media=media['slug'])
|
||||||
|
return render_to_response(
|
||||||
|
request,
|
||||||
|
'mediagoblin/edit/attachments.html',
|
||||||
|
{'media': media,
|
||||||
|
'form': form})
|
||||||
|
else:
|
||||||
|
return exc.HTTPForbidden()
|
||||||
|
|
||||||
|
|
||||||
@require_active_login
|
@require_active_login
|
||||||
def edit_profile(request):
|
def edit_profile(request):
|
||||||
|
|
||||||
# admins may edit any user profile given a username in the querystring
|
# admins may edit any user profile given a username in the querystring
|
||||||
edit_username = request.GET.get('username')
|
edit_username = request.GET.get('username')
|
||||||
if request.user['is_admin'] and request.user['username'] != edit_username:
|
if request.user['is_admin'] and request.user['username'] != edit_username:
|
||||||
|
@ -28,6 +28,3 @@ class SubmitStartForm(wtforms.Form):
|
|||||||
tags = wtforms.TextField(
|
tags = wtforms.TextField(
|
||||||
'Tags',
|
'Tags',
|
||||||
[tag_length_validator])
|
[tag_length_validator])
|
||||||
attachment = wtforms.FileField(
|
|
||||||
'Attachment',
|
|
||||||
[wtforms.validators.Optional()])
|
|
||||||
|
@ -76,31 +76,6 @@ def submit_start(request):
|
|||||||
# Generate a slug from the title
|
# Generate a slug from the title
|
||||||
entry.generate_slug()
|
entry.generate_slug()
|
||||||
|
|
||||||
# Add any attachements
|
|
||||||
if (mg_globals.app_config['allow_attachments']
|
|
||||||
and request.POST.has_key('attachment')
|
|
||||||
and isinstance(request.POST['attachment'], FieldStorage)
|
|
||||||
and request.POST['attachment'].file):
|
|
||||||
|
|
||||||
attachment_public_filepath = mg_globals.public_store.get_unique_filepath(
|
|
||||||
['media_entries',
|
|
||||||
unicode('attachment-%s' % entry['_id']),
|
|
||||||
secure_filename(request.POST['attachment'].filename)])
|
|
||||||
|
|
||||||
attachment_public_file = mg_globals.public_store.get_file(
|
|
||||||
attachment_public_filepath, 'wb')
|
|
||||||
|
|
||||||
try:
|
|
||||||
attachment_public_file.write(request.POST['attachment'].file.read())
|
|
||||||
finally:
|
|
||||||
request.POST['attachment'].file.close()
|
|
||||||
|
|
||||||
entry['attachment_files'] = [dict(
|
|
||||||
name=request.POST['attachment'].filename,
|
|
||||||
filepath=attachment_public_filepath,
|
|
||||||
created=datetime.utcnow()
|
|
||||||
)]
|
|
||||||
|
|
||||||
# Now store generate the queueing related filename
|
# Now store generate the queueing related filename
|
||||||
queue_filepath = request.app.queue_store.get_unique_filepath(
|
queue_filepath = request.app.queue_store.get_unique_filepath(
|
||||||
['media_entries',
|
['media_entries',
|
||||||
|
55
mediagoblin/templates/mediagoblin/edit/attachments.html
Normal file
55
mediagoblin/templates/mediagoblin/edit/attachments.html
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
{#
|
||||||
|
# GNU MediaGoblin -- federated, autonomous media hosting
|
||||||
|
# Copyright (C) 2011 Free Software Foundation, Inc
|
||||||
|
#
|
||||||
|
# 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 mediagoblin_content %}
|
||||||
|
<form action="{{ request.urlgen('mediagoblin.edit.attachments',
|
||||||
|
user= media.uploader().username,
|
||||||
|
media= media._id) }}"
|
||||||
|
method="POST" enctype="multipart/form-data">
|
||||||
|
<div class="grid_8 prefix_1 suffix_1 edit_box form_box">
|
||||||
|
<h1>Editing attachments for {{ media.title }}</h1>
|
||||||
|
<div style="text-align: center;" >
|
||||||
|
<img src="{{ request.app.public_store.file_url(
|
||||||
|
media['media_files']['thumb']) }}" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% if media.attachment_files|count %}
|
||||||
|
<h2>Attachments</h2>
|
||||||
|
<ul>
|
||||||
|
{% for attachment in media.attachment_files %}
|
||||||
|
<li>
|
||||||
|
<a target="_blank" href="{{ request.app.public_store.file_url(
|
||||||
|
attachment['filepath']) }}">
|
||||||
|
{{ attachment.name -}}
|
||||||
|
</a><br />
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
<h2>Add attachment</h2>
|
||||||
|
{{ wtforms_util.render_divs(form) }}
|
||||||
|
<div class="form_submit_buttons">
|
||||||
|
<a href="{{ media.url_for_self(request.urlgen) }}">Cancel</a>
|
||||||
|
<input type="submit" value="Save changes" class="button" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
{% endblock %}
|
@ -31,14 +31,7 @@
|
|||||||
<img src="{{ request.app.public_store.file_url(
|
<img src="{{ request.app.public_store.file_url(
|
||||||
media['media_files']['thumb']) }}" />
|
media['media_files']['thumb']) }}" />
|
||||||
</div>
|
</div>
|
||||||
{{ wtforms_util.render_field_div(form.title) }}
|
{{ wtforms_util.render_divs(form) }}
|
||||||
{{ wtforms_util.render_field_div(form.slug) }}
|
|
||||||
{{ wtforms_util.render_field_div(form.description) }}
|
|
||||||
{{ wtforms_util.render_field_div(form.tags) }}
|
|
||||||
{% if media.attachment_files %}
|
|
||||||
{{ wtforms_util.render_field_div(form.attachment_name) }}
|
|
||||||
{{ wtforms_util.render_field_div(form.attachment_delete) }}
|
|
||||||
{% endif %}
|
|
||||||
<div class="form_submit_buttons">
|
<div class="form_submit_buttons">
|
||||||
<a href="{{ media.url_for_self(request.urlgen) }}">Cancel</a>
|
<a href="{{ media.url_for_self(request.urlgen) }}">Cancel</a>
|
||||||
<input type="submit" value="Save changes" class="button" />
|
<input type="submit" value="Save changes" class="button" />
|
||||||
|
@ -28,9 +28,6 @@
|
|||||||
{{ wtforms_util.render_field_div(submit_form.title) }}
|
{{ wtforms_util.render_field_div(submit_form.title) }}
|
||||||
{{ wtforms_util.render_textarea_div(submit_form.description) }}
|
{{ wtforms_util.render_textarea_div(submit_form.description) }}
|
||||||
{{ wtforms_util.render_field_div(submit_form.tags) }}
|
{{ wtforms_util.render_field_div(submit_form.tags) }}
|
||||||
{% if app_config.allow_attachments %}
|
|
||||||
{{ wtforms_util.render_field_div(submit_form.attachment) }}
|
|
||||||
{% endif %}
|
|
||||||
<div class="form_submit_buttons">
|
<div class="form_submit_buttons">
|
||||||
<input type="submit" value="Submit" class="button" />
|
<input type="submit" value="Submit" class="button" />
|
||||||
</div>
|
</div>
|
||||||
|
@ -104,20 +104,8 @@
|
|||||||
<div class="grid_5 omega">
|
<div class="grid_5 omega">
|
||||||
{% include "mediagoblin/utils/prev_next.html" %}
|
{% include "mediagoblin/utils/prev_next.html" %}
|
||||||
<h3>Sidebar content here!</h3>
|
<h3>Sidebar content here!</h3>
|
||||||
|
{% if media.attachment_files or media['uploader'] == request.user['_id'] or
|
||||||
{% if media.attachment_files %}
|
request.user['is_admin'] %}
|
||||||
<dl>
|
|
||||||
<dd>Attachments</dd>
|
|
||||||
{% for attachment in media.attachment_files %}
|
|
||||||
<dt>
|
|
||||||
<a href="{{ request.app.public_store.file_url(
|
|
||||||
attachment.filepath) }}">
|
|
||||||
{{ attachment.name }}
|
|
||||||
</a>
|
|
||||||
</dt>
|
|
||||||
{% endfor %}
|
|
||||||
</dl>
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
{% if media['uploader'] == request.user['_id'] or
|
{% if media['uploader'] == request.user['_id'] or
|
||||||
@ -136,6 +124,26 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
{% if media.attachment_files|count %}
|
||||||
|
<h3>Attachments</h3>
|
||||||
|
<ul>
|
||||||
|
{% for attachment in media.attachment_files %}
|
||||||
|
<li>
|
||||||
|
<a href="{{ request.app.public_store.file_url(
|
||||||
|
attachment.filepath) }}">
|
||||||
|
{{ attachment.name }}
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
{% endif %}
|
||||||
|
</ul>
|
||||||
|
{% endif %}
|
||||||
|
{% if app_config['allow_attachments'] %}
|
||||||
|
<a href="{{ request.urlgen('mediagoblin.edit.attachments',
|
||||||
|
user= media.uploader().username,
|
||||||
|
media= media._id) }}">Add attachment</a>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
{% if media.tags %}
|
{% if media.tags %}
|
||||||
{% include "mediagoblin/utils/tags.html" %}
|
{% include "mediagoblin/utils/tags.html" %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -29,6 +29,9 @@ user_routes = [
|
|||||||
controller="mediagoblin.user_pages.views:media_home"),
|
controller="mediagoblin.user_pages.views:media_home"),
|
||||||
Route('mediagoblin.edit.edit_media', "/{user}/m/{media}/edit/",
|
Route('mediagoblin.edit.edit_media', "/{user}/m/{media}/edit/",
|
||||||
controller="mediagoblin.edit.views:edit_media"),
|
controller="mediagoblin.edit.views:edit_media"),
|
||||||
|
Route('mediagoblin.edit.attachments',
|
||||||
|
'/{user}/m/{media}/attachments/',
|
||||||
|
controller="mediagoblin.edit.views:edit_attachments"),
|
||||||
Route('mediagoblin.user_pages.atom_feed', '/{user}/atom/',
|
Route('mediagoblin.user_pages.atom_feed', '/{user}/atom/',
|
||||||
controller="mediagoblin.user_pages.views:atom_feed"),
|
controller="mediagoblin.user_pages.views:atom_feed"),
|
||||||
Route('mediagoblin.user_pages.media_post_comment',
|
Route('mediagoblin.user_pages.media_post_comment',
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
from webob import exc
|
from webob import exc
|
||||||
|
|
||||||
from mediagoblin import messages
|
from mediagoblin import messages, mg_globals
|
||||||
from mediagoblin.db.util import DESCENDING, ObjectId
|
from mediagoblin.db.util import DESCENDING, ObjectId
|
||||||
from mediagoblin.util import (
|
from mediagoblin.util import (
|
||||||
Pagination, render_to_response, redirect, cleaned_markdown_conversion)
|
Pagination, render_to_response, redirect, cleaned_markdown_conversion)
|
||||||
@ -117,7 +117,8 @@ def media_home(request, media, page, **kwargs):
|
|||||||
{'media': media,
|
{'media': media,
|
||||||
'comments': comments,
|
'comments': comments,
|
||||||
'pagination': pagination,
|
'pagination': pagination,
|
||||||
'comment_form': comment_form})
|
'comment_form': comment_form,
|
||||||
|
'app_config': mg_globals.app_config})
|
||||||
|
|
||||||
|
|
||||||
@require_active_login
|
@require_active_login
|
||||||
|
Loading…
x
Reference in New Issue
Block a user