views.py 5.31 KB
Newer Older
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
1
import os
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
2
from datetime import date
3

Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
4
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
5
from django.shortcuts import get_object_or_404, render
6
from django.contrib.auth.decorators import login_required
7
from django.utils.text import slugify
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
8
from sendfile import sendfile
9

10
from .models import BecomeAMemberDocument
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
11
from .models import Member
12

13
14

def index(request):
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
15
16
17
18
    query_filter = '' if request.GET.get(
        'filter') is None else request.GET.get('filter')
    keywords = '' if request.GET.get('keywords') is None else request.GET.get(
        'keywords')
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
19
20

    page = request.GET.get('page')
21
22
23
    page = 1 if page is None or not page.isdigit() else int(page)

    start_year = date.today().year - 4
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
24
25
    # If language is English show one year less
    # since the width is smaller than needed for the translations to fit
26
27
28
29
30
31
    if request.LANGUAGE_CODE == 'en':
        start_year += 1
    year_range = reversed(range(start_year, date.today().year + 1))

    members = Member.objects.all()
    if query_filter and query_filter.isdigit() and not (
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
32
33
34
                        query_filter == 'ex' or
                        query_filter == 'honor' or
                        query_filter == 'old'):
35
        members = [obj for obj in members if
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
36
37
                   obj.current_membership and
                   obj.current_membership.since.year == int(query_filter)]
38
    elif query_filter == 'old':
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
39
40
41
        members = [obj for obj in members if
                   obj.current_membership and
                   obj.current_membership.since.year < start_year]
42
    elif query_filter == 'ex':
43
        members = [obj for obj in members if not obj.current_membership and
44
                   obj.membership_set.all().filter(type='member').count() > 0]
45
    elif query_filter == 'honor':
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
46
47
48
        members = [obj for obj in members if
                   obj.current_membership and
                   obj.current_membership.type == 'honorary']
49
50
51
52
    else:
        members = [obj for obj in members if obj.current_membership]

    if keywords:
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
53
54
55
56
57
        members = [obj for obj in members if
                   keywords in obj.nickname.lower() or
                   keywords in obj.user.first_name.lower() or
                   keywords in obj.user.last_name.lower() or
                   keywords in obj.user.username.lower()]
58
59
60

    paginator = Paginator(members, 24)

Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
61
62
63
64
65
66
67
68
69
    try:
        members = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        members = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        members = paginator.page(paginator.num_pages)

70
71
72
    page_range = range(1, paginator.num_pages + 1)
    if paginator.num_pages > 7:
        if page > 3:
73
74
75
76
            page_range_end = paginator.num_pages
            if page + 3 <= paginator.num_pages:
                page_range_end = page + 3

Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
77
            page_range = range(page - 2, page_range_end)
78
79
80
81
            while page_range.stop - page_range.start < 5:
                page_range = range(page_range.start - 1, page_range.stop)
        else:
            page_range = range(1, 6)
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
82

Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
83
84
85
86
    return render(request, 'members/index.html',
                  {'members': members, 'filter': query_filter,
                   'year_range': year_range, 'page_range': page_range,
                   'keywords': keywords})
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
87
88


89
90
91
92
93
def profile(request, pk=None):
    if pk:
        member = get_object_or_404(Member, pk=int(pk))
    elif request.user.is_authenticated():
        member = get_object_or_404(Member, user=request.user)
Sébastiaan Versteeg's avatar
Sébastiaan Versteeg committed
94

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    # Group the memberships under the committees for easier template rendering
    memberships = member.committeemembership_set.all()
    achievements = {}
    for membership in memberships:
        name = membership.committee.name
        if achievements.get(name):
            achievements[name]['periods'].append({
                'since': membership.since,
                'until': membership.until,
                'chair': membership.chair
            })
        else:
            achievements[name] = {
                'name': name,
                'periods': [{
                    'since': membership.since,
                    'until': membership.until,
                    'chair': membership.chair
                }]
            }
        achievements[name]['periods'].sort(key=lambda period: period['since'])

117
    mentor_years = member.mentorship_set.all()
118
119
120
121
122
123
124
    for mentor_year in mentor_years:
        name = str(mentor_year)
        if not achievements.get(name):
            achievements[name] = {
                'name': name
            }

125
126
    return render(request, 'members/profile.html',
                  {'member': member, 'achievements': achievements.values()})
127

128
129
130
131
132
133
134
135
@login_required
def account(request):
    return render(request, 'members/account.html')

@login_required
def edit_profile(request):
    return render(request, 'members/account.html')

136
137

def become_a_member(request):
138
    context = {'documents': BecomeAMemberDocument.objects.all()}
139
140
141
142
    return render(request, 'singlepages/become_a_member.html', context)


def get_become_a_member_document(request, pk):
143
    document = get_object_or_404(BecomeAMemberDocument, pk=int(pk))
144
    ext = os.path.splitext(document.file.path)[1]
145
146
    return sendfile(request, document.file.path, attachment=True,
                    attachment_filename=slugify(document.name) + ext)