<commit_before>from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.shortcuts import get_object_or_404
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import user_passes_test
from django.core.urlresolvers import reverse
from django.db.models import Max

from pagelets.models import Pagelet, InlinePagelet, Page, PageAttachment, \
                            CONTENT_AREAS
from pagelets.forms import PageletForm, UploadForm


def view_page(request, page_slug, template='pagelets/view_page.html'):
    page = get_object_or_404(Page, slug=page_slug)
    
    context = {
        'page': page,
    }
    return render_to_response(
        page.base_template or template,
        context,
        context_instance=RequestContext(request),
    )


@user_passes_test(lambda u: u.has_perm('pagelets.add_pagelet'),
                  login_url=settings.LOGIN_URL)
def create_pagelet(request, pagelet_slug=None):
    page = None
    if 'page_id' in request.GET:
        try:
            page_id = int(request.GET['page_id'])
            page = Page.objects.get(pk=page_id)
        except (Page.DoesNotExist, ValueError):
            pass
    content_area = ''
    if 'content_area' in request.GET and\
       request.GET['content_area'] in [slug for slug, name in CONTENT_AREAS]:
        content_area = request.GET['content_area']
    pagelet = None
    if pagelet_slug:
        try:
            pagelet = Pagelet.objects.get(slug=pagelet_slug)
        except Pagelet.DoesNotExist:
            pass
    if not pagelet:
        order = None
        if page:
            # if the page exists, set the order of this pagelet to
            # max(page.inline_pagelets.order) + 1 
            order = page.inline_pagelets.aggregate(Max('order'))['order__max'] or 0
            order += 1
            pagelet = InlinePagelet.objects.create(
                slug=pagelet_slug or '',
                created_by=request.user,
                modified_by=request.user,
                page=page,
                area=content_area,
                order=order,
            )
        else:
            pagelet = Pagelet.objects.create(
                slug=pagelet_slug or '',
                created_by=request.user,
                modified_by=request.user,
            )
    edit_pagelet = reverse('edit_pagelet', kwargs={'pagelet_id': pagelet.id})
    if 'next' in request.GET:
        edit_pagelet += '?next=%s' % request.GET['next']
    return HttpResponseRedirect(edit_pagelet)


def edit_pagelet(
    request,
    pagelet_id,
    template='pagelets/edit_pagelet.html',
    redirect_field_name=REDIRECT_FIELD_NAME,
    redirect_to=None,
):<commit_msg>[#13] Use @condition decroator on edit_pagelet view, with both last-modified and etag callbacks.<commit_after>import hashlib

from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.shortcuts import get_object_or_404
from django.views.decorators.http import condition
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import user_passes_test
from django.core.urlresolvers import reverse
from django.db.models import Max

from pagelets.models import Pagelet, InlinePagelet, Page, PageAttachment, \
                            CONTENT_AREAS
from pagelets.forms import PageletForm, UploadForm


def view_page(request, page_slug, template='pagelets/view_page.html'):
    page = get_object_or_404(Page, slug=page_slug)
    
    context = {
        'page': page,
    }
    return render_to_response(
        page.base_template or template,
        context,
        context_instance=RequestContext(request),
    )


@user_passes_test(lambda u: u.has_perm('pagelets.add_pagelet'),
                  login_url=settings.LOGIN_URL)
def create_pagelet(request, pagelet_slug=None):
    page = None
    if 'page_id' in request.GET:
        try:
            page_id = int(request.GET['page_id'])
            page = Page.objects.get(pk=page_id)
        except (Page.DoesNotExist, ValueError):
            pass
    content_area = ''
    if 'content_area' in request.GET and\
       request.GET['content_area'] in [slug for slug, name in CONTENT_AREAS]:
        content_area = request.GET['content_area']
    pagelet = None
    if pagelet_slug:
        try:
            pagelet = Pagelet.objects.get(slug=pagelet_slug)
        except Pagelet.DoesNotExist:
            pass
    if not pagelet:
        order = None
        if page:
            # if the page exists, set the order of this pagelet to
            # max(page.inline_pagelets.order) + 1 
            order = page.inline_pagelets.aggregate(Max('order'))['order__max'] or 0
            order += 1
            pagelet = InlinePagelet.objects.create(
                slug=pagelet_slug or '',
                created_by=request.user,
                modified_by=request.user,
                page=page,
                area=content_area,
                order=order,
            )
        else:
            pagelet = Pagelet.objects.create(
                slug=pagelet_slug or '',
                created_by=request.user,
                modified_by=request.user,
            )
    edit_pagelet = reverse('edit_pagelet', kwargs={'pagelet_id': pagelet.id})
    if 'next' in request.GET:
        edit_pagelet += '?next=%s' % request.GET['next']
    return HttpResponseRedirect(edit_pagelet)


def _last_modified(request, pagelet_id):
    pagelet = get_object_or_404(Pagelet, pk=pagelet_id)
    return pagelet.last_changed
def _etag(request, pagelet_id):
    pagelet = get_object_or_404(Pagelet, pk=pagelet_id)
    etag = hashlib.md5(str(sorted(vars(pagelet).items()))).hexdigest()
    return etag

@condition(last_modified_func=_last_modified, etag_func=_etag)
def edit_pagelet(
    request,
    pagelet_id,
    template='pagelets/edit_pagelet.html',
    redirect_field_name=REDIRECT_FIELD_NAME,
    redirect_to=None,
):
{'fraction_non_alphanumeric': 0.08738974191440706, 'fraction_numerical': 0.0044103234237177396, 'mean_word_length': 2.6295198577356254, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1828022', 'n_tokens_mistral': 1871, 'n_tokens_neox': 1835, 'n_words': 390}