Commit 24bd7f4a authored by Jelle Besseling's avatar Jelle Besseling

Merge branch 'tc/refactor-activemembers-views' into 'master'

Refactor activemembers views to be class-based

See merge request !1256
parents 1266b9da ace64423
......@@ -18,18 +18,19 @@
<a href="mailto:info@thalia.nu">send them an email</a>.
{% endblocktrans %}
</p>
{% if current_board %}
<div class="row">
<a href="{{ board.get_absolute_url }}" class="current-board col-12 text-center">
{% if board.photo %}
<img alt="{{ board.name }}"
src="{% thumbnail board.photo THUMBNAIL_SIZE_LARGE fit=False %}">
<a href="{{ current_board.get_absolute_url }}" class="current-board col-12 text-center">
{% if current_board.photo %}
<img alt="{{ current_board.name }}"
src="{% thumbnail current_board.photo THUMBNAIL_SIZE_LARGE fit=False %}">
{% else %}
<img alt="{{ board.name }}"
<img alt="{{ current_board.name }}"
src="{% static 'activemembers/images/placeholder.png' %}">
{% endif %}
</a>
</div>
{% endif %}
</div>
</section>
<section class="page-section">
......
"""The routes defined by the activemembers package"""
from django.conf.urls import url
from . import views
from django.urls import path, re_path
from activemembers.views import (
CommitteeIndexView, CommitteeDetailView,
SocietyIndexView, SocietyDetailView,
BoardIndexView, BoardDetailView
)
app_name = "activemembers"
urlpatterns = [
url(r'committees/$', views.committee_index, name='committees'),
url(r'^committees/(?P<pk>\d+)/$', views.committee_detail, name='committee'),
url(r'societies/$', views.society_index, name='societies'),
url(r'^societies/(?P<pk>\d+)/$', views.society_detail, name='society'),
url(r'^boards/$', views.board_index, name='boards'),
url(r'^board/(?P<since>\d{4})$', views.board_detail, name='board'),
url(r'^board/(?P<since>\d{4})-(?P<until>\d{4})$', views.board_detail, name='board'),
path('committees/',
CommitteeIndexView.as_view(), name='committees'),
path('committees/<int:pk>/',
CommitteeDetailView.as_view(), name='committee'),
path('societies/',
SocietyIndexView.as_view(), name='societies'),
path('societies/<int:pk>/',
SocietyDetailView.as_view(), name='society'),
path('boards/',
BoardIndexView.as_view(), name='boards'),
re_path(r'boards/(?P<since>\d{4})-(?P<until>\d{4})/$',
BoardDetailView.as_view(), name='board'),
]
from django.shortcuts import get_object_or_404, render, redirect, reverse
import datetime
from django.db.models import QuerySet
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.views.generic import ListView, DetailView
from utils.snippets import datetime_to_lectureyear
from utils.translation import localize_attr_name
from .models import Board, MemberGroupMembership, Committee, Society
def committee_index(request):
class _MemberGroupDetailView(DetailView):
"""
View that renders the committee overview page
:param request: the request object
:return: response containing the HTML
Base view for membergroup details
"""
committees = Committee.active_objects.all().order_by(
localize_attr_name('name'))
context_object_name = 'membergroup'
return render(request, 'activemembers/committee_index.html',
{'committees': committees})
def get_context_data(self, **kwargs) -> dict:
context = super().get_context_data(**kwargs)
memberships = (MemberGroupMembership
.active_objects
.filter(group=context['membergroup'])
.prefetch_related('member__membergroupmembership_set'))
members = [{
'member': x.member,
'chair': x.chair,
'role': x.role,
'since': x.initial_connected_membership.since
} for x in memberships]
def committee_detail(request, pk):
"""
View that renders the page of one selected committee
members.sort(key=lambda x: x['since'])
:param request: the request object
:param pk: pk of the selected committee
:return:
"""
committee = get_object_or_404(Committee, pk=pk)
context.update({'members': members})
return context
memberships = (MemberGroupMembership
.active_objects
.filter(group=committee)
.prefetch_related('member__membergroupmembership_set'))
members = [{
'member': x.member,
'chair': x.chair,
'role': x.role,
'since': x.initial_connected_membership.since
} for x in memberships]
members.sort(key=lambda x: x['since'])
return render(request, 'activemembers/committee_detail.html',
{'membergroup': committee,
'members': members})
class CommitteeIndexView(ListView):
"""
View that renders the committee overview page
"""
template_name = 'activemembers/committee_index.html'
queryset = Committee.active_objects
context_object_name = 'committees'
def get_ordering(self) -> str:
return localize_attr_name('name')
def board_index(request):
"""
View that renders the board overview page
:param request: the request object
:return: response containing the HTML
class CommitteeDetailView(_MemberGroupDetailView):
"""
current_year = datetime_to_lectureyear(datetime.date.today())
board = get_object_or_404(
Board, since__year=current_year, until__year=current_year+1)
old_boards = Board.objects.all().exclude(pk=board.pk)
return render(request,
'activemembers/board_index.html',
{'old_boards': old_boards,
'board': board
})
def board_detail(request, since, until=None):
View that renders the page of one selected committee
"""
View that renders the board for a specific lecture year
template_name = 'activemembers/committee_detail.html'
model = Committee
:param request: the request object
:param since: xxxx in xxxx-yyyy of the lecture year
:param until: yyyy in xxxx-yyyy of the lecture year
:return: response containing the HTML
"""
if not until: # try to correct /board/2016 to /2016-2017
return redirect(reverse('activemembers:board',
kwargs={'since': since,
'until': int(since) + 1}))
board = get_object_or_404(Board, since__year=since, until__year=until)
memberships = (MemberGroupMembership
.objects
.filter(group=board)
.prefetch_related('member'))
members = [{
'member': x.member,
'chair': x.chair,
'role': x.role,
} for x in memberships]
return render(request, 'activemembers/board_detail.html',
{'membergroup': board,
'members': members})
def society_index(request):
"""
View that renders the committee overview page
:param request: the request object
:return: response containing the HTML
class SocietyIndexView(ListView):
"""
View that renders the societies overview page
"""
societies = Society.active_objects.all().order_by(
localize_attr_name('name'))
template_name = 'activemembers/society_index.html'
queryset = Society.active_objects
context_object_name = 'societies'
return render(request, 'activemembers/society_index.html',
{'societies': societies})
def get_ordering(self) -> str:
return localize_attr_name('name')
def society_detail(request, pk):
class SocietyDetailView(DetailView):
"""
View that renders the page of one selected committee
View that renders the page of one selected society
"""
template_name = 'activemembers/society_detail.html'
model = Society
:param request: the request object
:param pk: pk of the selected committee
:return:
class BoardIndexView(ListView):
"""
View that renders the board overview page
"""
template_name = 'activemembers/board_index.html'
context_object_name = 'old_boards'
current_board = None
def get_queryset(self) -> QuerySet:
if self.current_board:
return Board.objects.exclude(pk=self.current_board.pk)
return Board.objects.all()
def get_context_data(self, **kwargs) -> dict:
context = super().get_context_data(**kwargs)
context.update({
'current_board': self.current_board
})
return context
def dispatch(self, request, *args, **kwargs) -> HttpResponse:
lecture_year = datetime_to_lectureyear(datetime.date.today())
self.current_board = Board.objects.get(
since__year=lecture_year, until__year=lecture_year + 1)
return super().dispatch(request, *args, **kwargs)
class BoardDetailView(_MemberGroupDetailView):
"""
View that renders the page of one selected board
"""
society = get_object_or_404(Society, pk=pk)
memberships = (MemberGroupMembership
.active_objects
.filter(group=society)
.prefetch_related('member__membergroupmembership_set'))
members = [{
'member': x.member,
'chair': x.chair,
'role': x.role,
'since': x.initial_connected_membership.since
} for x in memberships]
members.sort(key=lambda x: x['since'])
return render(request, 'activemembers/society_detail.html',
{'membergroup': society,
'members': members})
template_name = 'activemembers/board_detail.html'
context_object_name = 'membergroup'
def get_object(self, queryset=None) -> Board:
return get_object_or_404(
Board,
since__year=self.kwargs.get('since'),
until__year=self.kwargs.get('until')
)
......@@ -84,7 +84,6 @@ urlpatterns = [ # pylint: disable=invalid-name
url(r'^pizzas/', include('pizzas.urls')),
url(r'^newsletters/', include('newsletters.urls')),
url(r'^', include([ # 'association' menu
url(r'^', include('activemembers.urls')),
url(r'^merchandise/', include('merchandise.urls')),
path('sibling-associations/', SiblingAssociationsView.as_view(), name='sibling-associations'),
url(r'^thabloid/', include('thabloid.urls')),
......@@ -132,6 +131,7 @@ urlpatterns = [ # pylint: disable=invalid-name
url(r'^media/private/(?P<request_path>.*)$', private_media, name='private-media'),
url('', include('members.urls')),
url('', include('payments.urls')),
url('', include('activemembers.urls')),
url('', include('documents.urls')),
] + static(settings.MEDIA_URL + 'public/',
document_root=os.path.join(settings.MEDIA_ROOT, 'public'))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment