utils.py¶
General helper functions.
-
utils.check_csrf_token()[source]¶ Compare the csrf token from the request (header) with the one in the cookie.session.
-
utils.check_mongo_update(document: dict)[source]¶ Make sure that some fields in a document are not changed during an update.
Also make sure indata is not empty.
- Parameters
document (dict) – received input to update a document
-
utils.convert_keys_to_camel(chunk)[source]¶ Convert keys given in snake_case to camelCase.
The capitalization of the first letter is preserved.
- Parameters
chunk – Object to convert
- Returns
chunk converted to camelCase dict, otherwise chunk
- Return type
-
utils.country_list()[source]¶ Provide a list of countries.
- Returns
A selection of countries.
- Return type
list
-
utils.gen_csrf_token() → str[source]¶ Genereate a csrf token.
- Returns
the csrf token
- Return type
str
-
utils.get_dataset(identifier: str)[source]¶ Query for a dataset from the database.
- Parameters
identifier (str) – the uuid of the dataset
- Returns
the dataset
- Return type
dict
-
utils.get_db(dbserver: pymongo.mongo_client.MongoClient) → pymongo.database.Database[source]¶ Get the connection to the MongoDB database.
- Parameters
dbserver – connection to the db
- Returns
the database connection
- Return type
pymongo.database.Database
-
utils.get_dbserver() → pymongo.mongo_client.MongoClient[source]¶ Get the connection to the MongoDB database server.
- Returns
the client connection
- Return type
pymongo.mongo_client.MongoClient
-
utils.get_project(identifier: str)[source]¶ Query for a project from the database.
- Parameters
identifier (str) – the uuid of the project
- Returns
the project
- Return type
dict
-
utils.is_email(indata: str)[source]¶ Check whether a string seems to be an email address or not.
- Parameters
indata (str) – data to check
- Returns
is the indata an email address or not
- Return type
bool
-
utils.is_owner(dataset: str = None, project: str = None)[source]¶ Check if the current user owns the given dataset or project.
If both a dataset and a project is provided, an exception will be raised.
- Parameters
dataset (str) – the dataset to check
project (str) – the project to check
- Returns
whether the current owns the dataset/project
- Return type
bool
- Raises
ValueError – one of dataset or project must be set, and not both
-
utils.make_log(data_type: str, action: str, data: dict = None)[source]¶ Log a change in the system.
Saves a complete copy of the new object.
It is assumed that all values are curated, e.g. that data only contains permitted fields.
- Parameters
action (str) – type of action (insert, update etc)
data_type (str) – the collection name
data (dict) – the new data for the entry
-
utils.make_timestamp()[source]¶ Generate a timestamp of the current time.
- Returns
the current time
- Return type
datetime.datetime
-
utils.new_uuid() → uuid.UUID[source]¶ Generate a uuid for a field in a MongoDB document.
- Returns
the new uuid in binary format
- Return type
uuid.UUID