# 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/>.
+import logging
+
+_log = logging.getLogger(__name__)
+
from datetime import datetime
from werkzeug.exceptions import Forbidden
-from mediagoblin import messages
from mediagoblin import mg_globals
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.decorators import (require_active_login, active_user_from_url,
get_media_entry_by_id, user_may_alter_collection,
convert_to_tag_list_of_dicts, media_tags_as_string)
from mediagoblin.tools.url import slugify
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
-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)
-
- if request.method == 'POST' and blog_form.validate():
- blog = request.db.Collection()
-
- blog.title = unicode(blog_form.title.data)
- blog.description = unicode(blog_form.description.data)
- blog.creator = request.user.id
- 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)
+ 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 form.validate():
+ _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.save()
+ return redirect(request, "mediagoblin.user_pages.user_home",
+ user=request.user.username)
else:
- blog.save()
-
- add_message(request, SUCCESS,
- _('Blog "%s" added!') % blog.title)
-
- return redirect(request, "mediagoblin.user_pages.user_home",
+ add_message(request, ERROR, "You can not create any more blogs")
+ return redirect(request, "mediagoblin.user_pages.user_home",
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:
- blog.title = unicode(blog_form.title.data)
- blog.description = unicode(blog_form.description.data)
+ if request.method == 'GET':
+ blog = Blog.query.filter_by(slug=blog_slug).first()
+ defaults = dict(
+ title = blog.title,
+ description = blog.description,
+ author = request.user.id)
- blog.save()
-
- return redirect_obj(request, blog)
-
- 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
+
+
+
+
+