use is_job_poster rules in views and templates, create renew view

This commit is contained in:
aatish 2018-10-20 13:33:55 +05:45
parent 95d2f9c3d1
commit 57d270879a
5 changed files with 57 additions and 18 deletions

View file

@ -22,6 +22,12 @@
</div> </div>
<div class="container"> <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">&#215;</button>
{{ message }}
</div>
{% endfor %}
{% block body_content %}{% endblock %} {% block body_content %}{% endblock %}
<footer class="pt-4 my-md-5 pt-md-5 border-top"> <footer class="pt-4 my-md-5 pt-md-5 border-top">

View file

@ -1,4 +1,6 @@
{% extends 'base.html' %} {% block body_content %} {% extends 'base.html' %}
{% load rules %}
{% block body_content %}
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
<div class="pricing-header px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center"> <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/> - {{question.name}} <br/>
{% endfor %} {% endfor %}
<br/> <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> <a href="{% url 'jobs:job_apply' job.pk %}" class="card-link">Apply</a>
{% endif %}
</div> </div>
</div> </div>
</div> </div>

View file

@ -1,15 +1,20 @@
from django.urls import path from django.urls import path
from .views import Index, JobCreate, JobList, JobDetail, ApplicationCreate from . import views
from . import autocomplete as autocomplete_views from . import autocomplete as autocomplete_views
app_name = 'jobs' app_name = 'jobs'
urlpatterns = [ urlpatterns = [
path('', Index.as_view(), name='index'), path('', views.Index.as_view(), name='index'),
path('jobs/create/', JobCreate.as_view(), name='create'), path('jobs/create/', views.JobCreate.as_view(), name='create'),
path('jobs/', JobList.as_view(), name='list'), path('jobs/', views.JobList.as_view(), name='list'),
path('jobs/<int:pk>/detail/', JobDetail.as_view(), name='job_detail'), path(
path('jobs/<int:job_pk>/apply/', ApplicationCreate.as_view(), name='job_apply'), '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 # autocomplete endpoints

View file

@ -1,8 +1,10 @@
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.views.generic import ( from django.contrib import messages
TemplateView, ListView, CreateView, DetailView 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 .models import Job, Application, Question
from .forms import JobForm, QuestionFormSet, ApplicationForm, AnswerForm from .forms import JobForm, QuestionFormSet, ApplicationForm, AnswerForm
@ -22,6 +24,21 @@ class JobDetail(DetailView):
model = Job 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): class JobCreate(CreateView):
model = Job model = Job
form_class = JobForm form_class = JobForm
@ -56,9 +73,7 @@ class JobCreate(CreateView):
def form_invalid(self, form, question_form): def form_invalid(self, form, question_form):
return self.render_to_response( return self.render_to_response(
self.get_context_data( self.get_context_data(form=form, question_form=question_form))
form=form,
question_form=question_form))
class ApplicationCreate(CreateView): class ApplicationCreate(CreateView):
@ -68,12 +83,15 @@ class ApplicationCreate(CreateView):
success_url = reverse_lazy("jobs:list") success_url = reverse_lazy("jobs:list")
def get_question_queryset(self): 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): def get_answer_formset(self, *args):
questions = self.get_question_queryset() questions = self.get_question_queryset()
return AnswerForm.inlineformset_factory( return AnswerForm.inlineformset_factory(extra=questions.count())(
extra=questions.count())(initial=[{ initial=[{
'question': q.id 'question': q.id
} for q in questions], *args) } for q in questions], *args)