Commit b1f6754e authored by Skia's avatar Skia
Browse files

Refactor lots of tabed templates, and create TabedViewMixin

parent 976ae09e
{% extends "club/club_base.jinja" %}
{% extends "core/base.jinja" %}
{% from 'core/macros.jinja' import user_profile_link %}
{% block club %}
{% block content %}
<h3>{% trans %}Club{% endtrans %}</h3>
{% endblock %}
......
{% extends "club/club_base.jinja" %}
{% block club %}
<h2>{% trans %}Edit club{% endtrans %}</h2>
<form action="{{ url('club:club_edit', club_id=club.id) }}" method="post">
{% csrf_token %}
{{ form.as_p() }}
<p><input type="submit" value="{% trans %}Save{% endtrans %}" /></p>
</form>
{% endblock %}
{% extends "club/club_base.jinja" %}
{% block club %}
<h2>{% trans %}Edit club properties{% endtrans %}</h2>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p() }}
<p><input type="submit" value="{% trans %}Save{% endtrans %}" /></p>
</form>
{% endblock %}
{% extends "club/club_base.jinja" %}
{% extends "core/base.jinja" %}
{% from 'core/macros.jinja' import user_profile_link %}
{% block club %}
{% block content %}
<h2>{% trans %}Club members{% endtrans %}</h2>
<table>
<thead>
......
{% extends "club/club_base.jinja" %}
{% extends "core/base.jinja" %}
{% from 'core/macros.jinja' import user_profile_link %}
{% block club %}
{% block content %}
<h2>{% trans %}Club old members{% endtrans %}</h2>
<table>
<thead>
......
{% extends "club/club_base.jinja" %}
{% extends "core/base.jinja" %}
{% block club %}
{% block content %}
<h3>{% trans %}Club tools{% endtrans %}</h3>
<div>
{% if object.counters.filter(type="OFFICE")|count > 0 %}
......
......@@ -7,11 +7,53 @@ from django.core.exceptions import ValidationError
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.utils import timezone
from django.utils.translation import ugettext as _
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, TabedViewMixin
from club.models import Club, Membership
from sith.settings import SITH_MAXIMUM_FREE_ROLE, SITH_MAIN_BOARD_GROUP
class ClubTabsMixin(TabedViewMixin):
def get_tabs_title(self):
return self.object.get_display_name()
def get_list_of_tabs(self):
tab_list = []
tab_list.append({
'url': reverse('club:club_view', kwargs={'club_id': self.object.id}),
'slug': 'infos',
'name': _("Infos"),
})
if self.request.user.can_view(self.object):
tab_list.append({
'url': reverse('club:club_members', kwargs={'club_id': self.object.id}),
'slug': 'members',
'name': _("Members"),
})
tab_list.append({
'url': reverse('club:club_old_members', kwargs={'club_id': self.object.id}),
'slug': 'elderlies',
'name': _("Old members"),
})
if self.request.user.can_edit(self.object):
tab_list.append({
'url': reverse('club:tools', kwargs={'club_id': self.object.id}),
'slug': 'tools',
'name': _("Tools"),
})
tab_list.append({
'url': reverse('club:club_edit', kwargs={'club_id': self.object.id}),
'slug': 'edit',
'name': _("Edit"),
})
if self.request.user.is_owner(self.object):
tab_list.append({
'url': reverse('club:club_prop', kwargs={'club_id': self.object.id}),
'slug': 'props',
'name': _("Props"),
})
return tab_list
class ClubListView(ListView):
"""
List the Clubs
......@@ -19,31 +61,23 @@ class ClubListView(ListView):
model = Club
template_name = 'club/club_list.jinja'
class ClubView(DetailView):
class ClubView(ClubTabsMixin, DetailView):
"""
Front page of a Club
"""
model = Club
pk_url_kwarg = "club_id"
template_name = 'club/club_detail.jinja'
current_tab = "infos"
def get_context_data(self, **kwargs):
kwargs = super(ClubView, self).get_context_data(**kwargs)
kwargs['tab'] = "infos"
return kwargs
class ClubToolsView(CanEditMixin, DetailView):
class ClubToolsView(ClubTabsMixin, CanEditMixin, DetailView):
"""
Tools page of a Club
"""
model = Club
pk_url_kwarg = "club_id"
template_name = 'club/club_tools.jinja'
def get_context_data(self, **kwargs):
kwargs = super(ClubToolsView, self).get_context_data(**kwargs)
kwargs['tab'] = "tools"
return kwargs
current_tab = "tools"
class ClubMemberForm(forms.ModelForm):
"""
......@@ -76,7 +110,7 @@ class ClubMemberForm(forms.ModelForm):
ret = super(ClubMemberForm, self).save(*args, **kwargs)
return self.instance.club
class ClubMembersView(CanViewMixin, UpdateView):
class ClubMembersView(ClubTabsMixin, CanViewMixin, UpdateView):
"""
View of a club's members
"""
......@@ -84,6 +118,7 @@ class ClubMembersView(CanViewMixin, UpdateView):
pk_url_kwarg = "club_id"
form_class = ClubMemberForm
template_name = 'club/club_members.jinja'
current_tab = "members"
def get_form(self):
"""
......@@ -99,51 +134,34 @@ class ClubMembersView(CanViewMixin, UpdateView):
form._user = self.request.user
return form
def get_context_data(self, **kwargs):
kwargs = super(ClubMembersView, self).get_context_data(**kwargs)
kwargs['tab'] = "members"
return kwargs
class ClubOldMembersView(CanViewMixin, DetailView):
class ClubOldMembersView(ClubTabsMixin, CanViewMixin, DetailView):
"""
Old members of a club
"""
model = Club
pk_url_kwarg = "club_id"
template_name = 'club/club_old_members.jinja'
current_tab = "elderlies"
def get_context_data(self, **kwargs):
kwargs = super(ClubOldMembersView, self).get_context_data(**kwargs)
kwargs['tab'] = "elderlies"
return kwargs
class ClubEditView(CanEditMixin, UpdateView):
class ClubEditView(ClubTabsMixin, CanEditMixin, UpdateView):
"""
Edit a Club's main informations (for the club's members)
"""
model = Club
pk_url_kwarg = "club_id"
fields = ['address']
template_name = 'club/club_edit.jinja'
def get_context_data(self, **kwargs):
kwargs = super(ClubEditView, self).get_context_data(**kwargs)
kwargs['tab'] = "edit"
return kwargs
template_name = 'core/edit.jinja'
current_tab = "edit"
class ClubEditPropView(CanEditPropMixin, UpdateView):
class ClubEditPropView(ClubTabsMixin, CanEditPropMixin, UpdateView):
"""
Edit the properties of a Club object (for the Sith admins)
"""
model = Club
pk_url_kwarg = "club_id"
fields = ['name', 'unix_name', 'parent']
template_name = 'club/club_edit_prop.jinja'
def get_context_data(self, **kwargs):
kwargs = super(ClubEditPropView, self).get_context_data(**kwargs)
kwargs['tab'] = "props"
return kwargs
template_name = 'core/edit.jinja'
current_tab = "props"
class ClubCreateView(CanEditPropMixin, CreateView):
"""
......@@ -152,7 +170,7 @@ class ClubCreateView(CanEditPropMixin, CreateView):
model = Club
pk_url_kwarg = "club_id"
fields = ['name', 'unix_name', 'parent']
template_name = 'club/club_edit_prop.jinja'
template_name = 'core/edit.jinja'
class MembershipSetOldView(CanEditMixin, DetailView):
"""
......
......@@ -74,6 +74,22 @@
{% endblock %}
<div id="content">
{% if list_of_tabs %}
<div class="tool-bar">
<div>{{ tabs_title }}</div>
<div class="tools">
{% for t in list_of_tabs %}
<a href="{{ t.url }}"
{%- if current_tab == t.slug -%}
class="selected_tab"
{%- endif -%}
>{{ t.name }}</a>
{% endfor %}
</div>
<hr>
</div>
{% endif %}
{% if error %}
{{ error }}
{% endif %}
......
{% extends "core/user_base.jinja" %}
{% extends "core/base.jinja" %}
{% block title %}
{% trans user_name=profile.get_display_name() %}{{ user_name }}'s account{% endtrans %}
{% endblock %}
{% block infos %}
{% block content %}
{% if customer %}
<h3>{% trans %}User account{% endtrans %}</h3>
<p>{% trans %}Amount: {% endtrans %}{{ customer.amount }}</p>
......
{% extends "core/base.jinja" %}
{% block content %}
<div class="tool-bar">
<div>{{ profile.get_display_name() }}</div>
<div class="tools">
<a href="{{ url('core:user_profile', user_id=profile.id) }}"
{%- if tab == "infos" -%}
class="selected_tab"
{%- endif -%}
>{% trans %}Infos{% endtrans %}</a>
{% if profile == user %}
<a href="{{ url('core:user_tools') }}"
{%- if tab == "tools" -%}
class="selected_tab"
{%- endif -%}
>{% trans %}Tools{% endtrans %}</a>
{% endif %}
<a href="{{ url('core:user_stats', user_id=profile.id) }}"
{%- if tab == "stats" -%}
class="selected_tab"
{%- endif -%}
>{% trans %}Stats{% endtrans %}</a>
{% if can_edit(profile, request.user) or user.id == profile.id %}
<a href="{{ url('core:user_edit', user_id=profile.id) }}"
{%- if tab == "edit" -%}
class="selected_tab"
{%- endif -%}
>{% trans %}Edit{% endtrans %}</a>
{% endif %}
{% if can_edit_prop(profile, request.user) %}
<a href="{{ url('core:user_groups', user_id=profile.id) }}"
{%- if tab == "groups" -%}
class="selected_tab"
{%- endif -%}
>{% trans %}Groups{% endtrans %}</a>
{% endif %}
{% if profile.customer and (profile == request.user
or request.user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name'])
or request.user.is_root) %}
<a href="{{ url('core:user_account', user_id=profile.id) }}"
{%- if tab == "account" -%}
class="selected_tab"
{%- endif -%}
>{% trans %}Account{% endtrans %} ({{ profile.customer.amount }}€)</a>
{% endif %}
</div>
<hr>
</div>
<div>
{% block infos %}
{% endblock %}
</div>
{% endblock %}
{% extends "core/user_base.jinja" %}
{% extends "core/base.jinja" %}
{% block title %}
{% trans user_name=profile.get_display_name() %}{{ user_name }}'s profile{% endtrans %}
{% endblock %}
{% block infos %}
{% block content %}
<div id="user_profile_container">
<div id="user_profile">
......
{% extends "core/user_base.jinja" %}
{% extends "core/base.jinja" %}
{% block title %}
{% trans %}Edit user{% endtrans %}
{% endblock %}
{% block infos %}
{% block content %}
<h2>{% trans %}Edit user profile{% endtrans %}</h2>
<form action="" method="post" enctype="multipart/form-data" id="user_edit">
{% csrf_token %}
......
{% extends "core/user_base.jinja" %}
{% extends "core/base.jinja" %}
{% block infos %}
{% block content %}
<h2>{% trans user_name=profile.get_full_name() %}Edit user groups for {{ user_name }}{% endtrans %}</h2>
<form action="" method="post">
{% csrf_token %}
......
{% extends "core/user_base.jinja" %}
{% extends "core/base.jinja" %}
{% block title %}
{% trans user_name=profile.get_display_name() %}{{ user_name }}'s stats{% endtrans %}
{% endblock %}
{% block infos %}
{% block content %}
{% if profile.permanencies %}
<div>
<h3>{% trans %}Permanencies{% endtrans %}</h3>
......
{% extends "core/user_base.jinja" %}
{% extends "core/base.jinja" %}
{% block title %}
{% trans user_name=user.get_display_name() %}{{ user_name }}'s tools{% endtrans %}
{% endblock %}
{% block infos %}
{% block content %}
<h3>{% trans %}User Tools{% endtrans %}</h3>
<hr>
......
from django.shortcuts import render
from django.http import HttpResponseForbidden, HttpResponseNotFound
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist, ImproperlyConfigured
from django.views.generic.base import View
from core.models import Group
......@@ -110,8 +110,39 @@ class CanViewMixin(View):
context['object_list'] = l
return context
class TabedViewMixin(View):
"""
This view provide the basic functions for displaying tabs in the template
"""
def get_tabs_title(self):
try:
return self.tabs_title
except:
raise ImproperlyConfigured("tabs_title is required")
def get_current_tab(self):
try:
return self.current_tab
except:
raise ImproperlyConfigured("current_tab is required")
def get_list_of_tabs(self):
try:
return self.list_of_tabs
except:
raise ImproperlyConfigured("list_of_tabs is required")
def get_context_data(self, **kwargs):
kwargs = super(TabedViewMixin, self).get_context_data(**kwargs)
kwargs['tabs_title'] = self.get_tabs_title()
kwargs['current_tab'] = self.get_current_tab()
kwargs['list_of_tabs'] = self.get_list_of_tabs()
return kwargs
from .user import *
from .page import *
from .files import *
from .site import *
from .group import *
......@@ -15,7 +15,7 @@ from django.conf import settings
from datetime import timedelta
import logging
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, TabedViewMixin
from core.views.forms import RegisteringForm, UserPropForm, UserProfileForm, LoginForm
from core.models import User, SithFile
......@@ -96,7 +96,6 @@ def password_reset_complete(request):
template_name="core/password_reset_complete.jinja",
)
def register(request):
context = {}
if request.method == 'POST':
......@@ -115,7 +114,53 @@ def register(request):
context['form'] = form.as_p()
return render(request, "core/register.jinja", context)
class UserView(CanViewMixin, DetailView):
class UserTabsMixin(TabedViewMixin):
def get_tabs_title(self):
return self.object.get_display_name()
def get_list_of_tabs(self):
tab_list = []
tab_list.append({
'url': reverse('core:user_profile', kwargs={'user_id': self.object.id}),
'slug': 'infos',
'name': _("Infos"),
})
if self.request.user == self.object:
tab_list.append({
'url': reverse('core:user_tools'),
'slug': 'tools',
'name': _("Tools"),
})
tab_list.append({
'url': reverse('core:user_stats', kwargs={'user_id': self.object.id}),
'slug': 'stats',
'name': _("Stats"),
})
if self.request.user.can_edit(self.object):
tab_list.append({
'url': reverse('core:user_edit', kwargs={'user_id': self.object.id}),
'slug': 'edit',
'name': _("Edit"),
})
if self.request.user.is_owner(self.object):
tab_list.append({
'url': reverse('core:user_groups', kwargs={'user_id': self.object.id}),
'slug': 'groups',
'name': _("Groups"),
})
try:
if (self.object.customer and (self.object == self.request.user
or self.request.user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name'])
or self.request.user.is_root)):
tab_list.append({
'url': reverse('core:user_account', kwargs={'user_id': self.object.id}),
'slug': 'account',
'name': _("Account")+" (%s €)" % self.object.customer.amount,
})
except: pass
return tab_list
class UserView(UserTabsMixin, CanViewMixin, DetailView):
"""
Display a user's profile
"""
......@@ -123,13 +168,9 @@ class UserView(CanViewMixin, DetailView):
pk_url_kwarg = "user_id"
context_object_name = "profile"
template_name = "core/user_detail.jinja"
current_tab = 'infos'
def get_context_data(self, **kwargs):
kwargs = super(UserView, self).get_context_data(**kwargs)
kwargs['tab'] = "infos"
return kwargs
class UserStatsView(CanViewMixin, DetailView):
class UserStatsView(UserTabsMixin, CanViewMixin, DetailView):
"""
Display a user's stats
"""
......@@ -137,6 +178,7 @@ class UserStatsView(CanViewMixin, DetailView):
pk_url_kwarg = "user_id"
context_object_name = "profile"
template_name = "core/user_stats.jinja"
current_tab = 'stats'
def get_context_data(self, **kwargs):
kwargs = super(UserStatsView, self).get_context_data(**kwargs)
......@@ -148,7 +190,6 @@ class UserStatsView(CanViewMixin, DetailView):
kwargs['total_foyer_time'] = sum([p.end-p.start for p in self.object.permanencies.filter(counter=foyer)], timedelta())
kwargs['total_mde_time'] = sum([p.end-p.start for p in self.object.permanencies.filter(counter=mde)], timedelta())
kwargs['total_gommette_time'] = sum([p.end-p.start for p in self.object.permanencies.filter(counter=gommette)], timedelta())
kwargs['tab'] = "stats"
return kwargs
class UserMiniView(CanViewMixin, DetailView):
......@@ -196,7 +237,7 @@ class UserUploadProfilePictView(CanEditMixin, DetailView):
self.object.save()
return redirect("core:user_edit", user_id=self.object.id)
class UserUpdateProfileView(CanEditMixin, UpdateView):
class UserUpdateProfileView(UserTabsMixin, CanEditMixin, UpdateView):
"""
Edit a user's profile
"""
......@@ -204,6 +245,7 @@ class UserUpdateProfileView(CanEditMixin, UpdateView):
pk_url_kwarg = "user_id"
template_name = "core/user_edit.jinja"
form_class = UserProfileForm
current_tab = "edit"
def get(self, request, *args, **kwargs):
self.object = self.get_object()
......@@ -227,10 +269,9 @@ class UserUpdateProfileView(CanEditMixin, UpdateView):
kwargs = super(UserUpdateProfileView, self).get_context_data(**kwargs)
kwargs['profile'] = self.form.instance
kwargs['form'] = self.form
kwargs['tab'] = "edit"
return kwargs
class UserUpdateGroupView(CanEditPropMixin, UpdateView):
class UserUpdateGroupView(UserTabsMixin, CanEditPropMixin, UpdateView):
"""
Edit a user's groups
"""
......@@ -240,33 +281,32 @@ class UserUpdateGroupView(CanEditPropMixin, UpdateView):
form_class = modelform_factory(User, fields=['groups'],
widgets={'groups':CheckboxSelectMultiple})
context_object_name = "profile"
current_tab = "groups"
def get_context_data(self, **kwargs):
kwargs = super(UserUpdateGroupView, self).get_context_data(**kwargs)
kwargs['tab'] = "groups"
return kwargs
class UserToolsView(TemplateView):
class UserToolsView(UserTabsMixin, TemplateView):
"""
Displays the logged user's tools
"""
template_name = "core/user_tools.jinja"
current_tab = "tools"
def get_context_data(self, **kwargs):