Example #1
0
def post_modify(request, post_pk):
    post = Post.objects.get(pk=post_pk)
    next = request.GET.get('next')
    # if request.method == 'GET':
    #     forms = ModifyPost(initial={'image': post.image})
    #     context = {
    #         'forms': forms,
    #         'post': post,
    #     }
    #     return render(request, 'post/post_modify.html', context=context)
    #
    # elif request.method == 'POST':
    #     forms = ModifyPost(request.POST, request.FILES)
    #     if forms.is_valid():
    #         post.image = request.FILES['image']
    #         post.save()
    #         return redirect('post:post_detail', post_pk=post.id)
    if request.method == 'POST':
        form = PostForm(data=request.POST, files=request.FILES, instance=post)
        if form.is_valid():
            form.save()
            if next:
                return redirect(next)
            return redirect('post:post_detail', post_pk)
    else:
        form = PostForm(instance=post)
    context = {
        'form': form,
    }
    return render(request, 'post/post_modify.html', context=context)
Example #2
0
    def post(self, request, *args, **kwargs):
        post = PostForm(request.POST, request.FILES)
        if post.is_valid():
            post.instance.usuario = request.user
            post.save()

        return redirect('core:index')
Example #3
0
def create_post(request):
    form = PostForm()
    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('/')
    return render(request, 'posts/create_post.html', {'form': form})
Example #4
0
def update_post(request, id):
    post = Post.objects.get(id=id)
    form = PostForm(
        request.POST or None, request.FILES or None, instance=post
    )
    if form.is_valid():
        form.save()
        return redirect('/')
    return render(request, 'post/create_post.html', {'form': form})
Example #5
0
 def post(self, request, pk):
     try:
         post = Post.objects.get(pk=pk)
     except Post.DoesNotExist:
         raise Http404
     form = PostForm(instance=post, author=request.user, data=request.POST)
     if form.is_valid():
         form.save()
         return HttpResponseRedirect(reverse("home:index"))
     return render(request, "post/edit.html", {"form": form, "pk": pk})
Example #6
0
def addpost(request):
    if request.POST and ('pause' not in request.session):
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.post_date = datetime.now()
            post.post_author = User.objects.get(id=request.user.id)
            form.save()
            request.session.set_expiry(10)
            request.session['pause'] = True
    return redirect('/')
Example #7
0
def adminposts(request):
    posts = Post.objects.all()
    postform = PostForm()
    args = {'posts' : posts, 'postform' : postform}
    if request.method == "POST":
        postform = PostForm(request.POST, request.FILES)
        if postform.is_valid():
            postform.save()
        else:
            print("errors : {}".format(postform.errors.as_data()))
    return render(request, 'webadmin/pages/database/posts/index.html',args)
Example #8
0
def add_post(request):
    if request.method == 'POST':  # If the form has been submitted...
        form = PostForm(request.POST)  # A form bound to the POST data
        if form.is_valid():  # All validation rules pass
            form.save()
            return HttpResponseRedirect('/account/blog/')

    else:
        form = PostForm()

    return render(request, 'post/add_post.html', {'form': form, })
Example #9
0
def post_edit(request, id):
    post = Post.objects.get(id=id)
    if request.method == 'POST':  # If the form has been submitted...
        form = PostForm(request.POST, instance=post)  # A form bound to the POST data
        if form.is_valid():  # All validation rules pass
            form.save()
            return HttpResponseRedirect('/account/blog/')    # Redirect after POST
    else:
        form = PostForm(instance=post)

    return render(request, 'post/post_edit.html', {'form': form, 'post': post, })
Example #10
0
def create_post(request):
    title = "Create"
    form = PostForm(request.POST or None, request.FILES or None)
    author = get_author(request.user)  # Get the Post author
    if request.method == "POST":
        if form.is_valid():
            form.instance.author = author
            form.save()
            return redirect(
                reverse("post-detail", kwargs={"myid": form.instance.id}))
    context = {"form": form, "title": title}
    return render(request, "create_post.html", context)
Example #11
0
def edit_post(request, post_id=None):
    post = get_object_or_404(Post, id=post_id)
    form = PostForm(request.POST or None, instance=post)
    if form.is_valid():
        form.save()
        messages.success(request, "Post edited successfully")
        return redirect("index")

    context = {
        'post_form': form,
    }

    return render(request, 'user/edit_post.html', context)
Example #12
0
def post_modify(request, post_pk):
    post = Post.objects.get(pk=post_pk)
    if request.method == "POST":
        form = PostForm(data=request.POST, files=request.FILES, instance=post)
        if form.is_valid():
            form.save()
        return redirect('post:post_detail', post_pk=post.pk)
    else:
        form = PostForm(instance=post)
        context = {
            'form': form,
        }
        return render(request, 'post/post_modify.html', context)
Example #13
0
def sendPost(request):
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            form.save(commit=False)
            form.instance.user = request.user
            form.save(commit=True)
            messages.success(request, _("Send Post Succesfully."))
            return redirect('index')
    else:
        form = PostForm()

    return render(request, 'post/sendpost.html', {'form': form})
Example #14
0
def save_post(request, post_id=None):
    post = post_id is None and Post(user_id=request.user.id) \
        or get_object_or_404(Post, id=post_id)
    if request.POST:
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            form.save()
            return redirect('edit_post', post.id)
    else:
        form = PostForm(instance=post)
    images = Image.objects.filter(user_id=request.user.id)
    form.new = post_id is None or False
    return render(request, 'post/form.html', {'form': form, 'images': images})
Example #15
0
def managepost(request, slug_url):
    post = get_object_or_404(Post, slug = slug_url)
    postform = PostForm(instance = post)
    args = {'postform' : postform, 'post' : post}
    if request.method == 'POST':
        postform = PostForm(request.POST, request.FILES, instance=post)
        if postform.is_valid():
            postform.save()
            args = {'postform' : postform, 'post' : post}
            return redirect('/webmaster/post/'+post.slug)
        else:
            print("errors : {}".format(postform.errors.as_data()))
    return render(request, 'webadmin/pages/manage/post/index.html',args)
Example #16
0
def save_post(request, post_id=None):
    post = post_id is None and Post(user_id=request.user.id) \
        or get_object_or_404(Post, id=post_id)
    if request.POST:
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            form.save()
            return redirect('edit_post', post.id)
    else:
        form = PostForm(instance=post)
    images = Image.objects.filter(user_id=request.user.id)
    form.new = post_id is None or False
    return render(request, 'post/form.html', {'form': form,
                                              'images': images})
Example #17
0
File: post.py Project: jcasmer/blog
    def post(self, request, *args, **kwargs):
        """
        Muestra el formulario 
        """
        form = PostForm(request.POST)
        if not form.is_valid():
            messages.error(request, 'Valide los errores')
            output = {
                'form': form,
            }
            return render(request, self.template_name, output)

        form.save()
        messages.success(request, 'Post registrado exitosamente')
        return HttpResponseRedirect(reverse('post:index'))
Example #18
0
def post_create(request):
    # # 로그인이 되어있는 상태인지 확인해야 한다.
    # if not request.user.is_authenticated():
    #     return redirect('member:login')
    if request.method == 'POST':
        form = PostForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            # Form 안에서 comment저장을 하기 위해 author를 인수로 할당하고
            # 원래 author를 추가로 저장하기 위해 설정했던 commit=False는 삭제
            post = form.save(author=request.user)

            # PostForm에 comment가 전달되었을 경우 Comment객체 생성
            # comment_string = form.cleaned_data['comment']
            # if comment_string:
            #     post.comment_set.create(
            #         author=request.user,
            #         content=comment_string,
            #     )
            return redirect('post:post_detail', post_pk=post.pk)
        else:
            context = {
                'form': form,
            }
            return render(request, 'post/post_create.html', context)
    else:
        form = PostForm()
    context = {
        'form': form,
    }
    return render(request, 'post/post_create.html', context)
Example #19
0
File: post.py Project: jcasmer/blog
    def post(self, request, *args, **kwargs):
        """
        Muestra el formulario 
        """
        post = PostBlog.objects.get(id=kwargs['id'])
        form = PostForm(request.POST, instance=post)
        if not form.is_valid():
            messages.error(request, 'Valide los errores')
            output = {
                'form': form,
            }
            return render(request, self.template_name, output)

        form.save()
        messages.success(request, 'Post actualizado exitosamente')
        return HttpResponseRedirect(reverse('post:index'))
Example #20
0
def edit_post(request, post_id):
    post = get_object_or_404(Post, 
                             pk=post_id,
                             user=request.user)

    if request.method == 'POST':
        form = PostForm(request.POST, 
                        request.FILES, 
                        instance=post)
        if form.is_valid():
            # set user to form instance
            form.instance.user = request.user
            
            # update post
            post = form.save()

            # add resize image task to celery
            resize_post_image.delay(post)

            messages.success(request, _("Post updated succesfully."))
            return redirect("my_posts")
    else:
        form = PostForm(instance=post)

    return render(request, 'post/edit_post.html', {
        'form': form,
    })
Example #21
0
def create(request):
    form = PostForm(request.POST, user=request.user)
    if form.is_valid():
        post = form.save()
        return JsonResponse(post.to_client())
    else:
        return JsonResponse(form.errors, status=400)
Example #22
0
def add_post(request):
    if request.method == "POST":
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            book = form.save(commit=False)
            book.create_by_id = request.user.pk
            book.save()
            return redirect('post_list')
    return render(request, 'post/add_post.html', {'form': PostForm})
Example #23
0
def add_post(request):
    form = PostForm(request.POST or None)
    if form.is_valid():
        post = form.save(commit=False)
        post.save()
        return redirect('http://localhost:8000/posts/')
    return render(template_name='add_post.html',
                  context={"form": form},
                  request=request)
Example #24
0
def add_post(request, add_post_success_url="home", template="post/add_post.html"):

	form = PostForm(request.POST or None)
	if form.is_valid():
		post = form.save(commit=False)
		post.author = request.user
		post.save()
		return redirect(add_post_success_url)

	return render(request, template, {'form': form})
Example #25
0
def create_post(request):
    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('posts:feed')

    else:
        form = PostForm()

    return render(
        request=request,
        template_name='post/new.html',
        context={
            'form':form,
            'user':request.user,
            'profile': request.user.profile
        }
    )
Example #26
0
def admindatabase(request):
    postform = PostForm()
    roleform = RoleForm()
    profileform = ProfileForm()
    userform = UserForm()
    publicationform = PublicationForm()
    args = {'postform' : postform, 'roleform' : roleform, 'publicationform' :publicationform, 'profileform' : profileform, 'userform' : userform }
    if request.method == "POST":
        if 'add_post' in request.POST:
            postform = PostForm(request.POST, request.FILES)
            if postform.is_valid():
                postform.save()
            else:
                print("errors : {}".format(postform.errors.as_data()))
        elif 'add_role' in request.POST:
            roleform = RoleForm(request.POST)
            if roleform.is_valid():
                data  = roleform.cleaned_data['user_id']
                data2 = roleform.cleaned_data['publication']
                if Role.objects.check_role(data,data2):
                    print("User already has role in this publication")
                else:
                    roleform.save()
            else:
                print("errors : {}".format(roleform.errors.as_data()))
        elif 'add_publication' in request.POST:
            publicationform = PublicationForm(request.POST, request.FILES)
            if publicationform.is_valid():
                publicationform.save()
            else:
                print("errors : {}".format(publicationform.errors.as_data()))
        elif 'add_user' in request.POST:
            profileform = ProfileForm(request.POST)
            userform = UserForm(request.POST)
            if profileform.is_valid() and userform.is_valid():
                profile = profileform.save(commit=False)
                user = userform.save()
                profile.user = user
                profile.save()
            else:
                print("errors : {}".format(profileform.errors.as_data()))
                print("errors : {}".format(userform.errors.as_data()))
    return render(request, 'webadmin/pages/database/index.html',args)
Example #27
0
def news_list(request):

    # BIBOD BSIX NOBIN
    news = News.objects.all()
    #new = get_object_or_404(NewP,)
    posts = NewP.objects.filter()
    if request.method == "POST":
        form = PostForm(request.POST)
        form = form.save(commit=False)
        form.user = request.user
        form.save()
        return redirect(news_list)
    else:
        form = PostForm()

    return render(request, "post/news_list.html", {
        "news": news,
        "posts": posts,
        "form": form
    })
Example #28
0
def createpost_view(request):
    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user  # save the user who created the post
            post.save()
            return redirect(post.get_absolute_url())
    else:
        form = PostForm
    context = {'form': form}
    return render(request, 'post/createpost.html', context)
Example #29
0
def post_edit(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            post.post_date = timezone.now()
            post.save()
            return redirect('post:post_detail', post.pk)
    else:
        form = PostForm(instance=post)
    return render(request, 'edit.html', {'form': form})
Example #30
0
def post_modify(request, post_pk):
    post = get_object_or_404(Post, pk=post_pk)
    if request.method == 'POST':
        form = PostForm(data=request.POST, files=request.FILES, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = User.objects.first()
            post.save()
            return redirect('post_list')
    else:
        form = PostForm(instance=post)
    context = {'form': form}
    return render(request, 'post/post_modify.html', context=context)
Example #31
0
def post_create(request):
    form = PostForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.user = request.user
        instance.save()
        # message success
        messages.success(request, "Successfully Created")
        return HttpResponseRedirect(instance.get_absolute_url())
    context = {
        "form": form,
    }
    return render(request, "post_form.html", context)
Example #32
0
def add(request):
    if request.method == "POST":
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            post = form.save(commit=False)
            post.post_author = request.user
            post.post_date = timezone.now()
            post.post_status = 0
            post.save()
            return redirect('post:post_detail', post.pk)
    else:
        form = PostForm()
    return render(request, 'add.html', {'form': form})
Example #33
0
    def post(self, request, title=None):
        form = PostImportForm(request.POST, files=request.FILES)

        if form.is_valid() is True:
            print("VALID")
            json_data_string = form.cleaned_data.get("file").read()
            json_data = json.loads(json_data_string)
            json_data["pk"] = None

            content_set = json_data["fields"].pop("content_set")

            json_data_string = json.dumps([json_data])

            deserialized_object = next(serializers.deserialize("json", json_data_string))
            instance = deserialized_object.object
            instance.slug_title = None

            instance.author = request.user
            data = model_to_dict(instance)

            post_form = PostForm(data)

            if post_form.is_valid() is True:
                instance = post_form.save()

                for content_json in content_set:
                    content_json["pk"] = None
                    content_json["fields"]["post"] = instance
                    # content_json["fields"]["sequence"] = None

                    image_base64_string = None

                    if content_json["fields"]["image"]:
                        image_base64_string = content_json["fields"]["image"]
                        content_json["fields"]["image"] = None

                    content_instance = Content.objects.create(**content_json["fields"])

                    if image_base64_string:
                        image = Image.objects.create(
                            content=content_instance)
                        image.image_file.save("", content=ContentFile(base64.b64decode(image_base64_string)))
                        print(content_json)
                        print(image.image_file)

                    instance.content_set.add(content_instance)

                return HttpResponseRedirect(
                    reverse_lazy("blog:post-detail", kwargs={"title": instance.slug_title}))
            else:
                return render(request, template_name="post/import.html", context={"form": form, "post_form": post_form})
Example #34
0
def create(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.created_at = timezone.now()
            post.save()
            pk = post.id
            url = reverse('post:detail', kwargs={'pk': pk})
            return redirect(to=url)
    else:
        form = PostForm()
    context = {'form': form}
    return render(request, 'post/create.html', context)
Example #35
0
def add_post(request):
    context = {}
    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            post = form.save()
            post.owner = request.user
            post.save()
            return redirect('post:get', post_id=post.pk)
        else:
            context['form'] = form
    else:
        context['form'] = PostForm()
    return render(request, 'add-post.html', context)
Example #36
0
    def post_community_new(request, community_url):
        """
        Function for creating post

        :param community_url: community custom_url field
        :type community_url: str
        :param request: object with request details
        :type request: :class:`django.http.HttpRequest`
        :return: responce object with HTML code
        :rtype: :class:`django.http.HttpResponse`
        :raises: :class:`django.http.Http404` if custom url not valid
        """

        community = get_object_or_404(Community, custom_url=community_url)

        post_image_form_set = modelformset_factory(PostImages,
                                                   form=PostImageForm,
                                                   extra=10,
                                                   max_num=10)

        if request.method == "POST":
            postForm = PostForm(request.POST)
            formset = post_image_form_set(request.POST,
                                          request.FILES,
                                          queryset=PostImages.objects.none())

            if postForm.is_valid() and formset.is_valid():
                post_form = postForm.save(commit=False)
                post_form.community = community
                post_form.owner_community = community
                post_form.save()

                if request.POST.get('music'):
                    for music_id in [
                            int(i) for i in request.POST.get('music').split()
                    ]:
                        music_item = get_object_or_404(Music, id=music_id)
                        post_form.music.add(music_item)

                community.posts.add(post_form)

                for form in formset.cleaned_data:
                    if form:
                        image = form['image']
                        photo = PostImages(post=post_form,
                                           image=image,
                                           from_user_id=request.user.id)
                        photo.save()

        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
Example #37
0
def profile(request):
    form = PostForm()
    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            return HttpResponseRedirect(reverse('home'))
    context = {
        'title': 'User',
        'form': form
    }
    return render(request, 'login/user.html', context=context)
Example #38
0
def create_post(request, *args, **kwargs):
    form = PostForm(request.POST or None, request.FILES or None)

    if form.is_valid():
        post = form.save(commit=False)
        post.author = request.user
        post.save()
        messages.success(request, "Your post has been created")
        return redirect("index")

    context = {
        'post_form': form,
    }

    return render(request, 'user/create_post.html', context)
Example #39
0
 def post(self, request):
     """
     Valida el formulario de creacion de nuevo post y lo crea.
     :param request: objeto HttpRequest con los datos de la peticion
     :return:
     """
     message = None
     post_with_user = Post(owner=request.user)
     post_form = PostForm(request.POST, instance=post_with_user)
     if post_form.is_valid():
         new_post = post_form.save()
         post_form = PostForm()
         message = "Post creado satisfactoriamente."
     context = {"form": post_form, "message": message}
     return render(request, "post/new_post.html", context)
Example #40
0
def edit_post(request, post_id=None):
    if post_id is not None:
        post = get_object_or_404(Post, pk=post_id)
    else:
        post = None
    if request.method == 'POST':
        if post and 'delete' in request.POST:
            post.delete()
            return redirect('posts')
        form = PostForm(request.POST, request.FILES, instance=post)
        if form.is_valid():
            post = form.save(request.profile)
            return HttpResponseRedirect(post.get_absolute_url())
    else:
        form = PostForm(instance=post)
    return locals()
Example #41
0
def write_post(request):
    lastposts = Post.objects.all()
    cat = Category.objects.all()
    layout = 'vertical'

    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            new_post = form.save(commit=False)
            new_post.save()
            return home(request)
    else:
        form = PostForm()

    return render_to_response('writepost.html', RequestContext(request, {
        'form': form,
        'layout': layout,
        'cat':cat,
        'lastposts':lastposts,
        }))
Example #42
0
def create_post(request):
    if request.method == 'POST':
        form = PostForm(request.POST, 
                        request.FILES)
        if form.is_valid():
            # set user to form instance
            form.instance.user = request.user

            # save form, create post
            post = form.save()

            # add resize image task to celery
            resize_post_image.delay(post)

            messages.success(request, _("Post created succesfully."))
            return redirect("my_posts")
    else:
        form = PostForm()

    return render(request, 'post/create_post.html', {
        'form': form,
    })
Example #43
0
 def post(self, request):
     """
     Crea un post en base a la información POST
     :param request: HttpRequest
     :return: HttpResponse
     """
     success_message = ""
     post_with_owner = Post()
     blog = self.get_blogs_queryset(request.user.username).all()
     post_with_owner.blog = blog[0]
     post_with_owner.owner = request.user  # asigno como propietario de la foto, el usuario autenticado
     form = PostForm(request.POST, instance=post_with_owner)
     if form.is_valid():
         new_post = form.save()  # Guarda el objeto Photo y me lo devuelve
         form = PostForm()
         success_message = "Guardado con éxito!"
         success_message += '<a href="{0}">'.format(
             reverse("post_detail", args=[new_post.blog.owner.username, new_post.pk])
         )
         success_message += "Ver post"
         success_message += "</a>"
     context = {"form": form, "success_message": success_message}
     return render(request, "post/new_post.html", context)
Example #44
0
 def post(self, request):
     form = PostForm(data=request.POST, author=request.user)
     if form.is_valid():
         form.save()
         return HttpResponseRedirect(reverse("home:index"))
     return render(request, "post/create.html", {"form": form})