Skip to content

Instantly share code, notes, and snippets.

@DeadWisdom
Last active December 16, 2015 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeadWisdom/5370522 to your computer and use it in GitHub Desktop.
Save DeadWisdom/5370522 to your computer and use it in GitHub Desktop.
This is a recreation of the view found here: https://github.com/jacobian/djobs/blob/master/jobs/views.py It is intended to show how class based views add unneeded complexity and should only be used in limited scenarios. It is *NOT* intended to be a criticism of the author in any way. I am merely using it as an example because it was brought to m…
import json
from django.contrib import messages
from django.core import urlresolvers
from django.db.models import Q, Count
from django.shortcuts import get_object_or_404, redirect, render
from django.contrib.auth.decorators import login_required, user_passes_test
from django.views.decorators.http import require_POST
from taggit.models import Tag
from .models import JobListing
from .forms import JobListingForm
### Views ###
def job_list(request):
"""
List of all published jobs.
"""
return render(request, 'jobs/index.html', {
'jobs': JobListing.objects.all(),
'navitem': 'all'
})
@login_required
def my_job_listing(request):
"""
"My listings" page.
"""
return render(request, 'jobs/mine.html', {
'jobs': request.user.job_listings.all(),
'navitem': 'mine'
})
def job_detail(request, pk):
"""
Individual job listing view.
"""
job = get_object_or_404(JobListing, pk=pk)
return render(request, 'jobs/detail.html', {
'job': job,
'navitem': 'mine',
'user_can_edit': (job.creator == request.user),
'has_flagged': 'flagged_%s' % job.id in request.session
})
@login_required
def job_create(request):
"""
Create a new job listing.
"""
if request.method == 'POST':
form = JobListingForm(request.POST)
if form.is_valid():
job = form.save()
messages.add_message(request, messages.SUCCESS, "Your job listing has been saved as a draft.")
return redirect("job_detail", job.id)
else:
form = JobListingForm()
tag_names = [t.name for t in Tag.objects.all()]
return render(request, 'jobs/edit.html', {
'form': form,
'json_tags': json.dumps(tag_names) # This is better to do as a filter: var tags = {{tags|json}}};
})
@login_required
def job_edit(request, pk):
"""
Edit an existing job.
Naturally only the person who created a job can edit it again.
"""
job = get_object_or_404(JobListing, pk=pk)
if request.method == 'POST':
form = JobListingForm(request.POST, instance=job)
if form.is_valid():
job = form.save()
messages.add_message(request, messages.SUCCESS, "Your job listing has been saved as a draft.")
return redirect("job_detail", job.id)
else:
form = JobListingForm(instance=job)
tag_names = [t.name for t in Tag.objects.all()]
return render(request, 'jobs/edit.html', {
'form': form,
'instance': job,
'json_tags': json.dumps(tag_names) # This is better to do as a filter: var tags = {{tags|json}}};
})
@login_required
def change_job_status(request, pk, status, msg):
"""
Abstract view to change a job's status for the views below.
"""
job = get_object_or_404(request.user.job_listings, pk=pk)
job.status = status
job.save()
messages.add_message(request, messages.SUCCESS, msg)
return redirect('job_detail', job.id)
def publish_job(request, pk):
"""
Publishes the job.
"""
return change_job_status(request, pk, JobListing.STATUS_ACTIVE,
msg = "Your job listing has been published.")
def archive_job(request, pk):
"""
Archives the job.
"""
return change_job_status(request, pk, JobListing.STATUS_ARCHIVED,
msg = "Your job listing has been archived and is no longer public.")
def login(request):
"""
Login screen.
"""
return render(request, "login.html", {'navitem': 'login'})
@login_required
@require_POST
def flag_job(request, pk):
"""
Flag a job as spam.
Has some basic protection against overposting, but for the most part we'll
just assume that people are good citizens and let flags through.
"""
jobs = JobListing.objects.filter(status=JobListing.STATUS_ACTIVE)
job = get_object_or_404(jobs, pk=pk)
# Flag the job, but only if we've not already recorded a flag from this session.
if 'flagged_%s' % pk not in request.session:
job.flags.create()
messages.add_message(request, messages.SUCCESS,
"Thanks for helping to keep our site spam-free! An adminstrator will review this posting shortly.")
request.session['flagged_%s' % pk] = True
return redirect('job_detail', job.id)
@login_required
@user_passes_test(lambda user: user.is_superuser)
def review_flags(request):
"""
View / Manage review flags.
"""
if request.method == 'POST':
try:
job = JobListing.objects.get(id=request.POST['job_id'])
action = request.POST['action']
except (KeyError, JobListing.DoesNotExist):
return redirect('review_flags')
if action == 'kill':
job.status = JobListing.STATUS_REMOVED
job.save()
job.flags.update(cleared=True)
messages.add_message(request, messages.SUCCESS, "'%s' removed." % job)
# FIXME: ban the user here?
elif action == 'keep':
job.flags.update(cleared=True)
messages.add_message(request, messages.SUCCESS, "'%s' kept." % job)
return redirect('review_flags') # Not sure why, but it matches the original code.
return render(request, 'flags.html', {
'flagged_jobs': JobListing.objects.filter(flags__cleared=False).annotate(Count('flags'))'
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment