Commit 2aabf1d1 authored by smilin_desperado's avatar smilin_desperado

Cleanup and extra tests

parent 3b3888bf
......@@ -14,7 +14,7 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include, path
from django.urls import include, path
from django.conf import settings
urlpatterns = [
......
from django.test import TestCase
from django.urls import reverse
from django.contrib.auth.models import User
from .models import Job
class IndexPageTests(TestCase):
def test_empty_dashboard(self):
self.client.force_login('test_user')
self.client.force_login(User.objects.get_or_create(username='test_user')[0])
response = self.client.get('/')
html = response.content.decode('utf8')
html = response.content.decode()
self.assertTemplateUsed(response, 'tracker/index.html')
self.assertIn('Dashboard', html)
self.assertIn('<title>Dashboard | JAT</title>', html)
self.assertIn('Add Job', html)
def test_can_save_new_job(self):
self.client.force_login(User.objects.get_or_create(username='test_user')[0])
response = self.client.post('/add/', {'position_title': 'test position'}, follow=True)
self.assertIn('Test Position', response.content.decode())
self.assertTemplateUsed(response, 'tracker/index.html')
class EditPageTests(TestCase):
def test_edit_page_layout(self):
pass
def test_wrong_user_gets_404(self):
user = User.objects.get_or_create(username='test_user')[0]
job = Job(position_title='test position', user=user)
job.save()
wrong_user = User.objects.get_or_create(username='wrong_user')[0]
self.client.force_login(wrong_user)
response = self.client.get(f"/{job.id}", follow=True)
self.assertEqual(response.status_code, 404)
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.urls import reverse, reverse_lazy
from django.views.generic import ListView, DetailView, DeleteView
from django.views.generic import ListView, DeleteView
from django.views.generic.edit import UpdateView
from django.contrib.messages.views import SuccessMessageMixin
from django.contrib import messages
......@@ -9,21 +8,23 @@ from django.contrib.auth.models import User
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.decorators import login_required
from .forms import JobAddForm, JobForm
from .models import Note, Job
from .models import Job
class IndexView(LoginRequiredMixin, ListView):
template_name = 'tracker/index.html'
model = Job
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
form = JobAddForm()
context['form'] = form
return context
context = super().get_context_data(**kwargs)
form = JobAddForm()
context['form'] = form
return context
def get_queryset(self):
return Job.objects.filter(user=self.request.user)
@login_required()
def add(request):
if request.method == 'POST':
......@@ -32,14 +33,15 @@ def add(request):
job = form.save(commit=False)
job.user = User.objects.get(pk=request.user.id)
job.save()
messages.success(request,f"{job.position_title} created")
messages.success(request, f"{job.position_title} created")
return redirect(reverse('tracker:index'))
else:
return render(request, 'tracker/add.html', {'form': form})
return render(request, 'tracker/add.html', {'form': form})
else:
form = JobAddForm()
form.user = request.user
return render(request, 'tracker/add.html', {'form': form})
return render(request, 'tracker/add.html', {'form': form})
class EditView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
template_name = 'tracker/edit.html'
......@@ -47,20 +49,20 @@ class EditView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
form_class = JobForm
success_url = reverse_lazy('tracker:index')
success_message = "%(position_title)s was successfully updated"
def get_queryset(self):
return super().get_queryset().filter(user=self.request.user)
return super().get_queryset().filter(user=self.request.user)
class DeleteJobView(LoginRequiredMixin, DeleteView):
model = Job
template_name = 'tracker/delete.html'
success_url = reverse_lazy('tracker:index')
success_message = "Job was successfully deleted"
def delete(self, request, *args, **kwargs):
messages.success(self.request, self.success_message)
return super(DeleteJobView, self).delete(request, *args, **kwargs)
def get_queryset(self):
return super().get_queryset().filter(user=self.request.user)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment