fixes to blog url generation
authorBoris Bobrov <breton@cynicmansion.ru>
Tue, 11 Mar 2014 11:23:58 +0000 (16:23 +0500)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Tue, 11 Mar 2014 16:29:09 +0000 (11:29 -0500)
mediagoblin/media_types/blog/lib.py
mediagoblin/media_types/blog/models.py
mediagoblin/media_types/blog/templates/mediagoblin/blog/blog_post_listing.html
mediagoblin/media_types/blog/templates/mediagoblin/blog/list_of_blogs.html
mediagoblin/media_types/blog/views.py

index 62696b55d6f5ad3028ad454971c861d8a175e242..b6e3dc06bd1d59ec47dc23b2b700857a95513314 100644 (file)
@@ -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
  
index 7c55e359a60342597d32ac2dba1ad3c8ddf69fe5..0e1ddf974350f3f72ce724655a63a167b76a5f85 100644 (file)
@@ -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)\
index 8d7a405e644ab4d39b237073ba28e02c3b7d1c28..0013f1a3b794799e46de8f8b46a4cfcfc7fe5b34 100644 (file)
@@ -59,7 +59,7 @@
     <br/>
     <br/>
     {% 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) %}
     <a style="text-decoration:underline" href="{{ blog_about_url}}">About Blog</a>
     <br/>
     {{ render_pagination(request, pagination) }}
index f19a922555dd23cab6f176de009c60dcf779475c..8c16daebcec0457a2ea6eca67c1bcf386886b519 100644 (file)
         <table id="blogs_list">
             {% 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) %}
                 
                 <tr>
                      {% if not request.user or request.user.username != user.username%}
                             <td><a href="{{ others_blog_url }}">{{ blog.title }}</a></td>
                      {% 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) %}
                             <td><a href="{{ my_blog_url }}">{{ blog.title }}</a></td>
                      {% endif %}
                      <td>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</td>
index 042881e4b6796fdfc03c9397566272dd12508bd1..a367bef857550228438d3daff42ebcb5e83028b5 100644 (file)
@@ -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)