from flask import request
from flask_restful import fields, marshal_with, Resource
from flask_security import current_user, login_required

from . import DT_FORMATTER
from .base import BaseDetailResource, BaseRequestParser
from .util import new_edit_parsers, RewriteUrl
from dockci.models.auth import User
from dockci.server import API, DB


BASIC_FIELDS = {
    'id': fields.Integer(),
    'email': fields.String(),
    'active': fields.Boolean(),
}

LIST_FIELDS = {
    'detail': RewriteUrl('user_detail', rewrites=dict(user_id='id')),
}
LIST_FIELDS.update(BASIC_FIELDS)


DETAIL_FIELDS = {
    'confirmed_at': DT_FORMATTER,
    #'roles': fields.Nested(),
}
DETAIL_FIELDS.update(BASIC_FIELDS)


SHARED_PARSER_ARGS = {
    'email': dict(
        help="Contact email address",
        required=None,
    ),
    'password': dict(
        help="Password for user to authenticate",
        required=None,
    ),
    'active': dict(
        help="Whether or not the user can login",
        required=False,
    ),
}

USER_NEW_PARSER = BaseRequestParser(bundle_errors=True)
USER_EDIT_PARSER = BaseRequestParser(bundle_errors=True)
new_edit_parsers(USER_NEW_PARSER, USER_EDIT_PARSER, SHARED_PARSER_ARGS)


class UserList(Resource):
    @login_required
    @marshal_with(LIST_FIELDS)
    def get(self):
        return User.query.all()


class UserDetail(BaseDetailResource):
    @login_required
    @marshal_with(DETAIL_FIELDS)
    def get(self, user_id):
        return User.query.get_or_404(user_id)

    @login_required
    @marshal_with(DETAIL_FIELDS)
    def put(self, user_id):
        user = User()
        return self.handle_write(user, USER_NEW_PARSER)

    @login_required
    @marshal_with(DETAIL_FIELDS)
    def post(self, user_id):
        user = User.query.get_or_404(user_id)
        return self.handle_write(user, USER_EDIT_PARSER)


class MeDetail(Resource):
    @login_required
    @marshal_with(DETAIL_FIELDS)
    def get(self):
        return current_user

    def post(self):
        return self.get()


API.add_resource(UserList,
                 '/users',
                 endpoint='user_list')
API.add_resource(UserDetail,
                 '/users/<int:user_id>',
                 endpoint='user_detail')
API.add_resource(MeDetail,
                 '/me',
                 endpoint='me_detail')

@@ -1720,35 +1720,75 @@
 st(self, user_id
-):%0A
+, user=None):%0A        if user is None:%0A    
         user = U
@@ -2003,16 +2003,36 @@
 t_user%0A%0A
+    @login_required%0A
     def 
@@ -2062,17 +2062,44 @@
 urn 
+U
 se
-lf.get(
+rDetail().post(none, current_user
 )%0A%0A%0A

Allow update user via /me