<commit_before># -*- coding: utf-8
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
import json
import datetime


def home(request):
    if not request.user.is_authenticated():
        return redirect('/session/login')

    grills = Grill.objects.all().order_by('created_time')  # 나중에 업데이트 순서로 바꾸기?
    return render(request, 'grill/main.html',
                  {
                      'grills': grills,
                  })


def view_grill(request, grill_id):
    if not request.user.is_authenticated():
        return redirect('/session/login')

    grill = get_object_or_404(Grill, pk=grill_id)
    edit_form = CommentAddForm()
    comments = GrillComment.objects.filter(
        grill=grill_id).order_by('created_time').reverse()
    profile = get_object_or_404(UserProfile, user=request.user)
    user_vote = GrillCommentVote.objects.filter(userprofile=profile)
    for comment in comments:
        # WARNING! 사용자가 HTML 코드를 작성하면 그대로 반영
        comment.content = comment.replace_tags()
        comment.like = GrillCommentVote.objects.filter(
            grill_comment=comment,
            is_up=True).count()
        comment.hate = GrillCommentVote.objects.filter(
            grill_comment=comment,
            is_up=False).count()
        
        if user_vote.filter(grill_comment=comment):
            comment.vote_disable = True

    return render(request,
                  'grill/view.html',
                  {
                      'form': edit_form,
                      'grill_id': grill_id,
                      'comments': comments,
                      'grill': grill,
                  })


def add_grill(request):
    if not request.user.is_authenticated():
        return redirect('session/login')

    if request.method == "GET":
        edit_form = GrillAddForm()
    elif request.method == "POST":
        edit_form = GrillAddForm(request.POST)
        if edit_form.is_valid():
            userprofile = UserProfile.objects.get(user=request.user)
            new_grill = Grill(title=edit_form.cleaned_data['title'],
                              author=userprofile,
                              content=edit_form.cleaned_data['content'])
            new_grill.save()
            return redirect(new_grill.get_absolute_url())

    return render(request,
                  'grill/add_grill.html',
                  {
                      'form': edit_form,
                  })


def add_comment(request, grill_id):
    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="a-comment-of-list">'
    ms += '<div class="a-comment-area">'
    ms += ' <span>' + str(new_comment.order) + '</span>'
    ms += '<div class="a-comment-content">' + \
        new_comment.replace_tags().encode('utf8') + '</div>'
    ms += '<div class="a-comment-info">'
    ms += '<div class="a-comment-author-container">'
    ms += '<span>' + new_comment.author.nickname.encode('utf8') + '</span>'
    ms += '<span class="a-comment-date">' + \
        str(new_comment.created_time) + '</span>'
    ms += '</div>'
    ms += '<div class="a-comment-vote-container">'
    ms += '<button class="vote_up a-comment-vote"> 추천 (+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)
    # Update Comments
    new_index = request.POST['required_index']<commit_msg>minor bug fix : there was no vote_down button on new comments<commit_after># -*- coding: utf-8
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
import json
import datetime


def home(request):
    if not request.user.is_authenticated():
        return redirect('/session/login')

    grills = Grill.objects.all().order_by('created_time')  # 나중에 업데이트 순서로 바꾸기?
    return render(request, 'grill/main.html',
                  {
                      'grills': grills,
                  })


def view_grill(request, grill_id):
    if not request.user.is_authenticated():
        return redirect('/session/login')

    grill = get_object_or_404(Grill, pk=grill_id)
    edit_form = CommentAddForm()
    comments = GrillComment.objects.filter(
        grill=grill_id).order_by('created_time').reverse()
    profile = get_object_or_404(UserProfile, user=request.user)
    user_vote = GrillCommentVote.objects.filter(userprofile=profile)
    for comment in comments:
        # WARNING! 사용자가 HTML 코드를 작성하면 그대로 반영
        comment.content = comment.replace_tags()
        comment.like = GrillCommentVote.objects.filter(
            grill_comment=comment,
            is_up=True).count()
        comment.hate = GrillCommentVote.objects.filter(
            grill_comment=comment,
            is_up=False).count()

        if user_vote.filter(grill_comment=comment):
            comment.vote_disable = True

    return render(request,
                  'grill/view.html',
                  {
                      'form': edit_form,
                      'grill_id': grill_id,
                      'comments': comments,
                      'grill': grill,
                  })


def add_grill(request):
    if not request.user.is_authenticated():
        return redirect('session/login')

    if request.method == "GET":
        edit_form = GrillAddForm()
    elif request.method == "POST":
        edit_form = GrillAddForm(request.POST)
        if edit_form.is_valid():
            userprofile = UserProfile.objects.get(user=request.user)
            new_grill = Grill(title=edit_form.cleaned_data['title'],
                              author=userprofile,
                              content=edit_form.cleaned_data['content'])
            new_grill.save()
            return redirect(new_grill.get_absolute_url())

    return render(request,
                  'grill/add_grill.html',
                  {
                      'form': edit_form,
                  })


def add_comment(request, grill_id):
    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="a-comment-of-list">'
    ms += '<div class="a-comment-area">'
    ms += ' <span>' + str(new_comment.order) + '</span>'
    ms += '<div class="a-comment-content">' + \
        new_comment.replace_tags().encode('utf8') + '</div>'
    ms += '<div class="a-comment-info">'
    ms += '<div class="a-comment-author-container">'
    ms += '<span>' + new_comment.author.nickname.encode('utf8') + '</span>'
    ms += '<span class="a-comment-date">' + \
        str(new_comment.created_time) + '</span>'
    ms += '</div>'
    ms += '<div class="a-comment-vote-container">'
    ms += '<button class="vote_up a-comment-vote"> 추천 (+0) </button>'
    ms += '<button class="vote_down a-comment-vote"> 반대 (-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)
    # Update Comments
    new_index = request.POST['required_index']
{'fraction_non_alphanumeric': 0.12850550230741925, 'fraction_numerical': 0.004614838480653177, 'mean_word_length': 2.5319682407020476, 'pattern_counts': {'":': 4, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5281960', 'n_tokens_mistral': 2665, 'n_tokens_neox': 2618, 'n_words': 475}