# Path: bluespot/extensions.py

# 
# Path: bluespot/base/utils/helper.py
# def format_url(url):
# 	if not url.startswith('http') or not url.startswith('https'):
# 		newurl =  '%s%s' % ('http://', url)
# 		return newurl
# 	return url
# 
# Path: bluespot/base/utils/forms.py
# def print_errors(form):
#     """Flashes form errors"""
#     for field, errors in form.errors.items():
# 	for error in errors:
# 	    print "Error in the %s field - %s" % (getattr(form, field).label.text,error)
# 
# def get_errors(form):
#     """ Return form errors as string """
#     form_errors = ""
#     for field, errors in form.errors.items():
#         for error in errors:
#             form_errors = form_errors+ "Error in the %s field - %s </br>" % (getattr(form, field).label.text,error)
#     return form_errors
# 
# Path: bluespot/guest/models.py
# class Guest(db.Model):
#     ''' Class to represent guest profile, it will be filled fully/partially depending upon site configuration
# 
#     '''
#     id          = db.Column(db.Integer, primary_key=True)
#     firstname   = db.Column(db.String(60))
#     lastname    = db.Column(db.String(60))
#     age         = db.Column(db.Integer,index=True)
#     gender      = db.Column(db.Integer,index=True)
#     state       = db.Column(db.Integer,index=True)
#     email       = db.Column(db.String(60))
#     phone       = db.Column(db.String(15))
#     devices     = db.relationship('Device', backref='guest',lazy='dynamic')
#     fb_profile  = db.Column(db.Integer, db.ForeignKey('facebookauth.id'))
#     fb_liked    = db.Column(db.Integer)
#     fb_posted   = db.Column(db.Integer)
# 
#     def populate_from_email_form(self,form,form_fields):
#         for form_field in form_fields:
#             val = getattr(form,form_field).data 
#             setattr(self,form_field,val)


from functools import wraps
from flask import request, Response,Blueprint,render_template,jsonify,current_app,abort,redirect,url_for
from bluespot.extensions import db
from bluespot.base.utils.helper import format_url
from bluespot.base.utils.forms import print_errors,get_errors
from bluespot.guest.models import Guest


def check_auth(username, password):
    """This function is called to check if a username /
    password combination is valid.
    """
    if username == current_app.config['LANDINGSITE']['adminusername'] and password == current_app.config['LANDINGSITE']['adminpassword'] :
        return 1
    else:
        return None

def authenticate():
    """Sends a 401 response that enables basic auth"""
    return Response(
    'Could not verify your access level for that URL.\n'
    'You have to login with proper credentials', 401,
    {'WWW-Authenticate': 'Basic realm="Login Required"'})

def requires_auth(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        auth = request.authorization
        if not auth or not check_auth(auth.username, auth.password):
            return authenticate()
        return f(*args, **kwargs)
    return decorated

@bp.route('/')
@requires_auth
def client_index( ):

# Path: bluespot/extensions.py

# 
# Path: bluespot/base/utils/helper.py
# def format_url(url):
# 	if not url.startswith('http') or not url.startswith('https'):
# 		newurl =  '%s%s' % ('http://', url)
# 		return newurl
# 	return url
# 
# Path: bluespot/base/utils/forms.py
# def print_errors(form):
#     """Flashes form errors"""
#     for field, errors in form.errors.items():
# 	for error in errors:
# 	    print "Error in the %s field - %s" % (getattr(form, field).label.text,error)
# 
# def get_errors(form):
#     """ Return form errors as string """
#     form_errors = ""
#     for field, errors in form.errors.items():
#         for error in errors:
#             form_errors = form_errors+ "Error in the %s field - %s </br>" % (getattr(form, field).label.text,error)
#     return form_errors
# 
# Path: bluespot/guest/models.py
# class Guest(db.Model):
#     ''' Class to represent guest profile, it will be filled fully/partially depending upon site configuration
# 
#     '''
#     id          = db.Column(db.Integer, primary_key=True)
#     firstname   = db.Column(db.String(60))
#     lastname    = db.Column(db.String(60))
#     age         = db.Column(db.Integer,index=True)
#     gender      = db.Column(db.Integer,index=True)
#     state       = db.Column(db.Integer,index=True)
#     email       = db.Column(db.String(60))
#     phone       = db.Column(db.String(15))
#     devices     = db.relationship('Device', backref='guest',lazy='dynamic')
#     fb_profile  = db.Column(db.Integer, db.ForeignKey('facebookauth.id'))
#     fb_liked    = db.Column(db.Integer)
#     fb_posted   = db.Column(db.Integer)
# 
#     def populate_from_email_form(self,form,form_fields):
#         for form_field in form_fields:
#             val = getattr(form,form_field).data 
#             setattr(self,form_field,val)


# Path: bluespot/admin/views.py
from functools import wraps
from flask import request, Response,Blueprint,render_template,jsonify,current_app,abort,redirect,url_for
from bluespot.extensions import db
from bluespot.base.utils.helper import format_url
from bluespot.base.utils.forms import print_errors,get_errors
from bluespot.guest.models import Guest



def check_auth(username, password):
    """This function is called to check if a username /
    password combination is valid.
    """
    if username == current_app.config['LANDINGSITE']['adminusername'] and password == current_app.config['LANDINGSITE']['adminpassword'] :
        return 1
    else:
        return None

def authenticate():
    """Sends a 401 response that enables basic auth"""
    return Response(
    'Could not verify your access level for that URL.\n'
    'You have to login with proper credentials', 401,
    {'WWW-Authenticate': 'Basic realm="Login Required"'})

def requires_auth(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        auth = request.authorization
        if not auth or not check_auth(auth.username, auth.password):
            return authenticate()
        return f(*args, **kwargs)
    return decorated

@bp.route('/')
@requires_auth
def client_index( ):

    items = Guest.query.filter_by().all()