Commit | Line | Data |
---|---|---|
cf46184a A |
1 | # GNU MediaGoblin -- federated, autonomous media hosting |
2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. | |
3 | # | |
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. | |
8 | # | |
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. | |
13 | # | |
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/>. | |
16 | ||
af8f995f A |
17 | import logging |
18 | ||
19 | _log = logging.getLogger(__name__) | |
20 | ||
cf46184a A |
21 | from datetime import datetime |
22 | ||
23 | from werkzeug.exceptions import Forbidden | |
24 | ||
cf46184a A |
25 | from mediagoblin import mg_globals |
26 | ||
27 | from mediagoblin.media_types.blog import forms as blog_forms | |
af8f995f A |
28 | from mediagoblin.media_types.blog.models import Blog |
29 | from mediagoblin.messages import add_message, SUCCESS, ERROR | |
cf46184a A |
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, | |
33 | get_user_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 | |
af8f995f A |
42 | from mediagoblin.db.models import User, Collection, MediaEntry |
43 | ||
fa5a13f8 A |
44 | from mediagoblin.notifications import add_comment_subscription |
45 | ||
cf46184a A |
46 | |
47 | @require_active_login | |
af8f995f | 48 | def blog_edit(request): |
cf46184a | 49 | """ |
af8f995f A |
50 | View for editing the existing blog or automatically |
51 | creating a new blog if user does not have any yet. | |
cf46184a | 52 | """ |
af8f995f A |
53 | url_user = request.matchdict.get('user', None) |
54 | blog_slug = request.matchdict.get('blog_slug', None) | |
cf46184a | 55 | |
af8f995f A |
56 | max_blog_count = 1 |
57 | form = blog_forms.BlogEditForm(request.form) | |
58 | # the blog doesn't exists yet | |
59 | if not blog_slug: | |
60 | if Blog.query.filter_by(author=request.user.id).count()<max_blog_count: | |
61 | if request.method=='GET': | |
62 | return render_to_response( | |
63 | request, | |
64 | 'mediagoblin/blog/blog_edit_create.html', | |
65 | {'form': form, | |
66 | 'user' : request.user, | |
67 | 'app_config': mg_globals.app_config}) | |
68 | ||
69 | if request.method=='POST' and form.validate(): | |
70 | _log.info("Here") | |
71 | blog = Blog() | |
72 | blog.title = unicode(form.title.data) | |
73 | blog.description = unicode(form.description.data) #remember clean html data. | |
74 | blog.author = request.user.id | |
75 | blog.generate_slug() | |
76 | ||
77 | blog.save() | |
78 | return redirect(request, "mediagoblin.user_pages.user_home", | |
79 | user=request.user.username) | |
cf46184a | 80 | else: |
af8f995f A |
81 | add_message(request, ERROR, "You can not create any more blogs") |
82 | return redirect(request, "mediagoblin.user_pages.user_home", | |
cf46184a A |
83 | user=request.user.username) |
84 | ||
cf46184a | 85 | |
6e947ab9 | 86 | |
cf46184a | 87 | else: |
af8f995f A |
88 | if request.method == 'GET': |
89 | blog = Blog.query.filter_by(slug=blog_slug).first() | |
90 | defaults = dict( | |
91 | title = blog.title, | |
92 | description = blog.description, | |
93 | author = request.user.id) | |
cf46184a | 94 | |
af8f995f A |
95 | form = blog_forms.BlogEditForm(**defaults) |
96 | ||
97 | return render_to_response( | |
98 | request, | |
99 | 'mediagoblin/blog/blog_edit_create.html', | |
100 | {'form': form, | |
101 | 'user': request.user, | |
102 | 'app_config': mg_globals.app_config}) | |
103 | else: | |
fa5a13f8 | 104 | if request.method == 'POST' and form.validate(): |
145a1738 A |
105 | blog.title = unicode(form.title.data) |
106 | blog.description = unicode(form.description.data) | |
107 | blog.author = request.user.id | |
108 | blog.generate_slug() | |
109 | ||
110 | blog.save() | |
111 | add_message(request, SUCCESS, "Your blog is updated.") | |
112 | return redirect(request, "mediagoblin.user_pages.user_home", | |
113 | user=request.user.username) | |
114 | ||
fa5a13f8 A |
115 | @require_active_login |
116 | def blogpost_create(request): | |
145a1738 A |
117 | form = blog_forms.BlogPostEditForm(request.form, license=request.user.license_preference) |
118 | ||
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' | |
128 | ||
129 | blogpost.generate_slug() | |
130 | ||
131 | blogpost.save() | |
132 | ||
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) | |
137 | ||
138 | return render_to_response( | |
fa5a13f8 A |
139 | request, |
140 | 'mediagoblin/blog/blog_post_edit_create.html', | |
141 | {'form': form, | |
142 | 'app_config': mg_globals.app_config, | |
143 | 'user': request.user.username}) | |
145a1738 A |
144 | |
145 | ||
146 | ||
147 | ||
148 | ||
af8f995f A |
149 | |
150 |