Commit 0aeae5b1 authored by Ola Hermann Opheim's avatar Ola Hermann Opheim
Browse files

task_view refac

parent 366ec242
from django.shortcuts import get_object_or_404
from django.utils import timezone
from projects.models import Task, TaskOffer, Delivery, Team, TaskFileTeam
from projects.forms import EditTaskOfferForm, EditProjectForm, TaskOfferForm, TaskOfferResponseForm, TaskDeliveryResponseForm, DeliveryForm, TeamForm, TeamAddForm
def delivery_handler(request, task):
deliver_form = DeliveryForm(request.POST, request.FILES)
if deliver_form.is_valid():
delivery = deliver_form.save(commit=False)
delivery.task = task
delivery.delivery_user = request.user.profile
delivery.save()
task.status = "pa"
task.save()
def delivery_response_handler(request, task):
instance = get_object_or_404(Delivery, id=request.POST.get('delivery-id'))
deliver_response_form = TaskDeliveryResponseForm(request.POST, instance=instance)
if deliver_response_form.is_valid():
delivery = deliver_response_form.save()
delivery.responding_time = timezone.now()
delivery.responding_user = request.user.profile
delivery.save()
if delivery.status == 'a':
task.status = "pp"
task.save()
elif delivery.status == 'd':
task.status = "dd"
task.save()
def team_handler(request, task):
team_form = TeamForm(request.POST)
if (team_form.is_valid()):
team = team_form.save(False)
team.task = task
team.save()
def team_add_handler(request):
instance = get_object_or_404(Team, id=request.POST.get('team-id'))
team_add_form = TeamAddForm(request.POST, instance=instance)
if team_add_form.is_valid():
team = team_add_form.save(False)
team.members.add(*team_add_form.cleaned_data['members'])
team.save()
def permissions_handler(request, task):
for team in task.teams.all():
for task_file in task.files.all():
try:
tft_string = 'permission-perobj-' + str(task_file.id) + '-' + str(team.id)
tft_id=request.POST.get(tft_string)
instance = TaskFileTeam.objects.get(id=tft_id)
except Exception as e:
instance = TaskFileTeam(
file = task_file,
team = team,
)
instance.read = request.POST.get('permission-read-' + str(task_file.id) + '-' + str(team.id)) or False
instance.write = request.POST.get('permission-write-' + str(task_file.id) + '-' + str(team.id)) or False
instance.modify = request.POST.get('permission-modify-' + str(task_file.id) + '-' + str(team.id)) or False
instance.save()
team.write = request.POST.get('permission-upload-' + str(team.id)) or False
team.save()
import unittest
from django.test import Client
from user.models import User
from projects.models import Project, Task, TaskOffer, Delivery, Team
from test_helpers import create_test_user, delete_test_data, create_multiple_test_data, create_delivery
class TaskViewRegressionTest(unittest.TestCase):
def setUp(self):
self.client = Client()
create_multiple_test_data()
self.project_id = Project.objects.get(title="Project1").pk
self.task = Task.objects.get(title="Task2")
def tearDown(self):
delete_test_data()
def test_post_delivery(self):
self.client.login(username= 'User2', password= 'top_secret')
with open('projects/tests/test_file.txt') as test_file:
response = self.client.post(f'/projects/{self.project_id}/tasks/{self.task.id}/', {
'delivery': True,
'comment': 'Comment',
'file': test_file
}, follow=True)
delivery = response.context['deliveries'][0]
self.assertEquals(delivery.comment, "Comment")
self.assertTrue("test_file" in delivery.file.path)
self.assertEquals(delivery.delivery_user.user.username, "User2")
def test_post_delivery_response(self):
create_delivery()
delivery = Delivery.objects.get(task=self.task)
self.client.login(username= 'User1', password= 'top_secret')
response = self.client.post(f'/projects/{self.project_id}/tasks/{self.task.id}/', {
'delivery-response': True,
'delivery-id': delivery.id,
'status': 'a',
'feedback': 'Feedback',
})
delivery = response.context['deliveries'][0]
self.assertEquals(delivery.responding_user.user.username, "User1")
self.assertEquals(response.context['task'].status, "pp")
def test_post_team(self):
self.client.login(username= 'User2', password= 'top_secret')
self.client.post(f'/projects/{self.project_id}/tasks/{self.task.id}/', {
'team': True,
'name': 'Name'
})
team = Team.objects.get(name="Name")
self.assertEquals(team.task, self.task)
def test_post_permissions(self):
# TODO
pass
def test_post_team_add(self):
# TODO: maybe
pass
......@@ -11,6 +11,7 @@ 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
from .templatetags.upload_extras import file_upload_and_access_handler
from .templatetags.task_permissions_extras import can_modify_permissions, change_permissions
from .templatetags.task_view_extras import delivery_handler, delivery_response_handler, team_handler, team_add_handler, permissions_handler
def projects(request):
......@@ -148,110 +149,45 @@ def get_user_task_permissions(user, task):
@login_required
def task_view(request, project_id, task_id):
user = request.user
project = Project.objects.get(pk=project_id)
task = Task.objects.get(pk=task_id)
accepted_task_offer = task.accepted_task_offer()
user_permissions = get_user_task_permissions(request.user, task)
if not user_permissions['read'] and not user_permissions['write'] and not user_permissions['modify'] and not user_permissions['owner'] and not user_permissions['view_task']:
return redirect('/user/login')
if request.method == 'POST' and 'delivery' in request.POST:
if accepted_task_offer and accepted_task_offer.offerer == user.profile:
deliver_form = DeliveryForm(request.POST, request.FILES)
if deliver_form.is_valid():
delivery = deliver_form.save(commit=False)
delivery.task = task
delivery.delivery_user = user.profile
delivery.save()
task.status = "pa"
task.save()
if request.method == 'POST' and 'delivery-response' in request.POST:
instance = get_object_or_404(Delivery, id=request.POST.get('delivery-id'))
deliver_response_form = TaskDeliveryResponseForm(request.POST, instance=instance)
if deliver_response_form.is_valid():
delivery = deliver_response_form.save()
from django.utils import timezone
delivery.responding_time = timezone.now()
delivery.responding_user = user.profile
delivery.save()
if delivery.status == 'a':
task.status = "pp"
task.save()
elif delivery.status == 'd':
task.status = "dd"
task.save()
if request.method == 'POST' and 'team' in request.POST:
if accepted_task_offer and accepted_task_offer.offerer == user.profile:
team_form = TeamForm(request.POST)
if (team_form.is_valid()):
team = team_form.save(False)
team.task = task
team.save()
if request.method == 'POST' and 'team-add' in request.POST:
if accepted_task_offer and accepted_task_offer.offerer == user.profile:
instance = get_object_or_404(Team, id=request.POST.get('team-id'))
team_add_form = TeamAddForm(request.POST, instance=instance)
if team_add_form.is_valid():
team = team_add_form.save(False)
team.members.add(*team_add_form.cleaned_data['members'])
team.save()
if request.method == 'POST' and 'permissions' in request.POST:
if accepted_task_offer and accepted_task_offer.offerer == user.profile:
for t in task.teams.all():
for f in task.files.all():
try:
tft_string = 'permission-perobj-' + str(f.id) + '-' + str(t.id)
tft_id=request.POST.get(tft_string)
instance = TaskFileTeam.objects.get(id=tft_id)
except Exception as e:
instance = TaskFileTeam(
file = f,
team = t,
)
instance.read = request.POST.get('permission-read-' + str(f.id) + '-' + str(t.id)) or False
instance.write = request.POST.get('permission-write-' + str(f.id) + '-' + str(t.id)) or False
instance.modify = request.POST.get('permission-modify-' + str(f.id) + '-' + str(t.id)) or False
instance.save()
t.write = request.POST.get('permission-upload-' + str(t.id)) or False
t.save()
deliver_form = DeliveryForm()
deliver_response_form = TaskDeliveryResponseForm()
team_form = TeamForm()
team_add_form = TeamAddForm()
if (user_permissions['read'] or
user_permissions['write'] or
user_permissions['modify'] or
user_permissions['owner'] or
user_permissions['view_task']):
if request.method == 'POST':
accepted_task_offer = task.accepted_task_offer()
if accepted_task_offer and accepted_task_offer.offerer == request.user.profile:
if 'delivery' in request.POST:
delivery_handler(request, task)
if 'team' in request.POST:
team_handler(request, task)
if 'team-add' in request.POST:
team_add_handler(request)
if 'permissions' in request.POST:
permissions_handler(request, task)
if 'delivery-response' in request.POST:
delivery_response_handler(request, task)
if user_permissions['read'] or user_permissions['write'] or user_permissions['modify'] or user_permissions['owner'] or user_permissions['view_task']:
deliveries = task.delivery.all()
team_files = []
teams = user.profile.teams.filter(task__id=task.id).all()
per = {}
for f in task.files.all():
per[f.name()] = {}
for p in f.teams.all():
per[f.name()][p.team.name] = p
if p.read:
team_files.append(p)
for task_file in task.files.all():
per[task_file.name()] = {}
for task_team in task_file.teams.all():
per[task_file.name()][task_team.team.name] = task_team
if task_team.read:
team_files.append(task_team)
return render(request, 'projects/task_view.html', {
'task': task,
'project': project,
'project': Project.objects.get(pk=project_id),
'user_permissions': user_permissions,
'deliver_form': deliver_form,
'deliveries': deliveries,
'deliver_response_form': deliver_response_form,
'team_form': team_form,
'team_add_form': team_add_form,
'deliver_form': DeliveryForm(),
'deliveries': task.delivery.all(),
'deliver_response_form': TaskDeliveryResponseForm(),
'team_form': TeamForm(),
'team_add_form': TeamAddForm(),
'team_files': team_files,
'per': per
})
......
from django.test import Client
from django.contrib.auth.models import User
......@@ -168,6 +169,18 @@ def create_multiple_test_data():
offerer=test_user_1.profile,
)
def create_delivery():
client = Client()
project_id = Project.objects.get(title="Project1").pk
task_id = Task.objects.get(title="Task2").id
client.login(username= 'User2', password= 'top_secret')
with open('projects/tests/test_file.txt') as test_file:
client.post(f'/projects/{project_id}/tasks/{task_id}/', {
'delivery': True,
'comment': 'Comment',
'file': test_file
})
def delete_test_data():
User.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