# Path: hackday/common.py
# def common_env():
#     env = {
#         'nav_pages': get_nav_pages(),
#     }
#     return env
# 
# Path: hackday/blog/models.py
# class Entry(models.Model):
#     title = models.CharField('title of entry', max_length=255)
#     slug = models.SlugField('slugified title', db_index=True,
#             unique_for_date="pub_date")
#     content = models.TextField('entry content')
#     format = models.CharField(max_length=5, choices=FORMAT.CHOICES)
#     status = models.CharField(max_length=1, choices=STATUS.CHOICES)
# 
#     attachments = models.ManyToManyField(Attachment,
#             related_name="%(app_label)s_%(class)s_attachments", \
#             blank=True)
#     images = models.ManyToManyField(ImageAttachment,
#             related_name="%(app_label)s_%(class)s_images", \
#             blank=True)
#     links = models.ManyToManyField(Link,
#             related_name="%(app_label)s_%(class)s_links", \
#             blank=True)
# 
#     author = models.ForeignKey(User)
# 
#     categories = models.ManyToManyField(Category, blank=True)
#     tags = models.ManyToManyField(Tag, blank=True)
# 
#     create_date = models.DateTimeField('date created', auto_now_add=True,
#             editable=False)
#     mod_date = models.DateTimeField('date modified', auto_now=True,
#             editable=False)
#     pub_date = models.DateTimeField('date published', null=True, blank=True)
# 
#     def __unicode__(self):
#         return self.title
# 
#     def get_absolute_url(self):
#         return reverse('blog-entry', args=[self.id])
# 
#     def short_url(self):
#         return get_short_url(self.get_absolute_url())
# 
#     class Meta:
#         verbose_name_plural = "Entries"
# 
# class EntryForm(ModelForm):
# 
#     class Meta:
#         model = Entry
# 
# class STATUS(object):
#     DRAFT = 'D'
#     PUBLISHED = 'P'
#     REVIEW = 'R'
#     DELETED = 'X'
# 
#     CHOICES = (
#         (DRAFT, 'Draft'),
#         (REVIEW, 'Review'),
#         (PUBLISHED, 'Published'),
#         (DELETED, 'Deleted'),
#     )
# 
# class FORMAT(object):
#     HTML = 'H'
#     MARKDOWN = 'MD'
#     RESTRUCTURED_TEXT = 'RST'
# 
#     CHOICES = (
#         (MARKDOWN, 'Markdown'),
#         (RESTRUCTURED_TEXT, 'Restructured Text'),
#         (HTML, 'HTML'),
#     )


import json
from hackday.common import common_env
from hackday.blog.models import Entry, EntryForm, STATUS, FORMAT
from django.core import serializers
from django.http import HttpResponse
from django.template import Context, loader
from django.forms.models import modelformset_factory
from django.core.context_processors import csrf
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render, get_object_or_404
from django.template import RequestContext
    except PageNotAnInteger:
        entries = paginator.page(1)
    except EmptyPage:
        entries = paginator.page(paginator.num_pages)

    return entries


def index(request):
    all_entries = Entry.objects.filter(status=STATUS.PUBLISHED).\
            order_by('-pub_date')
    page = request.GET.get('page', 1)

    env = common_env()
    env['entries'] = _get_paginated_entries(all_entries, page, 5)
    env['formats'] = FORMAT

    if request.GET.get('format', '') == 'json':
        return HttpResponse(serializers.serialize('json', all_entries), mimetype="application/json")
    else:
        return render(request, 'blog/index.html', env)


def entry(request, entry_id):
    if request.user.has_perm('blog.add_entry'):
        entry = Entry.objects.get(pk=entry_id)
    else:
        entry = get_object_or_404(Entry, pk=entry_id, status=STATUS.PUBLISHED)
    env = common_env()
    env['entry'] = entry

# Path: hackday/common.py
# def common_env():
#     env = {
#         'nav_pages': get_nav_pages(),
#     }
#     return env
# 
# Path: hackday/blog/models.py
# class Entry(models.Model):
#     title = models.CharField('title of entry', max_length=255)
#     slug = models.SlugField('slugified title', db_index=True,
#             unique_for_date="pub_date")
#     content = models.TextField('entry content')
#     format = models.CharField(max_length=5, choices=FORMAT.CHOICES)
#     status = models.CharField(max_length=1, choices=STATUS.CHOICES)
# 
#     attachments = models.ManyToManyField(Attachment,
#             related_name="%(app_label)s_%(class)s_attachments", \
#             blank=True)
#     images = models.ManyToManyField(ImageAttachment,
#             related_name="%(app_label)s_%(class)s_images", \
#             blank=True)
#     links = models.ManyToManyField(Link,
#             related_name="%(app_label)s_%(class)s_links", \
#             blank=True)
# 
#     author = models.ForeignKey(User)
# 
#     categories = models.ManyToManyField(Category, blank=True)
#     tags = models.ManyToManyField(Tag, blank=True)
# 
#     create_date = models.DateTimeField('date created', auto_now_add=True,
#             editable=False)
#     mod_date = models.DateTimeField('date modified', auto_now=True,
#             editable=False)
#     pub_date = models.DateTimeField('date published', null=True, blank=True)
# 
#     def __unicode__(self):
#         return self.title
# 
#     def get_absolute_url(self):
#         return reverse('blog-entry', args=[self.id])
# 
#     def short_url(self):
#         return get_short_url(self.get_absolute_url())
# 
#     class Meta:
#         verbose_name_plural = "Entries"
# 
# class EntryForm(ModelForm):
# 
#     class Meta:
#         model = Entry
# 
# class STATUS(object):
#     DRAFT = 'D'
#     PUBLISHED = 'P'
#     REVIEW = 'R'
#     DELETED = 'X'
# 
#     CHOICES = (
#         (DRAFT, 'Draft'),
#         (REVIEW, 'Review'),
#         (PUBLISHED, 'Published'),
#         (DELETED, 'Deleted'),
#     )
# 
# class FORMAT(object):
#     HTML = 'H'
#     MARKDOWN = 'MD'
#     RESTRUCTURED_TEXT = 'RST'
# 
#     CHOICES = (
#         (MARKDOWN, 'Markdown'),
#         (RESTRUCTURED_TEXT, 'Restructured Text'),
#         (HTML, 'HTML'),
#     )


# Path: hackday/blog/views.py
import json
from hackday.common import common_env
from hackday.blog.models import Entry, EntryForm, STATUS, FORMAT
from django.core import serializers
from django.http import HttpResponse
from django.template import Context, loader
from django.forms.models import modelformset_factory
from django.core.context_processors import csrf
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import render, get_object_or_404
from django.template import RequestContext

    except PageNotAnInteger:
        entries = paginator.page(1)
    except EmptyPage:
        entries = paginator.page(paginator.num_pages)

    return entries


def index(request):
    all_entries = Entry.objects.filter(status=STATUS.PUBLISHED).\
            order_by('-pub_date')
    page = request.GET.get('page', 1)

    env = common_env()
    env['entries'] = _get_paginated_entries(all_entries, page, 5)
    env['formats'] = FORMAT

    if request.GET.get('format', '') == 'json':
        return HttpResponse(serializers.serialize('json', all_entries), mimetype="application/json")
    else:
        return render(request, 'blog/index.html', env)


def entry(request, entry_id):
    if request.user.has_perm('blog.add_entry'):
        entry = Entry.objects.get(pk=entry_id)
    else:
        entry = get_object_or_404(Entry, pk=entry_id, status=STATUS.PUBLISHED)
    env = common_env()
    env['entry'] = entry

    env['formats'] = FORMAT