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

upload task files refac

parent 6eaf5444
......@@ -3,6 +3,7 @@ from projects.models import Task,ProjectCategory
from django.core import mail
from django.shortcuts import get_object_or_404
from django.contrib.sites.shortcuts import get_current_site
from django.contrib import messages
def create_new_project(request, project):
project.user = request.user.profile
......@@ -36,5 +37,4 @@ def mail_person_in_people(request, project, person):
connection=connection,
).send()
except Exception as e:
from django.contrib import messages
messages.success(request, 'Sending of email to ' + person.user.email + " failed: " + str(e))
from django.contrib import messages
from projects.models import Task, TaskOffer, TaskFileTeam, directory_path
from projects.forms import EditTaskOfferForm, EditProjectForm, TaskOfferForm, TaskOfferResponseForm, TaskFileForm
def file_upload_and_access_handler(request, project, task, user_permissions):
accepted_task_offer = task.accepted_task_offer()
task_file_form = TaskFileForm(request.POST, request.FILES)
task_file = task_file_form.save(commit=False)
task_file.task = task
existing_file = task.files.filter(file=directory_path(task_file, task_file.file.file)).first()
access = user_permissions['modify']
for team in request.user.profile.teams.all():
file_modify_access = TaskFileTeam.objects.filter(team=team, file=existing_file, modify=True).exists()
access = access or file_modify_access
if (access):
if existing_file:
existing_file.delete()
task_file.save()
if request.user.profile != project.user and request.user.profile != accepted_task_offer.offerer:
teams = request.user.profile.teams.filter(task__id=task.id)
for team in teams:
tft = TaskFileTeam()
tft.team = team
tft.file = task_file
tft.read = True
tft.save()
else:
messages.warning(request, "You do not have access to modify this file")
test
\ No newline at end of file
import unittest
from django.test import Client
from projects.models import Project, Task, TaskFile
from test_helpers import create_multiple_test_data, delete_test_data
class FileUploadRegressionTest(unittest.TestCase):
def setUp(self):
self.client = Client()
create_multiple_test_data()
def tearDown(self):
delete_test_data()
def test_task_file_upload(self):
project_id = Project.objects.get(title="Project1").pk
task_id = Task.objects.get(title="Task1").id
self.client.login(username= 'User1', password= 'top_secret')
with open('projects/tests/test_file.txt') as test_file:
self.client.post(f'/projects/{project_id}/tasks/{task_id}/upload/', {
'task': task_id,
'file': test_file
}, follow=True)
task_file = TaskFile.objects.get(task=task_id)
self.assertEquals(task_file.name(), "test_file.txt")
from django.http import HttpResponse, HttpResponseRedirect
from user.models import Profile
from .models import Project, Task, TaskFile, TaskOffer, Delivery, ProjectCategory, Team, TaskFileTeam, directory_path
from .models import Project, Task, TaskFile, TaskOffer, Delivery, ProjectCategory, Team, TaskFileTeam
from .forms import EditTaskOfferForm, EditProjectForm, ProjectForm, TaskFileForm, ProjectStatusForm, TaskOfferForm, TaskOfferResponseForm, TaskPermissionForm, DeliveryForm, TaskDeliveryResponseForm, TeamForm, TeamAddForm, SortForm
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
......@@ -9,6 +9,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
from .templatetags.upload_extras import file_upload_and_access_handler
def projects(request):
......@@ -83,57 +84,26 @@ def project_view(request, project_id):
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)
task = Task.objects.get(pk=task_id)
user_permissions = get_user_task_permissions(request.user, task)
accepted_task_offer = task.accepted_task_offer()
if user_permissions['modify'] or user_permissions['write'] or user_permissions['upload'] or isProjectOwner(request.user, project):
if user_permissions['upload']:
project = Project.objects.get(pk=project_id)
if request.method == 'POST':
task_file_form = TaskFileForm(request.POST, request.FILES)
if task_file_form.is_valid():
task_file = task_file_form.save(commit=False)
task_file.task = task
existing_file = task.files.filter(file=directory_path(task_file, task_file.file.file)).first()
access = user_permissions['modify'] or user_permissions['owner']
access_to_file = False # Initialize access_to_file to false
for team in request.user.profile.teams.all():
file_modify_access = TaskFileTeam.objects.filter(team=team, file=existing_file, modify=True).exists()
print(file_modify_access)
access = access or file_modify_access
access = access or user_permissions['modify']
if (access):
if existing_file:
existing_file.delete()
task_file.save()
if request.user.profile != project.user and request.user.profile != accepted_task_offer.offerer:
teams = request.user.profile.teams.filter(task__id=task.id)
for team in teams:
tft = TaskFileTeam()
tft.team = team
tft.file = task_file
tft.read = True
tft.save()
else:
from django.contrib import messages
messages.warning(request, "You do not have access to modify this file")
file_upload_and_access_handler(request, project, task, user_permissions)
return redirect('task_view', project_id=project_id, task_id=task_id)
task_file_form = TaskFileForm()
return render(
request,
'projects/upload_file_to_task.html',
{
'project': project,
'task': task,
'task_file_form': task_file_form,
'task_file_form': TaskFileForm(),
}
)
return redirect('/user/login') # Redirects to /user/login
......
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