def test_list_of_dicts_conversion(test_app): """ When the user adds tags to a media entry, the string from the form is converted into a list of tags, where each tag is stored in the database as a dict. Each tag dict should contain the tag's name and slug. Another function performs the reverse operation when populating a form to edit tags. """ # Leading, trailing, and internal whitespace should be removed and slugified assert text.convert_to_tag_list_of_dicts('sleep , 6 AM, chainsaw! ') == [ {'name': u'sleep', 'slug': u'sleep'}, {'name': u'6 AM', 'slug': u'6-am'}, {'name': u'chainsaw!', 'slug': u'chainsaw'}] # If the user enters two identical tags, record only one of them assert text.convert_to_tag_list_of_dicts('echo,echo') == [{'name': u'echo', 'slug': u'echo'}] # When checking for duplicates, use the slug, not the tag assert text.convert_to_tag_list_of_dicts('echo,#echo') == [{'name': u'#echo', 'slug': u'echo'}] # Make sure converting the list of dicts to a string works assert text.media_tags_as_string([{'name': u'yin', 'slug': u'yin'}, {'name': u'yang', 'slug': u'yang'}]) == \ u'yin, yang'
def edit_media(request, media): if not may_edit_media(request, media): return exc.HTTPForbidden() defaults = dict( title=media.title, slug=media.slug, description=media.description, tags=media_tags_as_string(media.tags), license=media.license) form = forms.EditForm( request.form, **defaults) if request.method == 'POST' and form.validate(): # Make sure there isn't already a MediaEntry with such a slug # and userid. slug_used = check_media_slug_used(request.db, media.uploader, request.form['slug'], media.id) if slug_used: form.slug.errors.append( _(u'An entry with that slug already exists for this user.')) else: media.title = unicode(request.form['title']) media.description = unicode(request.form.get('description')) media.tags = convert_to_tag_list_of_dicts( request.form.get('tags')) media.license = unicode(request.form.get('license', '')) or None media.slug = unicode(request.form['slug']) media.save() return exc.HTTPFound( location=media.url_for_self(request.urlgen)) if request.user.is_admin \ and media.uploader != request.user._id \ and request.method != 'POST': messages.add_message( request, messages.WARNING, _("You are editing another user's media. Proceed with caution.")) return render_to_response( request, 'mediagoblin/edit/edit.html', {'media': media, 'form': form})
def edit_media(request, media): if not may_edit_media(request, media): raise Forbidden("User may not edit this media") defaults = dict( title=media.title, slug=media.slug, description=media.description, tags=media_tags_as_string(media.tags), license=media.license) form = forms.EditForm( request.form, **defaults) if request.method == 'POST' and form.validate(): # Make sure there isn't already a MediaEntry with such a slug # and userid. slug = slugify(form.slug.data) slug_used = check_media_slug_used(media.uploader, slug, media.id) if slug_used: form.slug.errors.append( _(u'An entry with that slug already exists for this user.')) else: media.title = form.title.data media.description = form.description.data media.tags = convert_to_tag_list_of_dicts( form.tags.data) media.license = unicode(form.license.data) or None media.slug = slug media.save() return redirect_obj(request, media) if request.user.has_privilege(u'admin') \ and media.uploader != request.user.id \ and request.method != 'POST': messages.add_message( request, messages.WARNING, _("You are editing another user's media. Proceed with caution.")) return render_to_response( request, 'mediagoblin/edit/edit.html', {'media': media, 'form': form})
def edit_media(request, media): # If media is not processed, return NotFound. if not media.state == 'processed': return render_404(request) if not may_edit_media(request, media): raise Forbidden("User may not edit this media") defaults = dict(title=media.title, slug=media.slug, description=media.description, tags=media_tags_as_string(media.tags), license=media.license) form = forms.EditForm(request.method == 'POST' and request.form or None, **defaults) if request.method == 'POST' and form.validate(): # Make sure there isn't already a MediaEntry with such a slug # and userid. slug = slugify(form.slug.data) slug_used = check_media_slug_used(media.actor, slug, media.id) if slug_used: form.slug.errors.append( _('An entry with that slug already exists for this user.')) else: media.title = form.title.data media.description = form.description.data media.tags = convert_to_tag_list_of_dicts(form.tags.data) media.license = str(form.license.data) or None media.slug = slug media.save() return redirect_obj(request, media) if request.user.has_privilege('admin') \ and media.actor != request.user.id \ and request.method != 'POST': messages.add_message( request, messages.WARNING, _("You are editing another user's media. Proceed with caution.")) return render_to_response(request, 'mediagoblin/edit/edit.html', { 'media': media, 'form': form })
def blogpost_edit(request): blog_slug = request.matchdict.get('blog_slug', None) blog_post_slug = request.matchdict.get('blog_post_slug', None) blogpost = request.db.MediaEntry.query.filter_by(slug=blog_post_slug, actor=request.user.id).first() blog = get_blog_by_slug(request, blog_slug, author=request.user.id) if not blogpost or not blog: return render_404(request) defaults = dict( title = blogpost.title, description = cleaned_markdown_conversion(blogpost.description), tags=media_tags_as_string(blogpost.tags), license=blogpost.license) form = blog_forms.BlogPostEditForm(request.form, **defaults) if request.method == 'POST' and form.validate(): blogpost.title = six.text_type(form.title.data) blogpost.description = six.text_type(cleaned_markdown_conversion((form.description.data))) blogpost.tags = convert_to_tag_list_of_dicts(form.tags.data) blogpost.license = six.text_type(form.license.data) set_blogpost_state(request, blogpost) blogpost.generate_slug() blogpost.save() messages.add_message( request, messages.SUCCESS, _('Woohoo! edited blogpost is submitted')) return redirect(request, "mediagoblin.media_types.blog.blog-dashboard", user=request.user.username, blog_slug=blog.slug) return render_to_response( request, 'mediagoblin/blog/blog_post_edit_create.html', {'form': form, 'app_config': mg_globals.app_config, 'user': request.user.username, 'blog_post_slug': blog_post_slug })
def blogpost_edit(request): blog_slug = request.matchdict.get('blog_slug', None) blog_post_slug = request.matchdict.get('blog_post_slug', None) blogpost = request.db.MediaEntry.query.filter_by( slug=blog_post_slug, uploader=request.user.id).first() blog = get_blog_by_slug(request, blog_slug, author=request.user.id) if not blogpost or not blog: return render_404(request) defaults = dict(title=blogpost.title, description=cleaned_markdown_conversion( blogpost.description), tags=media_tags_as_string(blogpost.tags), license=blogpost.license) form = blog_forms.BlogPostEditForm(request.form, **defaults) if request.method == 'POST' and form.validate(): blogpost.title = six.text_type(form.title.data) blogpost.description = six.text_type( cleaned_markdown_conversion((form.description.data))) blogpost.tags = convert_to_tag_list_of_dicts(form.tags.data) blogpost.license = six.text_type(form.license.data) set_blogpost_state(request, blogpost) blogpost.generate_slug() blogpost.save() add_message(request, SUCCESS, _('Woohoo! edited blogpost is submitted')) return redirect(request, "mediagoblin.media_types.blog.blog-dashboard", user=request.user.username, blog_slug=blog.slug) return render_to_response( request, 'mediagoblin/blog/blog_post_edit_create.html', { 'form': form, 'app_config': mg_globals.app_config, 'user': request.user.username, 'blog_post_slug': blog_post_slug })
def test_list_of_dicts_conversion(test_app): """ When the user adds tags to a media entry, the string from the form is converted into a list of tags, where each tag is stored in the database as a dict. Each tag dict should contain the tag's name and slug. Another function performs the reverse operation when populating a form to edit tags. """ # Leading, trailing, and internal whitespace should be removed and slugified assert text.convert_to_tag_list_of_dicts('sleep , 6 AM, chainsaw! ') == [ {'name': u'sleep', 'slug': u'sleep'}, {'name': u'6 AM', 'slug': u'6-am'}, {'name': u'chainsaw!', 'slug': u'chainsaw'}] # If the user enters two identical tags, record only one of them assert text.convert_to_tag_list_of_dicts('echo,echo') == [{'name': u'echo', 'slug': u'echo'}] # Make sure converting the list of dicts to a string works assert text.media_tags_as_string([{'name': u'yin', 'slug': u'yin'}, {'name': u'yang', 'slug': u'yang'}]) == \ u'yin, yang'
def edit_media(request, media): if not may_edit_media(request, media): raise Forbidden("User may not edit this media") defaults = dict( title=media.title, slug=media.slug, description=media.description, tags=media_tags_as_string(media.tags), license=media.license, ) form = forms.EditForm(request.form, **defaults) if request.method == "POST" and form.validate(): # Make sure there isn't already a MediaEntry with such a slug # and userid. slug_used = check_media_slug_used(request.db, media.uploader, request.form["slug"], media.id) if slug_used: form.slug.errors.append(_(u"An entry with that slug already exists for this user.")) else: media.title = unicode(request.form["title"]) media.description = unicode(request.form.get("description")) media.tags = convert_to_tag_list_of_dicts(request.form.get("tags")) media.license = unicode(request.form.get("license", "")) or None media.slug = unicode(request.form["slug"]) media.save() return redirect(request, location=media.url_for_self(request.urlgen)) if request.user.is_admin and media.uploader != request.user.id and request.method != "POST": messages.add_message( request, messages.WARNING, _("You are editing another user's media. Proceed with caution.") ) return render_to_response(request, "mediagoblin/edit/edit.html", {"media": media, "form": form})