-
Notifications
You must be signed in to change notification settings - Fork 0
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 #71 from stuartmaxwell:plugin-storage
Plugin-storage
- Loading branch information
Showing
11 changed files
with
205 additions
and
10 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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
"""djpress module.""" | ||
|
||
__version__ = "0.12.2" | ||
__version__ = "0.13.0" |
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,24 @@ | ||
# Generated by Django 5.1.3 on 2024-11-20 11:06 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("djpress", "0006_alter_post_parent"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="PluginStorage", | ||
fields=[ | ||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
("plugin_name", models.CharField(max_length=100, unique=True)), | ||
("plugin_data", models.JSONField(default=dict)), | ||
], | ||
options={ | ||
"verbose_name": "plugin storage", | ||
"verbose_name_plural": "plugin storage", | ||
}, | ||
), | ||
] |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
"""Models package for djpress app.""" | ||
|
||
from djpress.models.category import Category | ||
from djpress.models.plugin_storage import PluginStorage | ||
from djpress.models.post import Post | ||
|
||
__all__ = ["Category", "Post"] | ||
__all__ = ["Category", "Post", "PluginStorage"] |
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,62 @@ | ||
"""PluginStorage model for storing plugin data in the database.""" | ||
|
||
from django.db import models | ||
|
||
|
||
class PluginStorageManager(models.Manager): | ||
"""Manager for the PluginStorage model.""" | ||
|
||
def get_data(self, plugin_name: str) -> dict: | ||
"""Get plugin data. | ||
Retrieve the plugin data from the database. If the plugin data does not exist, return an empty dictionary. | ||
Args: | ||
plugin_name (str): The name of the plugin. | ||
Returns: | ||
dict: The plugin data. | ||
""" | ||
try: | ||
data = self.get(plugin_name=plugin_name) | ||
except self.model.DoesNotExist: | ||
return {} | ||
else: | ||
return data.plugin_data or {} | ||
|
||
def save_data(self, plugin_name: str, data: dict) -> None: | ||
"""Save plugin data. | ||
Save or update the plugin data in the database. If no storage exists for this plugin, it will be created. | ||
Args: | ||
plugin_name (str): The name of the plugin. | ||
data (dict): The plugin data. | ||
Returns: | ||
None | ||
""" | ||
storage, created = self.update_or_create( | ||
plugin_name=plugin_name, | ||
defaults={"plugin_data": data}, | ||
) | ||
|
||
|
||
class PluginStorage(models.Model): | ||
"""Model for storing plugin data in the database.""" | ||
|
||
plugin_name = models.CharField(max_length=100, unique=True) | ||
plugin_data = models.JSONField(default=dict) | ||
|
||
# Manager | ||
objects = PluginStorageManager() | ||
|
||
class Meta: | ||
"""Meta options for the PluginStorage model.""" | ||
|
||
verbose_name = "plugin storage" | ||
verbose_name_plural = "plugin storage" | ||
|
||
def __str__(self) -> str: | ||
"""Return the string representation of the plugin item.""" | ||
return self.plugin_name |
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,37 @@ | ||
import pytest | ||
|
||
from djpress.models import PluginStorage | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_plugin_storage_model(): | ||
test_plugin = PluginStorage.objects.create(plugin_name="test_plugin") | ||
|
||
assert str(test_plugin) == "test_plugin" | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_plugin_storage_get_data(): | ||
"""Test getting plugin data.""" | ||
# Test empty case (no storage exists) | ||
data = PluginStorage.objects.get_data("test_plugin") | ||
assert data == {} | ||
|
||
# Test with stored data | ||
PluginStorage.objects.save_data("test_plugin", {"key": "value"}) | ||
data = PluginStorage.objects.get_data("test_plugin") | ||
assert data == {"key": "value"} | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_plugin_storage_save_data(): | ||
"""Test saving plugin data.""" | ||
# Test creating new storage | ||
PluginStorage.objects.save_data("test_plugin", {"key": "value"}) | ||
storage = PluginStorage.objects.get(plugin_name="test_plugin") | ||
assert storage.plugin_data == {"key": "value"} | ||
|
||
# Test updating existing storage | ||
PluginStorage.objects.save_data("test_plugin", {"new_key": "new_value"}) | ||
storage.refresh_from_db() | ||
assert storage.plugin_data == {"new_key": "new_value"} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.