Add view for blog edit and create page.
This commit is contained in:
parent
52b5d5a0f4
commit
af8f995f38
@ -14,15 +14,19 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
_log = logging.getLogger(__name__)
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from werkzeug.exceptions import Forbidden
|
from werkzeug.exceptions import Forbidden
|
||||||
|
|
||||||
from mediagoblin import messages
|
|
||||||
from mediagoblin import mg_globals
|
from mediagoblin import mg_globals
|
||||||
|
|
||||||
from mediagoblin.media_types.blog import forms as blog_forms
|
from mediagoblin.media_types.blog import forms as blog_forms
|
||||||
from mediagoblin.messages import add_message, SUCCESS
|
from mediagoblin.media_types.blog.models import Blog
|
||||||
|
from mediagoblin.messages import add_message, SUCCESS, ERROR
|
||||||
#from mediagoblin.edit.lib import may_edit_media
|
#from mediagoblin.edit.lib import may_edit_media
|
||||||
from mediagoblin.decorators import (require_active_login, active_user_from_url,
|
from mediagoblin.decorators import (require_active_login, active_user_from_url,
|
||||||
get_media_entry_by_id, user_may_alter_collection,
|
get_media_entry_by_id, user_may_alter_collection,
|
||||||
@ -35,94 +39,67 @@ from mediagoblin.tools.text import (
|
|||||||
convert_to_tag_list_of_dicts, media_tags_as_string)
|
convert_to_tag_list_of_dicts, media_tags_as_string)
|
||||||
from mediagoblin.tools.url import slugify
|
from mediagoblin.tools.url import slugify
|
||||||
from mediagoblin.db.util import check_media_slug_used, check_collection_slug_used
|
from mediagoblin.db.util import check_media_slug_used, check_collection_slug_used
|
||||||
from mediagoblin.db.models import User, Collection
|
from mediagoblin.db.models import User, Collection, MediaEntry
|
||||||
|
|
||||||
|
|
||||||
@require_active_login
|
@require_active_login
|
||||||
def blog_create(request, media=None):
|
def blog_edit(request):
|
||||||
"""
|
"""
|
||||||
View to create and edit a blog
|
View for editing the existing blog or automatically
|
||||||
|
creating a new blog if user does not have any yet.
|
||||||
"""
|
"""
|
||||||
|
url_user = request.matchdict.get('user', None)
|
||||||
|
blog_slug = request.matchdict.get('blog_slug', None)
|
||||||
|
|
||||||
blog_form = blog_forms.BlogEditForm(request.form)
|
max_blog_count = 1
|
||||||
|
form = blog_forms.BlogEditForm(request.form)
|
||||||
|
# the blog doesn't exists yet
|
||||||
|
if not blog_slug:
|
||||||
|
if Blog.query.filter_by(author=request.user.id).count()<max_blog_count:
|
||||||
|
if request.method=='GET':
|
||||||
|
return render_to_response(
|
||||||
|
request,
|
||||||
|
'mediagoblin/blog/blog_edit_create.html',
|
||||||
|
{'form': form,
|
||||||
|
'user' : request.user,
|
||||||
|
'app_config': mg_globals.app_config})
|
||||||
|
|
||||||
if request.method == 'POST' and blog_form.validate():
|
if request.method=='POST' and form.validate():
|
||||||
blog = request.db.Collection()
|
_log.info("Here")
|
||||||
|
blog = Blog()
|
||||||
|
blog.title = unicode(form.title.data)
|
||||||
|
blog.description = unicode(form.description.data) #remember clean html data.
|
||||||
|
blog.author = request.user.id
|
||||||
|
blog.generate_slug()
|
||||||
|
|
||||||
blog.title = unicode(blog_form.title.data)
|
blog.save()
|
||||||
blog.description = unicode(blog_form.description.data)
|
return redirect(request, "mediagoblin.user_pages.user_home",
|
||||||
blog.creator = request.user.id
|
user=request.user.username)
|
||||||
blog.generate_slug()
|
|
||||||
|
|
||||||
# Make sure this user isn't duplicating an existing collection
|
|
||||||
existing_blog_name = request.db.Collection.find_one({
|
|
||||||
'creator': request.user.id,
|
|
||||||
'title':blog.title})
|
|
||||||
|
|
||||||
if existing_blog_name:
|
|
||||||
add_message(request, messages.ERROR,
|
|
||||||
_('You already have a blog called "%s"!') \
|
|
||||||
% blog.title)
|
|
||||||
else:
|
else:
|
||||||
blog.save()
|
add_message(request, ERROR, "You can not create any more blogs")
|
||||||
|
return redirect(request, "mediagoblin.user_pages.user_home",
|
||||||
add_message(request, SUCCESS,
|
|
||||||
_('Blog "%s" added!') % blog.title)
|
|
||||||
|
|
||||||
return redirect(request, "mediagoblin.user_pages.user_home",
|
|
||||||
user=request.user.username)
|
user=request.user.username)
|
||||||
|
|
||||||
return render_to_response(
|
|
||||||
request,
|
|
||||||
'mediagoblin/blog/blog_edit_create.html',
|
|
||||||
{'blog_form': blog_form,
|
|
||||||
'user' : request.user,
|
|
||||||
'app_config': mg_globals.app_config})
|
|
||||||
|
|
||||||
|
|
||||||
@require_active_login
|
|
||||||
@user_may_alter_collection
|
|
||||||
@get_user_collection
|
|
||||||
def blog_edit(request, collection):
|
|
||||||
"""
|
|
||||||
View for editing an existing blog which is a collection of MediaEntries.
|
|
||||||
"""
|
|
||||||
blog = collection
|
|
||||||
defaults = dict(
|
|
||||||
title = blog.title,
|
|
||||||
description = blog.description)
|
|
||||||
existing_blog = request.db.Collection.find_one({
|
|
||||||
'creator': request.user.id,
|
|
||||||
'title':blog_form.title.data})
|
|
||||||
if existing_blog and existing_blog.id != blog.id:
|
|
||||||
messages.add_message(
|
|
||||||
request, messages.ERROR,
|
|
||||||
_('You already have a blog called "%s"!') % \
|
|
||||||
blog_form.title.data)
|
|
||||||
else:
|
else:
|
||||||
blog.title = unicode(blog_form.title.data)
|
if request.method == 'GET':
|
||||||
blog.description = unicode(blog_form.description.data)
|
blog = Blog.query.filter_by(slug=blog_slug).first()
|
||||||
|
defaults = dict(
|
||||||
blog.save()
|
title = blog.title,
|
||||||
|
description = blog.description,
|
||||||
return redirect_obj(request, blog)
|
author = request.user.id)
|
||||||
|
|
||||||
if request.user.is_admin \
|
|
||||||
and blog.creator != request.user.id \
|
|
||||||
and request.method != 'POST':
|
|
||||||
messages.add_message(
|
|
||||||
request, messages.WARNING,
|
|
||||||
_("You are editing another user's blog. Proceed with caution."))
|
|
||||||
|
|
||||||
return render_to_response(
|
|
||||||
request,
|
|
||||||
'mediagoblin/blog/blog_edit_create.html',
|
|
||||||
{'blog': blog,
|
|
||||||
'form': blog_form})
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
form = blog_forms.BlogEditForm(**defaults)
|
||||||
|
|
||||||
|
return render_to_response(
|
||||||
|
request,
|
||||||
|
'mediagoblin/blog/blog_edit_create.html',
|
||||||
|
{'form': form,
|
||||||
|
'user': request.user,
|
||||||
|
'app_config': mg_globals.app_config})
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user