# Path: Blog/posts/models.py
# class Post(models.Model):
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
#     title = models.CharField(max_length=120)
#     slug = models.SlugField(unique=True, null=True)
#     image = models.ImageField(null=True, blank=True,
#                               upload_to=upload_location,
#                               width_field="width_field",
#                               height_field="height_field")
#     width_field = models.IntegerField(default=0)
#     height_field = models.IntegerField(default=0)
#     content = models.TextField()
#     draft = models.BooleanField(default=False)
#     published = models.DateField(auto_now=False, auto_now_add=False)
#     updated = models.DateTimeField(auto_now=True, auto_now_add=False)  # auto_now triggered when model.save() called
#     timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)  # auto_now_add triggered when object first created
#     num_chinese = models.IntegerField(default=0)
#     num_english = models.IntegerField(default=0)
#     objects = ActivePostManager()
# 
#     def __str__(self):  # __unicode__ for py2
#         return self.title
# 
#     def get_absolute_url(self):
#         return reverse("posts:detail", kwargs={"slug": self.slug})
# 
#     def get_api_url(self):
#         return reverse("posts-api:detail", kwargs={"slug": self.slug})
# 
#     def get_markdown(self):
#         content = self.content
#         return mark_safe(markdown(content))
# 
#     @property
#     def comments(self):
#         from comments.models import Comment
#         instance = self
#         return Comment.objects.filter_by_instance(instance)
# 
#     @property
#     def get_content_type(self):
#         instance = self
#         content_type = ContentType.objects.get_for_model(instance.__class__)
#         return content_type
# 
#     class Meta:
#         ordering= ['-published', '-updated']
# 
# Path: Blog/posts/forms.py
# class PostForm(forms.ModelForm):
#     content = forms.CharField(widget=PagedownWidget(show_preview=False))
#     published = forms.DateField(widget=forms.SelectDateWidget(years=range(1985, datetime.date.today().year+10)))
#     class Meta:
#         model = Post
#         fields = [
#             "title",
#             "image",
#             'draft',
#             'published',
#             "content"
#         ]


from urllib.parse import quote_plus
from django.http import HttpResponseRedirect, Http404, HttpResponseForbidden
from django.shortcuts import render, get_object_or_404, redirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.auth.models import AnonymousUser
from django.contrib import messages
from django.utils import timezone
from django.db.models import Q
from django.contrib.contenttypes.models import ContentType
from .models import Post
from .forms import PostForm
from comments.forms import CommentForm
from comments.models import Comment
        # If page is out of range (e.g. 9999), deliver last page of results.
        posts = paginator.page(paginator.num_pages)
    context = {
        "title": "Borg's Blog",
        "object_list": posts,
        "today": timezone.now().date()
    }
    return render(request, "posts/post_list.html", context)

def post_update(request, slug):
    if not request.user.is_staff and not request.user.is_superuser:
        raise Http404
    obj = get_object_or_404(Post, slug=slug)
    form = PostForm(request.POST or None, request.FILES or None, instance=obj)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
        messages.success(request, "Saved!")
        return HttpResponseRedirect(instance.get_absolute_url())

    context = {
        'title': obj.title,
        'instance': obj,
        'form': form
    }
    return render(request,"posts/post_form.html",context)

def post_delete(request, slug):
    if not request.user.is_staff and not request.user.is_superuser:
        raise Http404

# Path: Blog/posts/models.py
# class Post(models.Model):
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
#     title = models.CharField(max_length=120)
#     slug = models.SlugField(unique=True, null=True)
#     image = models.ImageField(null=True, blank=True,
#                               upload_to=upload_location,
#                               width_field="width_field",
#                               height_field="height_field")
#     width_field = models.IntegerField(default=0)
#     height_field = models.IntegerField(default=0)
#     content = models.TextField()
#     draft = models.BooleanField(default=False)
#     published = models.DateField(auto_now=False, auto_now_add=False)
#     updated = models.DateTimeField(auto_now=True, auto_now_add=False)  # auto_now triggered when model.save() called
#     timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)  # auto_now_add triggered when object first created
#     num_chinese = models.IntegerField(default=0)
#     num_english = models.IntegerField(default=0)
#     objects = ActivePostManager()
# 
#     def __str__(self):  # __unicode__ for py2
#         return self.title
# 
#     def get_absolute_url(self):
#         return reverse("posts:detail", kwargs={"slug": self.slug})
# 
#     def get_api_url(self):
#         return reverse("posts-api:detail", kwargs={"slug": self.slug})
# 
#     def get_markdown(self):
#         content = self.content
#         return mark_safe(markdown(content))
# 
#     @property
#     def comments(self):
#         from comments.models import Comment
#         instance = self
#         return Comment.objects.filter_by_instance(instance)
# 
#     @property
#     def get_content_type(self):
#         instance = self
#         content_type = ContentType.objects.get_for_model(instance.__class__)
#         return content_type
# 
#     class Meta:
#         ordering= ['-published', '-updated']
# 
# Path: Blog/posts/forms.py
# class PostForm(forms.ModelForm):
#     content = forms.CharField(widget=PagedownWidget(show_preview=False))
#     published = forms.DateField(widget=forms.SelectDateWidget(years=range(1985, datetime.date.today().year+10)))
#     class Meta:
#         model = Post
#         fields = [
#             "title",
#             "image",
#             'draft',
#             'published',
#             "content"
#         ]


# Path: Blog/posts/views.py
from urllib.parse import quote_plus
from django.http import HttpResponseRedirect, Http404, HttpResponseForbidden
from django.shortcuts import render, get_object_or_404, redirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.auth.models import AnonymousUser
from django.contrib import messages
from django.utils import timezone
from django.db.models import Q
from django.contrib.contenttypes.models import ContentType
from .models import Post
from .forms import PostForm
from comments.forms import CommentForm
from comments.models import Comment

        # If page is out of range (e.g. 9999), deliver last page of results.
        posts = paginator.page(paginator.num_pages)
    context = {
        "title": "Borg's Blog",
        "object_list": posts,
        "today": timezone.now().date()
    }
    return render(request, "posts/post_list.html", context)

def post_update(request, slug):
    if not request.user.is_staff and not request.user.is_superuser:
        raise Http404
    obj = get_object_or_404(Post, slug=slug)
    form = PostForm(request.POST or None, request.FILES or None, instance=obj)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
        messages.success(request, "Saved!")
        return HttpResponseRedirect(instance.get_absolute_url())

    context = {
        'title': obj.title,
        'instance': obj,
        'form': form
    }
    return render(request,"posts/post_form.html",context)

def post_delete(request, slug):
    if not request.user.is_staff and not request.user.is_superuser:
        raise Http404

    obj = get_object_or_404(Post, slug=slug)