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/>.
19 _log
= logging
.getLogger(__name__
)
21 from datetime
import datetime
23 from werkzeug
.exceptions
import Forbidden
25 from mediagoblin
import mg_globals
27 from mediagoblin
.media_types
.blog
import forms
as blog_forms
28 from mediagoblin
.media_types
.blog
.models
import Blog
29 from mediagoblin
.messages
import add_message
, SUCCESS
, ERROR
30 #from mediagoblin.edit.lib import may_edit_media
31 from mediagoblin
.decorators
import (require_active_login
, active_user_from_url
,
32 get_media_entry_by_id
, user_may_alter_collection
,
34 from mediagoblin
.tools
.response
import (render_to_response
,
35 redirect
, redirect_obj
, render_404
)
36 from mediagoblin
.tools
.translate
import pass_to_ugettext
as _
37 from mediagoblin
.tools
.template
import render_template
38 from mediagoblin
.tools
.text
import (
39 convert_to_tag_list_of_dicts
, media_tags_as_string
)
40 from mediagoblin
.tools
.url
import slugify
41 from mediagoblin
.db
.util
import check_media_slug_used
, check_collection_slug_used
42 from mediagoblin
.db
.models
import User
, Collection
, MediaEntry
44 from mediagoblin
.notifications
import add_comment_subscription
48 def blog_edit(request
):
50 View for editing the existing blog or automatically
51 creating a new blog if user does not have any yet.
53 url_user
= request
.matchdict
.get('user', None)
54 blog_slug
= request
.matchdict
.get('blog_slug', None)
57 form
= blog_forms
.BlogEditForm(request
.form
)
58 # the blog doesn't exists yet
60 if Blog
.query
.filter_by(author
=request
.user
.id).count()<max_blog_count
:
61 if request
.method
=='GET':
62 return render_to_response(
64 'mediagoblin/blog/blog_edit_create.html',
66 'user' : request
.user
,
67 'app_config': mg_globals
.app_config
})
69 if request
.method
=='POST' and form
.validate():
72 blog
.title
= unicode(form
.title
.data
)
73 blog
.description
= unicode(form
.description
.data
) #remember clean html data.
74 blog
.author
= request
.user
.id
78 return redirect(request
, "mediagoblin.user_pages.user_home",
79 user
=request
.user
.username
)
81 add_message(request
, ERROR
, "You can not create any more blogs")
82 return redirect(request
, "mediagoblin.user_pages.user_home",
83 user
=request
.user
.username
)
88 if request
.method
== 'GET':
89 blog
= Blog
.query
.filter_by(slug
=blog_slug
).first()
92 description
= blog
.description
,
93 author
= request
.user
.id)
95 form
= blog_forms
.BlogEditForm(**defaults
)
97 return render_to_response(
99 'mediagoblin/blog/blog_edit_create.html',
101 'user': request
.user
,
102 'app_config': mg_globals
.app_config
})
104 if request
.method
== 'POST' and form
.validate():
105 blog
.title
= unicode(form
.title
.data
)
106 blog
.description
= unicode(form
.description
.data
)
107 blog
.author
= request
.user
.id
111 add_message(request
, SUCCESS
, "Your blog is updated.")
112 return redirect(request
, "mediagoblin.user_pages.user_home",
113 user
=request
.user
.username
)
115 @require_active_login
116 def blogpost_create(request
):
117 form
= blog_forms
.BlogPostEditForm(request
.form
, license
=request
.user
.license_preference
)
119 if request
.method
== 'POST' and form
.validate():
120 blogpost
= MediaEntry()
121 blogpost
.media_type
= 'blog_post'
122 blogpost
.title
= unicode(form
.title
.data
)
123 blogpost
.description
= unicode(form
.description
.data
)
124 blogpost
.tags
= convert_to_tag_list_of_dicts(form
.tags
.data
)
125 blogpost
.license
= unicode(form
.license
.data
) or None
126 blogpost
.uploader
= request
.user
.id
127 #blogpost.state = 'processed'
129 blogpost
.generate_slug()
133 add_message(request
, SUCCESS
, _('Woohoo! Submitted!'))
134 add_comment_subscription(request
.user
, blogpost
)
135 return redirect(request
, "mediagoblin.user_pages.user_home",
136 user
=request
.user
.username
)
138 return render_to_response(
140 'mediagoblin/blog/blog_post_edit_create.html',
142 'app_config': mg_globals
.app_config
,
143 'user': request
.user
.username
})