<commit_before>from django.shortcuts import render
from django.http import HttpResponse, Http404, HttpResponseRedirect, HttpResponseNotFound
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.db.models import Q

from manuscript import ORDER_TITLES_FN as order_titles
from manuscript.models import Author, Title, Chapter, Page, Paragraph, SiteCopyText
from manuscript.models import compile_paragraphs


def all_works(request):	
	copy_text, created = SiteCopyText.objects.get_or_create_for('all_works')

	works_by_author = []

	for author in Author.objects.all():

		works = order_titles(Title.objects.filter(author=author))

		works_by_author.append({
			"author" : author,
			"works" :  works,
		})

	return render(request, 'manuscript/all-titles.html', {
		"works_by_author" : works_by_author,
		"copy_text" : copy_text,
	})


def chapters(request, title):
	copy_text, created = SiteCopyText.objects.get_or_create_for('chapters')

	try:
		focus_title = Title.objects.get(slug=title)
	except Title.DoesNotExist:
		raise Http404
		
	all_titles = order_titles(Title.objects.all())
	
	return render(request, 'manuscript/chapters.html', {
		"copy_text" : copy_text,
		"focus_title" : focus_title,
		"all_titles" : all_titles,
	})
	
def paragraphs(request, title, chapter):
	copy_text, created = SiteCopyText.objects.get_or_create_for('paragraphs')

	try:
		title = Title.objects.get(slug=title)
		chapter = title.chapter_set.get(slug=chapter)
	except Title.DoesNotExist, Chapter.DoesNotExist:
		raise Http404
	
	return render(request, 'manuscript/paragraphs.html', {
		"copy_text" : copy_text,
		"title" : title,
		"chapter" : chapter,
	})

def chapter(request, title, chapter):
	try:
		title = Title.objects.get(slug=title)
		chapter = title.chapter_set.get(slug=chapter)
		#page = chapter.paragraph_set.filter(~Q(split="bottom")).order_by('number')[0].page
	except Title.DoesNotExist, Chapter.DoesNotExist:
		raise Http404
	
	paragraphs = chapter.get_full_paragraphs()
	all_chapters = title.chapter_set.all()
	
	return render(request, 'manuscript/chapter.html', {
		"title" : title,
		"chapter" : chapter,
		"all_chapters" : all_chapters,
		"paragraphs" : paragraphs,
	})

def page(request, title, page):
	copy_text, created = SiteCopyText.objects.get_or_create_for('page')

	try:
		title = Title.objects.get(slug=title)
		page = Page.objects.get(title=title, number=page)
	except Title.DoesNotExist, Page.DoesNotExist:
		raise Http404

	paragraphs = page.paragraph_set.all().order_by('chapter__start_page_no','number')
	if paragraphs.count() > 0:
		focus_chapter = paragraphs.reverse()[0].chapter
		focus_chapter_first_paragraph = focus_chapter.paragraph_set.all().order_by('number')[0]
	else:
		focus_chapter = None
		focus_chapter_first_paragraph = None

	all_chapters = title.chapter_set.all()

	page_first = Page.objects.filter(title=page.title).order_by('number')[0]
	page_last = Page.objects.filter(title=page.title).order_by('-number')[0]
<commit_msg>Fixed invalid format for exception handling on elements not existing.<commit_after>from django.shortcuts import render
from django.http import HttpResponse, Http404, HttpResponseRedirect, HttpResponseNotFound
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.db.models import Q

from manuscript import ORDER_TITLES_FN as order_titles
from manuscript.models import Author, Title, Chapter, Page, Paragraph, SiteCopyText
from manuscript.models import compile_paragraphs


def all_works(request):	
	copy_text, created = SiteCopyText.objects.get_or_create_for('all_works')

	works_by_author = []

	for author in Author.objects.all():

		works = order_titles(Title.objects.filter(author=author))

		works_by_author.append({
			"author" : author,
			"works" :  works,
		})

	return render(request, 'manuscript/all-titles.html', {
		"works_by_author" : works_by_author,
		"copy_text" : copy_text,
	})


def chapters(request, title):
	copy_text, created = SiteCopyText.objects.get_or_create_for('chapters')

	try:
		focus_title = Title.objects.get(slug=title)
	except Title.DoesNotExist:
		raise Http404
		
	all_titles = order_titles(Title.objects.all())
	
	return render(request, 'manuscript/chapters.html', {
		"copy_text" : copy_text,
		"focus_title" : focus_title,
		"all_titles" : all_titles,
	})
	
def paragraphs(request, title, chapter):
	copy_text, created = SiteCopyText.objects.get_or_create_for('paragraphs')

	try:
		title = Title.objects.get(slug=title)
		chapter = title.chapter_set.get(slug=chapter)
	except Title.DoesNotExist:
		raise Http404
	except Chapter.DoesNotExist:
		raise Http404
	
	return render(request, 'manuscript/paragraphs.html', {
		"copy_text" : copy_text,
		"title" : title,
		"chapter" : chapter,
	})

def chapter(request, title, chapter):
	try:
		title = Title.objects.get(slug=title)
		chapter = title.chapter_set.get(slug=chapter)
		#page = chapter.paragraph_set.filter(~Q(split="bottom")).order_by('number')[0].page
	except Title.DoesNotExist:
		raise Http404
	except Chapter.DoesNotExist:
		raise Http404
	
	paragraphs = chapter.get_full_paragraphs()
	all_chapters = title.chapter_set.all()
	
	return render(request, 'manuscript/chapter.html', {
		"title" : title,
		"chapter" : chapter,
		"all_chapters" : all_chapters,
		"paragraphs" : paragraphs,
	})

def page(request, title, page):
	copy_text, created = SiteCopyText.objects.get_or_create_for('page')

	try:
		title = Title.objects.get(slug=title)
		page = Page.objects.get(title=title, number=page)
	except Title.DoesNotExist:
		raise Http404
	except Page.DoesNotExist:
		raise Http404		

	paragraphs = page.paragraph_set.all().order_by('chapter__start_page_no','number')
	if paragraphs.count() > 0:
		focus_chapter = paragraphs.reverse()[0].chapter
		focus_chapter_first_paragraph = focus_chapter.paragraph_set.all().order_by('number')[0]
	else:
		focus_chapter = None
		focus_chapter_first_paragraph = None

	all_chapters = title.chapter_set.all()

	page_first = Page.objects.filter(title=page.title).order_by('number')[0]
	page_last = Page.objects.filter(title=page.title).order_by('-number')[0]

{'fraction_non_alphanumeric': 0.11869098013676327, 'fraction_numerical': 0.008303484207098664, 'mean_word_length': 5.626752966558792, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6844660', 'n_tokens_mistral': 2243, 'n_tokens_neox': 2048, 'n_words': 388}