#!/usr/bin/env python


from flask import render_template, make_response, Blueprint, request, abort
from flask.ext import restful
from flask.ext.restful import Api
from flask.ext.login import login_user
import json


api = Blueprint("root", __name__, template_folder="templates")

rest = Api(api)


class IndexPage(restful.Resource):
    def get(self):
        headers = {'Content-Type': 'text/html'}
        return make_response(render_template('index.html'), 200, headers)


class LoginRegister(restful.Resource):
    def get(self):
        headers = {'Content-Type': 'text/html'}
        return make_response(render_template('login.html'), 200, headers)

    def post(self):
        from models import User
        try:
            obj = json.loads(request.data)
        except ValueError:
            abort(400)

        if obj is not None:
            user = User.query.filter_by(
                username=obj['user']['username']).first_or_404()
            if user.verify_password(obj['user']['password']):
                login_user(user, remember=True)
                return {"status": 200, "user": user.id}
        abort(400)

    def put(self):
        from models import User
        from karma import db

        try:
            obj = json.loads(request.data)
        except ValueError:
            abort(400)

        if obj is not None:
            user = User.query.filter_by(
                username=obj['user']['username']).first()
            if not user:
                new_user = User(username=obj['user']['username'],
                                password=obj['user']['password'])
                db.session.add(new_user)
                db.session.commit()
                login_user(new_user, remember=True)
                return {"status": 200, "user": new_user.id}
        return {"status": 400}
        # TOOD: login the user


rest.add_resource(IndexPage, '/')
rest.add_resource(LoginRegister, '/login')

@@ -1274,32 +1274,33 @@
 %0A        except 
+(
 ValueError:%0A    
@@ -1285,32 +1285,43 @@
 cept (ValueError
+, KeyError)
 :%0A            ab
@@ -1350,32 +1350,170 @@
 bj is not None:%0A
+            try:%0A                description = obj%5B'user'%5D%5B'description'%5D%0A            except KeyError:%0A                description = None%0A
             user
@@ -1620,16 +1620,62 @@
 t user:%0A
+                print obj%5B'user'%5D%5B'password'%5D%0A
         
@@ -1732,16 +1732,16 @@
 name'%5D,%0A
-
         
@@ -1796,16 +1796,73 @@
 ssword'%5D
+,%0A                                description=description
 )%0A      

Add support for adding description to registration payload