<commit_before>import collections
import datetime

from django.contrib import messages
from django.contrib.admin.views.decorators import staff_member_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.urlresolvers import reverse
from django.db.models import Q, Count
from django.http import JsonResponse
from django.shortcuts import render, redirect
from django.utils.text import slugify
from django.views.decorators.http import require_POST

from books.api import CLIENT
from books.forms import NoteForm, SectionForm, ArtefactAuthorForm, BookForm, \
                        BookDetailsForm
from books.models import Book, Author, Note, NoteTag, Section, BookDetails
from bookmarker.forms import SearchFilterForm
from vocab.api import lookup_term
from vocab.forms import TermForm, TermOccurrenceForm
from vocab.models import Term, TermOccurrence


def home(request):
    books = Book.objects.filter(is_processed=False).order_by(
        'is_processed', 'completed_sections', '-pk'
    ).annotate(
        num_terms=Count('terms', distinct=True),
        num_notes=Count('notes', distinct=True),
    ).prefetch_related('details__default_authors')

    context = {
        'started_books': books.filter(
            completed_sections=True
        ),
        'new_books': books.filter(
            completed_sections=False,
            completed_read=True,
            details__isnull=False,
        ),
        'publications': books.filter(
            details__isnull=True
        ),
        'unread_books': books.filter(completed_read=False),
        'complete_books': Book.objects.filter(is_processed=True),
    }
    return render(request, 'home.html', context)


def view_complete(request):
    books = Book.objects.filter(is_processed=True).annotate(
        num_notes=Count('notes', distinct=True),
        num_terms=Count('terms', distinct=True),
    ).prefetch_related('details__default_authors').order_by('-pk')

    context = {
        'books': books,
    }
    return render(request, 'view_complete.html', context)


def view_book(request, slug):
    book = Book.objects.get(slug=slug)

    recent_terms = book.terms.order_by('-added')[:5].prefetch_related(
        'term', 'category', 'section'
    )
    recent_notes = book.notes.order_by('-added')[:5]

    sections = book.sections.all().prefetch_related(
        'authors', 'book__details__default_authors'
    ).annotate(
        num_terms=Count('terms', distinct=True),
        num_notes=Count('notes', distinct=True),
    )

    context = {
        'book': book,
        'details': book.details,
        'recent_terms': recent_terms,
        'recent_notes': recent_notes,
        'sections': sections,
    }
    return render(request, 'view_book.html', context)


def edit_book(request, slug):
    book = Book.objects.get(slug=slug)

    if request.method == 'POST':
        book_form = BookForm(request.POST, instance=book)

        if book.details:
            details_form = BookDetailsForm(request.POST, instance=book.details)
        else:
            details_form = None

        if book_form.is_valid() and (details_form is None or details_form.is_valid()):
            book_form.save()
            if details_form:
                details_form.save()

            messages.success(request, u'Edited book: {}'.format(book.title))
            return redirect(book)
        else:
            messages.error(request, 'Failed to edit book')<commit_msg>Add @staff_member_required to edit_book view<commit_after>import collections
import datetime

from django.contrib import messages
from django.contrib.admin.views.decorators import staff_member_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.urlresolvers import reverse
from django.db.models import Q, Count
from django.http import JsonResponse
from django.shortcuts import render, redirect
from django.utils.text import slugify
from django.views.decorators.http import require_POST

from books.api import CLIENT
from books.forms import NoteForm, SectionForm, ArtefactAuthorForm, BookForm, \
                        BookDetailsForm
from books.models import Book, Author, Note, NoteTag, Section, BookDetails
from bookmarker.forms import SearchFilterForm
from vocab.api import lookup_term
from vocab.forms import TermForm, TermOccurrenceForm
from vocab.models import Term, TermOccurrence


def home(request):
    books = Book.objects.filter(is_processed=False).order_by(
        'is_processed', 'completed_sections', '-pk'
    ).annotate(
        num_terms=Count('terms', distinct=True),
        num_notes=Count('notes', distinct=True),
    ).prefetch_related('details__default_authors')

    context = {
        'started_books': books.filter(
            completed_sections=True
        ),
        'new_books': books.filter(
            completed_sections=False,
            completed_read=True,
            details__isnull=False,
        ),
        'publications': books.filter(
            details__isnull=True
        ),
        'unread_books': books.filter(completed_read=False),
        'complete_books': Book.objects.filter(is_processed=True),
    }
    return render(request, 'home.html', context)


def view_complete(request):
    books = Book.objects.filter(is_processed=True).annotate(
        num_notes=Count('notes', distinct=True),
        num_terms=Count('terms', distinct=True),
    ).prefetch_related('details__default_authors').order_by('-pk')

    context = {
        'books': books,
    }
    return render(request, 'view_complete.html', context)


def view_book(request, slug):
    book = Book.objects.get(slug=slug)

    recent_terms = book.terms.order_by('-added')[:5].prefetch_related(
        'term', 'category', 'section'
    )
    recent_notes = book.notes.order_by('-added')[:5]

    sections = book.sections.all().prefetch_related(
        'authors', 'book__details__default_authors'
    ).annotate(
        num_terms=Count('terms', distinct=True),
        num_notes=Count('notes', distinct=True),
    )

    context = {
        'book': book,
        'details': book.details,
        'recent_terms': recent_terms,
        'recent_notes': recent_notes,
        'sections': sections,
    }
    return render(request, 'view_book.html', context)


@staff_member_required
def edit_book(request, slug):
    book = Book.objects.get(slug=slug)

    if request.method == 'POST':
        book_form = BookForm(request.POST, instance=book)

        if book.details:
            details_form = BookDetailsForm(request.POST, instance=book.details)
        else:
            details_form = None

        if book_form.is_valid() and (details_form is None or details_form.is_valid()):
            book_form.save()
            if details_form:
                details_form.save()

            messages.success(request, u'Edited book: {}'.format(book.title))
            return redirect(book)
        else:
            messages.error(request, 'Failed to edit book')
{'fraction_non_alphanumeric': 0.10755148741418764, 'fraction_numerical': 0.0005720823798627002, 'mean_word_length': 3.1924460431654675, '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': '4397068', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1974, 'n_words': 426}