Ticket #858: 0002-fixes-to-blog-url-generation.patch

File 0002-fixes-to-blog-url-generation.patch, 8.6 KB (added by Boris Bobrov, 11 years ago)
  • mediagoblin/media_types/blog/lib.py

    From f53e0ec82db9b89942769f177cfa1db59af4c801 Mon Sep 17 00:00:00 2001
    From: Boris Bobrov <breton@cynicmansion.ru>
    Date: Tue, 11 Mar 2014 16:23:58 +0500
    Subject: [PATCH 2/2] fixes to blog url generation
    
    ---
     mediagoblin/media_types/blog/lib.py                |  9 ++++++++-
     mediagoblin/media_types/blog/models.py             |  6 +++++-
     .../mediagoblin/blog/blog_post_listing.html        |  2 +-
     .../templates/mediagoblin/blog/list_of_blogs.html  |  4 ++--
     mediagoblin/media_types/blog/views.py              | 23 +++++++++++-----------
     5 files changed, 27 insertions(+), 17 deletions(-)
    
    diff --git a/mediagoblin/media_types/blog/lib.py b/mediagoblin/media_types/blog/lib.py
    index 62696b5..b6e3dc0 100644
    a b def get_all_blogposts_of_blog(request, blog, state=None):  
    4545            blog_posts_list.append(blog_post)
    4646    blog_posts_list.reverse()
    4747    return blog_posts_list
    48    
     48
     49def get_blog_by_slug(request, slug, **kwargs):
     50    if slug.startswith('blog_'):
     51        blog_id = int(slug[5:])
     52        blog = request.db.Blog.query.filter_by(id=blog_id, **kwargs).first()
     53    else:
     54        blog = request.db.Blog.query.filter_by(slug=slug, **kwargs).first()
     55    return blog
    4956 
  • mediagoblin/media_types/blog/models.py

    diff --git a/mediagoblin/media_types/blog/models.py b/mediagoblin/media_types/blog/models.py
    index 7c55e35..0e1ddf9 100644
    a b class BlogMixin(GenerateSlugMixin):  
    3434    def check_slug_used(self, slug):
    3535        return check_blog_slug_used(self.author, slug, self.id)
    3636
     37
    3738class Blog(Base, BlogMixin):
    3839    __tablename__ = "mediatype__blogs"
    3940    id = Column(Integer, primary_key=True)
    class Blog(Base, BlogMixin):  
    4243    author = Column(Integer, ForeignKey(User.id), nullable=False, index=True) #similar to uploader
    4344    created = Column(DateTime, nullable=False, default=datetime.datetime.now, index=True)
    4445    slug = Column(Unicode)
    45    
     46
     47    @property
     48    def slug_or_id(self):
     49        return (self.slug or u'blog_{0}'.format(self.id))
    4650 
    4751    def get_all_blog_posts(self, state=None):
    4852        blog_posts = Session.query(MediaEntry).join(BlogPostData)\
  • mediagoblin/media_types/blog/templates/mediagoblin/blog/blog_post_listing.html

    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 8d7a405..0013f1a 100644
    a b  
    5959    <br/>
    6060    <br/>
    6161    {% set blog_about_url = request.urlgen('mediagoblin.media_types.blog.blog_about',
    62                                       blog_slug=blog.slug, user=blog_owner_name) %}
     62                                      blog_slug=blog.slug_or_id, user=blog_owner_name) %}
    6363    <a style="text-decoration:underline" href="{{ blog_about_url}}">About Blog</a>
    6464    <br/>
    6565    {{ render_pagination(request, pagination) }}
  • mediagoblin/media_types/blog/templates/mediagoblin/blog/list_of_blogs.html

    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 f19a922..8c16dae 100644
    a b  
    3232        <table id="blogs_list">
    3333            {% for blog in blogs %}
    3434                {% set others_blog_url = request.urlgen('mediagoblin.media_types.blog.blog_post_listing',
    35                                                   blog_slug=blog.slug, user=user.username) %}
     35                                                  blog_slug=blog.slug_or_id, user=user.username) %}
    3636               
    3737                <tr>
    3838                     {% if not request.user or request.user.username != user.username%}
    3939                            <td><a href="{{ others_blog_url }}">{{ blog.title }}</a></td>
    4040                     {% else %}
    4141                       {% set my_blog_url = request.urlgen('mediagoblin.media_types.blog.blog-dashboard',
    42                        blog_slug=blog.slug, user=request.user.username) %}
     42                       blog_slug=blog.slug_or_id, user=request.user.username) %}
    4343                            <td><a href="{{ my_blog_url }}">{{ blog.title }}</a></td>
    4444                     {% endif %}
    4545                     <td>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</td>
  • mediagoblin/media_types/blog/views.py

    diff --git a/mediagoblin/media_types/blog/views.py b/mediagoblin/media_types/blog/views.py
    index 042881e..a367bef 100644
    a b from mediagoblin import mg_globals  
    2626
    2727from mediagoblin.media_types.blog import forms as blog_forms
    2828from mediagoblin.media_types.blog.models import Blog, BlogPostData
    29 from mediagoblin.media_types.blog.lib import may_edit_blogpost, set_blogpost_state, get_all_blogposts_of_blog
     29from mediagoblin.media_types.blog.lib import (
     30        may_edit_blogpost, set_blogpost_state, get_all_blogposts_of_blog,
     31        get_blog_by_slug)
    3032
    3133from mediagoblin.messages import add_message, SUCCESS, ERROR
    3234from mediagoblin.decorators import (require_active_login, active_user_from_url,
    def blog_edit(request):  
    9193
    9294    #Blog already exists.
    9395    else:
    94         blog = request.db.Blog.query.filter_by(slug=blog_slug).first()
     96        blog = get_blog_by_slug(request, blog_slug)
    9597        if not blog:
    9698            return render_404(request)
    9799        if request.method == 'GET':
    def blogpost_create(request):  
    129131
    130132    if request.method == 'POST' and form.validate():
    131133        blog_slug = request.matchdict.get('blog_slug')
    132         blog = request.db.Blog.query.filter_by(slug=blog_slug,
    133             author=request.user.id).first()
     134        blog = get_blog_by_slug(request, blog_slug, author=request.user.id)
    134135        if not blog:
    135136            return render_404(request)
    136137
    def blogpost_edit(request):  
    173174    blog_post_slug = request.matchdict.get('blog_post_slug', None)
    174175
    175176    blogpost = request.db.MediaEntry.query.filter_by(slug=blog_post_slug, uploader=request.user.id).first()
    176     blog = request.db.Blog.query.filter_by(slug=blog_slug, author=request.user.id).first()
     177    blog = get_blog_by_slug(request, blog_slug, author=request.user.id)
    177178
    178179    if not blogpost or not blog:
    179180        return render_404(request)
    def blog_dashboard(request, page, url_user=None):  
    222223    max_blog_count = config['max_blog_count']
    223224    if request.user and (request.user.id == url_user.id or request.user.has_privilege(u'admin')):
    224225        if blog_slug:
    225             blog = blogs.filter(Blog.slug==blog_slug).first()
     226            blog = get_blog_by_slug(request, blog_slug)
    226227            if not blog:
    227228                return render_404(request)
    228229            else:
    def blog_post_listing(request, page, url_user=None):  
    259260    Page, listing all the blog posts of a particular blog.
    260261    """
    261262    blog_slug = request.matchdict.get('blog_slug', None)
    262     blog = request.db.Blog.query.filter_by(slug=blog_slug).first()
     263    blog = get_blog_by_slug(request, blog_slug, author=request.user.id)
    263264    if not blog:
    264265        return render_404(request)
    265266
    def blog_post_listing(request, page, url_user=None):  
    280281
    281282@require_active_login
    282283def draft_view(request):
    283    
    284284    blog_slug = request.matchdict.get('blog_slug', None)
    285285    blog_post_slug = request.matchdict.get('blog_post_slug', None)
    286286    user = request.matchdict.get('user')
    287 
    288     blog = request.db.Blog.query.filter_by(author=request.user.id, slug=blog_slug).first()
     287    blog = get_blog_by_slug(request, blog_slug, author=request.user.id)
    289288    blogpost = request.db.MediaEntry.query.filter_by(state = u'failed', uploader=request.user.id, slug=blog_post_slug).first()
    290289
    291290    if not blog or not blogpost:
    def blog_delete(request, **kwargs):  
    308307    owner_user = request.db.User.query.filter_by(username=url_user).first()
    309308
    310309    blog_slug = request.matchdict.get('blog_slug', None)
    311     blog = request.db.Blog.query.filter_by(slug=blog_slug, author=owner_user.id).first()
     310    blog = get_blog_by_slug(request, blog_slug, author=owner_user.id)
    312311    if not blog:
    313312        return render_404(reequest)
    314313
    def blog_about_view(request):  
    355354    url_user = request.matchdict.get('user', None)
    356355   
    357356    user = request.db.User.query.filter_by(username=url_user).first()
    358     blog = request.db.Blog.query.filter_by(author=user.id, slug=blog_slug).first()
     357    blog = get_blog_by_slug(request, blog_slug, author=user.id)
    359358   
    360359    if not user or not blog:
    361360        return render_404(request)