Skip to content
This repository was archived by the owner on Jul 26, 2024. It is now read-only.
Open
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
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 99
19 changes: 15 additions & 4 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,28 @@ name: Python package

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
build:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup python environment
uses: actions/setup-python@v3
with:
python-version: '3.9'
- name: Lint check
uses: pre-commit/action@v2.0.3

build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ 3.7, 3.8, 3.9 ]
python-version: [3.7, 3.8, 3.9]

steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,6 @@ dmypy.json
.pyre/

# PyCharm
.idea/
.idea/

.vscode/
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: check-json
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
args: ['--profile', 'black', '--filter-files']
- repo: https://github.com/psf/black
rev: 21.12b0
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: '4.0.1'
hooks:
- id: flake8
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"printWidth": 3000,
"bracketSpacing": true
}
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Django ClearCache 🤠🧹💰
# Django ClearCache 🤠🧹💰

![License](https://img.shields.io/pypi/l/django-clearcache)
![Django versions](https://img.shields.io/pypi/djversions/django-clearcache)
Expand Down Expand Up @@ -28,7 +28,7 @@ Allows you to clear Django cache via admin UI or manage.py command.
```

3. Add url to the main **urls.py** right above root admin url:

```
urlpatterns = [
path('admin/clearcache/', include('clearcache.urls')),
Expand Down Expand Up @@ -58,6 +58,14 @@ Allows you to clear Django cache via admin UI or manage.py command.
python manage.py clearcache cache_name
```

## Development

There is a set of pre-commit rules that is validated on GH actions, you could install by youselve and rules will be checked too by running.

```
pre-commit run --all-files
```

## Follow me

1. Check my dev blog with Python and JavaScript tutorials at [https://timonweb.com](https://timonweb.com)
Expand Down
2 changes: 1 addition & 1 deletion clearcache/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


class ClearcacheConfig(AppConfig):
name = 'clearcache'
name = "clearcache"
6 changes: 3 additions & 3 deletions clearcache/management/commands/clearcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@


class Command(BaseCommand):
help = 'Clears cache_id'
help = "Clears cache_id"

def add_arguments(self, parser):
parser.add_argument('cache_name', nargs='?', type=str)
parser.add_argument("cache_name", nargs="?", type=str)

def handle(self, *args, **options):
cache_name = options['cache_name'] or 'default'
cache_name = options["cache_name"] or "default"
try:
clear_cache(cache_name)
self.stdout.write(self.style.SUCCESS(f'Successfully cleared "{cache_name}" cache'))
Expand Down
27 changes: 13 additions & 14 deletions clearcache/templates/admin/index.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{% extends 'admin/index.html' %}

{% block sidebar %}
{{ block.super }}
<div class="app-clear-cache module">
<table>
<caption>
<a href="{% url 'clearcache_admin' %}" class="section" title="Clear cache">Clear cache</a>
</caption>
<tbody>
{% extends 'admin/index.html' %} {% block sidebar %} {{ block.super }}
<div class="app-clear-cache module">
<table>
<caption>
<a href="{% url 'clearcache_admin' %}" class="section" title="Clear cache">Clear cache</a>
</caption>
<tbody>
<tr class="model-user">
<th scope="row"><a href="{% url 'clearcache_admin' %}">Clear cache</a></th>
<th scope="row">
<a href="{% url 'clearcache_admin' %}">Clear cache</a>
</th>
<td></td>
</tr>
</tbody>
</table>
</div>
</tbody>
</table>
</div>
{% endblock %}
23 changes: 10 additions & 13 deletions clearcache/templates/clearcache/admin/clearcache_form.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
{% extends 'admin/base_site.html' %}

{% block content %}
<div id="content-main">
<h2>Clear cache</h2>
<form method="post" action=".">
{% csrf_token %}
{{ form.as_p }}
<div class="submit-row">
<input type="submit" class="button" name="clearcache" value="Clear cache now 💣">
</div>
</form>
</div>
{% extends 'admin/base_site.html' %} {% block content %}
<div id="content-main">
<h2>Clear cache</h2>
<form method="post" action=".">
{% csrf_token %} {{ form.as_p }}
<div class="submit-row">
<input type="submit" class="button" name="clearcache" value="Clear cache now 💣" />
</div>
</form>
</div>
{% endblock %}
16 changes: 7 additions & 9 deletions clearcache/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from django.core.management import call_command
from django.urls import reverse

CACHE_KEY = 'example_cache_key'
CACHE_VALUE = 'example_cache_value'
CACHE_KEY = "example_cache_key"
CACHE_VALUE = "example_cache_value"


def test_cache_works():
Expand All @@ -25,26 +25,26 @@ def test_clears_cache_via_command_line():
cache.set(CACHE_KEY, CACHE_VALUE, 100)
assert cache.get(CACHE_KEY) == CACHE_VALUE, "Cache populated"

call_command('clearcache')
call_command("clearcache")

assert cache.get(CACHE_KEY) is None, "Cache cleared"


@pytest.mark.django_db
def test_clear_cache_is_in_admin_index(admin_client):
response = admin_client.get('/admin/')
response = admin_client.get("/admin/")
assert "Clear cache" in str(response.content)


@pytest.mark.django_db
def test_clear_cache_form_is_rendered(admin_client):
response = admin_client.get(reverse('clearcache_admin'))
response = admin_client.get(reverse("clearcache_admin"))
assert "Clear cache now" in str(response.content), "Clear cache now button is visible"


@pytest.mark.django_db
def test_non_superuser_cant_access_clearcache(client):
response = client.get(reverse('clearcache_admin'))
response = client.get(reverse("clearcache_admin"))
assert response.status_code == 302


Expand All @@ -53,8 +53,6 @@ def test_clears_cache_via_admin_ui(admin_client):
cache.set(CACHE_KEY, CACHE_VALUE, 100)
assert cache.get(CACHE_KEY) == CACHE_VALUE, "Cache populated"

admin_client.post(reverse('clearcache_admin'), {
'cache_name': 'default'
})
admin_client.post(reverse("clearcache_admin"), {"cache_name": "default"})

assert cache.get(CACHE_KEY) is None, "Cache cleared"
3 changes: 2 additions & 1 deletion clearcache/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.urls import path

from .views import ClearCacheAdminView

urlpatterns = [
path('', ClearCacheAdminView.as_view(), name="clearcache_admin"),
path("", ClearCacheAdminView.as_view(), name="clearcache_admin"),
]
16 changes: 11 additions & 5 deletions clearcache/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ClearCacheAdminView(UserPassesTestMixin, FormView):
form_class = ClearCacheForm
template_name = "clearcache/admin/clearcache_form.html"

success_url = reverse_lazy('clearcache_admin')
success_url = reverse_lazy("clearcache_admin")

def test_func(self):
# Only super user can clear caches via admin.
Expand All @@ -24,14 +24,20 @@ def dispatch(self, request, *args, **kwargs):

def form_valid(self, form):
try:
cache_name = form.cleaned_data['cache_name']
cache_name = form.cleaned_data["cache_name"]
clear_cache(cache_name)
messages.success(self.request, f"Successfully cleared '{form.cleaned_data['cache_name']}' cache")
messages.success(
self.request,
f"Successfully cleared '{form.cleaned_data['cache_name']}' cache",
)
except Exception as err:
messages.error(self.request, f"Couldn't clear cache, something went wrong. Received error: {err}")
messages.error(
self.request,
f"Couldn't clear cache, something went wrong. Received error: {err}",
)
return HttpResponseRedirect(self.success_url)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Clear cache'
context["title"] = "Clear cache"
return context
Loading