feat: add public homepage and downloads
This commit is contained in:
+70
-2
@@ -1,3 +1,71 @@
|
||||
from django.db import models
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import RegexValidator
|
||||
from django.db import models, transaction
|
||||
|
||||
# Create your models here.
|
||||
|
||||
class DownloadRelease(models.Model):
|
||||
class Platform(models.TextChoices):
|
||||
WINDOWS = "windows", "Windows"
|
||||
MACOS = "macos", "macOS"
|
||||
LINUX = "linux", "Linux"
|
||||
|
||||
platform = models.CharField("平台", max_length=32, choices=Platform.choices)
|
||||
version = models.CharField("版本", max_length=64)
|
||||
file = models.FileField("安装包文件", upload_to="downloads/", blank=True)
|
||||
external_url = models.URLField("外部下载地址", blank=True)
|
||||
sha256 = models.CharField(
|
||||
"SHA256",
|
||||
max_length=64,
|
||||
blank=True,
|
||||
validators=[
|
||||
RegexValidator(
|
||||
regex=r"^[A-Fa-f0-9]{64}$",
|
||||
message="SHA256 必须是 64 位十六进制字符。",
|
||||
)
|
||||
],
|
||||
)
|
||||
is_current = models.BooleanField("当前版本", default=False)
|
||||
release_notes = models.TextField("发布说明", blank=True)
|
||||
created_at = models.DateTimeField("创建时间", auto_now_add=True)
|
||||
updated_at = models.DateTimeField("更新时间", auto_now=True)
|
||||
|
||||
class Meta:
|
||||
db_table = "download_release"
|
||||
verbose_name = "客户端下载版本"
|
||||
verbose_name_plural = "客户端下载版本"
|
||||
ordering = ("platform", "-is_current", "-created_at", "-id")
|
||||
indexes = [
|
||||
models.Index(
|
||||
fields=("platform", "is_current"),
|
||||
name="dl_platform_current_idx",
|
||||
),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.get_platform_display()} {self.version}"
|
||||
|
||||
@property
|
||||
def download_url(self) -> str:
|
||||
if self.external_url:
|
||||
return self.external_url
|
||||
if self.file:
|
||||
return self.file.url
|
||||
return ""
|
||||
|
||||
@property
|
||||
def has_download(self) -> bool:
|
||||
return bool(self.download_url)
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
if not self.file and not self.external_url:
|
||||
raise ValidationError("安装包文件和外部下载地址至少填写一个。")
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
with transaction.atomic():
|
||||
super().save(*args, **kwargs)
|
||||
if self.is_current:
|
||||
type(self).objects.filter(
|
||||
platform=self.platform,
|
||||
is_current=True,
|
||||
).exclude(pk=self.pk).update(is_current=False)
|
||||
|
||||
Reference in New Issue
Block a user