from django.db import models, connection


class Category(models.Model):
    id = models.AutoField(primary_key=True)
    parent = models.ForeignKey('self')
    name = models.CharField(max_length=20)

    def __init__(self, *args, **kwargs):
        super(Category, self).__init__(*args, **kwargs)
        self.children = []

    def __str__(self):
        return self.name

    @classmethod
    def sorted_category(cls):
        return_category = list(cls.objects.all())
        childList = []
        for category in return_category:
            if (category.parent.id == 1):
                continue
            else:
                parent_index = return_category.index(category.parent)
                return_category[parent_index].children.append(category)
                childList.append(category)

        for child in childList:
            return_category.remove(child)

        return return_category

    @classmethod
    def init_category(cls):
        is_empty = cls.objects.count() == 0
        if is_empty:
            category_cursor = connection.cursor()
            try:
                category_cursor.execute('INSERT INTO jellyblog_category (name, parent_id) VALUES ("Home", 1)')
            finally:
                category_cursor.close()


class Document(models.Model):
    # 문서 Model
    def __str__(self):
        return self.title

    id = models.AutoField(primary_key=True)
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=100)
    content = models.TextField()
    time = models.DateTimeField(auto_now_add=True)
    meta_tag = models.CharField(max_length=150)
    view_count = models.IntegerField(default=0, editable=False)
    public_doc = models.BooleanField()
    update_time = models.DateTimeField(auto_now=True)

    def read(self):
        self.view_count += 1
        self.save()

    @models.permalink
    def get_absolute_url(self):
        return 'detail', (), {'document_id': self.id}


class Note(models.Model):
    # 노트 Model
    def __str__(self):
        return self.content

    id = models.AutoField(primary_key=True)
    content = models.CharField(max_length=300)
    time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

@@ -1032,25 +1032,12 @@
     
-category_cursor =
+with
  con
@@ -1056,24 +1056,14 @@
 or()
-%0A            try
+ as cs
 :%0A  
@@ -1077,30 +1077,17 @@
        c
-ategory_cursor
+s
 .execute
@@ -1162,69 +1162,8 @@
 )')%0A
-            finally:%0A                category_cursor.close()%0A
 %0A%0Acl

update init category logic