from app import db
from app.openapi.db_model.models import User
from flask.ext import restful
from flask     import request,jsonify,abort
from flask.ext.restful import reqparse

class userapi(restful.Resource):
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('username', type=str, required=True)
        parser.add_argument('password', type=str, required=True)
        user_info = parser.parse_args()
        username=user_info.username
        password=user_info.password

        if User.query.filter_by(username=username).first() is not None:
            return (jsonify({"msg":"user exist"}))
        else:
            user = User(username,password)
        db.session.add(user)
        status=db.session.commit()
        return (jsonify({"msg":"add user successful"}))

    def get(self,id):
        user = User.query.get(id)
        if not user:
            abort(400)
        return jsonify({'username': user.username})

    def put(self):
        username = request.json.get('username')
        password = request.json.get('password')
        if username is None or password is None:
            abort(400)    # missing arguments
        if User.query.filter_by(username=username).first() is  None:
            abort(400)    # existing user
        user_list=db.session.query(User).filter_by(username=username).all()
        print len(user_list)
        user=user_list[0]
        
        user.hash_password(password)
        db.session.commit()
        return (jsonify({"status":200,"msg":"modify User success"}))

    def delete(self,id):
        user = User.query.get(id)
        if not user:
            abort(400)
        else:
            db.session.delete(user)
        return jsonify({'status':200, 'msg':"delete "+user.username+" ok"})

@@ -994,94 +994,170 @@
     
-username = request.json.get('username')%0A        password = request.json.get('password'
+parser = reqparse.RequestParser()%0A        parser.add_argument('username', type=str, required=True)%0A        parser.add_argument('password', type=str, required=True
 )%0A  
@@ -1166,94 +1166,112 @@
     
-if 
 user
-name is None or password is None:%0A            abort(400)    # missing arguments
+_info = parser.parse_args()%0A        username=user_info.username%0A        password=user_info.password%0A
 %0A   
@@ -1352,37 +1352,64 @@
     
-abort(400)    # existing user
+return (jsonify(%7B%22msg%22:%22user not exists%22%7D))%0A            
 %0A   

modify user api method