handlers

DataHandler

class msdss_data_api.handlers.DataHandler(database=None, permitted_tables=[], restricted_tables=['data', 'user'])[source]

Class to handle dataset events.

Parameters
  • database (msdss_base_database.core.Database or None) – Database object to use for managing datasets. If None, a default database will be used.

  • permitted_tables (list(str)) – List of permitted table names that are only accessible. If any tables not in this list are accessed, a 401 unauthorized http exception will be thrown.

  • restricted_tables (list(str)) – List of restricted table names that are not accessible. If any of these are accessed, a 401 unauthorized http exception will be thrown.

Author

Richard Wen <rrwen.dev@gmail.com>

Example

from msdss_base_database import Database
from msdss_data_api.handlers import *
from msdss_data_api.managers import *

# Setup objects
db = Database()
dm = DataManager(database=db)
handler = DataHandler(database=db)

# Check if the table exists and drop if it does
if db.has_table('test_table'):
    db.drop_table('test_table')

# Check table before writing
# Should not raise exceptions
handler.handle_write('test_table')

# Create sample data
data = [
    {'id': 1, 'column_one': 'a', 'column_two': 2},
    {'id': 2, 'column_one': 'b', 'column_two': 4},
    {'id': 3, 'column_one': 'c', 'column_two': 6},
]
dm.create('test_table', data)

# Check table name
# Should not raise exceptions
handler.handle_read('test_table')

# Check table restrictions
# Should not raise exceptions
handler.handle_restrictions('test_table')

handle_permissions

DataHandler.handle_permissions(dataset)[source]

Handle a permitted table access.

Parameters

dataset (str) – Name of the table to check.

Author

Richard Wen <rrwen.dev@gmail.com>

Example

from msdss_base_database import Database
from msdss_data_api.handlers import *
from msdss_data_api.managers import *

# Setup objects
db = Database()
dm = DataManager(database=db)
handler = DataHandler(database=db)

# Check if the table exists and drop if it does
if db.has_table('test_table'):
    db.drop_table('test_table')

# Create sample data
data = [
    {'id': 1, 'column_one': 'a', 'column_two': 2},
    {'id': 2, 'column_one': 'b', 'column_two': 4},
    {'id': 3, 'column_one': 'c', 'column_two': 6},
]
dm.create('test_table', data)

# Check table name
# Should not raise exceptions
handler.handle_permissions('test_table')

handle_read

DataHandler.handle_read(dataset)[source]

Handle a table read, checking for existence and restrictions.

Parameters

dataset (str) – Name of the table to check.

Author

Richard Wen <rrwen.dev@gmail.com>

Example

from msdss_base_database import Database
from msdss_data_api.handlers import *
from msdss_data_api.managers import *

# Setup objects
db = Database()
dm = DataManager(database=db)
handler = DataHandler(database=db)

# Check if the table exists and drop if it does
if db.has_table('test_table'):
    db.drop_table('test_table')

# Create sample data
data = [
    {'id': 1, 'column_one': 'a', 'column_two': 2},
    {'id': 2, 'column_one': 'b', 'column_two': 4},
    {'id': 3, 'column_one': 'c', 'column_two': 6},
]
dm.create('test_table', data)

# Check table name
# Should not raise exceptions
handler.handle_read('test_table')

handle_restrictions

DataHandler.handle_restrictions(dataset)[source]

Handle restricted table access.

Parameters

dataset (str) – Name of the table to check.

Author

Richard Wen <rrwen.dev@gmail.com>

Example

from msdss_base_database import Database
from msdss_data_api.handlers import *
from msdss_data_api.managers import *

# Setup objects
db = Database()
dm = DataManager(database=db)
handler = DataHandler(database=db)

# Check if the table exists and drop if it does
if db.has_table('test_table'):
    db.drop_table('test_table')

# Create sample data
data = [
    {'id': 1, 'column_one': 'a', 'column_two': 2},
    {'id': 2, 'column_one': 'b', 'column_two': 4},
    {'id': 3, 'column_one': 'c', 'column_two': 6},
]
dm.create('test_table', data)

# Check table name
# Should not raise exceptions
handler.handle_restrictions('test_table')

handle_where

DataHandler.handle_where(where_list)[source]

Throw an exception if where statements do not match the expected format.

Parameters
  • table (str) – Table name to apply where to.

  • where_list (list(list(str))) –

    list of where statements the form of ['column', 'operator', 'value'] to further filter individual values or rows.

    • Operators are one of:

      =
      !=
      >
      >=
      >
      <
      <=
      LIKE
      NOTLIKE
      ILIKE
      NOTILIKE
      CONTAINS
      STARTSWITH
      ENDSWITH
      
    • Example: ['column_two', '<'. '3']'

Author

Richard Wen <rrwen.dev@gmail.com>

Example

from msdss_data_api.handlers import *

handler = DataHandler(database=db)
where_list = [['col', '<', '3'], ['col', '=', 'a']]

# Should not raise exceptions
handler.handle_where(where_list)

handle_write

DataHandler.handle_write(dataset)[source]

Handle a table write, checking for existence and restrictions.

Parameters

dataset (str) – Name of the table to check.

Author

Richard Wen <rrwen.dev@gmail.com>

Example

from msdss_base_database import Database
from msdss_data_api.handlers import *
from msdss_data_api.managers import *

# Setup objects
db = Database()
dm = DataManager(database=db)
handler = DataHandler(database=db)

# Check if the table exists and drop if it does
if db.has_table('test_table'):
    db.drop_table('test_table')

# Check table name
# Should not raise exceptions
handler.handle_write('test_table')

handle_update

DataHandler.handle_update(dataset, data, where_list=None)[source]

Handle a table update, checking for existence and restrictions.

Parameters
  • dataset (str) – Name of the table to check.

  • data (dict) – Dictionary of update values to check. Each key should represent a column name in the table.

  • where_list (list(list(str))) –

    list of where statements the form of ['column', 'operator', 'value'] to further filter individual values or rows.

    • Operators are one of:

      =
      !=
      >
      >=
      >
      <
      <=
      LIKE
      NOTLIKE
      ILIKE
      NOTILIKE
      CONTAINS
      STARTSWITH
      ENDSWITH
      
    • Example: ['column_two', '<'. '3']'

Author

Richard Wen <rrwen.dev@gmail.com>

Example

from msdss_base_database import Database
from msdss_data_api.handlers import *
from msdss_data_api.managers import *

# Setup objects
db = Database()
dm = DataManager(database=db)
handler = DataHandler(database=db)

# Check if the table exists and drop if it does
if db.has_table('test_table'):
    db.drop_table('test_table')

# Create sample data
data = [
    {'id': 1, 'column_one': 'a', 'column_two': 2},
    {'id': 2, 'column_one': 'b', 'column_two': 4},
    {'id': 3, 'column_one': 'c', 'column_two': 6},
]
dm.create('test_table', data)

# Handle update
# Should not throw any exceptions
newdata = {'id': 2, 'column_one': 'UPDATED'}
handler.handle_update('test_table', newdata)