and request.POST['file'].file):
submit_form.file.errors.append(
_(u'You must provide a file.'))
- elif not security.check_filetype(request.POST['file']):
- submit_form.file.errors.append(
- _(u"The file doesn't seem to be an image!"))
else:
- filename = request.POST['file'].filename
-
- # create entry and save in database
- entry = request.db.MediaEntry()
- entry['_id'] = ObjectId()
- entry['title'] = (
- unicode(request.POST['title'])
- or unicode(splitext(filename)[0]))
-
- entry['description'] = unicode(request.POST.get('description'))
- entry['description_html'] = cleaned_markdown_conversion(
- entry['description'])
-
- entry['license'] = (
- unicode(request.POST.get('license'))
- or '')
-
- entry['media_type'] = u'image' # heh
- entry['uploader'] = request.user['_id']
-
- # Process the user's folksonomy "tags"
- entry['tags'] = convert_to_tag_list_of_dicts(
- request.POST.get('tags'))
-
- # Generate a slug from the title
- entry.generate_slug()
-
- # Now store generate the queueing related filename
- queue_filepath = request.app.queue_store.get_unique_filepath(
- ['media_entries',
- unicode(entry['_id']),
- secure_filename(filename)])
-
- # queue appropriately
- queue_file = request.app.queue_store.get_file(
- queue_filepath, 'wb')
-
- with queue_file:
- queue_file.write(request.POST['file'].file.read())
-
- # Add queued filename to the entry
- entry['queued_media_file'] = queue_filepath
-
- # We generate this ourselves so we know what the taks id is for
- # retrieval later.
- # (If we got it off the task's auto-generation, there'd be a risk of
- # a race condition when we'd save after sending off the task)
- task_id = unicode(uuid.uuid4())
- entry['queued_task_id'] = task_id
-
- # Save now so we have this data before kicking off processing
- entry.save(validate=True)
-
- # Pass off to processing
- #
- # (... don't change entry after this point to avoid race
- # conditions with changes to the document via processing code)
try:
- process_media.apply_async(
- [unicode(entry['_id'])], {},
- task_id=task_id)
- except BaseException as exc:
- # The purpose of this section is because when running in "lazy"
- # or always-eager-with-exceptions-propagated celery mode that
- # the failure handling won't happen on Celery end. Since we
- # expect a lot of users to run things in this way we have to
- # capture stuff here.
- #
- # ... not completely the diaper pattern because the exception is
- # re-raised :)
- mark_entry_failed(entry[u'_id'], exc)
- # re-raise the exception
- raise
+ filename = request.POST['file'].filename
+ media_type, media_manager = get_media_type_and_manager(filename)
+
+ # create entry and save in database
+ entry = request.db.MediaEntry()
+ entry['_id'] = ObjectId()
+ entry.media_type = unicode(media_type)
+ entry.title = (
+ unicode(request.POST['title'])
+ or unicode(splitext(filename)[0]))
+
+ entry.description = unicode(request.POST.get('description'))
+ entry.description_html = cleaned_markdown_conversion(
+ entry.description)
+
++ entry['license'] = (
++ unicode(request.POST.get('license'))
++ or '')
++
+ entry.uploader = request.user._id
+
+ # Process the user's folksonomy "tags"
+ entry['tags'] = convert_to_tag_list_of_dicts(
+ request.POST.get('tags'))
+
+ # Generate a slug from the title
+ entry.generate_slug()
- add_message(request, SUCCESS, _('Woohoo! Submitted!'))
- return redirect(request, "mediagoblin.user_pages.user_home",
- user = request.user['username'])
+ # Now store generate the queueing related filename
+ queue_filepath = request.app.queue_store.get_unique_filepath(
+ ['media_entries',
+ unicode(entry._id),
+ secure_filename(filename)])
+
+ # queue appropriately
+ queue_file = request.app.queue_store.get_file(
+ queue_filepath, 'wb')
+
+ with queue_file:
+ queue_file.write(request.POST['file'].file.read())
+
+ # Add queued filename to the entry
+ entry.queued_media_file = queue_filepath
+
+ # We generate this ourselves so we know what the taks id is for
+ # retrieval later.
+
+ # (If we got it off the task's auto-generation, there'd be
+ # a risk of a race condition when we'd save after sending
+ # off the task)
+ task_id = unicode(uuid.uuid4())
+ entry['queued_task_id'] = task_id
+
+ # Save now so we have this data before kicking off processing
+ entry.save(validate=True)
+
+ # Pass off to processing
+ #
+ # (... don't change entry after this point to avoid race
+ # conditions with changes to the document via processing code)
+ process_media = registry.tasks[ProcessMedia.name]
+ try:
+ process_media.apply_async(
+ [unicode(entry._id)], {},
+ task_id=task_id)
+ except BaseException as exc:
+ # The purpose of this section is because when running in "lazy"
+ # or always-eager-with-exceptions-propagated celery mode that
+ # the failure handling won't happen on Celery end. Since we
+ # expect a lot of users to run things in this way we have to
+ # capture stuff here.
+ #
+ # ... not completely the diaper pattern because the
+ # exception is re-raised :)
+ mark_entry_failed(entry._id, exc)
+ # re-raise the exception
+ raise
+
+ add_message(request, SUCCESS, _('Woohoo! Submitted!'))
+
+ return redirect(request, "mediagoblin.user_pages.user_home",
+ user=request.user.username)
+ except Exception as e:
+ '''
+ This section is intended to catch exceptions raised in
+ mediagobling.media_types
+ '''
+
+ if isinstance(e, InvalidFileType) or \
+ isinstance(e, FileTypeNotSupported):
+ submit_form.file.errors.append(
+ e)
+ else:
+ raise
return render_to_response(
request,
src="{{ display_media }}"
alt="Image for {{ media.title }}" />
{% endif %}
- </div>
-
- <h2 class="media_title">
- {{ media.title }}
- </h2>
-
- <p class="media_uploader">
- {% trans date=media.created.strftime("%Y-%m-%d"),
- user_url=request.urlgen(
- 'mediagoblin.user_pages.user_home',
- user=media.uploader().username),
- username=media.uploader().username -%}
- Uploaded on {{ date }} by <a href="{{ user_url }}">{{ username }}</a>
- {%- endtrans %}
- </p>
-
- {% autoescape False %}
- <p>{{ media.description_html }}</p>
- {% endautoescape %}
-
- <br />
- <h3>{% trans %}Comments{% endtrans %}</h3>
-
+ {% endblock %}
+ </div>
+ <h2 class="media_title">
+ {{ media.title }}
+ </h2>
+ {% autoescape False %}
+ <p>{{ media.description_html }}</p>
+ {% endautoescape %}
+ <p class="media_specs">
+ {% trans date=media.created.strftime("%Y-%m-%d") -%}
+ Added on {{ date }}.
+ {%- endtrans %}
+ {% if request.user and
+ (media.uploader == request.user._id or
+ request.user.is_admin) %}
+ {% set edit_url = request.urlgen('mediagoblin.edit.edit_media',
+ user= media.get_uploader.username,
+ media= media._id) %}
+ <a class="button_action" href="{{ edit_url }}">{% trans %}Edit{% endtrans %}</a>
+ {% set delete_url = request.urlgen('mediagoblin.user_pages.media_confirm_delete',
+ user= media.get_uploader.username,
+ media= media._id) %}
+ <a class="button_action" href="{{ delete_url }}">{% trans %}Delete{% endtrans %}</a>
+ {% endif %}
+ </p>
+ {% if comments %}
+ <h3>
+ {% if comments.count()==1 %}
+ {% trans comment_count=comments.count() -%}{{ comment_count }} comment{%- endtrans %}
+ {% elif comments.count()>1 %}
+ {% trans comment_count=comments.count() -%}{{ comment_count }} comments{%- endtrans %}
+ {% else %}
+ {% trans %}No comments yet.{% endtrans %}
+ {% endif %}
+ <div class="right_align">
+ <a
+ {% if not request.user %}
+ href="{{ request.urlgen('mediagoblin.auth.login') }}"
+ {% endif %}
+ class="button_action" id="button_addcomment" title="Add a comment">
+ {% trans %}Add one{% endtrans %}
+ </a>
+ </div>
+ </h3>
{% if request.user %}
- <p><a href="#comment_form">{% trans %}Post a comment{% endtrans %}</a></p>
+ <form action="{{ request.urlgen('mediagoblin.user_pages.media_post_comment',
+ user= media.get_uploader.username,
+ media=media._id) }}" method="POST" id="form_comment">
+ <p>
+ {% trans %}You can use <a href="http://daringfireball.net/projects/markdown/basics">Markdown</a> for formatting.{% endtrans %}
+ </p>
+ {{ wtforms_util.render_divs(comment_form) }}
+ <div class="form_submit_buttons">
+ <input type="submit" value="{% trans %}Add this comment{% endtrans %}" class="button_action" />
+ {{ csrf_token }}
+ </div>
+ </form>
{% endif %}
-
- {% if comments %}
- {% for comment in comments %}
- {% set comment_author = comment.author() %}
- {% if pagination.active_id == comment._id %}
- <div class="comment_wrapper comment_active" id="comment-{{ comment['_id'] }}">
- <a name="comment" id="comment"></a>
- {% else %}
- <div class="comment_wrapper" id="comment-{{ comment['_id'] }}">
- {% endif %}
-
- <div class="comment_content">
- {% autoescape False %}
- {{ comment.content_html }}
- {% endautoescape %}
- </div>
-
- <div class="comment_author">—
- <a href="{{ request.urlgen('mediagoblin.user_pages.user_home',
- user = comment_author['username']) }}">
- {{ comment_author['username'] }}</a>
- {% trans %}at{% endtrans %}
- <a href="{{ request.urlgen('mediagoblin.user_pages.media_home.view_comment',
- comment = comment['_id'],
- user = media.uploader().username,
- media = media._id) }}#comment">
- {{ comment.created.strftime("%Y-%m-%d %I:%M%p") }}
- </a>
- </div>
+ {% for comment in comments %}
+ {% set comment_author = comment.get_author %}
+ {% if pagination.active_id == comment._id %}
+ <div class="comment_wrapper comment_active" id="comment-{{ comment._id }}">
+ <a name="comment" id="comment"></a>
+ {% else %}
+ <div class="comment_wrapper" id="comment-{{ comment._id }}">
+ {% endif %}
+ <div class="comment_content">
+ {% autoescape False %}
+ {{ comment.content_html }}
+ {% endautoescape %}
+ <img src="{{ request.staticdirect('/images/icon_comment.png') }}" />
+ <a href="{{ request.urlgen('mediagoblin.user_pages.user_home',
+ user = comment_author.username) }}">
+ {{ comment_author.username }}
+ </a>
+ {% trans %}at{% endtrans %}
+ <a href="{{ request.urlgen('mediagoblin.user_pages.media_home.view_comment',
+ comment = comment._id,
+ user = media.get_uploader.username,
+ media = media.slug) }}#comment">
+ {{ comment.created.strftime("%I:%M%p %Y-%m-%d") }}
+ </a>
</div>
+ </div>
+ {% endfor %}
+ {{ render_pagination(request, pagination,
+ media.url_for_self(request.urlgen)) }}
+ {% endif %}
+ </div>
+ <div class="media_sidebar">
+ {% trans user_url=request.urlgen(
+ 'mediagoblin.user_pages.user_home',
+ user=media.get_uploader.username),
+ username=media.get_uploader.username -%}
+ <p>❖ Browsing media by <a href="{{ user_url }}">{{ username }}</a></p>
+ {%- endtrans %}
+ {% include "mediagoblin/utils/prev_next.html" %}
+ {% 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 %}
-
- {% if request.user %}
- <form action="{{ request.urlgen('mediagoblin.user_pages.media_post_comment',
- user= media.uploader().username,
- media=media._id) }}" method="POST">
- {{ wtforms_util.render_divs(comment_form) }}
- <div class="form_submit_buttons">
- <input type="submit" value="{% trans %}Post comment!{% endtrans %}" class="button" />
- {{ csrf_token }}
- </div>
- </form>
- {% endif %}
-
- {{ render_pagination(request, pagination,
- request.urlgen('mediagoblin.user_pages.media_home',
- user = media.uploader().username,
- media = media._id)) }}
- </div>
+ </ul>
+ {% endif %}
+ {% if app_config['allow_attachments']
+ and request.user
+ and (media.uploader == request.user._id
+ or request.user.is_admin) %}
+ <p>
+ <a href="{{ request.urlgen('mediagoblin.edit.attachments',
+ user=media.get_uploader.username,
+ media=media._id) }}">Add attachment</a>
+ </p>
+ {% endif %}
+ {% if media.tags %}
+ {% include "mediagoblin/utils/tags.html" %}
{% endif %}
- <div class="grid_5 omega">
- {% include "mediagoblin/utils/prev_next.html" %}
-
- {% if media['uploader'] == request.user['_id'] or
- request.user['is_admin'] %}
- <h3>{% trans %}Actions{% endtrans %}</h3>
- <p>
- {% set edit_url = request.urlgen('mediagoblin.edit.edit_media',
- user= media.uploader().username,
- media= media._id) %}
- <a href="{{ edit_url }}"
- ><img src="{{ request.staticdirect('/images/icon_edit.png') }}"
- class="media_icon" /></a>
- <a href="{{ edit_url }}">{% trans %}edit{% endtrans %}</a>
- </p>
- <p>
- {% set delete_url = request.urlgen('mediagoblin.user_pages.media_confirm_delete',
- user= media.uploader().username,
- media= media._id) %}
- <a href="{{ delete_url }}"
- ><img src="{{ request.staticdirect('/images/icon_delete.png') }}"
- class="media_icon" /></a>
- <a href="{{ delete_url }}">{% trans %}delete{% endtrans %}</a>
- </p>
- {% endif %}
-
- {% 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 %}
- </ul>
- {% endif %}
-
- {% if app_config['allow_attachments']
- and (media['uploader'] == request.user['_id']
- or request.user['is_admin']) %}
- <p>
- <a href="{{ request.urlgen('mediagoblin.edit.attachments',
- user=media.uploader().username,
- media=media._id) }}">Add attachment</a>
- </p>
- {% endif %}
-
- {% if media.tags %}
- {% include "mediagoblin/utils/tags.html" %}
- {% endif %}
-
- {% include "mediagoblin/utils/license.html" %}
- </div>
- {% else %}
- <p>{% trans %}Sorry, no such media found.{% endtrans %}<p/>
- {% endif %}
+
++ {% include "mediagoblin/utils/license.html" %}
+ </div>
{% endblock %}