diff --git a/club/templates/club/club_detail.jinja b/club/templates/club/club_detail.jinja index aeeb7e08..6dbb62b2 100644 --- a/club/templates/club/club_detail.jinja +++ b/club/templates/club/club_detail.jinja @@ -6,10 +6,10 @@ {% if club.logo %} {% endif %} - {% if club.page and club.page.revisions.exists() %} - {{ club.page.revisions.last().content|markdown }} - {% else %} -

{% trans %}Club{% endtrans %}

+ {% if page_revision %} + {{ page_revision|markdown }} + {% else %} +

{% trans %}Club{% endtrans %}

{% endif %} {% endblock %} diff --git a/club/templates/club/page_history.jinja b/club/templates/club/page_history.jinja new file mode 100644 index 00000000..766ef8a1 --- /dev/null +++ b/club/templates/club/page_history.jinja @@ -0,0 +1,13 @@ +{% extends "core/base.jinja" %} +{% from 'core/macros_pages.jinja' import page_history %} + +{% block content %} + {% if club.page %} + {{ page_history(club.page) }} + {% else %} + {% trans %}No page existing for this club{% endtrans %} + {% endif %} +{% endblock %} + + + diff --git a/club/templates/club/pagerev_edit.jinja b/club/templates/club/pagerev_edit.jinja new file mode 100644 index 00000000..232fe559 --- /dev/null +++ b/club/templates/club/pagerev_edit.jinja @@ -0,0 +1,14 @@ +{% extends "core/base.jinja" %} +{% from 'core/macros_pages.jinja' import markdown_preview_script, page_edit_form %} + +{% block head %} +{{ super() }} +{{ markdown_preview_script(csrf_token) }} +{% endblock %} + +{% block content %} +{{ page_edit_form(page, form, url('club:club_edit_page', club_id=page.club.id), csrf_token) }} +{% endblock %} + + + diff --git a/club/urls.py b/club/urls.py index 96195b11..2d0c47d1 100644 --- a/club/urls.py +++ b/club/urls.py @@ -32,7 +32,10 @@ urlpatterns = [ url(r'^new$', ClubCreateView.as_view(), name='club_new'), url(r'^stats$', ClubStatView.as_view(), name='club_stats'), url(r'^(?P[0-9]+)/$', ClubView.as_view(), name='club_view'), + url(r'^(?P[0-9]+)/rev/(?P[0-9]+)/$', ClubRevView.as_view(), name='club_view_rev'), + url(r'^(?P[0-9]+)/hist$', ClubPageHistView.as_view(), name='club_hist'), url(r'^(?P[0-9]+)/edit$', ClubEditView.as_view(), name='club_edit'), + url(r'^(?P[0-9]+)/edit/page$', ClubPageEditView.as_view(), name='club_edit_page'), url(r'^(?P[0-9]+)/members$', ClubMembersView.as_view(), name='club_members'), url(r'^(?P[0-9]+)/elderlies$', ClubOldMembersView.as_view(), name='club_old_members'), url(r'^(?P[0-9]+)/sellings$', ClubSellingView.as_view(), name='club_sellings'), diff --git a/club/views.py b/club/views.py index 7da32e8b..4b6c67a5 100644 --- a/club/views.py +++ b/club/views.py @@ -28,7 +28,7 @@ from django.views.generic import ListView, DetailView, TemplateView from django.views.generic.edit import DeleteView from django.views.generic.detail import SingleObjectMixin from django.views.generic.edit import UpdateView, CreateView -from django.http import HttpResponseRedirect, HttpResponse +from django.http import HttpResponseRedirect, HttpResponse, Http404 from django.core.urlresolvers import reverse, reverse_lazy from django.utils import timezone from django.utils.translation import ugettext_lazy as _ @@ -37,12 +37,12 @@ from ajax_select.fields import AutoCompleteSelectField from django.core.exceptions import PermissionDenied from django.shortcuts import get_object_or_404 -from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, TabedViewMixin +from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, TabedViewMixin, PageEditViewBase from core.views.forms import SelectDate, SelectDateTime from club.models import Club, Membership, Mailing, MailingSubscription from sith.settings import SITH_MAXIMUM_FREE_ROLE from counter.models import Selling, Counter -from core.models import User +from core.models import User, PageRev from django.conf import settings @@ -86,6 +86,8 @@ class MailingSubscriptionForm(forms.ModelForm): class ClubTabsMixin(TabedViewMixin): def get_tabs_title(self): + if isinstance(self.object, PageRev): + self.object = self.object.page.club return self.object.get_display_name() def get_list_of_tabs(self): @@ -106,6 +108,12 @@ class ClubTabsMixin(TabedViewMixin): 'slug': 'elderlies', 'name': _("Old members"), }) + if self.object.page: + tab_list.append({ + 'url': reverse('club:club_hist', kwargs={'club_id': self.object.id}), + 'slug': 'history', + 'name': _("History"), + }) if self.request.user.can_edit(self.object): tab_list.append({ 'url': reverse('club:tools', kwargs={'club_id': self.object.id}), @@ -117,7 +125,7 @@ class ClubTabsMixin(TabedViewMixin): 'slug': 'edit', 'name': _("Edit"), }) - if self.object.page: + if self.object.page and self.request.user.can_edit(self.object.page): tab_list.append({ 'url': reverse('core:page_edit', kwargs={'page_name': self.object.page.get_full_name()}), 'slug': 'page_edit', @@ -159,6 +167,55 @@ class ClubView(ClubTabsMixin, DetailView): template_name = 'club/club_detail.jinja' current_tab = "infos" + def get_context_data(self, **kwargs): + kwargs = super(ClubView, self).get_context_data(**kwargs) + if self.object.page and self.object.page.revisions.exists(): + kwargs['page_revision'] = self.object.page.revisions.last().content + return kwargs + + +class ClubRevView(ClubView): + """ + Display a specific page revision + """ + def dispatch(self, request, *args, **kwargs): + obj = self.get_object() + self.revision = get_object_or_404(PageRev, pk=kwargs['rev_id'], page__club=obj) + return super(ClubRevView, self).dispatch(request, *args, **kwargs) + + def get_context_data(self, **kwargs): + kwargs = super(ClubRevView, self).get_context_data(**kwargs) + kwargs['page_revision'] = self.revision.content + return kwargs + + +class ClubPageEditView(ClubTabsMixin, PageEditViewBase): + template_name = 'club/pagerev_edit.jinja' + current_tab = "page_edit" + + def dispatch(self, request, *args, **kwargs): + self.club = get_object_or_404(Club, pk=kwargs['club_id']) + if not self.club.page: + raise Http404 + return super(ClubPageEditView, self).dispatch(request, *args, **kwargs) + + def get_object(self): + self.page = self.club.page + return self._get_revision() + + def get_success_url(self, **kwargs): + return reverse_lazy('club:club_view', kwargs={'club_id': self.club.id}) + + +class ClubPageHistView(ClubTabsMixin, CanViewMixin, DetailView): + """ + Modification hostory of the page + """ + model = Club + pk_url_kwarg = "club_id" + template_name = 'club/page_history.jinja' + current_tab = "history" + class ClubToolsView(ClubTabsMixin, CanEditMixin, DetailView): """ diff --git a/core/management/commands/populate.py b/core/management/commands/populate.py index 579a58ca..12d69e9f 100644 --- a/core/management/commands/populate.py +++ b/core/management/commands/populate.py @@ -103,6 +103,7 @@ class Command(BaseCommand): launderette_club = Club(id=84, name=settings.SITH_LAUNDERETTE_MANAGER['name'], unix_name=settings.SITH_LAUNDERETTE_MANAGER['unix_name'], address=settings.SITH_LAUNDERETTE_MANAGER['address']) + launderette_club.save() self.reset_index("club") for b in settings.SITH_COUNTER_BARS: diff --git a/core/models.py b/core/models.py index b5fc8c4a..6c4997b0 100644 --- a/core/models.py +++ b/core/models.py @@ -896,6 +896,13 @@ class Page(models.Model): if hasattr(self, 'club') and self.club.can_be_edited_by(user): # Override normal behavior for clubs return True + if self.name == settings.SITH_CLUB_ROOT_PAGE and user.is_board_member: + return True + return False + + def can_be_viewed_by(self, user): + if self.is_club_page: + return True return False def get_parent_list(self): @@ -1005,11 +1012,15 @@ class Page(models.Model): except: return self.name - @property + @cached_property def is_club_page(self): club_root_page = Page.objects.filter(name=settings.SITH_CLUB_ROOT_PAGE).first() return club_root_page is not None and (self == club_root_page or club_root_page in self.get_parent_list()) + @cached_property + def need_club_redirection(self): + return self.is_club_page and self.name != settings.SITH_CLUB_ROOT_PAGE + def delete(self): self.unset_lock_recursive() self.set_lock_recursive(User.objects.get(id=0)) @@ -1059,6 +1070,9 @@ class PageRev(models.Model): else: return object.__getattribute__(self, attr) + def can_be_edited_by(self, user): + return self.page.can_be_edited_by(user) + def save(self, *args, **kwargs): if self.revision is None: self.revision = self.page.revisions.all().count() + 1 diff --git a/core/templates/core/macros.jinja b/core/templates/core/macros.jinja index 8a9bfcbe..61f8928f 100644 --- a/core/templates/core/macros.jinja +++ b/core/templates/core/macros.jinja @@ -125,4 +125,4 @@ {% else %} {% trans %}Next{% endtrans %} {% endif %} -{% endmacro %} \ No newline at end of file +{% endmacro %} diff --git a/core/templates/core/macros_pages.jinja b/core/templates/core/macros_pages.jinja new file mode 100644 index 00000000..5e4396cb --- /dev/null +++ b/core/templates/core/macros_pages.jinja @@ -0,0 +1,50 @@ +{% from "core/macros.jinja" import user_profile_link %} + +{% macro page_history(page) %} +

{% trans page_name=page.name %}You're seeing the history of page "{{ page_name }}"{% endtrans %}

+
    + {% for r in (page.revisions.all()|sort(attribute='date', reverse=True)) %} + {% if loop.index < 2 %} +
  • {% trans %}last{% endtrans %} - + {{ user_profile_link(page.revisions.last().author) }} - + {{ page.revisions.last().date|localtime|date(DATETIME_FORMAT) }} {{ page.revisions.last().date|localtime|time(DATETIME_FORMAT) }}
  • + {% else %} +
  • {{ r.revision }} - + {{ user_profile_link(r.author) }} - + {{ r.date|localtime|date(DATETIME_FORMAT) }} {{ r.date|localtime|time(DATETIME_FORMAT) }}
  • + {% endif %} + {% endfor %} +
+{% endmacro %} + +{% macro page_edit_form(page, form, url, token) %} +

{% trans %}Edit page{% endtrans %}

+
+ + {{ form.as_p() }} + {{ markdown_preview_button() }} +

+
+
+
+{% endmacro %} + +{% macro markdown_preview_script(token) %} + +{% endmacro %} + +{% macro markdown_preview_button() %} +

+{% endmacro %} \ No newline at end of file diff --git a/core/templates/core/page.jinja b/core/templates/core/page.jinja index 0accc81b..73a58676 100644 --- a/core/templates/core/page.jinja +++ b/core/templates/core/page.jinja @@ -26,7 +26,7 @@
{% if page %} {% if page.club %} - {% trans %}Infos{% endtrans %} + {% trans %}Return to club management{% endtrans %} {% else %} {% trans %}View{% endtrans %} {% endif %} diff --git a/core/templates/core/page_hist.jinja b/core/templates/core/page_hist.jinja index a7554045..6f363f7b 100644 --- a/core/templates/core/page_hist.jinja +++ b/core/templates/core/page_hist.jinja @@ -1,23 +1,10 @@ {% extends "core/page.jinja" %} -{% from "core/macros.jinja" import user_profile_link %} +{% from "core/macros_pages.jinja" import page_history %} {% block page %}

{% trans %}Page history{% endtrans %}

-

{% trans page_name=page.name %}You're seeing the history of page "{{ page_name }}"{% endtrans %}

-
    -{% for r in (page.revisions.all()|sort(attribute='date', reverse=True)) %} - {% if loop.index < 2 %} -
  • {% trans %}last{% endtrans %} - - {{ user_profile_link(page.revisions.last().author) }} - - {{ page.revisions.last().date|localtime|date(DATETIME_FORMAT) }} {{ page.revisions.last().date|localtime|time(DATETIME_FORMAT) }}
  • - {% else %} -
  • {{ r.revision }} - - {{ user_profile_link(r.author) }} - - {{ r.date|localtime|date(DATETIME_FORMAT) }} {{ r.date|localtime|time(DATETIME_FORMAT) }}
  • - {% endif %} -{% endfor %} -
+{{ page_history(page) }} {% endblock %} diff --git a/core/templates/core/pagerev_edit.jinja b/core/templates/core/pagerev_edit.jinja index a91c0d04..f1b81762 100644 --- a/core/templates/core/pagerev_edit.jinja +++ b/core/templates/core/pagerev_edit.jinja @@ -1,32 +1,13 @@ {% extends "core/page.jinja" %} +{% from 'core/macros_pages.jinja' import markdown_preview_script, page_edit_form %} {% block head %} {{ super() }} - +{{ markdown_preview_script(csrf_token) }} {% endblock %} {% block page %} -

{% trans %}Edit page{% endtrans %}

-
- {% csrf_token %} - {{ form.as_p() }} -

-

-
-
-
+{{ page_edit_form(page, form, url('core:page_edit', page_name=page.get_full_name()), csrf_token) }} {% endblock %} diff --git a/core/views/page.py b/core/views/page.py index 64028fc5..7fd48308 100644 --- a/core/views/page.py +++ b/core/views/page.py @@ -28,6 +28,7 @@ from django.views.generic import ListView, DetailView from django.views.generic.edit import UpdateView, CreateView, DeleteView from django.forms.models import modelform_factory from django.http import Http404 +from django.shortcuts import redirect from core.models import Page, PageRev, LockError from core.views.forms import MarkdownInput, PageForm, PagePropForm @@ -52,6 +53,12 @@ class PageView(CanViewMixin, DetailView): model = Page template_name = 'core/page_detail.jinja' + def dispatch(self, request, *args, **kwargs): + res = super(PageView, self).dispatch(request, *args, **kwargs) + if self.object and self.object.need_club_redirection: + return redirect('club:club_view', club_id=self.object.club.id) + return res + def get_object(self): self.page = Page.get_page_by_full_name(self.kwargs['page_name']) return self.page @@ -67,6 +74,12 @@ class PageHistView(CanViewMixin, DetailView): model = Page template_name = 'core/page_hist.jinja' + def dispatch(self, request, *args, **kwargs): + res = super(PageHistView, self).dispatch(request, *args, **kwargs) + if self.object.need_club_redirection: + return redirect('club:club_hist', club_id=self.object.club.id) + return res + def get_object(self): self.page = Page.get_page_by_full_name(self.kwargs['page_name']) return self.page @@ -76,6 +89,12 @@ class PageRevView(CanViewMixin, DetailView): model = Page template_name = 'core/page_detail.jinja' + def dispatch(self, request, *args, **kwargs): + res = super(PageRevView, self).dispatch(request, *args, **kwargs) + if self.object.need_club_redirection: + return redirect('club:club_view_rev', club_id=self.object.club.id, rev_id=kwargs['rev']) + return res + def get_object(self): self.page = Page.get_page_by_full_name(self.kwargs['page_name']) return self.page @@ -148,17 +167,20 @@ class PagePropView(CanEditPagePropMixin, UpdateView): return self.page -class PageEditView(CanEditMixin, UpdateView): +class PageEditViewBase(CanEditMixin, UpdateView): model = PageRev form_class = modelform_factory(model=PageRev, fields=['title', 'content', ], widgets={'content': MarkdownInput}) template_name = 'core/pagerev_edit.jinja' def get_object(self): self.page = Page.get_page_by_full_name(self.kwargs['page_name']) + return self._get_revision() + + def _get_revision(self): if self.page is not None: # First edit if self.page.revisions.all() is None: - rev = PageRev(author=request.user) + rev = PageRev(author=self.request.user) rev.save() self.page.revisions.add(rev) try: @@ -169,7 +191,7 @@ class PageEditView(CanEditMixin, UpdateView): return None def get_context_data(self, **kwargs): - context = super(PageEditView, self).get_context_data(**kwargs) + context = super(PageEditViewBase, self).get_context_data(**kwargs) if self.page is not None: context['page'] = self.page else: @@ -185,7 +207,16 @@ class PageEditView(CanEditMixin, UpdateView): new_rev.author = self.request.user new_rev.page = self.page form.instance = new_rev - return super(PageEditView, self).form_valid(form) + return super(PageEditViewBase, self).form_valid(form) + + +class PageEditView(PageEditViewBase): + + def dispatch(self, request, *args, **kwargs): + res = super(PageEditView, self).dispatch(request, *args, **kwargs) + if self.object.page.need_club_redirection: + return redirect('club:club_edit_page', club_id=self.object.page.club.id) + return res class PageDeleteView(CanEditPagePropMixin, DeleteView):