2

I am trying to delete a report from a particular client, so currently in my url.py i am passing the client id and the report id, hoping to delete report Y from client X. I could have done this using def ReportScheduleDeleteView(request): , but was hoping to use the Class-Based DeleteView.

I had a look at this example but wasn't able to blend with my code.

So here is my code.

urls.py

url(r'^jsclient/(?P<pk>\d+)/report/(?P<r_pk>\d+)/delete/$', ReportScheduleDeleteView.as_view(), name="report-delete"),

models.py -

class JSClient(models.Model):
    name = models.CharField(max_length=255, unique=True)
    clientAccount = models.CharField(max_length=255)
    ....

class ReportSchedule(models.Model):
    client = models.ForeignKey(JSClient)
    schedRepName = models.CharField(max_length=255)
    reportType = models.CharField(max_length=255, choices=REPORT_TYPE)
    ....

views.py :

class ReportScheduleDeleteView(DeleteView):   
    model = ReportSchedule
    template_name = "report/report_confirm_delete.html"
    success_url = lazy(reverse, str)('jsclient-list')

I am sure there must be a way of doing this using the Class-Based DeleteView, any help on this is appreciated.

Community
  • 1
  • 1
shabeer90
  • 5,161
  • 4
  • 47
  • 65

2 Answers2

6

Thanks to the tip from EsseTi and CCBV site I managed to crack out the solution for my problem. It might have been obvious

class ReportScheduleDeleteView(DeleteView):
    model = ReportSchedule
    template_name = "report/report_confirm_delete.html"
    success_url = lazy(reverse, str)('jsclient-list')

    # Get the parameters passed in the url so they can be used in the 
    # "report/report_confirm_delete.html"     


**UPDATE:**

    def get_object(self, queryset=None):
        if queryset is None:
            queryset = self.get_queryset()

        client = self.kwargs['pk']
        report = self.kwargs['rpk']

        queryset = ReportSchedule.objects.filter(client_id=client, id=report)

        if not queryset:
            raise Http404

        context = {'client_id':client, 'report_id':report}
        return context

    # Override the delete function to delete report Y from client X
    # Finally redirect back to the client X page with the list of reports
    def delete(self, request, *args, **kwargs):
        client = self.kwargs['pk']
        report = self.kwargs['rpk']

        clientReport = ReportSchedule.objects.filter(client_id=client, id=report)
        clientReport.delete()

        return HttpResponseRedirect(reverse('report-list', kwargs={'pk': client}))

Hope it helps some one.

Community
  • 1
  • 1
shabeer90
  • 5,161
  • 4
  • 47
  • 65
  • 3
    it would have been easier if you did the checking in the get object (what you put in the delete function) and that's it (Returning 404 if filter return empty). Another thing, your app is insicure. every user can call the `/jsclient/1/report/1/delete` even if they are not the owners. you probably need to take care of this. – EsseTi May 17 '13 at 13:34
  • Thanks for the tip, I shall add it to my code. – shabeer90 May 17 '13 at 13:39
  • This is nice to use with unique_together, despite what @EsseTi said. – Arthur Alvim Sep 09 '14 at 19:46
1

it's simliar to this one Example of Django Class-Based DeleteView

redefine def get_object(self, queryset=None) and do your checking inside. using kwargs you should be able to get the parameters from url.

Community
  • 1
  • 1
EsseTi
  • 4,079
  • 5
  • 36
  • 63
  • I did have a look at that previously, but was confused on using it, would you be able to show how it applies to mine? – shabeer90 May 17 '13 at 10:53
  • it's exactly the same. you are overengingeering your solution. you don't have to pass the pk of the user (that sounds pretty unsafe), you can infeer the user from `self.request.user`. then you have the user and the object (which is automatically loaded using the pk value of url). so just check if `obj.client=user`. it's the same as the other question. – EsseTi May 17 '13 at 12:47
  • Thanks for the tip i did manage to find a solution, will post it once i complete it fully :D – shabeer90 May 17 '13 at 12:51