def delete(self, request, *args, **kwargs): self.object = self.get_object() user = request.user blog_posts = BlogPost.objects.published(for_user=user).select_related().filter(user=user) if blog_posts and blog_posts[0]: blog_post = blog_posts[0] blog_post.num_images = blog_post.num_images - self.object.images.all().count() blog_post.save() for image in self.object.images.all(): delete(image.image) content_type_id = ContentType.objects.get_for_model(image).pk deleteObject(request, content_type_id, image.pk) media_root = getattr(settings, 'MEDIA_ROOT', '/') album_dir = self.object.get_album_path() album_abs_dir = os.path.join(media_root, album_dir) os.rmdir(album_abs_dir) content_type_id = ContentType.objects.get_for_model(self.object).pk deleteObject(request, content_type_id, self.object.pk) return HttpResponseRedirect(self.get_success_url())
def delete(self, request, *args, **kwargs): self.object = self.get_object() if self.object.image: delete(self.object.image) content_type_id = ContentType.objects.get_for_model(self.object).pk deleteObject(request, content_type_id, self.object.pk) user = request.user blog_posts = BlogPost.objects.published(for_user=user).select_related().filter(user=user) if blog_posts and blog_posts[0]: blog_post = blog_posts[0] blog_post.num_images = blog_post.num_images - 1 blog_post.save() return HttpResponseRedirect(self.get_success_url())
def deleteAction(request, action_id): if not request.is_ajax(): return json_error_response('only supported with AJAX') actionObject = get_object_or_404(models.Action, pk=action_id) blog_posts = BlogPost.objects.published( for_user=request.user).select_related().filter(user=request.user) """ For now considering blog_posts as a list. Going forward we will restrict the #blogposts to be one per user therefore fetching the first element only is sufficient. Remove this loop then. """ blog_post = None if blog_posts: blog_post = blog_posts[0] if (actionObject.actor.__class__.__name__ == "User" and actionObject.actor == request.user) or (actionObject.actor.__class__.__name__ == "BlogPost" and blog_post and actionObject.actor == blog_post): """ Action can be subaction of shared actions. Find'em and kill. """ pActionObect = models.Action.objects.all().filter(target_object_id=actionObject.pk) if pActionObect is not None: for aObject in pActionObect: if aObject.verb == settings.SHARE_VERB: aObject.delete() """ If target is generic post, delete it along with feed. We do not use type method and not isinstance as BroadcastWish & BroadcastDeal models inherit from GenericWish and will pass the check. """ if actionObject.target and type(actionObject.target) == GenericWish: object_instance = actionObject.target content_type_id = ContentType.objects.get_for_model(object_instance).pk deleteObject(request, content_type_id, object_instance.pk) """ now delete the action """ actionObject.delete() return HttpResponse(simplejson.dumps(dict(success=True))) else: return json_error_response('Unauthorized operation!! request cannot be completed')