<commit_before>from django.shortcuts import render
from django.shortcuts import redirect
from django.http import HttpResponse, HttpResponseNotFound
from NamuMarkParser import NamuMarkParser
from bs4 import BeautifulSoup
from .models import *
from django.core.exceptions import ObjectDoesNotExist
from django.db import IntegrityError
import LocalSettings

# Create your views here.

def edit(request, title=None, section=0):
    if request.method == 'GET':
        if title == None:
            return redirect('/')
    
        if 'section' in request.GET:
            section = int(request.GET['section'])
        
        try:
            page_id = Page.objects.get(title=title).id
        except ObjectDoesNotExist:
            return render(request, 'edit.html', {'title': title, 'text': "", 'preview': "", 'section': 0})
        else:
            text = Revision.objects.filter(page=page_id).order_by('-id').first().text
            
        if section > 0:
            toc = NamuMarkParser(text, title).get_toc()
            try:
                text = toc[section - 1][3]
            except IndexError:
                section = 0
        
        return render(request, 'edit.html', {'title': title, 'text': text, 'preview': "", 'section': section})
        
    elif request.method == 'POST':
        # 미리보기
        if 'preview' in request.POST:
            soup = BeautifulSoup(NamuMarkParser(request.POST['text'], title).parse(), "html.parser")
            return render(request, 'edit.html', {'title': title, 'text': request.POST['text'], 'preview': soup.prettify()})
    
        # 저장
        try:
            Page(title=title, namespace=0, is_created=True).save()
        except IntegrityError:
            page = Page.objects.get(title=title)<commit_msg>Support save with namespace. Close #31<commit_after>from django.shortcuts import render
from django.shortcuts import redirect
from django.http import HttpResponse, HttpResponseNotFound
from NamuMarkParser import NamuMarkParser
from bs4 import BeautifulSoup
from .models import *
from django.core.exceptions import ObjectDoesNotExist
from django.db import IntegrityError
import LocalSettings

# Create your views here.

def edit(request, title=None, section=0):
    if request.method == 'GET':
        if title == None:
            return redirect('/')
    
        if 'section' in request.GET:
            section = int(request.GET['section'])
        
        try:
            page_id = Page.objects.get(title=title).id
        except ObjectDoesNotExist:
            return render(request, 'edit.html', {'title': title, 'text': "", 'preview': "", 'section': 0})
        else:
            text = Revision.objects.filter(page=page_id).order_by('-id').first().text
            
        if section > 0:
            toc = NamuMarkParser(text, title).get_toc()
            try:
                text = toc[section - 1][3]
            except IndexError:
                section = 0
        
        return render(request, 'edit.html', {'title': title, 'text': text, 'preview': "", 'section': section})
        
    elif request.method == 'POST':
        # 미리보기
        if 'preview' in request.POST:
            soup = BeautifulSoup(NamuMarkParser(request.POST['text'], title).parse(), "html.parser")
            return render(request, 'edit.html', {'title': title, 'text': request.POST['text'], 'preview': soup.prettify()})
    
        # 저장
        if title.startswith('DuckPy:'):
            namespace = 1
        elif title.startswith('파일:'):
            namespace = 3
        elif title.startswith('분류:'):
            namespace = 4
        elif title.startswith(LocalSettings.project_name + ':'):
            namespace = 5
        else:
            namespace = 0
        
        try:
            Page(title=title, namespace=namespace, is_created=True).save()
        except IntegrityError:
            page = Page.objects.get(title=title)
{'fraction_non_alphanumeric': 0.12291933418693982, 'fraction_numerical': 0.005633802816901409, 'mean_word_length': 2.324255319148936, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2433425', 'n_tokens_mistral': 1100, 'n_tokens_neox': 1086, 'n_words': 277}