Add missing downloads app

master
Alice 6 years ago
parent 64a4f96db4
commit c0d6718db8

1
.gitignore vendored

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

@ -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)

@ -0,0 +1,5 @@
from django.apps import AppConfig
class DownloadsConfig(AppConfig):
name = 'downloads'

@ -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 +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 +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)

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.
Loading…
Cancel
Save