# Path: daimaduan/models/base.py
# class Paste(BaseDocument):
#     user = db.ReferenceField(User)
# 
#     title = db.StringField()
#     hash_id = db.StringField(unique=True)
#     is_private = db.BooleanField(default=False)
#     codes = db.ListField(db.EmbeddedDocumentField(Code))
#     tags = db.ListField(db.ReferenceField('Tag'))
# 
#     views = db.IntField(default=0)
# 
#     @property
#     def comments(self):
#         page = get_page()
#         return Comment.objects(paste=self).order_by('-created_at').paginate(page=page, per_page=20)
# 
#     @property
#     def comments_count(self):
#         return Comment.objects(paste=self).count()
# 
#     def save(self, *args, **kwargs):
#         self.create_hash_id(self.user.salt, 'paste')
#         if not self.title:
#             self.title = u'代码集合: %s' % self.hash_id
#         super(Paste, self).save(*args, **kwargs)
# 
#     def increase_views(self):
#         self.views = self.views + 1
#         self.save()
# 
#     def is_user_owned(self, user):
#         return self.user == user
# 
#     @property
#     def likes_count(self):
#         return User.objects(likes=self).count()
# 
# Path: daimaduan/models/syntax.py
# class Syntax(BaseDocument):
#     key = db.StringField(required=True, unique=True)
#     name = db.StringField(required=True, unique=True)
# 
#     @property
#     def pastes(self):
#         return Paste.objects(codes__syntax=self.key)
# 
# Path: daimaduan/models/tag.py
# class Tag(BaseDocument):
#     key = db.StringField(required=True, unique=True)
#     name = db.StringField(required=True, unique=True)
#     popularity = db.IntField(default=1)
# 
#     def __unicode__(self):
#         return "<Tag:%s>" % self.name
# 
# Path: daimaduan/utils/pagination.py
# def get_page():
#     """ Get current page
# 
#     Get current page from query string `page=x`,
#     if page not given returns `1` instead.
#     """
#     try:
#         page = request.args.get('page', 1)
#         return int(page)
#     except ValueError:
#         return 1


from flask import Blueprint
from flask import render_template
from flask import request
from flask.ext.login import current_user
from daimaduan.models.base import Paste
from daimaduan.models.syntax import Syntax
from daimaduan.models.tag import Tag
from daimaduan.utils.pagination import get_page



tag_app = Blueprint('tag_app', __name__, template_folder="templates")


@tag_app.route('/<tag_name>', methods=['GET'])
def view(tag_name):
    criteria = {}
    tag = Tag.objects.get_or_404(key=tag_name)
    if request.args.get('filter', None) != 'mine' or not current_user.is_authenticated:
        criteria['is_private'] = False
    syntax = Syntax.objects(key=tag_name).first()
    if syntax:
        criteria['codes__syntax'] = syntax
    else:
        criteria['tags'] = tag
    pastes = Paste.objects(**criteria).order_by('-updated_at')
    page = get_page()

    pagination = pastes.paginate(page, per_page=20)

    return render_template('tags/view.html',
                           tag=tag,

# Path: daimaduan/models/base.py
# class Paste(BaseDocument):
#     user = db.ReferenceField(User)
# 
#     title = db.StringField()
#     hash_id = db.StringField(unique=True)
#     is_private = db.BooleanField(default=False)
#     codes = db.ListField(db.EmbeddedDocumentField(Code))
#     tags = db.ListField(db.ReferenceField('Tag'))
# 
#     views = db.IntField(default=0)
# 
#     @property
#     def comments(self):
#         page = get_page()
#         return Comment.objects(paste=self).order_by('-created_at').paginate(page=page, per_page=20)
# 
#     @property
#     def comments_count(self):
#         return Comment.objects(paste=self).count()
# 
#     def save(self, *args, **kwargs):
#         self.create_hash_id(self.user.salt, 'paste')
#         if not self.title:
#             self.title = u'代码集合: %s' % self.hash_id
#         super(Paste, self).save(*args, **kwargs)
# 
#     def increase_views(self):
#         self.views = self.views + 1
#         self.save()
# 
#     def is_user_owned(self, user):
#         return self.user == user
# 
#     @property
#     def likes_count(self):
#         return User.objects(likes=self).count()
# 
# Path: daimaduan/models/syntax.py
# class Syntax(BaseDocument):
#     key = db.StringField(required=True, unique=True)
#     name = db.StringField(required=True, unique=True)
# 
#     @property
#     def pastes(self):
#         return Paste.objects(codes__syntax=self.key)
# 
# Path: daimaduan/models/tag.py
# class Tag(BaseDocument):
#     key = db.StringField(required=True, unique=True)
#     name = db.StringField(required=True, unique=True)
#     popularity = db.IntField(default=1)
# 
#     def __unicode__(self):
#         return "<Tag:%s>" % self.name
# 
# Path: daimaduan/utils/pagination.py
# def get_page():
#     """ Get current page
# 
#     Get current page from query string `page=x`,
#     if page not given returns `1` instead.
#     """
#     try:
#         page = request.args.get('page', 1)
#         return int(page)
#     except ValueError:
#         return 1


# Path: daimaduan/views/tags.py
from flask import Blueprint
from flask import render_template
from flask import request
from flask.ext.login import current_user
from daimaduan.models.base import Paste
from daimaduan.models.syntax import Syntax
from daimaduan.models.tag import Tag
from daimaduan.utils.pagination import get_page




tag_app = Blueprint('tag_app', __name__, template_folder="templates")


@tag_app.route('/<tag_name>', methods=['GET'])
def view(tag_name):
    criteria = {}
    tag = Tag.objects.get_or_404(key=tag_name)
    if request.args.get('filter', None) != 'mine' or not current_user.is_authenticated:
        criteria['is_private'] = False
    syntax = Syntax.objects(key=tag_name).first()
    if syntax:
        criteria['codes__syntax'] = syntax
    else:
        criteria['tags'] = tag
    pastes = Paste.objects(**criteria).order_by('-updated_at')
    page = get_page()

    pagination = pastes.paginate(page, per_page=20)

    return render_template('tags/view.html',
                           tag=tag,

                           hot_pastes=Paste.objects(is_private=False).order_by('-views')[:10],