Exemple #1
0
def Create(request):
    """
    @params: logintoken, name, username, password
    """
    requestJSON = request.get_json(silent=True)
    try:
        assertJSON(requestJSON, ['logintoken', 'name', 'username', 'password'])
        User.InputRuleSet.validateFromPropertyNames(
            ['name', 'username', 'password'], requestJSON)

        with SessionHandler.app_and_db_session_scope(
                requestJSON['logintoken'],
                SessionHandler.PermissionLevel.ADMIN) as session:
            # check if same username already exists
            if User.loadFromDbByUsername(
                    session.db_session,
                    username=requestJSON['username']) is not None:
                raise HttpException(
                    HttpErrorType.GenericError, 'User create',
                    'User with that username already exists in the database.')
            addedUser = User.addNew(session=session.db_session,
                                    name=requestJSON['name'],
                                    username=requestJSON['username'],
                                    password=encryptPassword(
                                        requestJSON['password']))
            return SessionHandler.OK(addedUser.toJSONObject())
    except HttpException as exc:
        return exc.GetResponse()
Exemple #2
0
def Create(request):
    """
    @params: logintoken, name, description
    """
    requestJSON = request.get_json(silent=True)
    try:
        assertJSON(requestJSON, ['logintoken', 'name', 'description'])
        Folder.InputRuleSet.validateFromPropertyNames(['name', 'description'],
                                                      requestJSON)

        with SessionHandler.app_and_db_session_scope(
                requestJSON['logintoken'],
                SessionHandler.PermissionLevel.USER) as session:
            if Folder.getAllFromOwnerId(
                    session.db_session, session.current_user.id).filter_by(
                        name=requestJSON['name']).count() > 0:
                raise HttpException(
                    HttpErrorType.GenericError, 'Folder Create',
                    'A folder with that name already exists in the database.')

            addedFolder = Folder.addNew(session=session.db_session,
                                        ownerid=session.current_user.id,
                                        name=requestJSON['name'],
                                        description=requestJSON['description'])
            return SessionHandler.OK(addedFolder.toJSONObject())
    except HttpException as exc:
        return exc.GetResponse()
Exemple #3
0
def Login(request, logger):
    """
    @params: username, password, [userAgent]
    """
    requestJSON = request.get_json(silent=True)
    try:
        assertJSON(requestJSON, ['username', 'password'])
        with SessionHandler.app_and_db_session_scope(
                '', SessionHandler.PermissionLevel.NONE) as session:
            # here verify password and login are valid, create a session and return a token
            loadedUser = User.loadFromDbByUsername(
                session.db_session, username=requestJSON['username'])
            if not loadedUser:
                encryptPassword(requestJSON['password'])
            else:
                if (verifyPassword(requestJSON['password'],
                                   loadedUser.password)):
                    newSession = AppSession.addNewOrReturnLastIfValid(
                        session.db_session, loadedUser.id, 1,
                        requestJSON['userAgent']
                        if 'userAgent' in requestJSON else '')
                    return SessionHandler.OK({'logintoken': newSession.token})
            logger.error(
                'Failed logon attempt for username: {}, from remote address: {}'
                .format(requestJSON['username'], str(request.remote_addr)))
            raise HttpException(HttpErrorType.GenericError, 'login',
                                'Invalid username or password.')
    except HttpException as exc:
        return exc.GetResponse()
Exemple #4
0
def Create(request):
    """
    @params: logintoken, name, description, parent_folder_id
    """
    requestJSON = request.get_json(silent=True)
    try:
        assertJSON(requestJSON,
                   ['logintoken', 'name', 'description', 'parent_folder_id'])
        Task.InputRuleSet.validateFromPropertyNames(['name', 'description'],
                                                    requestJSON)

        with SessionHandler.app_and_db_session_scope(
                requestJSON['logintoken'],
                SessionHandler.PermissionLevel.USER) as session:
            if Folder.checkIfFolderExists(session.db_session,
                                          requestJSON['parent_folder_id'],
                                          session.current_user.id) < 1:
                raise HttpException(HttpErrorType.NotFound, 'Task Create',
                                    'No such folder found under current user')

            addedTask = Task.addNew(session.db_session,
                                    session.current_user.id,
                                    requestJSON['name'],
                                    requestJSON['description'],
                                    requestJSON['parent_folder_id'])
            return SessionHandler.OK(addedTask.toJSONObject())

    except HttpException as exc:
        return exc.GetResponse()
def GetPublicSetting(request):
    """
    @params: settingName
    """
    requestJSON = request.get_json(silent=True)
    try:
        assertJSON(requestJSON, ['settingName'])
        with SessionHandler.app_and_db_session_scope("", SessionHandler.PermissionLevel.NONE) as session:
            requestedSetting = ServerSetting.getByName(session.db_session, requestJSON['settingName'])
            if requestedSetting.count() > 0 and requestedSetting.first().isPublic:
                return SessionHandler.OK(requestedSetting.first().toJSONObject())
            raise HttpException(HttpErrorType.NotFound, "GetPublicSetting", "Setting was not found.")
    except HttpException as exc:
        return exc.GetResponse()
Exemple #6
0
def Logout(request):
    """
    @params: logintoken
    """
    requestJSON = request.get_json(silent=True)
    try:
        assertJSON(requestJSON, ['logintoken'])
        with SessionHandler.app_and_db_session_scope(
                requestJSON['logintoken'],
                SessionHandler.PermissionLevel.USER) as session:
            session.app_session.invalidate()
            return SessionHandler.OK()
    except HttpException as exc:
        return exc.GetResponse()
Exemple #7
0
def GetAll(request):
    """
    @params: logintoken
    """
    requestJSON = request.get_json(silent=True)
    try:
        assertJSON(requestJSON, ['logintoken'])
        with SessionHandler.app_and_db_session_scope(
                requestJSON['logintoken'],
                SessionHandler.PermissionLevel.USER) as session:
            allFolders = Folder.getAllFromOwnerId(session.db_session,
                                                  session.current_user.id)
            JSONFolders = [] if allFolders.count() == 0 else list(
                map(lambda x: x.toJSONObject(), allFolders))
            return SessionHandler.OK({"folders": JSONFolders})
    except HttpException as exc:
        return exc.GetResponse()
Exemple #8
0
def GetByFolderId(request):
    """
    @params: logintoken, parent_folder_id
    """
    requestJSON = request.get_json(silent=True)
    try:
        assertJSON(requestJSON, ['logintoken', 'parent_folder_id'])

        with SessionHandler.app_and_db_session_scope(
                requestJSON['logintoken'],
                SessionHandler.PermissionLevel.USER) as session:
            foundTasks = Task.getAllFromFolderId(
                session.db_session, requestJSON['parent_folder_id'],
                session.current_user.id)
            JSONTasks = [] if foundTasks.count() == 0 else list(
                map(lambda x: x.toJSONObject(), foundTasks))
            return SessionHandler.OK({"tasks": JSONTasks})

    except HttpException as exc:
        return exc.GetResponse()
Exemple #9
0
def CompleteTaskById(request):
    """
    @params: logintoken, task_id
    """
    requestJSON = request.get_json(silent=True)
    try:
        assertJSON(requestJSON, ['logintoken', 'task_id'])

        with SessionHandler.app_and_db_session_scope(
                requestJSON['logintoken'],
                SessionHandler.PermissionLevel.USER) as session:
            foundTask = Task.getById(session.db_session,
                                     session.current_user.id,
                                     requestJSON['task_id'])
            if foundTask.count() == 0:
                raise HttpException(HttpErrorType.NotFound, 'Updating Task',
                                    'No task found for given id.')

            foundTask.first().setAsCompleted()
            return SessionHandler.OK()

    except HttpException as exc:
        return exc.GetResponse()