Skip to content

Commit

Permalink
Merge pull request #340 from ucb-rit/develop
Browse files Browse the repository at this point in the history
Add minor bug fixes and email enhancements
  • Loading branch information
matthew-li authored Jan 20, 2022
2 parents 7fcb05b + 9887160 commit 586f258
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 23 deletions.
4 changes: 1 addition & 3 deletions coldfront/core/project/templates/project/project_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,6 @@ <h2 class="d-inline">
<a class="btn btn-success" href="{% url 'project-add-users-search' project.pk %}" role="button"><i class="fas fa-user-plus" aria-hidden="true"></i> Add Users</a>
<a class="btn btn-danger" href="{% url 'project-remove-users' project.pk %}" role="button"><i class="fas fa-user-times" aria-hidden="true"></i> Remove Users</a>
{% endif %}
</div>
<div class="float-right">
{% if project.status.name != 'Archived' and can_leave_project %}
<div class="modal fade" id="denial{{ user.pk }}Modal" tabindex="-1" role="dialog" aria-labelledby="denial{{ user.pk }}ModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
Expand Down Expand Up @@ -259,7 +257,7 @@ <h2 class="d-inline">
</form>
</div>
</div>
<button class="btn btn-danger mr-1" data-toggle="modal" data-target="#denial{{ user.pk }}Modal">
<button class="btn btn-danger" data-toggle="modal" data-target="#denial{{ user.pk }}Modal">
Leave Project
</button>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ <h1>Join a Project</h1><hr>
<hr>
<p>
Below is a list of join requests that await approval by project managers.
<b>
Please contact a project manager or PI if your request has not been
completed in a reasonable time frame.
reviewed in a reasonable time frame.
</b>
</p>
<p>
Once a request is approved, a separate request for cluster access under
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
from django.contrib.messages import get_messages
from django.urls import reverse

from coldfront.core.project.forms import ProjectReviewUserJoinForm
from coldfront.core.project.models import *
from coldfront.core.user.models import UserProfile
from django.test import TestCase
from django.contrib.auth.models import User
from django.core import mail
from django.core.management import call_command
from coldfront.core.utils.common import import_from_settings
from coldfront.core.utils.common import utc_now_offset_aware
from django.conf import settings

from io import StringIO
from urllib.parse import urljoin
import os
import sys

Expand Down Expand Up @@ -155,13 +156,21 @@ def test_project_join_request_email(self):
role__name__in=['Manager', 'Principal Investigator'],
status__name='Active')]

email_body = f'User {self.user1.first_name} {self.user1.last_name} ' \
f'({self.user1.email}) has requested to join your ' \
f'project, {self.project1.name} via MyBRC user portal. ' \
f'Please approve/deny this request.'
domain = import_from_settings('CENTER_BASE_URL')
view = reverse(
'project-review-join-requests', kwargs={'pk': self.project1.pk})
review_url = urljoin(domain, view)

body_components = [
(f'User {self.user1.first_name} {self.user1.last_name} '
f'({self.user1.email}) has requested to join your project, '
f'{self.project1.name} via the MyBRC User Portal.'),
f'Please approve/deny this request here: {review_url}.',
]

for email in mail.outbox:
self.assertIn(email_body, email.body)
for component in body_components:
self.assertIn(component, email.body)
for recipient in email.to:
self.assertIn(recipient, email_to_list)
self.assertEqual(settings.EMAIL_SENDER, email.from_email)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,34 @@ def test_remove_self(self):

self.client.logout()

def test_remove_self_superuser(self):
"""Test that ProjectRemoveSelf POST performs the correct actions when
requester is a superuser."""
self.user1.is_superuser = True
self.user1.save()
self.assertTrue(self.user1.is_superuser)
self.client.login(username=self.user1.username, password=self.password)

url = reverse('project-detail', kwargs={'pk': self.project1.pk})
response = self.client.get(url)
self.assertContains(response, 'Leave Project')

url = reverse(
'project-remove-self', kwargs={'pk': self.project1.pk})
pre_time = utc_now_offset_aware()
response = self.client.post(url, {})

self.assertRedirects(response, reverse('home'))
self.assertTrue(ProjectUserRemovalRequest.objects.filter(
requester=self.user1).exists())

removal_request = \
ProjectUserRemovalRequest.objects.filter(requester=self.user1).first()
self.assertTrue(pre_time <= removal_request.request_time <=
utc_now_offset_aware())

self.client.logout()


class TestProjectRemoveUsersView(TestBase):
"""A class for testing ProjectRemoveUsersView."""
Expand Down
33 changes: 22 additions & 11 deletions coldfront/core/project/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from django.core.mail import send_mail
from django.template.loader import render_to_string

from coldfront.api.statistics.utils import set_project_user_allocation_value
from coldfront.core.allocation.models import Allocation
from coldfront.core.allocation.models import AllocationAttribute
Expand Down Expand Up @@ -140,16 +143,11 @@ def send_project_join_notification_email(project, project_user):
user = project_user.user

subject = f'New request to join Project {project.name}'
context = {
'project_name': project.name,
'user_string': f'{user.first_name} {user.last_name} ({user.email})',
'signature': import_from_settings('EMAIL_SIGNATURE', ''),
}

template_name = 'email/new_project_join_request.txt'
context['url'] = __project_detail_url(project)

sender = settings.EMAIL_SENDER
context = {'project_name': project.name,
'user_string': f'{user.first_name} {user.last_name} ({user.email})',
'signature': import_from_settings('EMAIL_SIGNATURE', ''),
'review_url': review_project_join_requests_url(project),
'url': __project_detail_url(project)}

pi_condition = Q(
role__name='Principal Investigator', status__name='Active',
Expand All @@ -162,7 +160,20 @@ def send_project_join_notification_email(project, project_user):
'user__email', flat=True
))

send_email_template(subject, template_name, context, sender, receiver_list)
msg_plain = \
render_to_string('email/new_project_join_request.txt',
context)
msg_html = \
render_to_string('email/new_project_join_request.html',
context)

send_mail(
subject,
msg_plain,
settings.EMAIL_SENDER,
receiver_list,
html_message=msg_html,
)


def send_project_join_request_approval_email(project, project_user):
Expand Down
2 changes: 1 addition & 1 deletion coldfront/core/project/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def get_context_data(self, **kwargs):
if self.request.user.is_superuser:
context['is_allowed_to_update_project'] = True

elif self.object.projectuser_set.filter(user=self.request.user).exists():
if self.object.projectuser_set.filter(user=self.request.user).exists():
project_user = self.object.projectuser_set.get(user=self.request.user)

if project_user.role.name in ['Principal Investigator', 'Manager']:
Expand Down
8 changes: 8 additions & 0 deletions coldfront/templates/email/new_project_join_request.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Dear managers of {{ project_name }},

User {{ user_string }} has requested to join your project, {{ project_name }} via the MyBRC User Portal.

Please approve/deny this request <a href="{{ review_url }}">here</a>.

Thank you,
{{ signature }}
4 changes: 3 additions & 1 deletion coldfront/templates/email/new_project_join_request.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Dear managers of {{ project_name }},

User {{ user_string }} has requested to join your project, {{ project_name }} via MyBRC user portal. Please approve/deny this request.
User {{ user_string }} has requested to join your project, {{ project_name }} via the MyBRC User Portal.

Please approve/deny this request here: {{ review_url }}.

Thank you,
{{ signature }}

0 comments on commit 586f258

Please sign in to comment.