-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from tarsil/feature/extra_object
SaffierExtra
- Loading branch information
Showing
16 changed files
with
249 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Extras | ||
|
||
This section refers to the extras that Saffier offer and can be used in your application without | ||
incurring into extra overhead to make it happen. | ||
|
||
If you are in this section, you surely read about the [auto discovery](./migrations/discovery.md) | ||
and how it relates with the way Saffier handles and manages migrations for you. | ||
|
||
But, what if you simply would like to use the [shell](./shell.md) or any related command offered | ||
by Saffier that doesn't necessarily requires migration management? | ||
|
||
The Migrate object is the way of Saffier knowing what to do and how to manage your models but there | ||
are cases where that doesn't happen and it is not needed, for example, | ||
**a project using [reflect models](./reflection.md)**. | ||
|
||
A project using reflect models, means that somehow migrations are managed externally and not by | ||
Saffier and Saffier only needs to reflect those tables back into your code, so, do you really need | ||
the **Migrate** object here? **Short anwser is no**. | ||
|
||
So how can you still use those features without depending on the Migrate object? Enters | ||
[SaffierExtra](#saffierextra). | ||
|
||
## SaffierExtra | ||
|
||
This is the object you want to use when **you don't need Saffier to manage the migrations for you** | ||
and yet still being able to use Saffier tools like the [shell](./shell.md). | ||
|
||
### How does it work | ||
|
||
Well, its actually very similar to Migrate object in terms of setup. | ||
|
||
Let us use [Esmerald](https://esmerald.dev) again as an example like we did for the | ||
[tips and tricks](./tips-and-tricks.md). | ||
|
||
```python hl_lines="12 47" | ||
{!> ../docs_src/extras/app.py !} | ||
``` | ||
|
||
And that is it, you can use any tool that does not relate with migrations in your application. | ||
|
||
!!! Warning | ||
Be aware of the use of this special class in production! It is advised not to use it there. | ||
|
||
## Note | ||
|
||
For now, besides the migrations and the shell, Saffier does not offer any extra tools but there are | ||
plans to add more extras in the future and `SaffierExtra` is the way to go for that setup. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env python | ||
""" | ||
Generated by 'esmerald-admin createproject' | ||
""" | ||
import os | ||
import sys | ||
from pathlib import Path | ||
|
||
from esmerald import Esmerald, Include | ||
|
||
import saffier | ||
from saffier import Database, Registry, SaffierExtra | ||
|
||
database = Database("sqlite:///db.sqlite") | ||
registry = Registry(database) | ||
|
||
|
||
class CustomModel(saffier.Model): | ||
name = saffier.CharField(max_length=255) | ||
email = saffier.EmailField(max_length=255) | ||
|
||
class Meta: | ||
registry = registry | ||
|
||
|
||
def build_path(): | ||
""" | ||
Builds the path of the project and project root. | ||
""" | ||
Path(__file__).resolve().parent.parent | ||
SITE_ROOT = os.path.dirname(os.path.realpath(__file__)) | ||
|
||
if SITE_ROOT not in sys.path: | ||
sys.path.append(SITE_ROOT) | ||
sys.path.append(os.path.join(SITE_ROOT, "apps")) | ||
|
||
|
||
def get_application(): | ||
""" | ||
This is optional. The function is only used for organisation purposes. | ||
""" | ||
|
||
app = Esmerald( | ||
routes=[Include(namespace="my_project.urls")], | ||
) | ||
|
||
SaffierExtra(app=app, registry=registry) | ||
return app | ||
|
||
|
||
app = get_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .extra import SaffierExtra | ||
|
||
__all__ = ["SaffierExtra"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Any | ||
|
||
|
||
class BaseExtra(ABC): | ||
@abstractmethod | ||
def set_saffier_extension(self, app: Any) -> None: | ||
raise NotImplementedError( | ||
"Any class implementing the extra must implement set_saffier_extension() ." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from dataclasses import dataclass | ||
from typing import TYPE_CHECKING, Any | ||
|
||
from saffier.core.extras.base import BaseExtra | ||
from saffier.core.terminal import Print, Terminal | ||
from saffier.migrations.constants import SAFFIER_DB, SAFFIER_EXTRA | ||
|
||
if TYPE_CHECKING: | ||
from saffier.core.registry import Registry | ||
|
||
object_setattr = object.__setattr__ | ||
terminal = Terminal() | ||
printer = Print() | ||
|
||
|
||
@dataclass | ||
class Config: | ||
app: Any | ||
registry: "Registry" | ||
|
||
|
||
class SaffierExtra(BaseExtra): | ||
def __init__(self, app: Any, registry: "Registry", **kwargs: Any) -> None: | ||
super().__init__(**kwargs) | ||
self.app = app | ||
self.registry = registry | ||
|
||
self.set_saffier_extension(self.app, self.registry) | ||
|
||
def set_saffier_extension(self, app: Any, registry: "Registry") -> None: | ||
""" | ||
Sets a saffier dictionary for the app object. | ||
""" | ||
if hasattr(app, SAFFIER_DB): | ||
printer.write_warning( | ||
"The application already has a Migrate related configuration with the needed information. SaffierExtra will be ignored and it can be removed." | ||
) | ||
return | ||
|
||
config = Config(app=app, registry=registry) | ||
object_setattr(app, SAFFIER_EXTRA, {}) | ||
app._saffier_extra["extra"] = config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.