Commit 6eaf5444 authored by Ola Hermann Opheim's avatar Ola Hermann Opheim
Browse files

project_view refactor

parent ba83b1df
from django.shortcuts import get_object_or_404
from projects.models import Task, TaskOffer
from projects.forms import EditTaskOfferForm, EditProjectForm, TaskOfferForm, TaskOfferResponseForm
def offer_response_handler(request):
instance = get_object_or_404(TaskOffer, id=request.POST.get('taskofferid'))
offer_response_form = TaskOfferResponseForm(request.POST, instance=instance)
if offer_response_form.is_valid():
offer_response = offer_response_form.save(commit=False)
if offer_response.status == 'a':
offer_response.task.read.add(offer_response.offerer)
offer_response.task.write.add(offer_response.offerer)
project = offer_response.task.project
project.participants.add(offer_response.offerer)
offer_response.save()
def edit_project_handler(request, project, tasks):
edit_project_form = EditProjectForm(request.POST)
if edit_project_form.is_valid() and project.status == 'o':
if project.edit:
project.description = request.POST['project_description']
for task in tasks:
task.budget = request.POST[f'task{task.id}_budget']
task.description = request.POST[f'task{task.id}_description']
task.save()
project.edit = not project.edit
project.save()
def offer_submit_handler(request):
task_offer_form = TaskOfferForm(request.POST)
if task_offer_form.is_valid():
task_offer = task_offer_form.save(commit=False)
task_offer.task = Task.objects.get(pk=request.POST.get('taskvalue'))
task_offer.offerer = request.user.profile
task_offer.save()
def edit_offer_handler(request):
edit_task_offer_form = EditTaskOfferForm(request.POST)
if edit_task_offer_form.is_valid():
offer = TaskOffer.objects.get(id=request.POST.get('offer_id'))
if offer.status == 'p' and request.user == offer.offerer.user:
if offer.edit:
offer.description = request.POST['offer_description']
offer.price = request.POST['offer_price']
offer.edit = not offer.edit
offer.save()
"""
def status_change_handler(request, project):
status_form = ProjectStatusForm(request.POST)
if status_form.is_valid():
project_status = status_form.save(commit=False)
project.status = project_status.status
project.save()
"""
\ No newline at end of file
import unittest
from django.test import Client
from django.contrib.auth.models import User
from projects.templatetags.project_extras import get_all_taskoffers
from projects.models import Project, ProjectCategory, Task, TaskOffer
from projects.forms import ProjectStatusForm, TaskOfferResponseForm
from user.models import User
from projects.models import Project, Task, TaskOffer
from test_helpers import create_test_user, delete_test_data, create_multiple_test_data
......@@ -70,3 +68,94 @@ class ProjectViewStatementTest(unittest.TestCase):
'offer_description': 'Edited',
'offer_price': '30',
})
class ProjectViewRegressionTest(unittest.TestCase):
def setUp(self):
self.client = Client()
create_multiple_test_data()
self.project_id = Project.objects.get(title="Project2").pk
def tearDown(self):
delete_test_data()
def test_project_owner_request(self):
self.client.login(username= 'User2', password= 'top_secret')
response = self.client.post(f'/projects/{self.project_id}/')
self.assertIsNotNone(response.context['project'])
self.assertIsNotNone(response.context['tasks'])
self.assertIsNotNone(response.context['status_form'])
self.assertIsNotNone(response.context['total_budget'])
self.assertIsNotNone(response.context['offer_response_form'])
def test_non_project_owner_request(self):
response = self.client.post(f'/projects/{self.project_id}/')
self.assertIsNotNone(response.context['project'])
self.assertIsNotNone(response.context['tasks'])
self.assertIsNotNone(response.context['total_budget'])
self.assertIsNotNone(response.context['task_offer_form'])
def test_post_offer_response(self):
task_offer = TaskOffer.objects.get(title="TaskOffer4")
self.client.login(username= 'User2', password= 'top_secret')
response = self.client.post(f'/projects/{self.project_id}/', {
'offer_response': True,
'taskofferid': task_offer.id,
'status': 'a',
'feedback': 'Feedback',
})
test_offerer = User.objects.get(username="User1")
self.assertTrue(test_offerer.profile in response.context['project'].participants.all())
self.assertTrue(test_offerer.profile in task_offer.task.read.all())
self.assertTrue(test_offerer.profile in task_offer.task.write.all())
def test_post_edit_project(self):
test_task1 = Task.objects.get(title="Task3")
test_task2 = Task.objects.get(title="Task4")
self.client.login(username= 'User2', password= 'top_secret')
response = self.client.post(f'/projects/{self.project_id}/', {
'edit_project': True,
'project_description': 'Edited',
f'task{test_task1.id}_budget': '100',
f'task{test_task1.id}_description': 'Edited',
f'task{test_task2.id}_budget': '20',
f'task{test_task2.id}_description': 'Edited',
})
self.assertFalse(response.context['project'].edit)
self.assertEquals(response.context['project'].description, "Edited")
self.assertEquals(response.context['tasks'][0].budget, "100")
self.assertEquals(response.context['tasks'][0].description, "Edited")
self.assertEquals(response.context['tasks'][1].budget, "20")
self.assertEquals(response.context['tasks'][1].description, "Edited")
def test_post_offer_submit(self):
test_task = Task.objects.get(title="Task4")
self.client.login(username= 'User1', password= 'top_secret')
self.client.post(f'/projects/{self.project_id}/', {
'offer_submit': True,
'title': 'TaskOffer5',
'description': 'Offer description',
'price': 567,
'taskvalue': test_task.id
})
task_offer = TaskOffer.objects.get(title="TaskOffer5")
self.assertEquals(task_offer.task, test_task)
self.assertEquals(task_offer.description, "Offer description")
self.assertEquals(task_offer.price, 567)
self.assertEquals(task_offer.offerer.user.username, "User1")
def test_post_edit_offer(self):
task_offer = TaskOffer.objects.get(title="TaskOffer3")
self.client.login(username= 'User1', password= 'top_secret')
self.client.post(f'/projects/{self.project_id}/', {
'edit_offer': True,
'offer_id': task_offer.id,
'offer_description': 'Edited',
'offer_price': '30',
})
task_offer = TaskOffer.objects.get(title="TaskOffer3")
self.assertEquals(task_offer.description, "Edited")
self.assertEquals(task_offer.price, 30)
self.assertFalse(task_offer.edit)
......@@ -8,6 +8,7 @@ from django.contrib.auth.models import User
from .templatetags.projects_extras import sort_projects
from .templatetags.new_project_extras import create_new_project
from .templatetags.project_view_extras import offer_response_handler, edit_project_handler, offer_submit_handler, edit_offer_handler
def projects(request):
......@@ -42,7 +43,6 @@ def new_project(request):
form = ProjectForm()
return render(request, 'projects/new_project.html', {'form': form})
def project_view(request, project_id):
project = Project.objects.get(pk=project_id)
tasks = project.tasks.all()
......@@ -51,33 +51,19 @@ def project_view(request, project_id):
for item in tasks:
total_budget += item.budget
if request.user == project.user.user:
return_context = {
'project': project,
'tasks': tasks,
'total_budget': total_budget,
}
if request.user == project.user.user:
if request.method == 'POST' and 'offer_response' in request.POST:
instance = get_object_or_404(TaskOffer, id=request.POST.get('taskofferid'))
offer_response_form = TaskOfferResponseForm(request.POST, instance=instance)
if offer_response_form.is_valid():
offer_response = offer_response_form.save(commit=False)
if offer_response.status == 'a':
offer_response.task.read.add(offer_response.offerer)
offer_response.task.write.add(offer_response.offerer)
project = offer_response.task.project
project.participants.add(offer_response.offerer)
offer_response.save()
offer_response_form = TaskOfferResponseForm()
offer_response_handler(request)
return_context['offer_response_form'] = TaskOfferResponseForm()
if request.method == 'POST' and 'edit_project' in request.POST:
edit_project_form = EditProjectForm(request.POST)
if edit_project_form.is_valid() and project.status == 'o':
if project.edit:
project.description = request.POST['project_description']
for task in tasks:
task.budget = request.POST[f'task{task.id}_budget']
task.description = request.POST[f'task{task.id}_description']
task.save()
project.edit = not project.edit
project.save()
edit_project_handler(request, project, tasks)
if request.method == 'POST' and 'status_change' in request.POST:
status_form = ProjectStatusForm(request.POST)
......@@ -85,50 +71,21 @@ def project_view(request, project_id):
project_status = status_form.save(commit=False)
project.status = project_status.status
project.save()
status_form = ProjectStatusForm(initial={'status': project.status})
return render(request, 'projects/project_view.html', {
'project': project,
'tasks': tasks,
'status_form': status_form,
'total_budget': total_budget,
'offer_response_form': offer_response_form,
})
return_context['status_form'] = ProjectStatusForm(initial={'status': project.status})
else:
if request.method == 'POST' and 'offer_submit' in request.POST:
task_offer_form = TaskOfferForm(request.POST)
if task_offer_form.is_valid():
task_offer = task_offer_form.save(commit=False)
task_offer.task = Task.objects.get(pk=request.POST.get('taskvalue'))
task_offer.offerer = request.user.profile
task_offer.save()
task_offer_form = TaskOfferForm()
offer_submit_handler(request)
return_context['task_offer_form'] = TaskOfferForm()
if request.method == 'POST' and 'edit_offer' in request.POST:
edit_task_offer_form = EditTaskOfferForm(request.POST)
if edit_task_offer_form.is_valid():
offer = TaskOffer.objects.get(id=request.POST.get('offer_id'))
if offer.status == 'p' and request.user == offer.offerer.user:
if offer.edit:
offer.description = request.POST['offer_description']
offer.price = request.POST['offer_price']
offer.edit = not offer.edit
offer.save()
return render(request, 'projects/project_view.html', {
'project': project,
'tasks': tasks,
'task_offer_form': task_offer_form,
'total_budget': total_budget,
})
edit_offer_handler(request)
return render(request, 'projects/project_view.html', return_context)
def isProjectOwner(user, project):
return user == project.user.user
@login_required
def upload_file_to_task(request, project_id, task_id):
project = Project.objects.get(pk=project_id)
......
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