From: Boris Bobrov Date: Tue, 11 Mar 2014 11:23:58 +0000 (+0500) Subject: fixes to blog url generation X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=36990574cf2cad572b1dd52653666605306022da;p=mediagoblin.git fixes to blog url generation --- diff --git a/mediagoblin/media_types/blog/lib.py b/mediagoblin/media_types/blog/lib.py index 62696b55..b6e3dc06 100644 --- a/mediagoblin/media_types/blog/lib.py +++ b/mediagoblin/media_types/blog/lib.py @@ -45,5 +45,12 @@ def get_all_blogposts_of_blog(request, blog, state=None): blog_posts_list.append(blog_post) blog_posts_list.reverse() return blog_posts_list - + +def get_blog_by_slug(request, slug, **kwargs): + if slug.startswith('blog_'): + blog_id = int(slug[5:]) + blog = request.db.Blog.query.filter_by(id=blog_id, **kwargs).first() + else: + blog = request.db.Blog.query.filter_by(slug=slug, **kwargs).first() + return blog diff --git a/mediagoblin/media_types/blog/models.py b/mediagoblin/media_types/blog/models.py index 7c55e359..0e1ddf97 100644 --- a/mediagoblin/media_types/blog/models.py +++ b/mediagoblin/media_types/blog/models.py @@ -34,6 +34,7 @@ class BlogMixin(GenerateSlugMixin): def check_slug_used(self, slug): return check_blog_slug_used(self.author, slug, self.id) + class Blog(Base, BlogMixin): __tablename__ = "mediatype__blogs" id = Column(Integer, primary_key=True) @@ -42,7 +43,10 @@ class Blog(Base, BlogMixin): author = Column(Integer, ForeignKey(User.id), nullable=False, index=True) #similar to uploader created = Column(DateTime, nullable=False, default=datetime.datetime.now, index=True) slug = Column(Unicode) - + + @property + def slug_or_id(self): + return (self.slug or u'blog_{0}'.format(self.id)) def get_all_blog_posts(self, state=None): blog_posts = Session.query(MediaEntry).join(BlogPostData)\ diff --git a/mediagoblin/media_types/blog/templates/mediagoblin/blog/blog_post_listing.html b/mediagoblin/media_types/blog/templates/mediagoblin/blog/blog_post_listing.html index 8d7a405e..0013f1a3 100644 --- a/mediagoblin/media_types/blog/templates/mediagoblin/blog/blog_post_listing.html +++ b/mediagoblin/media_types/blog/templates/mediagoblin/blog/blog_post_listing.html @@ -59,7 +59,7 @@

{% set blog_about_url = request.urlgen('mediagoblin.media_types.blog.blog_about', - blog_slug=blog.slug, user=blog_owner_name) %} + blog_slug=blog.slug_or_id, user=blog_owner_name) %} About Blog
{{ render_pagination(request, pagination) }} diff --git a/mediagoblin/media_types/blog/templates/mediagoblin/blog/list_of_blogs.html b/mediagoblin/media_types/blog/templates/mediagoblin/blog/list_of_blogs.html index f19a9225..8c16daeb 100644 --- a/mediagoblin/media_types/blog/templates/mediagoblin/blog/list_of_blogs.html +++ b/mediagoblin/media_types/blog/templates/mediagoblin/blog/list_of_blogs.html @@ -32,14 +32,14 @@ {% for blog in blogs %} {% set others_blog_url = request.urlgen('mediagoblin.media_types.blog.blog_post_listing', - blog_slug=blog.slug, user=user.username) %} + blog_slug=blog.slug_or_id, user=user.username) %} {% if not request.user or request.user.username != user.username%} {% else %} {% set my_blog_url = request.urlgen('mediagoblin.media_types.blog.blog-dashboard', - blog_slug=blog.slug, user=request.user.username) %} + blog_slug=blog.slug_or_id, user=request.user.username) %} {% endif %} diff --git a/mediagoblin/media_types/blog/views.py b/mediagoblin/media_types/blog/views.py index 042881e4..a367bef8 100644 --- a/mediagoblin/media_types/blog/views.py +++ b/mediagoblin/media_types/blog/views.py @@ -26,7 +26,9 @@ from mediagoblin import mg_globals from mediagoblin.media_types.blog import forms as blog_forms from mediagoblin.media_types.blog.models import Blog, BlogPostData -from mediagoblin.media_types.blog.lib import may_edit_blogpost, set_blogpost_state, get_all_blogposts_of_blog +from mediagoblin.media_types.blog.lib import ( + may_edit_blogpost, set_blogpost_state, get_all_blogposts_of_blog, + get_blog_by_slug) from mediagoblin.messages import add_message, SUCCESS, ERROR from mediagoblin.decorators import (require_active_login, active_user_from_url, @@ -91,7 +93,7 @@ def blog_edit(request): #Blog already exists. else: - blog = request.db.Blog.query.filter_by(slug=blog_slug).first() + blog = get_blog_by_slug(request, blog_slug) if not blog: return render_404(request) if request.method == 'GET': @@ -129,8 +131,7 @@ def blogpost_create(request): if request.method == 'POST' and form.validate(): blog_slug = request.matchdict.get('blog_slug') - blog = request.db.Blog.query.filter_by(slug=blog_slug, - author=request.user.id).first() + blog = get_blog_by_slug(request, blog_slug, author=request.user.id) if not blog: return render_404(request) @@ -173,7 +174,7 @@ def blogpost_edit(request): 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 = request.db.Blog.query.filter_by(slug=blog_slug, author=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) @@ -222,7 +223,7 @@ def blog_dashboard(request, page, url_user=None): max_blog_count = config['max_blog_count'] if request.user and (request.user.id == url_user.id or request.user.has_privilege(u'admin')): if blog_slug: - blog = blogs.filter(Blog.slug==blog_slug).first() + blog = get_blog_by_slug(request, blog_slug) if not blog: return render_404(request) else: @@ -259,7 +260,7 @@ def blog_post_listing(request, page, url_user=None): Page, listing all the blog posts of a particular blog. """ blog_slug = request.matchdict.get('blog_slug', None) - blog = request.db.Blog.query.filter_by(slug=blog_slug).first() + blog = get_blog_by_slug(request, blog_slug, author=request.user.id) if not blog: return render_404(request) @@ -280,12 +281,10 @@ def blog_post_listing(request, page, url_user=None): @require_active_login def draft_view(request): - blog_slug = request.matchdict.get('blog_slug', None) blog_post_slug = request.matchdict.get('blog_post_slug', None) user = request.matchdict.get('user') - - blog = request.db.Blog.query.filter_by(author=request.user.id, slug=blog_slug).first() + blog = get_blog_by_slug(request, blog_slug, author=request.user.id) blogpost = request.db.MediaEntry.query.filter_by(state = u'failed', uploader=request.user.id, slug=blog_post_slug).first() if not blog or not blogpost: @@ -308,7 +307,7 @@ def blog_delete(request, **kwargs): owner_user = request.db.User.query.filter_by(username=url_user).first() blog_slug = request.matchdict.get('blog_slug', None) - blog = request.db.Blog.query.filter_by(slug=blog_slug, author=owner_user.id).first() + blog = get_blog_by_slug(request, blog_slug, author=owner_user.id) if not blog: return render_404(reequest) @@ -355,7 +354,7 @@ def blog_about_view(request): url_user = request.matchdict.get('user', None) user = request.db.User.query.filter_by(username=url_user).first() - blog = request.db.Blog.query.filter_by(author=user.id, slug=blog_slug).first() + blog = get_blog_by_slug(request, blog_slug, author=user.id) if not user or not blog: return render_404(request)
{{ blog.title }}{{ blog.title }}