1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 from datetime
import datetime
19 from werkzeug
.exceptions
import Forbidden
21 from mediagoblin
import messages
22 from mediagoblin
import mg_globals
24 from mediagoblin
.media_types
.blog
import forms
as blog_forms
25 from mediagoblin
.messages
import add_message
, SUCCESS
26 #from mediagoblin.edit.lib import may_edit_media
27 from mediagoblin
.decorators
import (require_active_login
, active_user_from_url
,
28 get_media_entry_by_id
, user_may_alter_collection
,
30 from mediagoblin
.tools
.response
import (render_to_response
,
31 redirect
, redirect_obj
, render_404
)
32 from mediagoblin
.tools
.translate
import pass_to_ugettext
as _
33 from mediagoblin
.tools
.template
import render_template
34 from mediagoblin
.tools
.text
import (
35 convert_to_tag_list_of_dicts
, media_tags_as_string
)
36 from mediagoblin
.tools
.url
import slugify
37 from mediagoblin
.db
.util
import check_media_slug_used
, check_collection_slug_used
38 from mediagoblin
.db
.models
import User
, Collection
41 def blog_create(request
, media
=None):
43 View to create and edit a blog
46 blog_form
= blog_forms
.BlogEditForm(request
.form
)
48 if request
.method
== 'POST' and blog_form
.validate():
49 blog
= request
.db
.Collection()
51 blog
.title
= unicode(blog_form
.title
.data
)
52 blog
.description
= unicode(blog_form
.description
.data
)
53 blog
.creator
= request
.user
.id
56 # Make sure this user isn't duplicating an existing collection
57 existing_blog_name
= request
.db
.Collection
.find_one({
58 'creator': request
.user
.id,
61 if existing_blog_name
:
62 add_message(request
, messages
.ERROR
,
63 _('You already have a blog called "%s"!') \
68 add_message(request
, SUCCESS
,
69 _('Blog "%s" added!') % blog
.title
)
71 return redirect(request
, "mediagoblin.user_pages.user_home",
72 user
=request
.user
.username
)
74 return render_to_response(
76 'mediagoblin/blog/blog_edit_create.html',
77 {'blog_form': blog_form
,
78 'user' : request
.user
,
79 'app_config': mg_globals
.app_config
})
83 @user_may_alter_collection
85 def blog_edit(request
, collection
):
87 View for editing an existing blog which is a collection of MediaEntries.
92 description
= blog
.description
)
93 existing_blog
= request
.db
.Collection
.find_one({
94 'creator': request
.user
.id,
95 'title':blog_form
.title
.data
})
96 if existing_blog
and existing_blog
.id != blog
.id:
98 request
, messages
.ERROR
,
99 _('You already have a blog called "%s"!') % \
100 blog_form
.title
.data
)
102 blog
.title
= unicode(blog_form
.title
.data
)
103 blog
.description
= unicode(blog_form
.description
.data
)
107 return redirect_obj(request
, blog
)
109 if request
.user
.is_admin \
110 and blog
.creator
!= request
.user
.id \
111 and request
.method
!= 'POST':
112 messages
.add_message(
113 request
, messages
.WARNING
,
114 _("You are editing another user's blog. Proceed with caution."))
116 return render_to_response(
118 'mediagoblin/blog/blog_edit_create.html',