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

Merge branch 'exercise_3' into 'master'

Exercise 3

See merge request olahop/agreelance123!11
parents 2bab2467 b5f87cae
[
{
"model": "myapp.person",
"pk": 1,
"fields": {
"first_name": "John",
"last_name": "Lennon"
}
},
{
"model": "myapp.person",
"pk": 2,
"fields": {
"first_name": "Paul",
"last_name": "McCartney"
}
}
]
......@@ -26,7 +26,7 @@ SECRET_KEY = '$n%^#g%qx#82w6t^dvjqwv)q*1cy+fwh1ohku7-rbjqcei2^jr'
ADMINS = [('APU', 'tdt4242apu@gmail.com')]
ALLOWED_HOSTS = ['127.0.0.1','localhost','0.0.0.0']
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '0.0.0.0']
# Application definition
......@@ -61,7 +61,14 @@ ROOT_URLCONF = 'agreelance.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'core'), os.path.join(BASE_DIR, 'templates'), ],
'DIRS': [
os.path.join(
BASE_DIR,
'core'),
os.path.join(
BASE_DIR,
'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
......@@ -146,7 +153,7 @@ if not TEST and not is_prod:
DEBUG = True
try:
from agreelance.local_settings import *
except:
except BaseException:
print("#########")
print("")
print("")
......
empty
\ No newline at end of file
......@@ -3,88 +3,67 @@ from projects.models import Project, Task, TaskOffer
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:
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
pending_offers += 1
elif taskoffer.status == TaskOffer.ACCEPTED:
accepted_offers+=1
accepted_offers += 1
elif taskoffer.status == TaskOffer.DECLINED:
declined_offers+=1
declined_offers += 1
return declined_offers, pending_offers, accepted_offers
offers['declined'] = declined_offers
offers['pending'] = pending_offers
offers['accepted'] = accepted_offers
#print(offers)
return offers
@register.filter
def task_status(task):
status = "You are awaiting delivery"
if task.status == Task.PENDING_ACCEPTANCE:
return "You have deliveries waiting for acceptance"
status = "You have deliveries waiting for acceptance"
elif task.status == Task.PENDING_PAYMENT:
return "You have deliveries waiting for payment"
status = "You have deliveries waiting for payment"
elif task.status == Task.PAYMENT_SENT:
return "You have sent payment"
return "You are awaiting delivery"
status = "You have sent payment"
return status
@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_status_counter(project, None)
return task_statuses
@register.filter
def all_tasks(project):
return project.tasks.all()
@register.filter
def offers(task):
task_offers = task.taskoffer_set.all()
......@@ -94,14 +73,19 @@ def offers(task):
x = 0
msg = "You have "
for t in task_offers:
x+=1
x += 1
if t.status == 'a':
return "You have accepted an offer for this task"
msg += str(x) + " pending offers"
return msg
@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 +97,24 @@ def get_user_task_statuses(project, user):
tasks = project.tasks.all()
for task in tasks:
if user:
task_offer = None
try:
task_offer = task.taskoffer_set.get(status='a')
if task_offer.offerer == user.profile:
except TaskOffer.DoesNotExist:
continue
if task_offer and task_offer.offerer != user.profile:
continue
if task.status == Task.AWAITING_DELIVERY:
awaiting_delivery+=1
awaiting_delivery += 1
elif task.status == Task.PENDING_ACCEPTANCE:
pending_acceptance+=1
pending_acceptance += 1
elif task.status == Task.PENDING_PAYMENT:
pending_payment+=1
pending_payment += 1
elif task.status == Task.PAYMENT_SENT:
payment_sent+=1
payment_sent += 1
elif task.status == Task.DECLINED_DELIVERY:
declined_delivery+=1
except TaskOffer.DoesNotExist:
pass
declined_delivery += 1
task_statuses['awaiting_delivery'] = awaiting_delivery
task_statuses['pending_acceptance'] = pending_acceptance
......
import unittest
from django.contrib.auth.models import User
from projects.models import Project, Task
from home.templatetags.home_extras import (
check_nr_pending_offers,
check_nr_user_offers,
task_status,
get_task_statuses,
get_user_task_statuses)
from test_helpers import (
delete_test_data,
create_multiple_test_data,
create_task_status_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 TaskStatusRegressionTest(unittest.TestCase):
def setUp(self):
create_task_status_data()
def tearDown(self):
delete_test_data()
def test_task_pending_acc(self):
result = task_status(Task.objects.get(title="Task5"))
self.assertEquals(result, "You have deliveries waiting for acceptance")
def test_task_pending_pay(self):
result = task_status(Task.objects.get(title="Task6"))
self.assertEquals(result, "You have deliveries waiting for payment")
def test_task_payment_sent(self):
result = task_status(Task.objects.get(title="Task2"))
self.assertEquals(result, "You have sent payment")
def test_task_awaiting_del(self):
result = task_status(Task.objects.get(title="Task1"))
self.assertEquals(result, "You are awaiting delivery")
class GetTaskStatusesRegressionTest(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
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)
from django.shortcuts import render, redirect
from pprint import pprint
from projects.models import Project
def home(request):
if (request.user.is_authenticated):
if request.user.is_authenticated:
user = request.user
user_projects = Project.objects.filter(user = user.profile)
customer_projects = list(Project.objects.filter(participants__id=user.id).order_by().distinct())
user_projects = Project.objects.filter(user=user.profile)
customer_projects = list(Project.objects.filter(
participants__id=user.id).order_by().distinct())
for team in user.profile.teams.all():
customer_projects.append(team.task.project)
cd = {}
......@@ -15,7 +16,8 @@ def home(request):
cd[customer_project.id] = customer_project
customer_projects = cd.values()
given_offers_projects = Project.objects.filter(pk__in=get_given_offer_projects(user)).distinct()
given_offers_projects = Project.objects.filter(
pk__in=get_given_offer_projects(user)).distinct()
return render(
request,
'index.html',
......@@ -24,9 +26,9 @@ def home(request):
'customer_projects': customer_projects,
'given_offers_projects': given_offers_projects,
})
else:
return redirect('projects')
def get_given_offer_projects(user):
project_ids = set()
......
from django.apps import AppConfig
class PaymentConfig(AppConfig):
name = 'payment'
from django import forms
from .models import Payment
class PaymentForm(forms.Form):
cardnumber = forms.CharField(max_length=20, label="Card Number")
......
......@@ -14,7 +14,12 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='Payment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('id',
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name='ID')),
],
),
]
......@@ -18,16 +18,24 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='payment',
name='payer',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='payer', to='user.Profile'),
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name='payer',
to='user.Profile'),
),
migrations.AddField(
model_name='payment',
name='receiver',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='receiver', to='user.Profile'),
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name='receiver',
to='user.Profile'),
),
migrations.AddField(
model_name='payment',
name='task',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='projects.Task'),
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to='projects.Task'),
),
]
......@@ -3,10 +3,18 @@ from django.db import models
from projects.models import Task
from user.models import Profile
class Payment(models.Model):
payer = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name="payer")
receiver = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name="receiver")
payer = models.ForeignKey(
Profile,
on_delete=models.CASCADE,
related_name="payer")
receiver = models.ForeignKey(
Profile,
on_delete=models.CASCADE,
related_name="receiver")
task = models.ForeignKey(Task, on_delete=models.CASCADE)
def __str__(self):
return self.payer.user.username + " " + self.receiver.user.username + " " + str(self.task.budget)
return self.payer.user.username + " " + \
self.receiver.user.username + " " + str(self.task.budget)
from django.test import TestCase
import unittest
from django.test import Client
from django.contrib.auth.models import User
# Create your tests here.
from projects.models import Project, Task
from payment.models import Payment
from test_helpers import delete_test_data, create_multiple_test_data
class PaymentRegressionTest(unittest.TestCase):
def setUp(self):
self.client = Client()
create_multiple_test_data()
def tearDown(self):
delete_test_data()
def test_payment_output(self):
project = Project.objects.get(title="Project1")
task = Task.objects.get(title="Task2")
self.client.login(username='User1', password='top_secret')
response = self.client.post(
f'/payment/{project.pk}/{task.id}', follow=True)
task_payment = Payment.objects.get(task=task)
self.assertEquals(
task_payment.payer,
User.objects.get(
username="User1").profile)
self.assertEquals(
task_payment.receiver,
User.objects.get(
username="User2").profile)
self.assertEquals(Task.objects.get(title="Task2").status, 'ps')
self.assertTrue(
(f'/payment/{project.pk}/{task.id}/receipt/',
302) in response.redirect_chain)
......@@ -2,6 +2,6 @@ from django.urls import path
from . import views
urlpatterns = [
path('<project_id>/<task_id>', views.payment, name='payment'),
path('<project_id>/<task_id>', views.handle_payment, name='payment'),
path('<project_id>/<task_id>/receipt/', views.receipt, name='receipt'),
]
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .models import Payment
from projects.models import Project, Task, TaskOffer
from projects.models import Project, Task
from projects.templatetags.project_extras import get_accepted_task_offer
from .forms import PaymentForm
from django.contrib.auth.decorators import login_required
from .models import Payment
@login_required
def payment(request, project_id, task_id):
def handle_payment(request, project_id, task_id):
if request.method == 'POST':
task = Task.objects.get(pk=task_id)
sender = Project.objects.get(pk=project_id).user
receiver = get_accepted_task_offer(task).offerer
if request.method == 'POST':
payment = Payment(payer=sender, receiver=receiver, task=task)
payment.save()
task.status = Task.PAYMENT_SENT # Set task status to payment sent
......@@ -23,12 +22,12 @@ def payment(request, project_id, task_id):
return redirect('receipt', project_id=project_id, task_id=task_id)
form = PaymentForm()
return render(request,
'payment/payment.html', {
'form': form,
})
@login_required
def receipt(request, project_id, task_id):
project = Project.objects.get(pk=project_id)
......
from django.contrib import admin
from .models import Project, Task, TaskFile, Delivery, ProjectCategory, Team, TaskFileTeam
from .models import (
Project,
Task,
TaskFile,
Delivery,
ProjectCategory,
Team,
TaskFileTeam)
class TaskInline(admin.TabularInline):
model = Task
verbose_name_plural = 'Tasks'
class ProjectAdmin(admin.ModelAdmin):
inlines = (TaskInline, )
......@@ -14,6 +23,7 @@ class ProjectAdmin(admin.ModelAdmin):
return super(ProjectAdmin, self).get_inline_instances(request, obj)
return list()
admin.site.register(Project, ProjectAdmin)
admin.site.register(Task)
admin.site.register(TaskFile)
......
from django.apps import AppConfig
class ProjectConfig(AppConfig):
name = 'projects'
from django import forms
from user.models import Profile
from .models import Project, TaskFile, TaskOffer, Delivery, ProjectCategory, Team, Projects
from django.contrib.auth.models import User
from user.models import Profile
from .models import (
Project,
TaskFile,
TaskOffer,
Delivery,
ProjectCategory,
Team,
Projects)
class SortForm(forms.ModelForm):
......@@ -9,21 +17,25 @@ class SortForm(forms.ModelForm):
model = Projects
fields = ('sort_by',)