<commit_before># -*- coding: utf-8 -*-

from __future__ import unicode_literals

from functools import update_wrapper

from django.conf import settings
from django.conf.urls import url
from django.contrib import admin
from django.contrib.admin.options import csrf_protect_m
from django.core.urlresolvers import reverse
from django.db import transaction
from django.http import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404

from django_mptt_admin.admin import DjangoMpttAdmin
from mptt.admin import MPTTModelAdmin

from glitter.admin import GlitterAdminMixin
from glitter.models import Version

from .forms import DuplicatePageForm
from .models import Page


def page_admin_fields():
    fields = ['url', 'title', 'parent', 'login_required', 'show_in_navigation']

    # Don't show login_required unless needed
    if not getattr(settings, 'GLITTER_SHOW_LOGIN_REQUIRED', False):
        fields.remove('login_required')

    return fields


@admin.register(Page)
class PageAdmin(GlitterAdminMixin, DjangoMpttAdmin, MPTTModelAdmin):
    list_display = (
        'title', 'url', 'view_url', 'is_published', 'in_nav', 'admin_unpublished_count',
    )
    fields = page_admin_fields()
    mptt_level_indent = 25
    glitter_render = True
    change_list_template = 'admin/pages/page/change_list.html'
    change_form_template = 'admin/pages/page/change_form.html'

    def view_url(self, obj):
        info = self.model._meta.app_label, self.model._meta.model_name
        redirect_url = reverse('admin:%s_%s_redirect' % info, kwargs={'object_id': obj.id})
        return '<a href="%s">View page</a>' % (redirect_url)
    view_url.short_description = 'View page'
    view_url.allow_tags = True

    def in_nav(self, obj):
        return obj.show_in_navigation
    in_nav.boolean = True

    def admin_unpublished_count(self, obj):
        return obj.unpublished_count or ''
    admin_unpublished_count.short_description = 'Unpublished pages'
    admin_unpublished_count.allow_tags = True

    @csrf_protect_m
    def changelist_view(self, request, extra_context=None):
        template_response = super(PageAdmin, self).changelist_view(request, extra_context)
        template_response.template_name = 'admin/pages/page/change_list_tree.html'
        return template_response

    def get_urls(self):
        def wrap(view):
            def wrapper(*args, **kwargs):
                return self.admin_site.admin_view(view)(*args, **kwargs)
            return update_wrapper(wrapper, view)
        urlpatterns = super(PageAdmin, self).get_urls()

        info = self.model._meta.app_label, self.model._meta.model_name

        urlpatterns = [
            url(r'^$', wrap(self.grid_view)),
            url(r'^tree/$', wrap(self.changelist_view), name='%s_%s_tree' % info),
            url(r'^(\d+)/duplicate/$', wrap(self.duplicate_page), name='%s_%s_duplicate' % info),
        ] + urlpatterns
        return urlpatterns

    @csrf_protect_m
    @transaction.atomic
    def duplicate_page(self, request, obj_id):
        obj = get_object_or_404(Page, id=obj_id)

        if request.method == "POST":
            form = DuplicatePageForm(request.POST or None)
            if form.is_valid():
                new_page = form.save()

                # Use current version if exists if not get the latest
                if obj.current_version:
                    current_version = obj.current_version
                else:
                    current_version = obj.get_latest_version()
<commit_msg>Check if user has permission to duplicate page<commit_after># -*- coding: utf-8 -*-

from __future__ import unicode_literals

from functools import update_wrapper

from django.conf import settings
from django.conf.urls import url
from django.contrib import admin
from django.contrib.admin.options import csrf_protect_m
from django.core.urlresolvers import reverse
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.http import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404

from django_mptt_admin.admin import DjangoMpttAdmin
from mptt.admin import MPTTModelAdmin

from glitter.admin import GlitterAdminMixin
from glitter.models import Version

from .forms import DuplicatePageForm
from .models import Page


def page_admin_fields():
    fields = ['url', 'title', 'parent', 'login_required', 'show_in_navigation']

    # Don't show login_required unless needed
    if not getattr(settings, 'GLITTER_SHOW_LOGIN_REQUIRED', False):
        fields.remove('login_required')

    return fields


@admin.register(Page)
class PageAdmin(GlitterAdminMixin, DjangoMpttAdmin, MPTTModelAdmin):
    list_display = (
        'title', 'url', 'view_url', 'is_published', 'in_nav', 'admin_unpublished_count',
    )
    fields = page_admin_fields()
    mptt_level_indent = 25
    glitter_render = True
    change_list_template = 'admin/pages/page/change_list.html'
    change_form_template = 'admin/pages/page/change_form.html'

    def view_url(self, obj):
        info = self.model._meta.app_label, self.model._meta.model_name
        redirect_url = reverse('admin:%s_%s_redirect' % info, kwargs={'object_id': obj.id})
        return '<a href="%s">View page</a>' % (redirect_url)
    view_url.short_description = 'View page'
    view_url.allow_tags = True

    def in_nav(self, obj):
        return obj.show_in_navigation
    in_nav.boolean = True

    def admin_unpublished_count(self, obj):
        return obj.unpublished_count or ''
    admin_unpublished_count.short_description = 'Unpublished pages'
    admin_unpublished_count.allow_tags = True

    @csrf_protect_m
    def changelist_view(self, request, extra_context=None):
        template_response = super(PageAdmin, self).changelist_view(request, extra_context)
        template_response.template_name = 'admin/pages/page/change_list_tree.html'
        return template_response

    def get_urls(self):
        def wrap(view):
            def wrapper(*args, **kwargs):
                return self.admin_site.admin_view(view)(*args, **kwargs)
            return update_wrapper(wrapper, view)
        urlpatterns = super(PageAdmin, self).get_urls()

        info = self.model._meta.app_label, self.model._meta.model_name

        urlpatterns = [
            url(r'^$', wrap(self.grid_view)),
            url(r'^tree/$', wrap(self.changelist_view), name='%s_%s_tree' % info),
            url(r'^(\d+)/duplicate/$', wrap(self.duplicate_page), name='%s_%s_duplicate' % info),
        ] + urlpatterns
        return urlpatterns

    @csrf_protect_m
    @transaction.atomic
    def duplicate_page(self, request, obj_id):
        obj = get_object_or_404(Page, id=obj_id)

        if not self.has_add_permission(request):
            raise PermissionDenied

        if request.method == "POST":
            form = DuplicatePageForm(request.POST or None)
            if form.is_valid():
                new_page = form.save()

                # Use current version if exists if not get the latest
                if obj.current_version:
                    current_version = obj.current_version
                else:
                    current_version = obj.get_latest_version()

{'fraction_non_alphanumeric': 0.09680998613037448, 'fraction_numerical': 0.0024965325936199723, 'mean_word_length': 3.289708506841166, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5358227', 'n_tokens_mistral': 2213, 'n_tokens_neox': 2133, 'n_words': 482}