API

Application Object

class djask.Djask(import_name, config=None, swagger_path='/admin/api/docs', redoc_path='/admin/api/redoc', title='Djask API', version='0.1.0', *args, **kwargs)[source]

The djask object implements an APIFlask application and acts as a central object for all djask applications. You can refer to the flask documentation and the apiflask documentation for more detailed information. Note that config class or dict can be passed directly to the __init__ function. I achieved this by adding an optional argument named config to the argument list.

New in version 0.1.0.

Parameters:
  • swagger_path (Optional[str]) – The url path to swagger-ui web api documentation.

  • redoc_path (Optional[str]) – The url path to redoc web api documentation.

  • config (Optional[Any]) – The config object for the application, can be a dict or another Python object.

  • import_name (str) – Exactly the same as the import_name parameter in Flask.

  • title (Optional[str]) –

  • version (Optional[str]) –

model(model)

A decorator to register a database model directly to the app.

New in version 0.1.0.

Parameters:

model (Type[Model]) – The database model to wrap.

Return type:

Type[Model]

register_model(model)

Register a model.

Parameters:

model (Type[Model]) – The model to register

Returns:

None

Return type:

None

register_models(models)

Register multiple models at a time.

Parameters:

models (Iterable[Type[Model]]) – The models to register

Returns:

None

Return type:

None

Blueprint Objects

class djask.Blueprint(name, import_name, static_folder=None, static_url_path=None, template_folder=None, url_prefix=None, subdomain=None, url_defaults=None, root_path=None, cli_group=<object object>)[source]

Flask’s Blueprint object with some SQL support.

Parameters:
  • name (str) –

  • import_name (str) –

  • static_folder (Optional[Union[str, PathLike]]) –

  • static_url_path (Optional[str]) –

  • template_folder (Optional[str]) –

  • url_prefix (Optional[str]) –

  • subdomain (Optional[str]) –

  • url_defaults (Optional[dict]) –

  • root_path (Optional[str]) –

  • cli_group (Optional[str]) –

model(model)

A decorator to register a database model directly to the app.

New in version 0.1.0.

Parameters:

model (Type[Model]) – The database model to wrap.

Return type:

Type[Model]

register_model(model)

Register a model.

Parameters:

model (Type[Model]) – The model to register

Returns:

None

Return type:

None

register_models(models)

Register multiple models at a time.

Parameters:

models (Iterable[Type[Model]]) – The models to register

Returns:

None

Return type:

None

class djask.APIBlueprint(name, import_name, tag=None, enable_openapi=True, static_folder=None, static_url_path=None, template_folder=None, url_prefix=None, subdomain=None, url_defaults=None, root_path=None, cli_group=<object object>)[source]

APIFlask’s APIBlueprint object with some SQL support.

Parameters:
  • name (str) –

  • import_name (str) –

  • tag (Optional[Union[str, dict]]) –

  • enable_openapi (bool) –

  • static_folder (Optional[str]) –

  • static_url_path (Optional[str]) –

  • template_folder (Optional[str]) –

  • url_prefix (Optional[str]) –

  • subdomain (Optional[str]) –

  • url_defaults (Optional[dict]) –

  • root_path (Optional[str]) –

  • cli_group (Optional[str]) –

model(model)

A decorator to register a database model directly to the app.

New in version 0.1.0.

Parameters:

model (Type[Model]) – The database model to wrap.

Return type:

Type[Model]

register_model(model)

Register a model.

Parameters:

model (Type[Model]) – The model to register

Returns:

None

Return type:

None

register_models(models)

Register multiple models at a time.

Parameters:

models (Iterable[Type[Model]]) – The models to register

Returns:

None

Return type:

None

Admin Decorators

class djask.admin.ext.Admin(app=None, admin_prefix=None, mode=None)[source]

The admin interface for Djask applications.

Parameters:
  • app (Djask) –

  • admin_prefix (Optional[str]) –

  • mode (Tuple[Union[Literal['api', 'ui'], ~typing.Sequence[~typing.Literal['api', 'ui']]]]) –

init_app(app, admin_prefix='/admin', mode=('api', 'ui'))[source]

Another way to initialize the Admin extension.

New in version 0.1.0.

Changed in version 0.5.0.

Parameters:
  • app (Djask) –

  • admin_prefix (Optional[str]) –

  • mode (Optional[Tuple[Union[Literal['api', 'ui'], ~typing.Sequence[~typing.Literal['api', 'ui']]]]]) –

Return type:

None

djask.admin.ui.decorators.admin_required(func)[source]

Decorator to require admin access.

New in version 0.1.0.

Parameters:

func (Callable) – The route to be wrapped by this decorator.

Return type:

Callable

djask.admin.api.decorators.admin_required_api(f)[source]

Require admin access in web api

Parameters:

f (Callable) – The view function/method to be decorated

Returns:

The view function

Return type:

Callable

Helper Functions

djask.helpers.get_model_form(model_name)[source]

Generate and return a form for a model

Parameters:

model_name (str) – The name of the model

Returns:

A tuple of the model and the form

Return type:

tuple[Type[sqlalchemy.orm.decl_api.Model], flask_wtf.form.FlaskForm]

djask.helpers.get_user_from_headers()[source]

Get the user from the request headers. Expected to be called from the web api.

Return type:

AbstractUser | None

djask.helpers.get_user_from_token(token)[source]

Get the user from an access token

Parameters:

token (str) – The access token

Returns:

A user

Return type:

AbstractUser | None

Models

class djask.auth.models.User(*args, **kwargs)[source]

An implementation of the AbstractUser class used for admin interface.

New in version 0.1.0.

created_at
email
id
is_admin
name
password_hash
permissions
updated_at
username
class djask.auth.abstract.AbstractUser(*args, **kwargs)[source]

A base class for all user models.

It enables you to define a user model other than the User model below.

Changed in version 0.7.0: Add permissions

New in version 0.1.0.

add_permission(perm)[source]

Add a permission to a user

Parameters:

perm (Permission) –

Return type:

None

api_token(expiration=604800)[source]

Generate a new API token for the user.

Parameters:

expiration – The expiration time of the token in seconds

Returns:

The API token

Return type:

str

New in version 0.3.0.

Changed in version 0.4.2.

check_password(password)[source]

Check if the password is correct.

Parameters:

password (str) – The password to check

Return type:

bool

New in version 0.1.0.

has_permission(perm)[source]

Check if a permission is in a user’s permissions

Parameters:

perm (Permission) –

Return type:

bool

set_password(password)[source]

Set the password for the user.

Parameters:

password (str) – The password to set

Return type:

None

New in version 0.1.0.

update(data)[source]

Update the user with the given dict.

Parameters:

data (dict[str, Any]) – The dict containing user data

Return type:

None

New in version 0.4.2.

class djask.auth.anonymous.AnonymousUser[source]

An implementation of the AnonymousUserMixin provided by flask-login.

New in version 0.1.0.

exception djask.auth.anonymous.NoDBForAnonymousError[source]
class djask.db.models.PureModel(**kwargs)

Provide a base model class with no pre-built columns.

New in version 0.4.1.

to_dict(exclude=None)

Convert Model to dict.

New in version 0.4.1.

Parameters:

exclude (Optional[Iterable[str]]) –

Return type:

dict[str, Any]

classmethod to_schema()

Convert Model to a marshmallow schema.

New in version 0.4.1.

Return type:

type[marshmallow_sqlalchemy.schema.SQLAlchemyAutoSchema]

class djask.db.models.Model(**kwargs)

The base model class.

New in version 0.1.0.

id = Column(None, Integer(), table=None, primary_key=True, nullable=False)
created_at = Column(None, DateTime(), table=None, default=ColumnDefault(<function datetime.utcnow>))
updated_at = Column(None, DateTime(), table=None, default=ColumnDefault(<function datetime.utcnow>))
to_dict(exclude=None)

Convert Model to dict.

New in version 0.4.1.

Parameters:

exclude (Optional[Iterable[str]]) –

Return type:

dict[str, Any]

classmethod to_schema()

Convert Model to a marshmallow schema.

New in version 0.4.1.

Return type:

type[marshmallow_sqlalchemy.schema.SQLAlchemyAutoSchema]

More Information

This documentation only gives new functionalities and differences between those implemented in Djask and those in Flask and APIFlask.

To learn more about Djask, please refer to the documentations of Flask and APIFlask: