feat: add import template download link
This commit is contained in:
@@ -70,3 +70,64 @@ class DownloadRelease(models.Model):
|
||||
platform=self.platform,
|
||||
is_current=True,
|
||||
).exclude(pk=self.pk).update(is_current=False)
|
||||
|
||||
|
||||
class ImportTemplate(models.Model):
|
||||
name = models.CharField("模板名称", max_length=128)
|
||||
file = models.FileField("模板文件", upload_to="import_templates/", 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)
|
||||
notes = models.TextField("说明", blank=True)
|
||||
created_at = models.DateTimeField("创建时间", auto_now_add=True)
|
||||
updated_at = models.DateTimeField("更新时间", auto_now=True)
|
||||
|
||||
class Meta:
|
||||
db_table = "import_template"
|
||||
verbose_name = "导入模板"
|
||||
verbose_name_plural = "导入模板"
|
||||
ordering = ("-is_current", "-created_at", "-id")
|
||||
indexes = [
|
||||
models.Index(
|
||||
fields=("is_current",),
|
||||
name="it_current_idx",
|
||||
),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@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(is_current=True).exclude(pk=self.pk).update(
|
||||
is_current=False
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user