# Path: bamboo/models.py
# class Post(models.Model):
#     # author = models.ForeignKey('auth.User', null=True)
#     writer = models.CharField(max_length=100)
#     title = models.CharField(max_length=200)
#     text = models.TextField()
# #    created_date = models.DateTimeField(default=timezone.now)
#     published_date = models.DateTimeField(blank=True, null=True)
#     category = models.ForeignKey(Category, default=1, blank=True)
#     post_password = models.CharField(verbose_name=u'password', max_length=100, default='', help_text="글을 수정하거나 지울 때 사용할 비밀번호를 적어주세요")
# 
#     def publish(self):
#         self.published_date = timezone.now()
#         self.save()
# 
#     def __str__(self):
#         return self.title
# 
# class Category(models.Model):
# #    class Meta:
# #        ordering = ['name']
#     name = models.CharField(verbose_name=u'이름', max_length=50)
#     slug = models.SlugField(unique=True, null=True, blank=True)
#     description = models.TextField(verbose_name=u'말머리 설명', blank=True)
# 
#     def __str__(self):
#         return self.name
# 
# Path: bamboo/forms.py
# class PostForm(forms.ModelForm):
#     post_password = forms.CharField(widget=forms.PasswordInput)
# 
#     class Meta:
#         model = Post
#         fields = ('category', 'writer', 'post_password', 'title', 'text', )
# 
# class CommentForm(forms.ModelForm):
# 
#     class Meta:
#         model = Comment
#         fields = ('author', 'text', )
# 
# class PasswordCheckForm(forms.Form):
#     password = forms.CharField(widget=forms.PasswordInput)


from django.shortcuts import render, redirect, get_object_or_404
from django.utils import timezone
from .models import Post, Category
from .forms import PostForm, CommentForm, PasswordCheckForm

    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            # post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm(instance=post)
    return render(request, 'bamboo/post_edit.html', {'form': form})


def post_remove(request, pk):
    post = get_object_or_404(Post, pk=pk)
    post.delete()
    return redirect('post_list')


def add_comment_to_post(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.save()
            return redirect('post_detail', pk=post.pk)
    else:

# Path: bamboo/models.py
# class Post(models.Model):
#     # author = models.ForeignKey('auth.User', null=True)
#     writer = models.CharField(max_length=100)
#     title = models.CharField(max_length=200)
#     text = models.TextField()
# #    created_date = models.DateTimeField(default=timezone.now)
#     published_date = models.DateTimeField(blank=True, null=True)
#     category = models.ForeignKey(Category, default=1, blank=True)
#     post_password = models.CharField(verbose_name=u'password', max_length=100, default='', help_text="글을 수정하거나 지울 때 사용할 비밀번호를 적어주세요")
# 
#     def publish(self):
#         self.published_date = timezone.now()
#         self.save()
# 
#     def __str__(self):
#         return self.title
# 
# class Category(models.Model):
# #    class Meta:
# #        ordering = ['name']
#     name = models.CharField(verbose_name=u'이름', max_length=50)
#     slug = models.SlugField(unique=True, null=True, blank=True)
#     description = models.TextField(verbose_name=u'말머리 설명', blank=True)
# 
#     def __str__(self):
#         return self.name
# 
# Path: bamboo/forms.py
# class PostForm(forms.ModelForm):
#     post_password = forms.CharField(widget=forms.PasswordInput)
# 
#     class Meta:
#         model = Post
#         fields = ('category', 'writer', 'post_password', 'title', 'text', )
# 
# class CommentForm(forms.ModelForm):
# 
#     class Meta:
#         model = Comment
#         fields = ('author', 'text', )
# 
# class PasswordCheckForm(forms.Form):
#     password = forms.CharField(widget=forms.PasswordInput)


# Path: bamboo/views.py
from django.shortcuts import render, redirect, get_object_or_404
from django.utils import timezone
from .models import Post, Category
from .forms import PostForm, CommentForm, PasswordCheckForm

    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            # post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm(instance=post)
    return render(request, 'bamboo/post_edit.html', {'form': form})


def post_remove(request, pk):
    post = get_object_or_404(Post, pk=pk)
    post.delete()
    return redirect('post_list')


def add_comment_to_post(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.save()
            return redirect('post_detail', pk=post.pk)
    else:

        form = CommentForm()
