Unverified Commit e18f0e51 authored by Johannes Tomren Røsvik's avatar Johannes Tomren Røsvik
Browse files

Merge branch 'refactoring'

parents 6fb4aae9 c3c352f7
Pipeline #81142 failed with stages
in 1 minute and 55 seconds
...@@ -120,3 +120,5 @@ venv.bak/ ...@@ -120,3 +120,5 @@ venv.bak/
# Visual Studio Code # Visual Studio Code
.vscode/ .vscode/
.scannerwork/
#!/usr/bin/env bash
sonar-scanner \
-Dsonar.projectKey=agreelance \
-Dsonar.sources=. \
-Dsonar.host.url=http://localhost:9000 \
-Dsonar.login=767973fda0b0e64e4ce76a2200e982b4aadee2fb
...@@ -40,21 +40,6 @@ ...@@ -40,21 +40,6 @@
<li class="nav-item"> <li class="nav-item">
<a class="nav-link font-weight-light" href="{% url 'projects' %}">Projects</a> <a class="nav-link font-weight-light" href="{% url 'projects' %}">Projects</a>
</li> </li>
<!--
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
-->
</ul> </ul>
<div class="my-2 my-lg-0 mr-5"> <div class="my-2 my-lg-0 mr-5">
......
...@@ -167,5 +167,3 @@ class TaskOffer(models.Model): ...@@ -167,5 +167,3 @@ class TaskOffer(models.Model):
status = models.CharField(max_length=8, choices=STATUS_CHOICES, default=PENDING) status = models.CharField(max_length=8, choices=STATUS_CHOICES, default=PENDING)
feedback = models.TextField(max_length=500) feedback = models.TextField(max_length=500)
def calculate_average_task_offer(self):
pass
...@@ -6,6 +6,8 @@ from django.shortcuts import render, redirect, get_object_or_404 ...@@ -6,6 +6,8 @@ from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User from django.contrib.auth.models import User
LOGIN_PAGE = '/user/login'
def projects(request): def projects(request):
projects = Project.objects.all() projects = Project.objects.all()
project_categories = ProjectCategory.objects.all() project_categories = ProjectCategory.objects.all()
...@@ -30,18 +32,7 @@ def new_project(request): ...@@ -30,18 +32,7 @@ def new_project(request):
project.save() project.save()
people = Profile.objects.filter(categories__id=project.category.id) people = Profile.objects.filter(categories__id=project.category.id)
from django.core import mail send_project_email(people, project, request, current_site)
for person in people:
if person.user.email:
try:
with mail.get_connection() as connection:
mail.EmailMessage(
"New Project: " + project.title , "A new project you might be interested in was created and can be viwed at " + current_site.domain + '/projects/' + str(project.id), "Agreelancer", [person.user.email],
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))
task_title = request.POST.getlist('task_title') task_title = request.POST.getlist('task_title')
task_description = request.POST.getlist('task_description') task_description = request.POST.getlist('task_description')
...@@ -58,11 +49,31 @@ def new_project(request): ...@@ -58,11 +49,31 @@ def new_project(request):
form = ProjectForm() form = ProjectForm()
return render(request, 'projects/new_project.html', {'form': form}) return render(request, 'projects/new_project.html', {'form': form})
def send_project_email(people, project, request, current_site):
from django.core import mail
for person in people:
if person.user.email:
try:
with mail.get_connection() as connection:
mail.EmailMessage(
"New Project: " + project.title ,
"A new project you might be interested in was created and can be viwed at "
+ current_site.domain
+ '/projects/'
+ str(project.id),
"Agreelancer",
[person.user.email],
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))
def project_view(request, project_id): def project_view(request, project_id):
project = Project.objects.get(pk=project_id) project = Project.objects.get(pk=project_id)
tasks = project.tasks.all() tasks = project.tasks.all()
total_budget = 0 # Initializes the total budget to 0 total_budget = 0
for item in tasks: for item in tasks:
total_budget += item.budget total_budget += item.budget
...@@ -120,7 +131,7 @@ def project_view(request, project_id): ...@@ -120,7 +131,7 @@ def project_view(request, project_id):
}) })
def isProjectOwner(user, project): def is_project_owner(user, project):
return user == project.user.user return user == project.user.user
...@@ -131,7 +142,7 @@ def upload_file_to_task(request, project_id, task_id): ...@@ -131,7 +142,7 @@ def upload_file_to_task(request, project_id, task_id):
user_permissions = get_user_task_permissions(request.user, task) user_permissions = get_user_task_permissions(request.user, task)
accepted_task_offer = task.accepted_task_offer() 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['modify'] or user_permissions['write'] or user_permissions['upload'] or is_project_owner(request.user, project):
if request.method == 'POST': if request.method == 'POST':
task_file_form = TaskFileForm(request.POST, request.FILES) task_file_form = TaskFileForm(request.POST, request.FILES)
if task_file_form.is_valid(): if task_file_form.is_valid():
...@@ -139,7 +150,6 @@ def upload_file_to_task(request, project_id, task_id): ...@@ -139,7 +150,6 @@ def upload_file_to_task(request, project_id, task_id):
task_file.task = task task_file.task = task
existing_file = task.files.filter(file=directory_path(task_file, task_file.file.file)).first() existing_file = task.files.filter(file=directory_path(task_file, task_file.file.file)).first()
access = user_permissions['modify'] or user_permissions['owner'] 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(): for team in request.user.profile.teams.all():
file_modify_access = TaskFileTeam.objects.filter(team=team, file=existing_file, modify=True).exists() file_modify_access = TaskFileTeam.objects.filter(team=team, file=existing_file, modify=True).exists()
print(file_modify_access) print(file_modify_access)
...@@ -174,8 +184,10 @@ def upload_file_to_task(request, project_id, task_id): ...@@ -174,8 +184,10 @@ def upload_file_to_task(request, project_id, task_id):
'task_file_form': task_file_form, 'task_file_form': task_file_form,
} }
) )
return redirect('/user/login') # Redirects to /user/login return redirect(LOGIN_PAGE) # Redirects to /user/login
# Method shall get different permission based on user, and decide what permission a user has
def get_user_task_permissions(user, task): def get_user_task_permissions(user, task):
if user == task.project.user.user: if user == task.project.user.user:
...@@ -202,14 +214,28 @@ def get_user_task_permissions(user, task): ...@@ -202,14 +214,28 @@ def get_user_task_permissions(user, task):
'view_task': False, 'view_task': False,
'upload': False, 'upload': False,
} }
user_permissions['read'] = user_permissions['read'] or user.profile.task_participants_read.filter(id=task.id).exists() user_permissions['read'] = (
user_permissions['read'] or
user.profile.task_participants_read.filter(id=task.id).exists()
)
# Team members can view its teams tasks # Team members can view its teams tasks
user_permissions['upload'] = user_permissions['upload'] or user.profile.teams.filter(task__id=task.id, write=True).exists() user_permissions['upload'] = (
user_permissions['view_task'] = user_permissions['view_task'] or user.profile.teams.filter(task__id=task.id).exists() user_permissions['upload'] or
user.profile.teams.filter(task__id=task.id, write=True).exists()
)
user_permissions['view_task'] = (
user_permissions['view_task'] or
user.profile.teams.filter(task__id=task.id).exists()
)
user_permissions['write'] = user_permissions['write'] or user.profile.task_participants_write.filter(id=task.id).exists() user_permissions['write'] = (
user_permissions['modify'] = user_permissions['modify'] or user.profile.task_participants_modify.filter(id=task.id).exists() user_permissions['write'] or
user.profile.task_participants_write.filter(id=task.id).exists())
user_permissions['modify'] = (
user_permissions['modify'] or
user.profile.task_participants_modify.filter(id=task.id).exists()
)
return user_permissions return user_permissions
...@@ -222,11 +248,11 @@ def task_view(request, project_id, task_id): ...@@ -222,11 +248,11 @@ def task_view(request, project_id, task_id):
user_permissions = get_user_task_permissions(request.user, task) 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']: 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') return redirect(LOGIN_PAGE)
if request.method == 'POST' and 'delivery' in request.POST: if (request.method == 'POST' and 'delivery' in request.POST and
if accepted_task_offer and accepted_task_offer.offerer == user.profile: accepted_task_offer and accepted_task_offer.offerer == user.profile):
deliver_form = DeliveryForm(request.POST, request.FILES) deliver_form = DeliveryForm(request.POST, request.FILES)
if deliver_form.is_valid(): if deliver_form.is_valid():
delivery = deliver_form.save(commit=False) delivery = deliver_form.save(commit=False)
...@@ -323,7 +349,7 @@ def task_view(request, project_id, task_id): ...@@ -323,7 +349,7 @@ def task_view(request, project_id, task_id):
'per': per 'per': per
}) })
return redirect('/user/login') return redirect(LOGIN_PAGE)
@login_required @login_required
def task_permissions(request, project_id, task_id): def task_permissions(request, project_id, task_id):
......
...@@ -74,7 +74,7 @@ class AgreelanceSignUp(unittest.TestCase): ...@@ -74,7 +74,7 @@ class AgreelanceSignUp(unittest.TestCase):
Select(driver.find_element_by_id("id_status")).select_by_index(status_index) Select(driver.find_element_by_id("id_status")).select_by_index(status_index)
driver.find_element_by_id("id_feedback").send_keys("FEEDBACK_TEXT") driver.find_element_by_id("id_feedback").send_keys("FEEDBACK_TEXT")
project = driver.find_element_by_xpath("//*[contains(text(), \"Send Response\")]") driver.find_element_by_xpath("//*[contains(text(), \"Send Response\")]")
sleep(1) sleep(1)
......
...@@ -6,6 +6,8 @@ import os ...@@ -6,6 +6,8 @@ import os
url = "http://127.0.0.1:8000/" url = "http://127.0.0.1:8000/"
# Testing of DarkMode functionality with Unittest: https://docs.python.org/3/library/unittest.html
# and using end-to-end testing framework Selenium: https://selenium-python.readthedocs.io/
class AgreelanceDarkMode(unittest.TestCase): class AgreelanceDarkMode(unittest.TestCase):
def setUp(self): def setUp(self):
......
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