From 0b0d9b383079b7627577b790dc2770240907d06c Mon Sep 17 00:00:00 2001 From: HugoNeveux Date: Fri, 1 May 2020 00:01:42 +0200 Subject: [PATCH] Add downloads count for modules --- PDMI/api/views.py | 2 ++ PDMI/store/migrations/0011_module_downloads.py | 18 ++++++++++++++++++ PDMI/store/models.py | 1 + PDMI/store/templates/store/module_detail.html | 3 +++ 4 files changed, 24 insertions(+) create mode 100644 PDMI/store/migrations/0011_module_downloads.py diff --git a/PDMI/api/views.py b/PDMI/api/views.py index 86e7bc2..d128f98 100644 --- a/PDMI/api/views.py +++ b/PDMI/api/views.py @@ -15,9 +15,11 @@ class ModuleDownloadView(View): def get(self, request, req_mod, req_ver): module = get_object_or_404(Module, name=req_mod) version = get_object_or_404(Version, module=module, ver=req_ver) + module.downloads += 1 with open(version.file.path, 'rb') as zf: response = HttpResponse(zf, content_type="application/force-download") response['Content-Disposition'] = f'attachment; filename={module.name}-{version.ver}.zip' + module.save(update_fields=['downloads']) return response class ModuleInfosView(View): diff --git a/PDMI/store/migrations/0011_module_downloads.py b/PDMI/store/migrations/0011_module_downloads.py new file mode 100644 index 0000000..3184841 --- /dev/null +++ b/PDMI/store/migrations/0011_module_downloads.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0 on 2020-04-30 21:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('store', '0010_version_toml'), + ] + + operations = [ + migrations.AddField( + model_name='module', + name='downloads', + field=models.PositiveIntegerField(default=0), + ), + ] diff --git a/PDMI/store/models.py b/PDMI/store/models.py index 008ca2a..7b5447a 100644 --- a/PDMI/store/models.py +++ b/PDMI/store/models.py @@ -31,6 +31,7 @@ class Module(models.Model): creator = models.ForeignKey(User, on_delete=models.SET_NULL, null=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) + downloads = models.PositiveIntegerField(default=0) def __str__(self): return self.name diff --git a/PDMI/store/templates/store/module_detail.html b/PDMI/store/templates/store/module_detail.html index dd023a3..0d379c4 100644 --- a/PDMI/store/templates/store/module_detail.html +++ b/PDMI/store/templates/store/module_detail.html @@ -44,6 +44,9 @@ Download

+

+ Downloaded {{ module.downloads }} times. +

Module by {{ module.creator }}