
from ... import db
from ...mongo import TranslationsField



class Category(db.Document):
    rank = db.IntField()
    slug = db.StringField()
    parent = db.ReferenceField('self', reverse_delete_rule=db.DENY)
    name = TranslationsField()
    description = TranslationsField()

    meta = {
        'ordering': ['rank']
    }


from ... import db, get_locale
from ...mongo import TranslationsField



class Category(db.Document):
    rank = db.IntField()
    slug = db.StringField()
    parent = db.ReferenceField('self', reverse_delete_rule=db.DENY)
    name = TranslationsField()
    description = TranslationsField()

    meta = {
        'ordering': ['rank']
    }

    @classmethod
    def ordered_all(self):
        catlist = []
        def add_cats(categories, level=0):
            for cat in categories:
                cat.level = level
                catlist.append(cat)
                add_cats(cat.children, level+1)
        add_cats(self.objects(parent=None))
        return catlist

    @property
    def children(self):
        return Category.objects(parent=self)

    @property
    def products(self):
        from .product import Product
        return Product.objects(category=self).count()

    @property
    def full_name(self):
        name = self.name.get(get_locale(), '')
        if self.parent:
            return u'{} » {}'.format(self.parent.full_name, name)
        else:
            return name


Make categories easier to request
Make categories easier to request

  
- from ... import db
+ from ... import db, get_locale
  from ...mongo import TranslationsField
  
  
  
  class Category(db.Document):
      rank = db.IntField()
      slug = db.StringField()
      parent = db.ReferenceField('self', reverse_delete_rule=db.DENY)
      name = TranslationsField()
      description = TranslationsField()
  
      meta = {
          'ordering': ['rank']
      }
  
+     @classmethod
+     def ordered_all(self):
+         catlist = []
+         def add_cats(categories, level=0):
+             for cat in categories:
+                 cat.level = level
+                 catlist.append(cat)
+                 add_cats(cat.children, level+1)
+         add_cats(self.objects(parent=None))
+         return catlist
+ 
+     @property
+     def children(self):
+         return Category.objects(parent=self)
+ 
+     @property
+     def products(self):
+         from .product import Product
+         return Product.objects(category=self).count()
+ 
+     @property
+     def full_name(self):
+         name = self.name.get(get_locale(), '')
+         if self.parent:
+             return u'{} » {}'.format(self.parent.full_name, name)
+         else:
+             return name
+ 
+ 
Make categories easier to request
## Code Before:

from ... import db
from ...mongo import TranslationsField



class Category(db.Document):
    rank = db.IntField()
    slug = db.StringField()
    parent = db.ReferenceField('self', reverse_delete_rule=db.DENY)
    name = TranslationsField()
    description = TranslationsField()

    meta = {
        'ordering': ['rank']
    }

## Instruction:
Make categories easier to request
## Code After:

from ... import db, get_locale
from ...mongo import TranslationsField



class Category(db.Document):
    rank = db.IntField()
    slug = db.StringField()
    parent = db.ReferenceField('self', reverse_delete_rule=db.DENY)
    name = TranslationsField()
    description = TranslationsField()

    meta = {
        'ordering': ['rank']
    }

    @classmethod
    def ordered_all(self):
        catlist = []
        def add_cats(categories, level=0):
            for cat in categories:
                cat.level = level
                catlist.append(cat)
                add_cats(cat.children, level+1)
        add_cats(self.objects(parent=None))
        return catlist

    @property
    def children(self):
        return Category.objects(parent=self)

    @property
    def products(self):
        from .product import Product
        return Product.objects(category=self).count()

    @property
    def full_name(self):
        name = self.name.get(get_locale(), '')
        if self.parent:
            return u'{} » {}'.format(self.parent.full_name, name)
        else:
            return name

