# Path: apps/grill/models.py
# class GrillComment(models.Model):
# 
#     def save(self, *args, **kwargs):
#         no = GrillComment.objects.filter(grill=self.grill).count()
#         if no:
#             self.order = no + 1
#         else:
#             self.order = 1
#         super(GrillComment, self).save(*args, **kwargs)
# 
#     grill = models.ForeignKey(Grill)
#     author = models.ForeignKey(UserProfile)
#     content = models.TextField(max_length=140)
#     created_time = models.DateTimeField(auto_now_add=True)
#     order = models.IntegerField(
#         db_index=True)
# 
#     def to_json(self):
#         return dict(
#             author=self.author.nickname,
#             content=self.replace_tags(),
#             created_time=self.created_time.isoformat(),
#             order=self.order)
# 
#     # return new contents with tags
#     # 태그를 <a>로 바꿔서 contents에 저장해둬야하나 아니면
#     # 매 번 comment를 부를 때 마다 만들어줘야하나
#     # 일단 두 번째 방법으로 구현
#     def replace_tags(self):
#         return re.sub(r'@(?P<target>\d+)',
#                       '<a href="#comment_\g<target>">@\g<target></a>',
#                       self.content)
# 
# class Grill(models.Model):
#     title = models.CharField(max_length=80)
#     created_time = models.DateTimeField(auto_now_add=True)
#     author = models.ForeignKey(UserProfile)
#     updated_time = models.DateTimeField(auto_now_add=True)
#     content = models.TextField()
# 
#     def get_absolute_url(self):
#         return reverse_lazy('view_grill', kwargs={'grill_id': self.id})
# 
# class GrillCommentVote(models.Model):
#     grill_comment = models.ForeignKey(GrillComment, null=False)
#     userprofile = models.ForeignKey(UserProfile)
#     is_up = models.BooleanField()
#     created_time = models.DateTimeField(auto_now_add=True, db_index=True)
# 
# Path: apps/session/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User,
#                                 primary_key=True, related_name='userprofile')
#     user = models.OneToOneField(User)
#     gender = models.CharField(max_length=1, choices=GENDER, default=ETC)
#     birthday = models.DateTimeField(blank=True, null=True)
# 
#     nickname = models.TextField(max_length=12)
#     points = models.IntegerField(default=0)
# 
#     def __str__(self):
#         return "User %s (%s)'s profile object" % (self.user, self.nickname)
# 
# Path: apps/grill/forms.py
# class GrillAddForm(forms.ModelForm):
# 
#     class Meta:
#         model = Grill
#         fields = "__all__"
#         exclude = ["commented_at", "author"]
# 
# class CommentAddForm(forms.ModelForm):
# 
#     class Meta:
#         model = GrillComment
#         fields = ["content"]


from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse
from .models import GrillComment, Grill, GrillCommentVote
from apps.session.models import UserProfile
from .forms import GrillAddForm, CommentAddForm
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models import Count
from django.contrib.auth.decorators import login_required
import json
import datetime
    post_data = request.POST
    grill = get_object_or_404(Grill, pk=grill_id)
    userprofile = UserProfile.objects.get(user=request.user)
    new_comment = GrillComment(
        grill=grill, author=userprofile, content=post_data.get('new_content'))
    new_comment.save()
    new_comment.grill.updated_time = datetime.datetime.now()
    new_comment.grill.save()

    ms = '<li id="comment-' + str(new_comment.order) +\
         '" class="list-group-item">'
    ms += '<div>'
    ms += '<div class="comment-meta">'
    ms += '<span class="col-md-1">' + str(new_comment.order) + '</span>'
    ms += '<strong>' + new_comment.author.nickname.encode('utf-8')
    ms += '</strong>'
    ms += '<span class="pull-right">' + str(new_comment.created_time)
    ms += '</span></div>'
    ms += '<div class="comment-content-container">'
    ms += '<span>' + new_comment.replace_tags().encode('utf8') + '</span>'
    ms += '<div class="comment-vote-container">'
    ms += '<button class="vote-up btn btn-default"> 추천 (+0) </button>'
    ms += '<button class="vote-down btn btn-default"> 반대 (-0)</button>'
    ms += '</div></div></div></li>'
    data = {'html': ms}
    return HttpResponse(json.dumps(data, cls=DjangoJSONEncoder),
                        content_type="application/json")


def refresh_comment(request, grill_id):

# Path: apps/grill/models.py
# class GrillComment(models.Model):
# 
#     def save(self, *args, **kwargs):
#         no = GrillComment.objects.filter(grill=self.grill).count()
#         if no:
#             self.order = no + 1
#         else:
#             self.order = 1
#         super(GrillComment, self).save(*args, **kwargs)
# 
#     grill = models.ForeignKey(Grill)
#     author = models.ForeignKey(UserProfile)
#     content = models.TextField(max_length=140)
#     created_time = models.DateTimeField(auto_now_add=True)
#     order = models.IntegerField(
#         db_index=True)
# 
#     def to_json(self):
#         return dict(
#             author=self.author.nickname,
#             content=self.replace_tags(),
#             created_time=self.created_time.isoformat(),
#             order=self.order)
# 
#     # return new contents with tags
#     # 태그를 <a>로 바꿔서 contents에 저장해둬야하나 아니면
#     # 매 번 comment를 부를 때 마다 만들어줘야하나
#     # 일단 두 번째 방법으로 구현
#     def replace_tags(self):
#         return re.sub(r'@(?P<target>\d+)',
#                       '<a href="#comment_\g<target>">@\g<target></a>',
#                       self.content)
# 
# class Grill(models.Model):
#     title = models.CharField(max_length=80)
#     created_time = models.DateTimeField(auto_now_add=True)
#     author = models.ForeignKey(UserProfile)
#     updated_time = models.DateTimeField(auto_now_add=True)
#     content = models.TextField()
# 
#     def get_absolute_url(self):
#         return reverse_lazy('view_grill', kwargs={'grill_id': self.id})
# 
# class GrillCommentVote(models.Model):
#     grill_comment = models.ForeignKey(GrillComment, null=False)
#     userprofile = models.ForeignKey(UserProfile)
#     is_up = models.BooleanField()
#     created_time = models.DateTimeField(auto_now_add=True, db_index=True)
# 
# Path: apps/session/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User,
#                                 primary_key=True, related_name='userprofile')
#     user = models.OneToOneField(User)
#     gender = models.CharField(max_length=1, choices=GENDER, default=ETC)
#     birthday = models.DateTimeField(blank=True, null=True)
# 
#     nickname = models.TextField(max_length=12)
#     points = models.IntegerField(default=0)
# 
#     def __str__(self):
#         return "User %s (%s)'s profile object" % (self.user, self.nickname)
# 
# Path: apps/grill/forms.py
# class GrillAddForm(forms.ModelForm):
# 
#     class Meta:
#         model = Grill
#         fields = "__all__"
#         exclude = ["commented_at", "author"]
# 
# class CommentAddForm(forms.ModelForm):
# 
#     class Meta:
#         model = GrillComment
#         fields = ["content"]


# Path: apps/grill/views.py
from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse
from .models import GrillComment, Grill, GrillCommentVote
from apps.session.models import UserProfile
from .forms import GrillAddForm, CommentAddForm
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models import Count
from django.contrib.auth.decorators import login_required
import json
import datetime

    post_data = request.POST
    grill = get_object_or_404(Grill, pk=grill_id)
    userprofile = UserProfile.objects.get(user=request.user)
    new_comment = GrillComment(
        grill=grill, author=userprofile, content=post_data.get('new_content'))
    new_comment.save()
    new_comment.grill.updated_time = datetime.datetime.now()
    new_comment.grill.save()

    ms = '<li id="comment-' + str(new_comment.order) +\
         '" class="list-group-item">'
    ms += '<div>'
    ms += '<div class="comment-meta">'
    ms += '<span class="col-md-1">' + str(new_comment.order) + '</span>'
    ms += '<strong>' + new_comment.author.nickname.encode('utf-8')
    ms += '</strong>'
    ms += '<span class="pull-right">' + str(new_comment.created_time)
    ms += '</span></div>'
    ms += '<div class="comment-content-container">'
    ms += '<span>' + new_comment.replace_tags().encode('utf8') + '</span>'
    ms += '<div class="comment-vote-container">'
    ms += '<button class="vote-up btn btn-default"> 추천 (+0) </button>'
    ms += '<button class="vote-down btn btn-default"> 반대 (-0)</button>'
    ms += '</div></div></div></li>'
    data = {'html': ms}
    return HttpResponse(json.dumps(data, cls=DjangoJSONEncoder),
                        content_type="application/json")


def refresh_comment(request, grill_id):

    grill = get_object_or_404(Grill, pk=grill_id)