Commit a7e41386 authored by Ola Hermann Opheim's avatar Ola Hermann Opheim
Browse files

Code review home/

parent 2bab2467
[
{
"model": "myapp.person",
"pk": 1,
"fields": {
"first_name": "John",
"last_name": "Lennon"
}
},
{
"model": "myapp.person",
"pk": 2,
"fields": {
"first_name": "Paul",
"last_name": "McCartney"
}
}
]
empty
\ No newline at end of file
......@@ -5,26 +5,30 @@ register = template.Library()
@register.filter
def check_nr_pending_offers(project):
pending_offers = 0
tasks = project.tasks.all()
for task in tasks:
taskoffers = task.taskoffer_set.all()
for taskoffer in taskoffers:
if taskoffer.status == TaskOffer.PENDING:
pending_offers+=1
#print(pending_offers)
return pending_offers
result = offer_status_counter(project, None)
return result[1]
@register.filter
def check_nr_user_offers(project, user):
result = offer_status_counter(project, user)
offers = {}
offers['declined'] = result[0]
offers['pending'] = result[1]
offers['accepted'] = result[2]
return offers
def offer_status_counter(project, user):
pending_offers = 0
declined_offers = 0
accepted_offers = 0
tasks = project.tasks.all()
for task in tasks:
taskoffers = task.taskoffer_set.filter(offerer=user.profile)
if user:
taskoffers = task.taskoffer_set.filter(offerer=user.profile)
else:
taskoffers = task.taskoffer_set.all()
for taskoffer in taskoffers:
if taskoffer.status == TaskOffer.PENDING:
pending_offers+=1
......@@ -32,12 +36,7 @@ def check_nr_user_offers(project, user):
accepted_offers+=1
elif taskoffer.status == TaskOffer.DECLINED:
declined_offers+=1
offers['declined'] = declined_offers
offers['pending'] = pending_offers
offers['accepted'] = accepted_offers
#print(offers)
return offers
return declined_offers, pending_offers, accepted_offers
@register.filter
def task_status(task):
......@@ -49,37 +48,9 @@ def task_status(task):
return "You have sent payment"
return "You are awaiting delivery"
@register.filter
def get_task_statuses(project):
task_statuses = {}
awaiting_delivery = 0
pending_acceptance = 0
pending_payment = 0
payment_sent = 0
declined_delivery = 0
tasks = project.tasks.all()
for task in tasks:
if task.status == Task.AWAITING_DELIVERY:
awaiting_delivery+=1
elif task.status == Task.PENDING_ACCEPTANCE:
pending_acceptance+=1
elif task.status == Task.PENDING_PAYMENT:
pending_payment+=1
elif task.status == Task.PAYMENT_SENT:
payment_sent+=1
elif task.status == Task.DECLINED_DELIVERY:
declined_delivery+=1
task_statuses['awaiting_delivery'] = awaiting_delivery
task_statuses['pending_acceptance'] = pending_acceptance
task_statuses['pending_payment'] = pending_payment
task_statuses['payment_sent'] = payment_sent
task_statuses['declined_delivery'] = declined_delivery
return task_statuses
return task_status_counter(project, None)
@register.filter
def all_tasks(project):
......@@ -102,6 +73,9 @@ def offers(task):
@register.filter
def get_user_task_statuses(project, user):
return task_status_counter(project, user)
def task_status_counter(project, user):
task_statuses = {}
awaiting_delivery = 0
......@@ -113,23 +87,24 @@ def get_user_task_statuses(project, user):
tasks = project.tasks.all()
for task in tasks:
try:
task_offer = task.taskoffer_set.get(status='a')
if task_offer.offerer == user.profile:
if task.status == Task.AWAITING_DELIVERY:
awaiting_delivery+=1
elif task.status == Task.PENDING_ACCEPTANCE:
pending_acceptance+=1
elif task.status == Task.PENDING_PAYMENT:
pending_payment+=1
elif task.status == Task.PAYMENT_SENT:
payment_sent+=1
elif task.status == Task.DECLINED_DELIVERY:
declined_delivery+=1
except TaskOffer.DoesNotExist:
pass
if user:
task_offer = None
try:
task_offer = task.taskoffer_set.get(status='a')
except TaskOffer.DoesNotExist:
continue
if task_offer and task_offer.offerer != user.profile:
continue
if task.status == Task.AWAITING_DELIVERY:
awaiting_delivery+=1
elif task.status == Task.PENDING_ACCEPTANCE:
pending_acceptance+=1
elif task.status == Task.PENDING_PAYMENT:
pending_payment+=1
elif task.status == Task.PAYMENT_SENT:
payment_sent+=1
elif task.status == Task.DECLINED_DELIVERY:
declined_delivery+=1
task_statuses['awaiting_delivery'] = awaiting_delivery
task_statuses['pending_acceptance'] = pending_acceptance
......
import unittest
from django.test import Client
from django.contrib.auth.models import User
from projects.models import Project
from home.templatetags.home_extras import check_nr_pending_offers, check_nr_user_offers, get_task_statuses, get_user_task_statuses
from test_helpers import create_test_user, delete_test_data, create_multiple_test_data
class OfferCountingRegressionTest(unittest.TestCase):
def setUp(self):
create_multiple_test_data()
def tearDown(self):
delete_test_data()
def test_check_nr_pending_offers(self):
test_project = Project.objects.get(title="Project1")
self.assertEquals(check_nr_pending_offers(test_project), 1)
def test_check_nr_user_offers(self):
test_project = Project.objects.get(title="Project1")
test_user = User.objects.get(username="User2")
result = check_nr_user_offers(test_project, test_user)
self.assertEquals(result['declined'], 0)
self.assertEquals(result['pending'], 1)
self.assertEquals(result['accepted'], 1)
class TaskStatusesRegressionTest(unittest.TestCase):
def setUp(self):
create_multiple_test_data()
def tearDown(self):
delete_test_data()
def test_get_task_statuses(self):
test_project = Project.objects.get(title="Project2")
result = get_task_statuses(test_project)
self.assertEquals(result['awaiting_delivery'], 2)
self.assertEquals(result['pending_acceptance'], 0)
self.assertEquals(result['pending_payment'], 0)
self.assertEquals(result['payment_sent'], 0)
self.assertEquals(result['declined_delivery'], 0)
def test_get_user_task_statuses(self):
test_project = Project.objects.get(title="Project1")
test_user = User.objects.get(username="User2")
result = get_user_task_statuses(test_project, test_user)
self.assertEquals(result['awaiting_delivery'], 0)
self.assertEquals(result['pending_acceptance'], 0)
self.assertEquals(result['pending_payment'], 0)
self.assertEquals(result['payment_sent'], 1)
self.assertEquals(result['declined_delivery'], 0)
import unittest
from django.test import Client
from test_helpers import create_test_user, delete_test_data, create_multiple_test_data
class HomeViewRegressionTest(unittest.TestCase):
def setUp(self):
self.client = Client()
create_test_user()
def tearDown(self):
delete_test_data()
def test_authenticated_user(self):
self.client.login(username= 'User', password= 'top_secret')
response = self.client.get('/', follow=True)
self.assertTrue(('/projects/', 302) not in response.redirect_chain)
def test_unauthenticated_user(self):
response = self.client.get('/', follow=True)
self.assertTrue(('/projects/', 302) in response.redirect_chain)
\ No newline at end of file
......@@ -24,8 +24,7 @@ def home(request):
'customer_projects': customer_projects,
'given_offers_projects': given_offers_projects,
})
else:
return redirect('projects')
return redirect('projects')
def get_given_offer_projects(user):
project_ids = set()
......
from django.contrib.auth.models import User
from projects.models import Project, ProjectCategory, Task, TaskOffer
def create_test_user():
User.objects.create_user(
username='User',
email='test@test.com',
password='top_secret'
)
def create_multiple_test_data():
test_user_1 = User.objects.create_user(
username='User1',
email='test@test.com',
password='top_secret'
)
test_user_2 = User.objects.create_user(
username='User2',
email='test@test.com',
password='top_secret'
)
test_category = ProjectCategory.objects.create(name='Category')
test_project_1 = Project.objects.create(
user=test_user_1.profile,
title="Project1",
description='',
category=test_category,
status='o'
)
test_project_2 = Project.objects.create(
user=test_user_2.profile,
title="Project2",
description='',
category=test_category,
status='o'
)
test_task_1 = Task.objects.create(
project=test_project_1,
title='Task1',
description='',
budget=20,
status='ad',
feedback=''
)
test_task_2 = Task.objects.create(
project=test_project_1,
title='Task2',
description='',
budget=20,
status='ps',
feedback=''
)
test_task_3 = Task.objects.create(
project=test_project_2,
title='Task3',
description='',
budget=20,
status='ad',
feedback=''
)
test_task_4 = Task.objects.create(
project=test_project_2,
title='Task4',
description='',
budget=20,
status='ad',
feedback=''
)
test_taskoffer_1 = TaskOffer.objects.create(
task=test_task_1,
title='TaskOffer1',
description='',
price=20,
offerer=test_user_2.profile,
)
test_taskoffer_2 = TaskOffer.objects.create(
task=test_task_2,
title='TaskOffer2',
description='',
price=20,
offerer=test_user_2.profile,
status='a'
)
test_taskoffer_3 = TaskOffer.objects.create(
task=test_task_3,
title='TaskOffer3',
description='',
price=20,
offerer=test_user_1.profile,
)
test_taskoffer_4 = TaskOffer.objects.create(
task=test_task_4,
title='TaskOffer4',
description='',
price=20,
offerer=test_user_1.profile,
status='a'
)
def delete_test_data():
User.objects.all().delete()
ProjectCategory.objects.all().delete()
Project.objects.all().delete()
Task.objects.all().delete()
TaskOffer.objects.all().delete()
Supports Markdown
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