Beispiel #1
0
def new_article(request):
    """
       Save new article
    """
    user = request.user

    if request.method == "POST":
        form = BlogPostForm(request.user, request.POST)
        if form.is_valid() == True:
            article = form.save(commit=False)
            article.user = user
            article.slug = slughifi(article.title)
            article.creator_ip = request.META['REMOTE_ADDR']
            article.type = Post.TYPE_POST
            article.temp_related_film = form.cleaned_data['related_film']
            if "save" in request.POST:
                article.status = Post.DRAFT_STATUS
                article.save()
                form.save_m2m()
                request.user.message_set.create(message=_(
                    "Successfully saved post '%s'. You have saved the review as draft. If you want to make it visible to others, you need to publish it."
                ) % article.title)
            elif "publish" in request.POST:
                article.status = Post.PUBLIC_STATUS
                article.save()
                form.save_m2m()

                # FLM-1596 save again to use m2m in model
                article.save()

                request.user.message_set.create(
                    message=_("Successfully published post '%s'.") %
                    article.title)
            else:
                raise Http404
            return HttpResponseRedirect("/" + urls.urls["ARTICLES"] + "/")
    else:
        form = BlogPostForm()

    data = {
        'form': form,
    }

    return render(request, templates['NEW_ARTICLE'], data)
Beispiel #2
0
def edit_blog_post(request, permalink=None, post_id=None):
    user = request.user
    try:
        if permalink is not None:
            # user is trying to edit post from profile
            post = Post.objects.get(Q(permalink=permalink),Q(user=user), Q(status = Post.PUBLIC_STATUS)|Q(status = Post.DRAFT_STATUS))
        else:
            # user is trying to edit post from planet
            post = Post.objects.get(Q(id=post_id),Q(user=user), Q(status = Post.PUBLIC_STATUS)|Q(status = Post.DRAFT_STATUS))
    except Post.DoesNotExist:
        raise Http404

    next = request.META.get('HTTP_REFERER', None)
    if request.method == 'POST':
        form = BlogPostForm(request.user, request.POST, instance=post)
        if form.is_valid():
            blog = form.save(commit=False)
            if "save" in request.POST:
                request.user.message_set.create(message=_("Successfully saved post '%s'.") % blog.title)
            if "unpublish" in request.POST:
                blog.status = Post.DRAFT_STATUS
                request.user.message_set.create(message=_("Successfully unpublished post '%s'.") % blog.title)
            if "publish" in request.POST:
                blog.status = Post.PUBLIC_STATUS
                request.user.message_set.create(message=_("Successfully published post '%s'.") % blog.title)

            blog.save()
            form.save_m2m()
            #saving activity here because many-to-many relation
            blog.save_activity()
            blog.update_activity()
            if next is not None:
                is_preview = re.search("\?"+_("preview"), next)
            if next is None:
                next = full_url('EDIT_NOTE')
            elif is_preview:
                next = full_url('EDIT_NOTE')
            #request.user.message_set.create(message=_("Successfully saved post '%s'") % blog.title)     
            return HttpResponseRedirect(next) 
    else:
        related_movies = post.related_film.select_related()
        related_people = post.related_person.select_related()        
        related_names = get_related_people_as_comma_separated_string(related_people)
        related_film = ', '.join(comma_escape(unicode(m)) for m in related_movies)
        form = BlogPostForm(instance=post, initial={'related_film': related_film, 'related_person':related_names})
        return render_to_response(templates['EDIT_NOTE'],{'form':form,'next':next,'post':post}, context_instance=RequestContext(request))
    return render_to_response(templates['EDIT_NOTE'],{'form':form,'next':next,'post':post}, context_instance=RequestContext(request))
Beispiel #3
0
def add_blog_post(request, film_id=None):
    #blog_object = Blog.objects.get_or_create(user=request.user, LANG=LANGUAGE_CODE)
    if request.method == "POST":
        logger.debug('Request is post')
        form = BlogPostForm(request.user, request.POST)
        if form.is_valid():
            logger.debug('Form is valid')
            blog = form.save(commit=False)
            #blog_instance = blog_object[0]
            #blog.author = blog_instance
            blog.user = request.user
            blog.creator_ip = request.META['REMOTE_ADDR']
            blog.type = Object.TYPE_POST
            blog.version = 0
            if "save" in request.POST:
                blog.status = Post.DRAFT_STATUS
                request.user.message_set.create(message=_("Successfully saved post '%s'. You have saved the review as draft. If you want to make it visible to others, you need to publish it.") % blog.title)
            if "publish" in request.POST:
                blog.status = Post.PUBLIC_STATUS
                request.user.message_set.create(message=_("Successfully published post '%s'.") % blog.title)
            blog.save()
            form.save_m2m()
            blog.save()
            return HttpResponseRedirect(reverse(edit_blog))
    else:
        related_film = None
        related_people = ""
        if film_id:
            try:
                related_film = Film.objects.get(pk=film_id)
            except Film.DoesNotExist:
                related_film = None
            directors = related_film.directors.select_related()
            for director in directors:
                related_people = related_people + unicode(director) + ", "
            actors = Character.objects.filter(film=related_film).order_by("importance")[:5]
            for actor in actors:
                 related_people = related_people + unicode(actor.person) + ", "
        form = BlogPostForm(initial={'related_film': comma_escape(unicode(related_film)), 'related_person':related_people})
        return render_to_response(templates['EDIT_NOTE'], {'form' : form}, context_instance=RequestContext(request))
    return render_to_response(templates['EDIT_NOTE'], {'form' : form}, context_instance=RequestContext(request))
Beispiel #4
0
def edit_article(request, id):
    """
       Save edited article
    """
    user = request.user
    article = None
    try:
        article = Post.objects.get(id=id, user=request.user)
    except Post.DoesNotExist:
        raise Http404

    next = request.REQUEST.get('next', '')
    if request.method == "POST":
        form = BlogPostForm(request.user, request.POST, instance=article)
        if form.is_valid() == True:
            article = form.save(commit=False)
            article.user = user
            article.slug = slughifi(article.title)
            article.creator_ip = request.META['REMOTE_ADDR']
            article.type = Post.TYPE_POST
            article.temp_related_film = form.cleaned_data['related_film']

            if "save" in request.POST:
                article.status = Post.DRAFT_STATUS
                article.save()
                request.user.message_set.create(message=_(
                    "Successfully saved post '%s'. You have saved the review as draft. If you want to make it visible to others, you need to publish it."
                ) % article.title)
                form.save_m2m()
                article.save_activity()
                return HttpResponseRedirect(next if next else "/" +
                                            urls.urls["ARTICLES"] + "/?" +
                                            _("drafts"))
            elif "publish" in request.POST:
                article.status = Post.PUBLIC_STATUS
                article.save()
                request.user.message_set.create(
                    message=_("Successfully published post '%s'.") %
                    article.title)
                form.save_m2m()
                article.save_activity()
                return HttpResponseRedirect(next if next else "/" +
                                            urls.urls["ARTICLES"] + "/")

    else:
        new_related_film = []

        def comma_escape(s):
            return s.replace('\\', '\\\\').replace(',', '\\,')

        related_movies = article.related_film.select_related()
        related_people = article.related_person.select_related()
        related_names = ', '.join(
            comma_escape(unicode(m)) for m in related_people)
        related_film = ', '.join(
            comma_escape(unicode(m)) for m in related_movies)
        form = BlogPostForm(instance=article,
                            initial={
                                'related_film': related_film,
                                'related_person': related_names
                            })

    data = {
        'form': form,
        'next': next,
        'article': article,
        'film': article.get_main_related_film()
    }

    return render(request, templates['EDIT_ARTICLE'], data)