Skip to content

Commit

Permalink
Merge pull request #757 from dotKom/event-mail
Browse files Browse the repository at this point in the history
Event mail
  • Loading branch information
christiansyoung committed Feb 12, 2014
2 parents c164998 + 7bdc85f commit e72badc
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apps/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ def wait_list(self):
return self.attendance_event.attendees.all()[self.attendance_event.max_capacity:]
return [] if self.number_of_attendees_on_waiting_list is 0 else self.attendance_event.attendees[self.attendance_event.max_capacity:]

@property
def attendees_not_paid(self):
return self.attendance_event.attendees.filter(paid=False)

def what_place_is_user_on_wait_list(self, user):
if self.attendance_event:
Expand Down
1 change: 1 addition & 0 deletions apps/events/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
url(r'^(?P<event_id>\d+)/unattend/$', 'unattendEvent', name='unattend_event'),
url(r'^(?P<event_id>\d+)/(?P<event_slug>[a-zA-Z0-9_-]+)/$', 'details', name='events_details'),
url(r'^search/.*$', 'search_events', name="search_events"),
url(r'^mail-participants/(?P<event_id>\d+)$', 'mail_participants', name="event_mail_participants"),
# iCalendar
url(r'^events.ics$', 'calendar_export', name='events_ics'),
url(r'^(?P<event_id>\d+).ics$', 'calendar_export', name='event_ics'),
Expand Down
24 changes: 24 additions & 0 deletions apps/events/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from apps.events.models import Event

def get_group_restricted_events(user):
""" Returns a queryset of events with attendance_event that a user has access to """
types_allowed = []

groups = user.groups.all()

if 'Hovedstyret' or 'dotKom' in groups:
return Event.objects.filter(attendance_event__isnull=False)

for group in groups:
if group.name == 'arrKom':
types_allowed.append(1)
types_allowed.append(4)

if group.name == 'bedKom':
types_allowed.append(2)

if group.name == 'fagKom':
types_allowed.append(3)


return Event.objects.filter(attendance_event__isnull=False, type__in=types_allowed)
72 changes: 72 additions & 0 deletions apps/events/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import login_required, user_passes_test
from django.core.mail import EmailMessage
from django.core.signing import Signer, BadSignature
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404, redirect
from django.utils.translation import ugettext as _
Expand All @@ -20,6 +22,7 @@
from apps.events.forms import CaptchaForm
from apps.events.models import Event, AttendanceEvent, Attendee
from apps.events.pdf_generator import EventPDF
from apps.events.utils import get_group_restricted_events


def index(request):
Expand All @@ -41,6 +44,11 @@ def details(request, event_id, event_slug):
rules = []
user_status = False

user_access_to_event = False
if request.user:
if event in get_group_restricted_events(request.user):
user_access_to_event = True

try:
attendance_event = AttendanceEvent.objects.get(pk=event_id)
is_attendance_event = True
Expand Down Expand Up @@ -79,6 +87,7 @@ def details(request, event_id, event_slug):
'place_on_wait_list': int(place_on_wait_list),
#'position_in_wait_list': position_in_wait_list,
'captcha_form': form,
'user_access_to_event': user_access_to_event,
})
return render(request, 'events/details.html', context)

Expand Down Expand Up @@ -248,3 +257,66 @@ def calendar_export(request, event_id=None, user=None):
response['Content-Disposition'] = 'attachment; filename=' + filename + '.ics'

return response


@login_required
def mail_participants(request, event_id):

event = get_object_or_404(Event, pk=event_id)

if not event.attendance_event:
return HttpResponse(status=503)

# Check access
if not event in get_group_restricted_events(request.user):
messages.error(request, _(u'Du har ikke tilgang til å vise denne siden.'))
return redirect(event)

all_attendees = event.attendance_event.attendees
attendees_on_waitlist = event.wait_list
attendees_not_paid = event.attendees_not_paid

if request.method == 'POST':

# Decide from email
from_email = 'kontakt@online.ntnu.no'
from_email_value = request.POST.get('from_email')

if from_email_value == 1:
from_email = settings.ARRKOM_EMAIL
elif from_email_value == 2:
from_email = settings.BEDKOM_EMAIL
elif from_email_value == 3:
from_email = settings.FAGKOM_EMAIL

signature = u'\n\nVennlig hilsen Linjeforeningen Online.\n(Denne eposten kan besvares til %s)' % from_email

# Decide who to send mail to
to_emails = []
to_emails_value = request.POST.get('to_email')

if to_emails_value == 1:
to_emails = [attendee.user.email for attendee in all_attendees.all()]
elif to_emails_value == 2:
to_emails = [attendee.user.email for attendee in attendees_on_waitlist.all()]
else:
to_emails = [attendee.user.email for attendee in attendees_not_paid.all()]

message = '%s%s' % (request.POST.get('message'), signature)
subject = request.POST.get('subject')

# Send mail
try:

if EmailMessage(unicode(subject), unicode(message), from_email, [], to_emails).send():
messages.success(request, _(u'Mailen ble sendt'))
return redirect(event)
else:
messages.error(request, _(u'Vi klarte ikke å sende mailene dine. Prøv igjen'))
return redirect(event)
except Exception, e:
messages.error(request, str(e))
return redirect(event)

return render(request, 'events/mail_participants.html', {
'all_attendees' : all_attendees, 'attendees_on_waitlist': attendees_on_waitlist, 'attendees_not_paid': attendees_not_paid, 'event' : event})
1 change: 1 addition & 0 deletions templates/events/details.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ <h2 id="event-details-heading">
<li><a href="{% url 'admin:events_event_change' event.id %}">Rediger</a></li>
{% if event.is_attendance_event %}
<li><a href="{% url 'event_attendees_pdf' event.id %}">Påmeldingsliste</a></li>
<li><a href="{% url 'event_mail_participants' event.id %}">Send epost til deltakere<a/></li>
{% endif %}
</ul>
</div>
Expand Down
89 changes: 89 additions & 0 deletions templates/events/mail_participants.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{% extends 'base.html' %}

{% block content %}
<section>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Send epost til påmeldte for «{{ event.title }}»</h2>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="page-header">
<h3>Alle påmeldte</h3>
</div>
{% if all_attendees %}
{% for attendee in all_attendees.all %}
{{ attendee.user.email }},
{% endfor %}
{% else %}
Ingen er påmeldt dette arrangementet.
{% endif %}

</div>
<div class="col-md-4">
<div class="page-header">
<h3>Påmeldte som er på venteliste</h3>
</div>
{% if attendees_on_waitlist %}
{% for attendee in attendees_on_waitlist.all %}
{{ attendee.user.email }},
{% endfor %}
{% else %}
Ingen er på venteliste.
{% endif %}
</div>
<div class="col-md-4">
<div class="page-header">
<h3>Påmeldte som ikke har betalt</h3>
</div>
{% if attendees_not_paid %}
{% for attendee in attendees_not_paid.all %}
{{ attendee.user.email }},
{% endfor %}
{% else %}
Alle som er meldt på har betalt
{% endif %}
</div>
</div>
<div class="row" style="margin-top:30px">
<div class="col-md-6">
<h3>Send epost direkte</h3>
<form method="post" onsubmit="return confirm('Har du dobbeltsjekket informasjonen og vil sende denne eposten?');">
{% csrf_token %}
<div class="form-group">
<label for="to_email">Fra epost</label>
<select id="to_email" name="to_email" class="form-control">
<option value="1">Alle påmeldte</option>
<option value="2">Påmeldte på venteliste</option>
<option value="3">Påmeldte som ikke har betalt</option>
</select>
</div>
<div class="form-group">
<label for="from_email">Fra epost</label>
<select id="from_email" name="from_email" class="form-control">
<option value="1">arrkom@online.ntnu.no</option>
<option value="2">bedkom@online.ntnu.no</option>
<option value="3">fagkom@online.ntnu.no</option>
<option value="4">kontakt@online.ntnu.no</option>
</select>
</div>
<div class="form-group">
<label for="subject">Emne</label>
<input type="text" class="form-control" name="subject" id="subject" />
</div>
<div class="form-group">
<label for="message">Melding</label>
<textarea id="message" name="message" class="form-control" rows="5"></textarea>
</div>
<p style="padding:10px; text-align:center" class="alert-warning">Vi legger automatisk på signatur for deg, skriv BARE din beskjed.</p>
<input name="submit" class="btn btn-success" type="submit" value="Send" />
</form>
</div>
</div>
</div>
</section>
{% endblock %}

0 comments on commit e72badc

Please sign in to comment.