From 52d74c36be4e2983adec97c08eb1351e84ec6cb2 Mon Sep 17 00:00:00 2001 From: Eligiusz Kurzawa Date: Sat, 16 Jan 2021 08:52:53 +0100 Subject: [PATCH] 27# added update, delete job --- jobs/views.py | 32 ++++++++++++++++++++++- templates/jobs/delete.html | 53 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 templates/jobs/delete.html diff --git a/jobs/views.py b/jobs/views.py index 2aed01e..6d11863 100644 --- a/jobs/views.py +++ b/jobs/views.py @@ -4,6 +4,7 @@ from django.db.models import Q from django.shortcuts import get_object_or_404 from django.utils.decorators import method_decorator from django.urls import reverse +from django.http import HttpResponseRedirect from django.views.generic import TemplateView, ListView, CreateView, DetailView, UpdateView, DeleteView from jobs.forms import CreateJobForm, ApplyJobForm, UpdateJobForm @@ -114,6 +115,35 @@ class UpdateJobView(SuccessMessageMixin ,UpdateView): form_class = UpdateJobForm success_message = "You updated your job!" + def form_valid(self, form): + form.instance.employer = self.request.user + return super(UpdateJobView, self).form_valid(form) + + def get(self, request, *args, **kwargs): + self.object = self.get_object() + if self.object.employer != request.user: + return HttpResponseRedirect('/') + return super(UpdateJobView, self).get(request, *args, **kwargs) + + def get_success_url(self): + return reverse('jobs:single_job', kwargs={"pk":self.object.pk, "slug":self.object.slug}) + class DeleteJobView(SuccessMessageMixin, DeleteView): - pass + model = Job + success_url = '/' + template_name = 'jobs/delete.html' + + def delete(self, request, *args, **kwargs): + self.object = self.get_object() + if self.object.employer == request.user: + self.object.delete() + return HttpResponseRedirect(self.success_url) + else: + return HttpResponseRedirect(self.success_url) + + def get(self, request, *args, **kwargs): + self.object = self.get_object() + if self.object.employer != request.user: + return HttpResponseRedirect('/') + return super(DeleteJobView, self).get(request, *args, **kwargs) diff --git a/templates/jobs/delete.html b/templates/jobs/delete.html new file mode 100644 index 0000000..51b8133 --- /dev/null +++ b/templates/jobs/delete.html @@ -0,0 +1,53 @@ +{% extends 'base.html' %} +{% load static %} +{% block content %} + +
+
+
+
+ +

Delete a job

+
+
+
+
+ +
+
+
+ +
+

Are you sure to delete?

+
+ {% csrf_token %} + No + +
+
+ +
+
+

Contact Info

+

Address

+

203 Fake St. Mountain View, San Francisco, California, USA

+ +

Phone

+

+1 232 3235 324

+ +

Email Address

+

[email protected]

+ +
+ +
+

More Info

+

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsa ad iure porro mollitia architecto hic consequuntur. Distinctio nisi perferendis dolore, ipsa consectetur

+

Learn More

+
+
+
+
+
+ +{% endblock %} \ No newline at end of file