~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to notification/views.py

  • Committer: kaputtnik
  • Date: 2019-05-30 18:20:02 UTC
  • mto: This revision was merged to the branch mainline in revision 540.
  • Revision ID: kaputtnik-20190530182002-g7l91m1xo28clghv
adjusted README; first commit on the new server

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from django.shortcuts import render
 
2
from django.contrib.auth.decorators import login_required
 
3
from collections import OrderedDict
 
4
from notification.models import *
 
5
 
 
6
 
 
7
@login_required
 
8
def notice_settings(request):
 
9
    app_tables = {}
 
10
    for notice_type in NoticeType.objects.all().order_by('label'):
 
11
        # Assuming each notice_type.label begins with the name of the app
 
12
        # followed by an underscore:
 
13
        app = notice_type.label.partition('_')[0]
 
14
        app_tables.setdefault(app, [])
 
15
        checkbox_values = []
 
16
        for medium_id, medium_display in NOTICE_MEDIA:
 
17
            form_label = '%s_%s' % (notice_type.label, medium_id)
 
18
            setting = get_notification_setting(
 
19
                request.user, notice_type, medium_id)
 
20
            if request.method == 'POST':
 
21
                if request.POST.get(form_label) == 'on':
 
22
                    setting.send = True
 
23
                else:
 
24
                    setting.send = False
 
25
                setting.save()
 
26
            checkbox_values.append((form_label, setting.send))
 
27
 
 
28
        app_tables[app].append({'notice_type': notice_type, 'html_values': checkbox_values})
 
29
 
 
30
    return render(request, 'notification/notice_settings.html', {
 
31
        'column_headers': [medium_display for medium_id, medium_display in NOTICE_MEDIA],
 
32
        'app_tables': OrderedDict(sorted(list(app_tables.items()), key=lambda t: t[0]))
 
33
    })