# Path: blogapp/models.py
# class Post(models.Model):
# 
#     author = models.ForeignKey('auth.User')
#     title = models.CharField(max_length=400)
#     text = models.TextField()
# 
#     created_date = models.DateTimeField(default=timezone.now)
#     published_date = models.DateTimeField(blank=True, null=True)
#     tags = TaggableManager()
# 
#     def publish(self):
#         self.published_date = timezone.now()
#         self.save()
# 
#     def __unicode__(self):
#         return self.title
# 
# Path: blogapp/forms.py
# class PostForm(forms.ModelForm):
#     #text = forms.CharField(widget=MarkdownEditor())
# 
#     class Meta:
#         model = Post
#         widgets = {
#           #'text': forms.Textarea(attrs={'rows':40, 'cols':50}),
#           'text': MarkdownEditor(attrs={'id': 'title',
#                                         'width': "80%",
#                                         'height': 400,
#                                         'locale': None,
#                                         'boostrap_cdn': True,
#                                         'autofocus': True,
#                                         'resize': 'both',
#                                         'icon': 'glyph',
#                                         'footer': '> Powered by http://www.codingdrama.com/bootstrap-markdown',
#                                         'fullscreen': True,
#                                     }),
#           'title': forms.TextInput(attrs={'size': 80, 'title': 'Title',})
#         }
#         fields = ('title', 'text',)


from django.shortcuts import render,render_to_response
from django.utils import timezone
from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic.dates import MonthArchiveView
from .models import Post
from .forms import PostForm
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.http import HttpResponseRedirect
    nav = nav + "Page {} of {}".format(ps.number,ps.paginator.num_pages)
    if ps.has_next():
        nav=nav+" | [next >](?page={})".format(ps.next_page_number())
    nav=nav+'</nav>'

    return render(request, 'blogapp/index.html', {'ps': ps,'nav': nav})

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blogapp/post_detail.html', {'post': post})

@login_required
def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            return redirect('blogapp.views.post_detail', pk=post.pk)
    else:
        form = PostForm()
    return render(request, 'blogapp/post_edit.html', {'form': form})

@login_required
def post_edit(request, pk):
    #print "pk: ",pk
    post = get_object_or_404(Post, pk=pk)
    #print "post: ",post
    if request.method == "POST":

# Path: blogapp/models.py
# class Post(models.Model):
# 
#     author = models.ForeignKey('auth.User')
#     title = models.CharField(max_length=400)
#     text = models.TextField()
# 
#     created_date = models.DateTimeField(default=timezone.now)
#     published_date = models.DateTimeField(blank=True, null=True)
#     tags = TaggableManager()
# 
#     def publish(self):
#         self.published_date = timezone.now()
#         self.save()
# 
#     def __unicode__(self):
#         return self.title
# 
# Path: blogapp/forms.py
# class PostForm(forms.ModelForm):
#     #text = forms.CharField(widget=MarkdownEditor())
# 
#     class Meta:
#         model = Post
#         widgets = {
#           #'text': forms.Textarea(attrs={'rows':40, 'cols':50}),
#           'text': MarkdownEditor(attrs={'id': 'title',
#                                         'width': "80%",
#                                         'height': 400,
#                                         'locale': None,
#                                         'boostrap_cdn': True,
#                                         'autofocus': True,
#                                         'resize': 'both',
#                                         'icon': 'glyph',
#                                         'footer': '> Powered by http://www.codingdrama.com/bootstrap-markdown',
#                                         'fullscreen': True,
#                                     }),
#           'title': forms.TextInput(attrs={'size': 80, 'title': 'Title',})
#         }
#         fields = ('title', 'text',)


# Path: blogapp/views.py
from django.shortcuts import render,render_to_response
from django.utils import timezone
from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic.dates import MonthArchiveView
from .models import Post
from .forms import PostForm
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.http import HttpResponseRedirect

    nav = nav + "Page {} of {}".format(ps.number,ps.paginator.num_pages)
    if ps.has_next():
        nav=nav+" | [next >](?page={})".format(ps.next_page_number())
    nav=nav+'</nav>'

    return render(request, 'blogapp/index.html', {'ps': ps,'nav': nav})

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blogapp/post_detail.html', {'post': post})

@login_required
def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            return redirect('blogapp.views.post_detail', pk=post.pk)
    else:
        form = PostForm()
    return render(request, 'blogapp/post_edit.html', {'form': form})

@login_required
def post_edit(request, pk):
    #print "pk: ",pk
    post = get_object_or_404(Post, pk=pk)
    #print "post: ",post
    if request.method == "POST":

        form = PostForm(request.POST, instance=post)