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):
|
45 | 45 | blog_posts_list.append(blog_post) |
46 | 46 | blog_posts_list.reverse() |
47 | 47 | return blog_posts_list |
48 | | |
| 48 | |
| 49 | def 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 |
49 | 56 | |
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):
|
34 | 34 | def check_slug_used(self, slug): |
35 | 35 | return check_blog_slug_used(self.author, slug, self.id) |
36 | 36 | |
| 37 | |
37 | 38 | class Blog(Base, BlogMixin): |
38 | 39 | __tablename__ = "mediatype__blogs" |
39 | 40 | id = Column(Integer, primary_key=True) |
… |
… |
class Blog(Base, BlogMixin):
|
42 | 43 | author = Column(Integer, ForeignKey(User.id), nullable=False, index=True) #similar to uploader |
43 | 44 | created = Column(DateTime, nullable=False, default=datetime.datetime.now, index=True) |
44 | 45 | slug = Column(Unicode) |
45 | | |
| 46 | |
| 47 | @property |
| 48 | def slug_or_id(self): |
| 49 | return (self.slug or u'blog_{0}'.format(self.id)) |
46 | 50 | |
47 | 51 | def get_all_blog_posts(self, state=None): |
48 | 52 | 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 8d7a405..0013f1a 100644
a
|
b
|
|
59 | 59 | <br/> |
60 | 60 | <br/> |
61 | 61 | {% 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) %} |
63 | 63 | <a style="text-decoration:underline" href="{{ blog_about_url}}">About Blog</a> |
64 | 64 | <br/> |
65 | 65 | {{ 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 f19a922..8c16dae 100644
a
|
b
|
|
32 | 32 | <table id="blogs_list"> |
33 | 33 | {% for blog in blogs %} |
34 | 34 | {% 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) %} |
36 | 36 | |
37 | 37 | <tr> |
38 | 38 | {% if not request.user or request.user.username != user.username%} |
39 | 39 | <td><a href="{{ others_blog_url }}">{{ blog.title }}</a></td> |
40 | 40 | {% else %} |
41 | 41 | {% 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) %} |
43 | 43 | <td><a href="{{ my_blog_url }}">{{ blog.title }}</a></td> |
44 | 44 | {% endif %} |
45 | 45 | <td> </td> |
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
|
26 | 26 | |
27 | 27 | from mediagoblin.media_types.blog import forms as blog_forms |
28 | 28 | from 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 |
| 29 | from mediagoblin.media_types.blog.lib import ( |
| 30 | may_edit_blogpost, set_blogpost_state, get_all_blogposts_of_blog, |
| 31 | get_blog_by_slug) |
30 | 32 | |
31 | 33 | from mediagoblin.messages import add_message, SUCCESS, ERROR |
32 | 34 | from mediagoblin.decorators import (require_active_login, active_user_from_url, |
… |
… |
def blog_edit(request):
|
91 | 93 | |
92 | 94 | #Blog already exists. |
93 | 95 | else: |
94 | | blog = request.db.Blog.query.filter_by(slug=blog_slug).first() |
| 96 | blog = get_blog_by_slug(request, blog_slug) |
95 | 97 | if not blog: |
96 | 98 | return render_404(request) |
97 | 99 | if request.method == 'GET': |
… |
… |
def blogpost_create(request):
|
129 | 131 | |
130 | 132 | if request.method == 'POST' and form.validate(): |
131 | 133 | 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) |
134 | 135 | if not blog: |
135 | 136 | return render_404(request) |
136 | 137 | |
… |
… |
def blogpost_edit(request):
|
173 | 174 | blog_post_slug = request.matchdict.get('blog_post_slug', None) |
174 | 175 | |
175 | 176 | 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) |
177 | 178 | |
178 | 179 | if not blogpost or not blog: |
179 | 180 | return render_404(request) |
… |
… |
def blog_dashboard(request, page, url_user=None):
|
222 | 223 | max_blog_count = config['max_blog_count'] |
223 | 224 | if request.user and (request.user.id == url_user.id or request.user.has_privilege(u'admin')): |
224 | 225 | if blog_slug: |
225 | | blog = blogs.filter(Blog.slug==blog_slug).first() |
| 226 | blog = get_blog_by_slug(request, blog_slug) |
226 | 227 | if not blog: |
227 | 228 | return render_404(request) |
228 | 229 | else: |
… |
… |
def blog_post_listing(request, page, url_user=None):
|
259 | 260 | Page, listing all the blog posts of a particular blog. |
260 | 261 | """ |
261 | 262 | 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) |
263 | 264 | if not blog: |
264 | 265 | return render_404(request) |
265 | 266 | |
… |
… |
def blog_post_listing(request, page, url_user=None):
|
280 | 281 | |
281 | 282 | @require_active_login |
282 | 283 | def draft_view(request): |
283 | | |
284 | 284 | blog_slug = request.matchdict.get('blog_slug', None) |
285 | 285 | blog_post_slug = request.matchdict.get('blog_post_slug', None) |
286 | 286 | 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) |
289 | 288 | blogpost = request.db.MediaEntry.query.filter_by(state = u'failed', uploader=request.user.id, slug=blog_post_slug).first() |
290 | 289 | |
291 | 290 | if not blog or not blogpost: |
… |
… |
def blog_delete(request, **kwargs):
|
308 | 307 | owner_user = request.db.User.query.filter_by(username=url_user).first() |
309 | 308 | |
310 | 309 | 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) |
312 | 311 | if not blog: |
313 | 312 | return render_404(reequest) |
314 | 313 | |
… |
… |
def blog_about_view(request):
|
355 | 354 | url_user = request.matchdict.get('user', None) |
356 | 355 | |
357 | 356 | 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) |
359 | 358 | |
360 | 359 | if not user or not blog: |
361 | 360 | return render_404(request) |