PDMI/PDMI/store/models.py

47 lines
1.5 KiB
Python

from django.db import models
from django.db.models.signals import pre_save
from django.dispatch import receiver
from django.utils import timezone
import PDMI.settings as settings
import os
from django.contrib.auth.models import User
from markdownx.models import MarkdownxField
def upload_path(instance, filename):
path = os.path.join(
'modules', instance.module.name.lower(), instance.ver, instance.module.name+'.zip')
if os.path.isfile(os.path.join(settings.MEDIA_ROOT, path)):
# Delete file if already exists
os.remove(os.path.join(settings.MEDIA_ROOT, path))
return path
class Module(models.Model):
name = models.CharField(max_length=255, unique=True)
desc = models.TextField(max_length=2048, null=True)
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)
def __str__(self):
return self.name
class Version(models.Model):
module = models.ForeignKey(Module, on_delete=models.CASCADE)
ver = models.CharField(max_length=15)
bot_ver = models.CharField(max_length=15)
metamodule = models.BooleanField(default=False)
file = models.FileField(upload_to=upload_path)
readme = MarkdownxField(default="No readme provided.")
def __str__(self):
return self.ver
class Dependency(models.Model):
version = models.ForeignKey(Version, on_delete=models.CASCADE)
dep_module = models.CharField(max_length=255)
dep_version = models.CharField(max_length=15)