SHARE
TWEET

Untitled

a guest Jun 25th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Project(models.Model):
  2.     name = models.CharField(max_length=100)
  3.     description = models.TextField()
  4.     date_created = models.DateTimeField(default=timezone.now)
  5.     author = models.ForeignKey(User, on_delete=models.CASCADE)
  6.  
  7. class Task(models.Model):
  8.     name = models.CharField(max_length=50, default='New Model')
  9.     project = models.ForeignKey(Project, on_delete=models.CASCADE)
  10.     date_created = models.DateTimeField(default=timezone.now)
  11.      
  12. class TaskDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
  13.     model = Task
  14.     template_name = 'tasks/confirm_delete.html'   # template for deletion
  15.     success_url ='/projects/'    
  16.  
  17.     # Test user permission
  18.     def test_func(self):
  19.         task = self.get_object()
  20.  
  21.         if self.request.user == task .project.author:
  22.             return True
  23.         else:
  24.             return False
  25.  
  26.     def get_success_url(self):
  27.         project = self.object.
  28.         return reverse_lazy('tasks-listview', kwargs={'pk': project.id })
  29.      
  30. urlpatterns = [
  31.     path('tasks/<int:pk>/list/', TasksListview.as_view(), name='tasks-listview'),
  32.     path('tasks/<int:pk>/delete/', TaskDeleteView.as_view(), name='task-delete'),
  33. ]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top