# Copyright 2014 SolidBuilds.com. All rights reserved
#
# Authors: Ling Thio <ling.thio@gmail.com>


from flask import redirect, render_template, render_template_string
from flask import request, url_for
from app.app_and_db import app, db
from app.users.forms import UserProfileForm
from flask_user import current_user, login_required, roles_required
from flask import request
from flask import json
from models import *
#
# User Profile form
#
@app.route('/user/profile', methods=['GET', 'POST'])
@login_required
def user_profile_page():
    # Initialize form
    form = UserProfileForm(request.form, current_user)

    # Process valid POST
    if request.method=='POST' and form.validate():

        # Copy form fields to user_profile fields
        form.populate_obj(current_user)

        # Save user_profile
        db.session.commit()

        # Redirect to home page
        return redirect(url_for('home_page'))

    # Process GET or invalid POST
    return render_template('users/user_profile_page.html',
        form=form)

@app.route('/test', methods=['GET', 'POST'])
@login_required             # Limits access to authenticated users
def testPage():
    user_id = current_user.id
    links = Url.query.filter_by(user_id = user_id).all()
    uniqueID = UserAuth.query.filter_by(user_id = user_id).first()
        
    if request.method == 'POST':
        if request.form['delete'] > 0:
            id_get = request.form['delete']
            id_row = Url.query.get(id_get)
            db.session.delete(id_row)
            db.session.commit()
        else:    
            clicks = 0
            title = request.form['title']
            url = request.form['url']
            if url.startswith('http://') or url.startswith('https://'):
                url = url
            else:
                url = 'http://' + url
            description = request.form['description']
            data = Url(title, url, description, user_id)
            db.session.add(data)
            db.session.commit()
            links = Url.query.filter_by(user_id = user_id).all()
    print 'worked'
    return render_template('pages/test.html', links= links, user = uniqueID)


@app.route('/<username>', methods=['GET', 'POST'])
def show(username):
    #user_id
    userID = UserAuth.query.filter_by(username = username).first()
    #links
    links = Url.query.filter_by(user_id = userID.user_id).all()

    print links
    return render_template('pages/show.html', user = username, links = links)

@app.route('/', methods=['GET', 'POST'])
def alt_home():
    return render_template('pages/home_page.html')

@app.route('/log', methods= ['GET', 'POST'])
@roles_required('admin')
def log():
    users = UserAuth.query.all()
    url = Url.query.all()
    print users
    print url
    return render_template("pages/log.html", users= users )
    return 'none'

@app.errorhandler(500)
def page_not_found(e):
    return render_template('pages/404.html'), 500

@app.errorhandler(404)
def page_not_found(e):
    return render_template('pages/404.html'), 404

@@ -1313,54 +1313,8 @@
 t()%0A
-        %0A    if request.method == 'POST':%0A    
     
@@ -1344,16 +1344,16 @@
 '%5D %3E 0:%0A
+
         
@@ -1509,21 +1509,16 @@
         
-else:
     %0A   
@@ -1514,24 +1514,53 @@
        %0A    
+if request.method == 'POST':%0A
         clic
@@ -1566,20 +1566,16 @@
 cks = 0%0A
-    
         
@@ -1608,28 +1608,24 @@
 e'%5D%0A        
- 
-   
 url = reques
@@ -1638,20 +1638,16 @@
 %5B'url'%5D%0A
-    
         
@@ -1714,28 +1714,24 @@
             
-   
- 
 url = url%0A  
@@ -1740,22 +1740,14 @@
     
-    
 else:%0A
-    
     
@@ -1784,20 +1784,16 @@
         
- 
-   
 descript
@@ -1826,20 +1826,16 @@
 ption'%5D%0A
-    
         
@@ -1879,36 +1879,32 @@
 ser_id)%0A        
-   
- 
 db.session.add(d
@@ -1908,36 +1908,32 @@
 d(data)%0A        
-    
 db.session.commi
@@ -1928,36 +1928,32 @@
 ession.commit()%0A
-    
         links = 

check http