html-over-websockets-with-d.../app/website/models.py

92 lines
2.5 KiB
Python
Raw Normal View History

2021-11-08 23:32:13 +01:00
from django.db import models
2021-11-11 22:46:49 +01:00
from django.contrib.auth.models import AbstractBaseUser
from tinymce import models as tinymce_models
2021-11-08 23:32:13 +01:00
class Profile(AbstractBaseUser):
"""User model"""
email = models.EmailField("Email", unique=True)
2021-11-12 13:52:41 +01:00
full_name = models.CharField(max_length=100, verbose_name="Full name", default="")
2021-11-08 23:32:13 +01:00
avatar = models.ImageField(verbose_name="Avatar", upload_to="uploads/avatars/")
USERNAME_FIELD = "email" # make the user log in with the email
def __str__(self):
return self.email
class Category(models.Model):
"""Category model"""
name = models.CharField(max_length=100, verbose_name="Nombre")
class Meta:
ordering = ("name",)
verbose_name = "Categoria"
verbose_name_plural = "Categorias"
def __str__(self):
return self.name
class Talk(models.Model):
"""Talk model"""
title = models.CharField(max_length=100, verbose_name="Título")
category = models.ForeignKey(
Category,
on_delete=models.SET_NULL,
null=True,
2021-11-21 09:21:39 +01:00
related_name="talkcategory",
2021-11-08 23:32:13 +01:00
verbose_name="Categoría",
)
2021-11-21 09:21:39 +01:00
speaker = models.ForeignKey(
2021-11-08 23:32:13 +01:00
Profile,
on_delete=models.SET_NULL,
null=True,
2021-11-21 09:21:39 +01:00
related_name="talkspeaker",
verbose_name="Ponente",
2021-11-08 23:32:13 +01:00
)
2021-11-11 22:46:49 +01:00
image = models.ImageField(verbose_name="Imagen", upload_to="uploads/talks/")
2021-11-08 23:32:13 +01:00
is_draft = models.BooleanField(default=True, verbose_name="¿Es un borrador?")
content = tinymce_models.HTMLField(verbose_name="Contenido")
created_at = models.DateTimeField(auto_now=True, verbose_name="Creado")
@property
def slug(self):
return slugify(self.title)
@property
def reading_time_min(self):
# https://help.medium.com/hc/en-us/articles/214991667-Read-time
READING_SPEED_OF_AN_ADULT = 265
return ceil(
len(strip_tags(self.content).split(" ")) / READING_SPEED_OF_AN_ADULT
)
class Meta:
ordering = ("-created_at",)
verbose_name = "Charla"
verbose_name_plural = "Charlas"
def __str__(self):
return self.title
2021-11-23 21:41:52 +01:00
class Message(models.Model):
username = models.CharField(max_length=100, verbose_name="Alias", default="")
text = models.TextField(max_length=500)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ("-created_at",)
verbose_name = "Message"
verbose_name_plural = "Messages"
def __str__(self):
return self.text[:10] + "..."