Browse Source

Add missing downloads app

master
Alice 2 years ago
parent
commit
c0d6718db8
11 changed files with 152 additions and 1 deletions
  1. +0
    -1
      .gitignore
  2. +0
    -0
      downloads/__init__.py
  3. +12
    -0
      downloads/admin.py
  4. +5
    -0
      downloads/apps.py
  5. +43
    -0
      downloads/migrations/0001_initial.py
  6. +0
    -0
      downloads/migrations/__init__.py
  7. +65
    -0
      downloads/models.py
  8. +0
    -0
      downloads/templatetags/__init__.py
  9. +21
    -0
      downloads/templatetags/dltags.py
  10. +3
    -0
      downloads/tests.py
  11. +3
    -0
      downloads/views.py

+ 0
- 1
.gitignore View File

@@ -12,7 +12,6 @@ env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/


+ 0
- 0
downloads/__init__.py View File


+ 12
- 0
downloads/admin.py View File

@@ -0,0 +1,12 @@
from django.contrib import admin
from .models import Download, Platform, Version

# Register your models here.

class VersionAdmin(admin.ModelAdmin):
list_display = ('download', 'platform', 'version', 'latest', 'available')
list_filter = ('download__name', 'platform__name', 'latest', 'available')

admin.site.register(Download)
admin.site.register(Platform)
admin.site.register(Version, VersionAdmin)

+ 5
- 0
downloads/apps.py View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig


class DownloadsConfig(AppConfig):
name = 'downloads'

+ 43
- 0
downloads/migrations/0001_initial.py View File

@@ -0,0 +1,43 @@
# Generated by Django 2.2.1 on 2019-05-14 15:09

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Download',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('slug', models.SlugField()),
('name', models.CharField(max_length=100)),
],
),
migrations.CreateModel(
name='Platform',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('slug', models.SlugField()),
('name', models.CharField(max_length=100)),
],
),
migrations.CreateModel(
name='Version',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('version', models.CharField(max_length=50)),
('latest', models.BooleanField(blank=True, default=False)),
('available', models.BooleanField(blank=True, default=True)),
('url', models.URLField()),
('download', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='downloads.Download')),
('platform', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='downloads.Platform')),
],
),
]

+ 0
- 0
downloads/migrations/__init__.py View File


+ 65
- 0
downloads/models.py View File

@@ -0,0 +1,65 @@
from django.db import models


class Platform(models.Model):
slug = models.SlugField()
name = models.CharField(max_length=100)

def __str__(self):
return self.name


class Download(models.Model):
slug = models.SlugField()
name = models.CharField(max_length=100)

def __str__(self):
return self.name


class VersionManager(models.Manager):
def get_latest(self, download, platform):
if isinstance(download, str):
try:
download = Download.objects.get(slug=download)
except Download.DoesNotExist:
return None

if isinstance(platform, str):
try:
platform = Platform.objects.get(slug=platform)
except Platform.DoesNotExist:
return None

latest = (Version.objects
.filter(platform=platform, download=download)
.filter(available=True)
.order_by('-latest', '-id')
.first())
if not latest:
return None

return latest

class Version(models.Model):
download = models.ForeignKey(Download, on_delete=models.CASCADE, db_index=True)
platform = models.ForeignKey(Platform, on_delete=models.CASCADE, db_index=True)
version = models.CharField(max_length=50)
latest = models.BooleanField(default=False, blank=True)
available = models.BooleanField(default=True, blank=True)
url = models.URLField()

objects = VersionManager()

def save(self, *args, **kwargs):
if self.latest:
# Unmark as latest any other version for this download/platform
latest = Version.objects.filter(platform=self.platform, download=self.download, latest=True)
for k in latest:
k.latest = False
k.save()

super().save(*args, **kwargs)

def __str__(self):
return "%s v%s for %s" % (self.download, self.version, self.platform)

+ 0
- 0
downloads/templatetags/__init__.py View File


+ 21
- 0
downloads/templatetags/dltags.py View File

@@ -0,0 +1,21 @@
from django import template
from django.utils.html import format_html
from downloads.models import Version, Platform
from django.utils.translation import ugettext as _

register = template.Library()

BUTTON_HTML = """
<a class="pure-button pure-button-primary download-button" href="{url}">{title}</a>
[(sig)]({url}.asc)
"""

@register.simple_tag
def download_button(download, platform):
latest = Version.objects.get_latest(download, platform)
if not latest:
return format_html("\ufffc")

title = _("Download {} v{}").format(latest.download, latest.version)
return format_html(BUTTON_HTML.strip(), url=latest.url, title=title)


+ 3
- 0
downloads/tests.py View File

@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.

+ 3
- 0
downloads/views.py View File

@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.

Loading…
Cancel
Save