@RequireLogin(role='admin')
def post(request):
    """
    Create a new user.
    """
    try:
        new = json.loads(request.body)
        assert "username" in new
        assert "password" in new
        assert "email" in new
    except AssertionError:
        return HttpResponseBadRequest("argument mismatch")
    except __HOLE__:
        return HttpResponseBadRequest("invalid JSON")

    from api.models import db_model
    dbc = db_model.connect()
    user = dbc.hbuser.find_one({"username": new['username']})
    if user is not None:
        return HttpResponseBadRequest("user already exists")
    else:
        m = hashlib.sha512()
        m.update(new['password'])
        roles = []
        if "roles" in new:
            roles = new['roles']
        dbc.hbuser.save({
            'username': new['username'],
            'email': new['email'],
            'password': m.hexdigest(),
            'createdAt': datetime.isoformat(datetime.now()),
            'updatedAt': datetime.isoformat(datetime.now()),
            'roles': roles,
        })
        r = HttpResponse(status=200)
        r['location'] = "/api/user/%s" % new['username']
        logger.info("user '%s' created by '%s'" % (new['username'], request.user['username']))
        return r
dataset/ETHPy150Open emccode/heliosburn/heliosburn/django/hbproject/api/views/user.py/post