# Path: social/apps/flask_app/utils.py
# def login_required(fn):
#     @wraps(fn)
#     def decorated_view(*args, **kwargs):
#         if not g.user.is_authenticated:
#             return redirect('/')
#         return fn(*args, **kwargs)
#     return decorated_view
# 
# Path: social/apps/flask_app/models.py
# class User(Document, UserMixin):
#     username = StringField(max_length=200)
#     email = StringField(max_length=200)
#     active = BooleanField(default=True)
# 
#     def is_active(self):
#         return self.active
# 
# class UserSocialAuth(Document, MongoengineUserMixin):
#     #id = StringField(primary_key=True)   
#     user = ReferenceField(User, dbref=True)
#     provider = StringField(max_length=32)
#     uid = StringField(max_length=255)
#     extra_data = DictField()
#     meta = {
#         'indexes': ['uid']
#         }
# 
#     @classmethod
#     def username_max_length(cls):
#         return User.username.max_length
# 
#     @classmethod
#     def user_model(cls):
#         return User
# 
#     @classmethod
#     def _del_session(cls, qs):
#         session.clear()
#         #session.pop(username, None)
# 
# Path: models.py
# class Logic(Document):
#     tid = IntField(min_value=1)
#     username = StringField(max_length=200)
#     pubtime = DateTimeField(default=datetime.datetime.now)
#     views = IntField(default=0)
#     uptime = DateTimeField()
#     transition = StringField(max_length=200, default="default")
#     data = StringField(default="<section><h1>Untitled</h1></section>")
#     description = StringField(default="")
#     title = StringField(max_length=200)
#     should_loop = BooleanField(default=False)
#     published = BooleanField(default=True)
#     rolling_links = BooleanField(default=True)
#     theme_font = StringField(max_length=200, default="league")
#     rtl = BooleanField(default=False)
#     theme_color = StringField(max_length=200, default="grey-blue")
#     center = BooleanField(default=False)


from flask import render_template, request, redirect, session, jsonify
from mongoengine import ValidationError
from flask_reveal import app
from social.apps.flask_app.utils import login_required
from social.apps.flask_app.models import User, UserSocialAuth
from models import Logic
    import urllib
    import hashlib
    '''Construction dict'''
    query = {}
    for k, v in immutablemultidict.iteritems():
        query.update({k[5:-1]: v})
    return query


def get_gravatar(user):
    '''Get gravatar's Avatar'''
    domain = 'http://' + request.url.split('/')[2] + '/'
    email = user[0].email
    if not email:
        email = 'ciici123@hotmail.com'
    default = "static/images/default-profile-picture.png"
    size = 140
    gravatar_url = "http://www.gravatar.com/avatar/" + \
        hashlib.md5(email.lower()).hexdigest() + "?"
    gravatar_url += urllib.urlencode({'d': default, 's': str(size)})
    return gravatar_url


@app.route('/')
def index():
    return render_template("index.html")


@app.route('/<string:username>/')
#@login_required
def home(username):
    if checkuser(username):

# Path: social/apps/flask_app/utils.py
# def login_required(fn):
#     @wraps(fn)
#     def decorated_view(*args, **kwargs):
#         if not g.user.is_authenticated:
#             return redirect('/')
#         return fn(*args, **kwargs)
#     return decorated_view
# 
# Path: social/apps/flask_app/models.py
# class User(Document, UserMixin):
#     username = StringField(max_length=200)
#     email = StringField(max_length=200)
#     active = BooleanField(default=True)
# 
#     def is_active(self):
#         return self.active
# 
# class UserSocialAuth(Document, MongoengineUserMixin):
#     #id = StringField(primary_key=True)   
#     user = ReferenceField(User, dbref=True)
#     provider = StringField(max_length=32)
#     uid = StringField(max_length=255)
#     extra_data = DictField()
#     meta = {
#         'indexes': ['uid']
#         }
# 
#     @classmethod
#     def username_max_length(cls):
#         return User.username.max_length
# 
#     @classmethod
#     def user_model(cls):
#         return User
# 
#     @classmethod
#     def _del_session(cls, qs):
#         session.clear()
#         #session.pop(username, None)
# 
# Path: models.py
# class Logic(Document):
#     tid = IntField(min_value=1)
#     username = StringField(max_length=200)
#     pubtime = DateTimeField(default=datetime.datetime.now)
#     views = IntField(default=0)
#     uptime = DateTimeField()
#     transition = StringField(max_length=200, default="default")
#     data = StringField(default="<section><h1>Untitled</h1></section>")
#     description = StringField(default="")
#     title = StringField(max_length=200)
#     should_loop = BooleanField(default=False)
#     published = BooleanField(default=True)
#     rolling_links = BooleanField(default=True)
#     theme_font = StringField(max_length=200, default="league")
#     rtl = BooleanField(default=False)
#     theme_color = StringField(max_length=200, default="grey-blue")
#     center = BooleanField(default=False)


# Path: routes.py
from flask import render_template, request, redirect, session, jsonify
from mongoengine import ValidationError
from flask_reveal import app
from social.apps.flask_app.utils import login_required
from social.apps.flask_app.models import User, UserSocialAuth
from models import Logic
    import urllib
    import hashlib

    '''Construction dict'''
    query = {}
    for k, v in immutablemultidict.iteritems():
        query.update({k[5:-1]: v})
    return query


def get_gravatar(user):
    '''Get gravatar's Avatar'''
    domain = 'http://' + request.url.split('/')[2] + '/'
    email = user[0].email
    if not email:
        email = 'ciici123@hotmail.com'
    default = "static/images/default-profile-picture.png"
    size = 140
    gravatar_url = "http://www.gravatar.com/avatar/" + \
        hashlib.md5(email.lower()).hexdigest() + "?"
    gravatar_url += urllib.urlencode({'d': default, 's': str(size)})
    return gravatar_url


@app.route('/')
def index():
    return render_template("index.html")


@app.route('/<string:username>/')
#@login_required
def home(username):
    if checkuser(username):

        decks = Logic.objects(username=username).order_by("+pubtime")