Skip to content

Commit

Permalink
fixing email sending
Browse files Browse the repository at this point in the history
  • Loading branch information
Ederporto committed Feb 20, 2024
1 parent 2953150 commit ff295b0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
18 changes: 14 additions & 4 deletions agenda/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
15 changes: 6 additions & 9 deletions agenda/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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"))
Expand Down

0 comments on commit ff295b0

Please sign in to comment.