def post(self, request, pk, idPost): """ Actualiza un post en base a la información POST :param request: HttpRequest :return: HttpResponse """ success_message = '' blog = UserUtils.getUserBlog(request.user.username) post_with_blog = Post() post_with_blog.blog = blog form = PostForm(request.POST, instance=post_with_blog) # Coge los datos del formulario sobreescribe los campos que tiene el objeto 'photo_with_owner' if form.is_valid(): new_post = form.save() # Guarda el objeto en BB.DD. y me lo devuelve form = PostForm() # Forzamos a que el formulario se vacie si la creacion de la foto ha sido satisfactoria success_message = 'Guardado con éxito!' success_message += '<a href = "{0}">'.format( reverse('post_detail', args=[new_post.blog.author.username,new_post.pk]) ) success_message += 'Ver post' success_message += '</a>' blog = UserUtils.getUserBlog(pk) possible_posts = self.get_posts_queryset(request).filter(blog__name=blog.name, pk=idPost).order_by('-publication_date') post_to_delete = possible_posts[0] if len(possible_posts) >= 1 else None if post_to_delete is not None: post_to_delete.delete() context = { 'form': form, 'success_message': success_message } return self.render(request, context)
def post(self, request): """ Crea un post en base a la información POST :param request: HttpRequest :return: HttpResponse """ success_message = '' blog = UserUtils.getUserBlog(request.user.username) post_with_blog = Post() post_with_blog.blog = blog form = PostForm(request.POST, instance=post_with_blog) # Coge los datos del formulario sobreescribe los campos que tiene el objeto 'photo_with_owner' if form.is_valid(): new_post = form.save() # Guarda el objeto en BB.DD. y me lo devuelve form = PostForm() # Forzamos a que el formulario se vacie si la creacion de la foto ha sido satisfactoria success_message = 'Guardado con éxito!' success_message += '<a href = "{0}">'.format( reverse('post_detail', args=[new_post.blog.author.username,new_post.pk]) ) success_message += 'Ver post' success_message += '</a>' context = { 'form': form, 'success_message': success_message } return self.render(request, context)
def get(self, request, pk): """ Carga la página de detalle de un blog :param request: HttpRequest :param pk: username :return: HttpResponse """ blog = UserUtils.getUserBlog(pk) posts = self.get_posts_queryset(request).filter(blog__name=blog.name).order_by('-publication_date') context = { 'posts_list': posts, 'pk': pk, 'blog': blog } return render(request, 'blogs/blog_detail.html', context)
def get(self, request, pk, idPost): """ Carga la página de detalle de un post :param request: HttpRequest :param pk: username, idPost: id del post :return: HttpResponse """ blog = UserUtils.getUserBlog(pk) possible_posts = self.get_posts_queryset(request).filter(blog__name=blog.name, pk=idPost).order_by('-publication_date') post = possible_posts[0] if len(possible_posts) >= 1 else None if post is not None: context = { 'post': post, 'url_no_image': NO_IMAGE } return render(request, 'posts/post_detail.html', context) else: return HttpResponseNotFound('No existe el post o no tiene acceso') # 404 not found
def get(self, request, pk, idPost): """ Muestra un formulario para crear un post :param request: HttpRequest :return: HttpResponse """ blog = UserUtils.getUserBlog(pk) possible_posts = self.get_posts_queryset(request).filter(blog__name=blog.name, pk=idPost).order_by('-publication_date') post = possible_posts[0] if len(possible_posts) >= 1 else None if post is not None: form = PostForm(instance=post) else: form = PostForm() context = { 'form': form, 'success_message': '' } return self.render(request, context)
def get(self, request, pk): blog = UserUtils.getUserBlog(pk) posts = self.get_posts_queryset(request).filter(blog__name=blog.name).order_by('-publication_date') serializer = PostListSerializer(posts, many=True) return Response(serializer.data)