use is_job_poster rules in views and templates, create renew view
This commit is contained in:
parent
95d2f9c3d1
commit
57d270879a
5 changed files with 57 additions and 18 deletions
|
@ -22,6 +22,12 @@
|
|||
</div>
|
||||
|
||||
<div class="container">
|
||||
{% for message in messages %}
|
||||
<div class="alert alert-{{ message.tags }} alert-dismissable">
|
||||
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
|
||||
{{ message }}
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% block body_content %}{% endblock %}
|
||||
|
||||
<footer class="pt-4 my-md-5 pt-md-5 border-top">
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
{% extends 'base.html' %} {% block body_content %}
|
||||
{% extends 'base.html' %}
|
||||
{% load rules %}
|
||||
{% block body_content %}
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<div class="pricing-header px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center">
|
||||
|
@ -22,7 +24,15 @@
|
|||
- {{question.name}} <br/>
|
||||
{% endfor %}
|
||||
<br/>
|
||||
{% has_perm 'jobs.change_job' request.user job as can_change_job %}
|
||||
{% if can_change_job %}
|
||||
<form method="POST" action="{% url 'jobs:job_renew' job.pk %}">
|
||||
{% csrf_token %}
|
||||
<button type="submit" class="btn btn-primary">Renew Job</button>
|
||||
</form>
|
||||
{% else %}
|
||||
<a href="{% url 'jobs:job_apply' job.pk %}" class="card-link">Apply</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
17
jobs/urls.py
17
jobs/urls.py
|
@ -1,15 +1,20 @@
|
|||
from django.urls import path
|
||||
|
||||
from .views import Index, JobCreate, JobList, JobDetail, ApplicationCreate
|
||||
from . import views
|
||||
from . import autocomplete as autocomplete_views
|
||||
|
||||
app_name = 'jobs'
|
||||
urlpatterns = [
|
||||
path('', Index.as_view(), name='index'),
|
||||
path('jobs/create/', JobCreate.as_view(), name='create'),
|
||||
path('jobs/', JobList.as_view(), name='list'),
|
||||
path('jobs/<int:pk>/detail/', JobDetail.as_view(), name='job_detail'),
|
||||
path('jobs/<int:job_pk>/apply/', ApplicationCreate.as_view(), name='job_apply'),
|
||||
path('', views.Index.as_view(), name='index'),
|
||||
path('jobs/create/', views.JobCreate.as_view(), name='create'),
|
||||
path('jobs/', views.JobList.as_view(), name='list'),
|
||||
path(
|
||||
'jobs/<int:pk>/detail/', views.JobDetail.as_view(), name='job_detail'),
|
||||
path('jobs/<int:pk>/renew/', views.JobRenew.as_view(), name='job_renew'),
|
||||
path(
|
||||
'jobs/<int:job_pk>/apply/',
|
||||
views.ApplicationCreate.as_view(),
|
||||
name='job_apply'),
|
||||
]
|
||||
|
||||
# autocomplete endpoints
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
from django.urls import reverse_lazy
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.views.generic import (
|
||||
TemplateView, ListView, CreateView, DetailView
|
||||
)
|
||||
from django.contrib import messages
|
||||
from django.views.generic import (View, TemplateView, ListView, CreateView,
|
||||
DetailView)
|
||||
from django.views.generic.detail import SingleObjectMixin
|
||||
from rules.contrib.views import PermissionRequiredMixin
|
||||
|
||||
from .models import Job, Application, Question
|
||||
from .forms import JobForm, QuestionFormSet, ApplicationForm, AnswerForm
|
||||
|
@ -22,6 +24,21 @@ class JobDetail(DetailView):
|
|||
model = Job
|
||||
|
||||
|
||||
class JobRenew(PermissionRequiredMixin, SingleObjectMixin, View):
|
||||
model = Job
|
||||
permission_required = 'jobs.change_job'
|
||||
http_method_names = ['post']
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
job = self.get_object()
|
||||
job.renew()
|
||||
messages.add_message(
|
||||
request, messages.SUCCESSS,
|
||||
'Job has been renewed until {}.'.format(
|
||||
job.expires.isoformat(' ', 'seconds')))
|
||||
return HttpResponseRedirect(job.get_absolute_url())
|
||||
|
||||
|
||||
class JobCreate(CreateView):
|
||||
model = Job
|
||||
form_class = JobForm
|
||||
|
@ -56,9 +73,7 @@ class JobCreate(CreateView):
|
|||
|
||||
def form_invalid(self, form, question_form):
|
||||
return self.render_to_response(
|
||||
self.get_context_data(
|
||||
form=form,
|
||||
question_form=question_form))
|
||||
self.get_context_data(form=form, question_form=question_form))
|
||||
|
||||
|
||||
class ApplicationCreate(CreateView):
|
||||
|
@ -68,12 +83,15 @@ class ApplicationCreate(CreateView):
|
|||
success_url = reverse_lazy("jobs:list")
|
||||
|
||||
def get_question_queryset(self):
|
||||
return Question.objects.filter(job_id=self.kwargs['job_pk']).order_by('id')
|
||||
# filter questions for particular job and order it so same queryset
|
||||
# can be used stably as initial params for GET and POST requests
|
||||
return Question.objects.filter(
|
||||
job_id=self.kwargs['job_pk']).order_by('id')
|
||||
|
||||
def get_answer_formset(self, *args):
|
||||
questions = self.get_question_queryset()
|
||||
return AnswerForm.inlineformset_factory(
|
||||
extra=questions.count())(initial=[{
|
||||
return AnswerForm.inlineformset_factory(extra=questions.count())(
|
||||
initial=[{
|
||||
'question': q.id
|
||||
} for q in questions], *args)
|
||||
|
||||
|
|
Loading…
Reference in a new issue