Include last-modified in cache

This commit is contained in:
Gabriel Augendre 2022-09-25 21:46:14 +02:00
parent df1881a3a3
commit 672b2bdaf0
7 changed files with 70 additions and 19 deletions

View file

@ -3,7 +3,7 @@ from django.contrib.admin import register
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from solo.admin import SingletonModelAdmin from solo.admin import SingletonModelAdmin
from purchase.models import Basket, BasketItem, CacheEtag, PaymentMethod, Product from purchase.models import Basket, BasketItem, Cache, PaymentMethod, Product
from purchase.templatetags.purchase import currency from purchase.templatetags.purchase import currency
@ -73,4 +73,4 @@ class BasketAdmin(admin.ModelAdmin):
return currency(instance.price) return currency(instance.price)
admin.site.register(CacheEtag, SingletonModelAdmin) admin.site.register(Cache, SingletonModelAdmin)

View file

@ -0,0 +1,17 @@
# Generated by Django 4.1.1 on 2022-09-25 19:41
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("purchase", "0010_rename_basketitemetag_cacheetag"),
]
operations = [
migrations.RenameModel(
old_name="CacheEtag",
new_name="Cache",
),
]

View file

@ -0,0 +1,23 @@
# Generated by Django 4.1.1 on 2022-09-25 19:45
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("purchase", "0011_rename_cacheetag_cache"),
]
operations = [
migrations.RenameField(
model_name="cache",
old_name="value",
new_name="etag",
),
migrations.AddField(
model_name="cache",
name="last_modified",
field=models.DateTimeField(auto_now=True),
),
]

View file

@ -224,12 +224,21 @@ class BasketItem(Model):
] ]
class CacheEtag(SingletonModel): class Cache(SingletonModel):
value = models.UUIDField(default=uuid.uuid4) etag = models.UUIDField(default=uuid.uuid4)
last_modified = models.DateTimeField(auto_now=True)
def __str__(self) -> str: def __str__(self) -> str:
return str(self.value) return str(self.etag)
def refresh(self): def refresh(self):
self.value = uuid.uuid4() self.etag = uuid.uuid4()
self.save() self.save()
def reports_etag(request):
return str(Cache.get_solo().etag)
def reports_last_modified(request):
return Cache.get_solo().last_modified

View file

@ -1,4 +1,4 @@
def basket_item_on_save(sender, **kwargs): def basket_item_on_save(sender, **kwargs):
from purchase.models import CacheEtag from purchase.models import Cache
CacheEtag.get_solo().refresh() Cache.get_solo().refresh()

View file

@ -8,8 +8,7 @@ from django.utils.translation import gettext_lazy as _
from django.views.decorators.http import condition, require_http_methods from django.views.decorators.http import condition, require_http_methods
from purchase.forms import BasketForm from purchase.forms import BasketForm
from purchase.models import Basket from purchase.models import Basket, reports_etag, reports_last_modified
from purchase.views.reports import reports_etag
@require_http_methods(["GET", "POST"]) @require_http_methods(["GET", "POST"])
@ -50,7 +49,7 @@ def update_basket(request: HttpRequest, pk: int) -> HttpResponse:
@permission_required("purchase.view_basket") @permission_required("purchase.view_basket")
@condition(etag_func=reports_etag) @condition(etag_func=reports_etag, last_modified_func=reports_last_modified)
def list_baskets(request: HttpRequest) -> HttpResponse: def list_baskets(request: HttpRequest) -> HttpResponse:
context = {"baskets": Basket.objects.priced().order_by("-id")} context = {"baskets": Basket.objects.priced().order_by("-id")}
return TemplateResponse(request, "purchase/basket_list.html", context) return TemplateResponse(request, "purchase/basket_list.html", context)

View file

@ -18,17 +18,20 @@ from matplotlib.container import BarContainer
from matplotlib.dates import AutoDateLocator, ConciseDateFormatter, HourLocator from matplotlib.dates import AutoDateLocator, ConciseDateFormatter, HourLocator
from matplotlib.figure import Figure from matplotlib.figure import Figure
from purchase.models import Basket, CacheEtag, PaymentMethod, Product, ProductQuerySet from purchase.models import (
Basket,
PaymentMethod,
Product,
ProductQuerySet,
reports_etag,
reports_last_modified,
)
matplotlib.use("SVG") matplotlib.use("SVG")
def reports_etag(request):
return str(CacheEtag.get_solo().value)
@permission_required("purchase.view_basket") @permission_required("purchase.view_basket")
@condition(etag_func=reports_etag) @condition(etag_func=reports_etag, last_modified_func=reports_last_modified)
def products_plots_view(request): def products_plots_view(request):
products = Product.objects.with_turnover().with_sold() products = Product.objects.with_turnover().with_sold()
( (
@ -43,7 +46,7 @@ def products_plots_view(request):
@permission_required("purchase.view_basket") @permission_required("purchase.view_basket")
@condition(etag_func=reports_etag) @condition(etag_func=reports_etag, last_modified_func=reports_last_modified)
def by_hour_plot_view(request): def by_hour_plot_view(request):
baskets = list(Basket.objects.priced().order_by("created_at")) baskets = list(Basket.objects.priced().order_by("created_at"))
context = { context = {
@ -53,7 +56,7 @@ def by_hour_plot_view(request):
@permission_required("purchase.view_basket") @permission_required("purchase.view_basket")
@condition(etag_func=reports_etag) @condition(etag_func=reports_etag, last_modified_func=reports_last_modified)
def reports(request): def reports(request):
template_name = "purchase/reports.html" template_name = "purchase/reports.html"
baskets = list(Basket.objects.priced().order_by("created_at")) baskets = list(Basket.objects.priced().order_by("created_at"))