Exemplo n.º 1
0
def edit(request, id):
    tags = ''
    if id:
        article = get_object_or_404(Article, pk=id)
        for tag in article.get_tags():
            tags = u'{0} {1}'.format(tags, tag.tag)
        tags = tags.strip()
    else:
        article = Article(create_user=request.user)

    if article.create_user.id != request.user.id:
        return redirect('home')

    if request.POST:
        old_state = {'title': article.title, 'content': article.content}
        form = ArticleForm(request.POST, instance=article)
        if form.is_valid():
            q_memento = ArticleMemento()
            q_memento.state = json.dumps(old_state)
            q_memento.caretaker = article.caretaker
            q_memento.save()
            form.save()
            return redirect('/articles/')
    else:
        form = ArticleForm(instance=article, initial={'tags': tags})
    return render(request, 'articles/edit.html', {'form': form})
Exemplo n.º 2
0
def edit(request, id):
    tags = ''
    if id:
        article = get_object_or_404(Article, pk=id)
        for tag in article.get_tags():
            tags = u'{0} {1}'.format(tags, tag.tag)
        tags = tags.strip()
    else:
        article = Article(create_user=request.user)

    if article.create_user.id != request.user.id:
        return redirect('home')

    if request.POST:
        old_state = {'title': article.title, 'content': article.content}
        form = ArticleForm(request.POST, instance=article)
        if form.is_valid():
            q_memento = ArticleMemento()
            q_memento.state = json.dumps(old_state)
            q_memento.caretaker = article.caretaker
            q_memento.save()
            form.save()
            return redirect('/articles/')
    else:
        form = ArticleForm(instance=article, initial={'tags': tags})
    return render(request, 'articles/edit.html', {'form': form})
Exemplo n.º 3
0
def edit(request, id):
    # 编辑
    tags = ''
    if id:
        # 如果id存在的话,说明是update
        article = get_object_or_404(Article, pk=id)
        for tag in article.get_tags():
            tags = u'{0} {1}'.format(tags, tag.tag)
        tags = tags.strip()
    else:
        # 否则的话,是一个新文章
        article = Article(create_user=request.user)

    if article.create_user.id != request.user.id:
        # 如果修改者不是创建者的话,那么redirect到home
        return redirect('home')

    if request.POST:
        # 如果是post的话,那么获取表单数据,保存
        form = ArticleForm(request.POST, instance=article)
        if form.is_valid():
            # 这个方法根据表单绑定的数据创建并保存数据库对象
            form.save()
            return redirect('/articles/')
    else:
        # 否则的话,创建一个新的form,内容填充找到的内容,
        form = ArticleForm(instance=article, initial={'tags': tags})
    return render(request, 'articles/edit.html', {'form': form})
Exemplo n.º 4
0
def edit(request, id):
    tags = ''
    if id:
        article = get_object_or_404(Article, pk=id)
        for tag in article.get_tags():
            tags = u'{0} {1}'.format(tags, tag.tag)
        tags = tags.strip()
    else:
        article = Article(create_user=request.user)

    if request.POST:
        form = ArticleForm(request.POST, instance=article)
        if form.is_valid():
            form.save()
            return redirect('/articles/')
    else:
        form = ArticleForm(instance=article, initial={'tags': tags})
    return render(request, 'articles/edit.html', {'form': form})
Exemplo n.º 5
0
def write(request):
    if request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            article = Article()
            article.create_user = request.user
            article.title = form.cleaned_data.get('title')
            article.content = form.cleaned_data.get('content')
            status = form.cleaned_data.get('status')
            if status in [Article.PUBLISHED, Article.DRAFT]:
                article.status = form.cleaned_data.get('status')
            article.save()
            tags = form.cleaned_data.get('tags')
            article.create_tags(tags)
            return redirect('/articles/')
    else:
        form = ArticleForm()
    return render(request, 'articles/write.html', {'form': form})
Exemplo n.º 6
0
def write(request):
    if request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            article = Article()
            article.create_user = request.user
            article.title = form.cleaned_data.get('title')
            article.content = form.cleaned_data.get('content')
            status = form.cleaned_data.get('status')
            if status in [Article.PUBLISHED, Article.DRAFT]:
                article.status = form.cleaned_data.get('status')
            article.save()
            tags = form.cleaned_data.get('tags')
            article.create_tags(tags)
            return redirect('/articles/')
    else:
        form = ArticleForm()
    return render(request, 'articles/write.html', {'form': form})
Exemplo n.º 7
0
def edit(request, id):
    tags = ""
    if id:
        article = get_object_or_404(Article, pk=id)
        for tag in article.get_tags():
            tags = "{0} {1}".format(tags, tag.tag)
        tags = tags.strip()
    else:
        article = Article(create_user=request.user)

    if request.POST:
        form = ArticleForm(request.POST, instance=article)
        if form.is_valid():
            form.save()
            return redirect("/articles/")
    else:
        form = ArticleForm(instance=article, initial={"tags": tags})
    return render(request, "articles/edit.html", {"form": form})
Exemplo n.º 8
0
def write(request):
    if request.method == "POST":
        form = ArticleForm(request.POST)
        if form.is_valid():
            article = Article()
            article.create_user = request.user
            article.title = form.cleaned_data.get("title")
            article.content = form.cleaned_data.get("content")
            status = form.cleaned_data.get("status")
            if status in [Article.PUBLISHED, Article.DRAFT]:
                article.status = form.cleaned_data.get("status")
            article.save()
            tags = form.cleaned_data.get("tags")
            article.create_tags(tags)
            return redirect("/articles/")
    else:
        form = ArticleForm()
    return render(request, "articles/write.html", {"form": form})
Exemplo n.º 9
0
def edit(request, id):
    tags = ''
    if id:
        article = get_object_or_404(Article, pk=id)
        for tag in article.get_tags():
            tags = u'{0} {1}'.format(tags, tag.tag)
        tags = tags.strip()
    else:
        article = Article(create_user=request.user)

    if request.POST:
        form = ArticleForm(request.POST, instance=article)
        if form.is_valid():
            form.save()
            return redirect('/articles/')
    else:
        form = ArticleForm(instance=article, initial={'tags': tags})
    return render(request, 'articles/edit.html', {'form': form})
Exemplo n.º 10
0
 def post(self, request):
     form = ArticleForm(request.POST, request.FILES)
     a = request.POST.get('action').lower()
     if form.is_valid():
         if a == 'publish':
             status = Article.PUBLISHED
         elif a == 'draft':
             status = Article.DRAFT
         article = Article(title=form.cleaned_data.get('title'),
                           content=form.cleaned_data.get('content'),
                           create_user=request.user,
                           status=status)
         article.save()
         tags = form.cleaned_data.get('tags')
         article.create_tags(tags)
         return redirect(self.success_url)
     else:
         return super(ArticleCreateView, self).form_invalid(form)
Exemplo n.º 11
0
 def post(self, request):
     form = ArticleForm(request.POST, request.FILES)
     a = request.POST.get('action').lower()
     if form.is_valid():
         if a == 'publish':
             status = Article.PUBLISHED
         elif a == 'draft':
             status = Article.DRAFT
         article = Article(
             title = form.cleaned_data.get('title'),
             content = form.cleaned_data.get('content'),
             create_user = request.user,
             status = status
         )
         article.save()
         tags = form.cleaned_data.get('tags')
         article.create_tags(tags)
         return redirect(self.success_url)
     else:
         return super(ArticleCreateView, self).form_invalid(form) 
Exemplo n.º 12
0
def edit(request, id):
    if id:
        article = get_object_or_404(Article, pk=id)

    else:
        article = Article(create_user=request.user)

    if article.create_user.id != request.user.id:
        return redirect('home')

    if request.POST:
        form = ArticleForm(request.POST, instance=article)
        if form.is_valid():
            form.save()
            return redirect('/articles/')

    else:
        form = ArticleForm(instance=article)

    return render(request, 'articles/edit.html', {'form': form})
Exemplo n.º 13
0
def edit(request, id):
    tags = ''
    if id:
        article = get_object_or_404(Article, pk=id)
        for tag in article.get_tags():
            tags = u'{0} {1}'.format(tags, tag.tag)
        tags = tags.strip()
    else:
        article = Article(create_user=request.user)

    if request.POST:
        form = ArticleForm(request.POST, instance=article)
        if form.is_valid():
            form.save()
            return redirect('/articles/')
    else:
        # linea creada con el fin que unicamente el creador del articulo pueda editar el mismo
        if article.create_user == request.user:
            form = ArticleForm(instance=article, initial={'tags': tags})
            return render(request, 'articles/edit.html', {'form': form})
        else:
            messages.add_message(request, messages.SUCCESS, 'Solo puede editar los Articulos que ha creado.')
            return redirect('/articles/')