# Path: models/user.py
# class User(Document, UserMixin):
#     username = StringField(max_length=200)
#     password = StringField(max_length=200, default='')
#     name = StringField(max_length=100)
#     email = EmailField()
#     active = BooleanField(default=True)
#     admin = BooleanField(default=False)
#     last_login = DateTimeField(default=datetime.datetime.now)
#     skipped = IntField(default=0)
#     processed = IntField(default=0)
#     tags = ListField(StringField(), default=[])
#     tags_count = IntField(default=0, db_field='tagsCount')
# 
#     def is_active(self):
#         return self.active
# 
#     def is_admin(self):
#         return self.admin or False
# 
#     def __unicode__(self):
#         return self.username


from collections import Counter
from mongoengine import (
    StringField, IntField, DateTimeField, ListField, BooleanField,
    ReferenceField, EmbeddedDocument, EmbeddedDocumentField, FloatField,
    CASCADE, QuerySet)
from flask_mongoengine import Document
from .user import User
import datetime
import random
import jinja2



class TagsQS(QuerySet):
    _synonyms_cache = None
    def get_base_tags(self, order_by_category=False):
        qs = self.filter(is_base=True)
        if order_by_category:
            return qs.order_by("category", "-usages")

        return qs.order_by("-usages")

    def get_tag_synonyms(self):
        if self._synonyms_cache is None:
            mapping = {}
            for t in self.filter(synonyms__exists=True):
                for s in t["synonyms"]:
                    mapping[s] = t["title"]
            self.__class__._synonyms_cache = mapping
        return self._synonyms_cache


class Tags(Document):
    title = StringField(max_length=200, default='', primary_key=True)
    description = StringField(max_length=200, default='')
    usages = IntField(default=0)
    shreds = ListField(ReferenceField(Cluster))
    synonyms = ListField(StringField(max_length=200))
    is_base = BooleanField(default=True)
    category = StringField(max_length=200, default='')

# Path: models/user.py
# class User(Document, UserMixin):
#     username = StringField(max_length=200)
#     password = StringField(max_length=200, default='')
#     name = StringField(max_length=100)
#     email = EmailField()
#     active = BooleanField(default=True)
#     admin = BooleanField(default=False)
#     last_login = DateTimeField(default=datetime.datetime.now)
#     skipped = IntField(default=0)
#     processed = IntField(default=0)
#     tags = ListField(StringField(), default=[])
#     tags_count = IntField(default=0, db_field='tagsCount')
# 
#     def is_active(self):
#         return self.active
# 
#     def is_admin(self):
#         return self.admin or False
# 
#     def __unicode__(self):
#         return self.username


# Path: models/shreds.py
from collections import Counter
from mongoengine import (
    StringField, IntField, DateTimeField, ListField, BooleanField,
    ReferenceField, EmbeddedDocument, EmbeddedDocumentField, FloatField,
    CASCADE, QuerySet)
from flask_mongoengine import Document
from .user import User
import datetime
import random
import jinja2




class TagsQS(QuerySet):
    _synonyms_cache = None
    def get_base_tags(self, order_by_category=False):
        qs = self.filter(is_base=True)
        if order_by_category:
            return qs.order_by("category", "-usages")

        return qs.order_by("-usages")

    def get_tag_synonyms(self):
        if self._synonyms_cache is None:
            mapping = {}
            for t in self.filter(synonyms__exists=True):
                for s in t["synonyms"]:
                    mapping[s] = t["title"]
            self.__class__._synonyms_cache = mapping
        return self._synonyms_cache


class Tags(Document):
    title = StringField(max_length=200, default='', primary_key=True)
    description = StringField(max_length=200, default='')
    usages = IntField(default=0)
    shreds = ListField(ReferenceField(Cluster))
    synonyms = ListField(StringField(max_length=200))
    is_base = BooleanField(default=True)
    category = StringField(max_length=200, default='')

    created_by = ReferenceField(User, reverse_delete_rule=CASCADE)