diff --git a/agenda/tests.py b/agenda/tests.py index 954a0de..c708f6a 100644 --- a/agenda/tests.py +++ b/agenda/tests.py @@ -290,7 +290,7 @@ def test_get_activities_about_to_kickoff_returns_empty_queryset_if_events_starte self.assertQuerysetEqual(events, Event.objects.none()) def test_trying_to_send_email_for_manager_without_email_doesnt_sends_the_email(self): - group = Group.objects.create(name="Group_name") + group = Group.objects.create(name="Manager") position = Position.objects.create(text="Position", type=group, area_associated=self.area_responsible) user = User.objects.create_user(username="Username", password="Password") user.userprofile.position = position @@ -300,7 +300,7 @@ def test_trying_to_send_email_for_manager_without_email_doesnt_sends_the_email(s self.assertEqual(response.status_code, 302) def test_trying_to_send_email_for_manager_with_email_sends_the_email(self): - group = Group.objects.create(name="Group_name") + group = Group.objects.create(name="Manager") position = Position.objects.create(text="Position", type=group, area_associated=self.area_responsible) user = User.objects.create_user(username="Username", password="Password") user.userprofile.position = position @@ -312,7 +312,7 @@ def test_trying_to_send_email_for_manager_with_email_sends_the_email(self): self.assertEqual(response.status_code, 302) def test_trying_to_send_email_with_no_activities_doesnt_sends_the_email(self): - group = Group.objects.create(name="Group_name") + group = Group.objects.create(name="Manager") position = Position.objects.create(text="Position", type=group, area_associated=self.area_responsible) user = User.objects.create_user(username="Username", password="Password") user.userprofile.position = position @@ -326,7 +326,7 @@ def test_trying_to_send_email_with_no_activities_doesnt_sends_the_email(self): self.assertEqual(response.status_code, 302) def test_trying_to_send_email_with_activities_of_one_day_sends_the_email_with_proper_representation(self): - group = Group.objects.create(name="Group_name") + group = Group.objects.create(name="Manager") position = Position.objects.create(text="Position", type=group, area_associated=self.area_responsible) user = User.objects.create_user(username="Username", password="Password") user.userprofile.position = position @@ -337,5 +337,15 @@ def test_trying_to_send_email_with_activities_of_one_day_sends_the_email_with_pr self.event.initial_date=self.event.end_date self.event.save() + response = self.client.get(reverse('agenda:send_email')) + self.assertEqual(response.status_code, 302) + + def test_trying_to_send_email_when_manager_hasnt_email_fails(self): + group = Group.objects.create(name="Manager") + position = Position.objects.create(text="Position", type=group, area_associated=self.area_responsible) + user = User.objects.create_user(username="Username", password="Password") + user.userprofile.position = position + user.userprofile.save() + response = self.client.get(reverse('agenda:send_email')) self.assertEqual(response.status_code, 302) \ No newline at end of file diff --git a/agenda/views.py b/agenda/views.py index 3ef397f..25774cd 100644 --- a/agenda/views.py +++ b/agenda/views.py @@ -11,7 +11,7 @@ from django.conf import settings from agenda.forms import EventForm from agenda.models import Event -from users.models import TeamArea +from users.models import TeamArea, UserProfile # MONTH CALENDAR @@ -175,14 +175,10 @@ def update_event(request, event_id): def send_email(request): html_template_path = "agenda/email_template.html" - areas = TeamArea.objects.all() + areas = TeamArea.objects.filter(team_area_of_position__type__name="Manager") for area in areas: - try: - manager = area.team_area_of_position.first().user_position.first() - manager_email = manager.user.email - except AttributeError: - manager = "" - manager_email = "" + manager = UserProfile.objects.filter(user__is_active=True, position__area_associated=area).first() + manager_email = manager.user.email if manager_email: upcoming_reports = get_activities_soon_to_be_finished(area) @@ -208,7 +204,8 @@ def send_email(request): reply_to = [settings.EMAIL_HOST_USER] ) email_msg.content_subtype = "html" - email_msg.send(fail_silently=False) + print(manager_email) + # email_msg.send(fail_silently=False) else: pass return redirect(reverse("metrics:index"))