这是indexloc提供的服务,不要输入任何密码
Skip to content

Add backend for using default static storage #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ MAINTENANCE_MODE_STATE_BACKEND = "maintenance_mode.backends.LocalFileBackend"

# alternatively it is possible to use the default storage backend
MAINTENANCE_MODE_STATE_BACKEND = "maintenance_mode.backends.DefaultStorageBackend"

# alternatively it is possible to use the static storage backend
# make sure that STATIC_ROOT and SATIC_URL are also set
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: SATIC_URL -> STATIC_URL

MAINTENANCE_MODE_STATE_BACKEND = "maintenance_mode.backends.StaticStorageBackend"
```

```python
Expand Down
22 changes: 22 additions & 0 deletions maintenance_mode/backends.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage

Expand Down Expand Up @@ -55,6 +56,27 @@ def set_value(self, value):
default_storage.save(filename, content)


class StaticStorageBackend(AbstractStateBackend):
"""
django-maintenance-mode backend which uses the staticfiles storage.
"""

def get_value(self):
filename = settings.MAINTENANCE_MODE_STATE_FILE_NAME
try:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you check if the file exists before trying to open it?

with staticfiles_storage.open(filename, "r") as statefile:
return self.from_str_to_bool_value(statefile.read())
except IOError:
return False

def set_value(self, value):
filename = settings.MAINTENANCE_MODE_STATE_FILE_NAME
if staticfiles_storage.exists(filename):
staticfiles_storage.delete(filename)
content = ContentFile(self.from_bool_to_str_value(value).encode())
staticfiles_storage.save(filename, content)


class LocalFileBackend(AbstractStateBackend):
"""
django-maintenance-mode backend which uses the local file-sistem.
Expand Down
23 changes: 23 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,29 @@ def test_backend_default_storage(self):
"maintenance_mode.backends.LocalFileBackend"
)

def test_backend_static_storage(self):

self.__reset_state()

settings.MAINTENANCE_MODE_STATE_BACKEND = (
"maintenance_mode.backends.StaticStorageBackend"
)
settings.STATIC_URL = "/static/"
settings.STATIC_ROOT = "static"

backend = core.get_maintenance_mode_backend()
self.assertEqual(backend.get_value(), False)

backend.set_value(True)
self.assertEqual(backend.get_value(), True)

backend.set_value(False)
self.assertEqual(backend.get_value(), False)

settings.MAINTENANCE_MODE_STATE_BACKEND = (
"maintenance_mode.backends.LocalFileBackend"
)

def test_backend_custom_invalid(self):

self.__reset_state()
Expand Down