<commit_before>import json

from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, render
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.auth.decorators import permission_required

from wagtail.wagtailadmin.modal_workflow import render_modal_workflow
from wagtail.wagtailadmin.forms import SearchForm
from wagtail.wagtailsearch.backends import get_search_backends

from embed_video.backends import detect_backend

from wagtail_embed_videos.models import get_embed_video_model
from wagtail_embed_videos.forms import get_embed_video_form, EmbedVideoInsertionForm
# from wagtail_embed_videos.formats import get_embed_video_format


def get_embed_video_json(embed_video):
    """
    helper function: given an embed video, return the json to pass back to the
    embed video chooser panel
    """
    if embed_video.thumbnail:
        preview_embed_video = embed_video.thumbnail.get_rendition('max-130x100').url
    else:
        preview_embed_video = detect_backend(embed_video.url).get_thumbnail_url()

    return json.dumps({
        'id': embed_video.id,
        'edit_link': reverse('wagtail_embed_videos_edit_embed_video', args=(embed_video.id,)),
        'title': embed_video.title,
        'preview': {
            'url': preview_embed_video,
        }
    })


def chooser(request):
    EmbedVideo = get_embed_video_model()

    if request.user.has_perm('wagtail_embed_videos.add_embedvideo'):
        can_add = True
    else:
        can_add = False

    q = None
    if 'q' in request.GET or 'p' in request.GET:
        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']

            # page number
            p = request.GET.get("p", 1)

            embed_videos = EmbedVideo.search(q, results_per_page=10, page=p)

            is_searching = True

        else:
            embed_videos = EmbedVideo.objects.order_by('-created_at')
            p = request.GET.get("p", 1)
            paginator = Paginator(embed_videos, 10)

            try:
                embed_videos = paginator.page(p)
            except PageNotAnInteger:
                embed_videos = paginator.page(1)
            except EmptyPage:
                embed_videos = paginator.page(paginator.num_pages)

            is_searching = False
        return render(request, "wagtail_embed_videos/chooser/results.html", {
            'embed_videos': embed_videos,
            'is_searching': is_searching,
            'can_add': can_add,
            'query_string': q,
        })
    else:
        searchform = SearchForm()

        embed_videos = EmbedVideo.objects.order_by('-created_at')
        p = request.GET.get("p", 1)
        paginator = Paginator(embed_videos, 10)

        try:
            embed_videos = paginator.page(p)
        except PageNotAnInteger:
            embed_videos = paginator.page(1)
        except EmptyPage:
            embed_videos = paginator.page(paginator.num_pages)
    print can_add<commit_msg>Fix python3 compatibility<commit_after>import json

from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, render
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.auth.decorators import permission_required

from wagtail.wagtailadmin.modal_workflow import render_modal_workflow
from wagtail.wagtailadmin.forms import SearchForm
from wagtail.wagtailsearch.backends import get_search_backends

from embed_video.backends import detect_backend

from wagtail_embed_videos.models import get_embed_video_model
from wagtail_embed_videos.forms import get_embed_video_form, EmbedVideoInsertionForm
# from wagtail_embed_videos.formats import get_embed_video_format


def get_embed_video_json(embed_video):
    """
    helper function: given an embed video, return the json to pass back to the
    embed video chooser panel
    """
    if embed_video.thumbnail:
        preview_embed_video = embed_video.thumbnail.get_rendition('max-130x100').url
    else:
        preview_embed_video = detect_backend(embed_video.url).get_thumbnail_url()

    return json.dumps({
        'id': embed_video.id,
        'edit_link': reverse('wagtail_embed_videos_edit_embed_video', args=(embed_video.id,)),
        'title': embed_video.title,
        'preview': {
            'url': preview_embed_video,
        }
    })


def chooser(request):
    EmbedVideo = get_embed_video_model()

    if request.user.has_perm('wagtail_embed_videos.add_embedvideo'):
        can_add = True
    else:
        can_add = False

    q = None
    if 'q' in request.GET or 'p' in request.GET:
        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']

            # page number
            p = request.GET.get("p", 1)

            embed_videos = EmbedVideo.search(q, results_per_page=10, page=p)

            is_searching = True

        else:
            embed_videos = EmbedVideo.objects.order_by('-created_at')
            p = request.GET.get("p", 1)
            paginator = Paginator(embed_videos, 10)

            try:
                embed_videos = paginator.page(p)
            except PageNotAnInteger:
                embed_videos = paginator.page(1)
            except EmptyPage:
                embed_videos = paginator.page(paginator.num_pages)

            is_searching = False
        return render(request, "wagtail_embed_videos/chooser/results.html", {
            'embed_videos': embed_videos,
            'is_searching': is_searching,
            'can_add': can_add,
            'query_string': q,
        })
    else:
        searchform = SearchForm()

        embed_videos = EmbedVideo.objects.order_by('-created_at')
        p = request.GET.get("p", 1)
        paginator = Paginator(embed_videos, 10)

        try:
            embed_videos = paginator.page(p)
        except PageNotAnInteger:
            embed_videos = paginator.page(1)
        except EmptyPage:
            embed_videos = paginator.page(paginator.num_pages)
    print(can_add)
{'fraction_non_alphanumeric': 0.08402811835867255, 'fraction_numerical': 0.006702632009154814, 'mean_word_length': 2.757985257985258, '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': '5092590', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1855, 'n_words': 368}