Repository: dotkom/onlineweb4

Issue:
Send mail from a committe to the participants
Send email to the participants of an event

## What kind of an issue is this?
- Feature request

## What is the expected behaviour?
Be able to send mail from Trikom (or other committees)

## What is the current behaviour?
Currently only possible to send mail from the mails in the picture.
Maybe automatically set it to the committee that arranges the event.

## Other information
<img width="553" alt="screenshot 2018-11-26 at 12 57 02" src="https://user-images.githubusercontent.com/22390745/49013142-019b6500-f17c-11e8-8424-716799192410.png">




Discussion:
I'm guessing this could be refactored into simply emailing from the organizing committee rather than having to select who sends the email.
OnlineGroups have a field for email, so using that would work
I would appreciate if it defaulted to the email corresponding to the commitee you are part of, it is easy to forget to select the correct email if you are in a hurry

Resolving patch:
diff --git a/apps/events/utils.py b/apps/events/utils.py
--- a/apps/events/utils.py
+++ b/apps/events/utils.py
@@ -15,6 +15,7 @@
 from filebrowser.settings import VERSIONS
 from pytz import timezone as tz
 
+from apps.authentication.models import OnlineGroup
 from apps.authentication.models import OnlineUser as User
 from apps.events.models import TYPE_CHOICES, Attendee, Event, Extras
 from apps.payment.models import PaymentDelay, PaymentRelation
@@ -351,7 +352,7 @@ def handle_attend_event_payment(event, user):
         EmailMessage(subject, content, event.feedback_mail(), [user.primary_email]).send()
 
 
-def handle_mail_participants(event, _from_email, _to_email_value, subject, _message, _images,
+def handle_mail_participants(event, _to_email_value, subject, _message, _images,
                              all_attendees, attendees_on_waitlist, attendees_not_paid):
     logger = logging.getLogger(__name__)
 
@@ -361,31 +362,19 @@ def handle_mail_participants(event, _from_email, _to_email_value, subject, _mess
         '3': (attendees_not_paid, 'attendees not paid')
     }
 
-    # Decide from email
-    from_email = 'kontakt@online.ntnu.no'
-    from_email_value = _from_email
-
-    if from_email_value == '1' or from_email_value == '4':
-        from_email = settings.EMAIL_ARRKOM
-    elif from_email_value == '2':
-        from_email = settings.EMAIL_BEDKOM
-    elif from_email_value == '3':
-        from_email = settings.EMAIL_FAGKOM
-    elif from_email_value == '5':
-        from_email = settings.EMAIL_EKSKOM
-    elif from_email_value == '6':
-        from_email = settings.EMAIL_ITEX
-    elif from_email_value == '7':
-        from_email = settings.EMAIL_XSPORT
+    from_email = "kontakt@online.ntnu.no"
+    organizer_group: OnlineGroup = OnlineGroup.objects.filter(group=event.organizer).first()
+    if organizer_group and organizer_group.email:
+        from_email = organizer_group.email
 
     if _to_email_value not in _to_email_options:
         return False
     # Who to send emails to
     send_to_users = _to_email_options[_to_email_value][0]
 
-    signature = '\n\nVennlig hilsen Linjeforeningen Online.\n(Denne eposten kan besvares til %s)' % from_email
+    signature = f"\n\nVennlig hilsen Linjeforeningen Online.\n(Denne eposten kan besvares til {from_email})"
 
-    message = '%s%s' % (_message, signature)
+    message = f"{_message}{signature}"
 
     # Send mail
     try:
diff --git a/apps/events/views.py b/apps/events/views.py
--- a/apps/events/views.py
+++ b/apps/events/views.py
@@ -337,7 +337,6 @@ def mail_participants(request, event_id):
         images = [(image.name, image.read(), image.content_type) for image in request.FILES.getlist('image')]
         mail_sent = handle_mail_participants(
             event,
-            request.POST.get('from_email'),
             request.POST.get('to_email'),
             subject,
             message,


Test patch:
diff --git a/apps/events/tests/view_tests.py b/apps/events/tests/view_tests.py
--- a/apps/events/tests/view_tests.py
+++ b/apps/events/tests/view_tests.py
@@ -4,14 +4,14 @@
 from captcha.client import RecaptchaResponse
 from django.contrib.auth.models import Group
 from django.core import mail
-from django.test import TestCase, override_settings
+from django.test import TestCase
 from django.urls import reverse
 from django.utils import timezone
 from django_dynamic_fixture import G
 from freezegun import freeze_time
 from rest_framework import status
 
-from apps.authentication.models import AllowedUsername
+from apps.authentication.models import AllowedUsername, OnlineGroup
 from apps.marks.models import MarkRuleSet
 from apps.payment.models import PaymentDelay, PaymentPrice
 
@@ -608,14 +608,14 @@ def test_post_as_arrkom_missing_data(self):
             'Vi klarte ikke å sende mailene dine. Prøv igjen', response)
         self.assertEqual(len(mail.outbox), 0)
 
-    @override_settings(EMAIL_ARRKOM='arrkom@online.ntnu.no')
     def test_post_as_arrkom_successfully(self):
+        organizer_email = 'arrkom@online.ntnu.no'
         add_to_arrkom(self.user)
         event = generate_event(TYPE_CHOICES[0][0])
+        G(OnlineGroup, email=organizer_email, group=event.organizer)
         url = reverse('event_mail_participants', args=(event.id,))
 
         response = self.client.post(url, {
-            'from_email': '1',
             'to_email': '1',
             'subject': 'Test',
             'message': 'Test message'
@@ -631,10 +631,10 @@ def test_post_as_arrkom_successfully(self):
     def test_post_as_arrkom_invalid_from_email_defaults_to_kontakt(self):
         add_to_arrkom(self.user)
         event = generate_event(TYPE_CHOICES[0][0])
+        G(OnlineGroup, email='', group=event.organizer)
         url = reverse('event_mail_participants', args=(event.id,))
 
         response = self.client.post(url, {
-            'from_email': '1000',
             'to_email': '1',
             'subject': 'Test',
             'message': 'Test message'
@@ -654,7 +654,6 @@ def test_post_as_arrkom_invalid_to_email(self):
         url = reverse('event_mail_participants', args=(event.id,))
 
         response = self.client.post(url, {
-            'from_email': '1',
             'to_email': '1000',
             'subject': 'Test',
             'message': 'Test message'