gulasch-r0mstore/roms/models.py

64 lines
2.3 KiB
Python
Raw Permalink Normal View History

import os
import uuid
2017-02-01 22:05:48 +01:00
from django.db import models
from django.core.exceptions import ValidationError
2019-06-07 15:03:29 +02:00
from django.urls import reverse
2017-02-01 22:05:48 +01:00
from taggit.managers import TaggableManager
2017-05-07 01:31:35 +02:00
from stdimage.models import StdImageField
from stdimage.validators import MinSizeValidator
2017-05-14 01:53:16 +02:00
from users.models import User
def upload_cover_to(instance, filename):
_, ext = os.path.splitext(filename)
2017-05-07 01:31:35 +02:00
return "covers/%s%s" % (uuid.uuid4(), ext)
def upload_binary_to(instance, filename):
return "roms/%s.bin" % uuid.uuid4()
2017-02-01 22:05:48 +01:00
class Rom(models.Model):
2017-05-09 00:00:26 +02:00
name = models.CharField("name", max_length = 128, unique=True)
2017-05-25 12:57:54 +02:00
description = models.TextField("Beschreibung", max_length=1024)
2017-05-20 23:38:21 +02:00
cover = StdImageField("cover-Bild",
2017-05-07 01:31:35 +02:00
upload_to = upload_cover_to,
validators = [MinSizeValidator(300,300)],
variations = {'large': {'width': 600, 'height': 600, 'crop': True},
'small': {'width': 300, 'height': 300, 'crop': True}})
2017-05-09 00:00:26 +02:00
low_binary = models.FileField("low binary", upload_to = upload_binary_to)
high_binary = models.FileField("high binary", upload_to = upload_binary_to)
2017-05-14 01:53:16 +02:00
approved = models.BooleanField("approved", default=False)
2017-05-07 01:31:35 +02:00
tags = TaggableManager(blank = True)
2019-06-07 15:03:29 +02:00
user = models.ForeignKey(User, blank=True, null=True, on_delete=models.PROTECT)
2017-05-26 01:57:52 +02:00
download_count = models.IntegerField(default = 0)
2017-05-09 00:00:26 +02:00
creation_time = models.DateTimeField("creation time", auto_now_add = True)
edit_time = models.DateTimeField("edit time", auto_now = True)
2017-05-14 01:53:16 +02:00
def get_absolute_url(self):
return reverse('romdetails', kwargs={'id' : self.pk})
2017-02-01 22:05:48 +01:00
def tag_list(self):
return [t.name for t in self.tags.all()]
def to_json(self):
json = {
'id' : self.pk,
'name' : self.name,
2017-05-16 10:54:22 +02:00
'user' : self.user.username,
'description' : self.description,
'tags' : self.tag_list(),
2017-05-07 01:31:35 +02:00
'low_binary' : self.low_binary.url,
2017-05-12 21:54:25 +02:00
'high_binary' : self.high_binary.url,
2017-05-25 13:11:05 +02:00
'download_count' : self.download_count,
2017-05-12 21:54:25 +02:00
'creation_time' : self.creation_time,
'edit_time' : self.edit_time
}
return json
def __str__(self):
2017-05-07 01:31:35 +02:00
return self.name