mappa-ricezione-radio/rxmap/rxmapp/models.py
2020-03-08 00:02:00 +01:00

79 lines
2.2 KiB
Python

from django.db import models
from django.urls import reverse
from datetime import datetime
from django.contrib.auth.models import AbstractUser
# Create your models here.
class User(AbstractUser):
@property
def display_name(self):
name = '%s %s' % (self.first_name, self.last_name)
if name:
return name
return self.username
class TipoRadio(models.Model):
class Meta:
verbose_name = 'tipo radio'
verbose_name_plural = 'tipi radio'
nome = models.CharField(max_length=64, unique=True)
descrizione = models.CharField(max_length=1000)
def __str__(self):
return self.nome
class RapportoRicezione(models.Model):
class Meta:
verbose_name = "rapporto ricezione"
verbose_name_plural = 'rapporti ricezione'
author = models.ForeignKey(User, related_name="rapporti", on_delete=models.PROTECT)
tipo_radio = models.ForeignKey(
TipoRadio, on_delete=models.PROTECT, related_name="rapporti"
)
created = models.DateField(default=datetime.now)
lat = models.FloatField()
lng = models.FloatField()
comprensibile = models.IntegerField() # TODO: >= 0 <= 5
stabilita = models.IntegerField() # TODO: >= 0 <= 5
def serialize(self):
d = {k: v for k, v in self.__dict__.items() if not k.startswith("_")}
d['id'] = self.id
d["colore"] = self.colore
d["radius"] = self.radius
d["explaination"] = self.explaination
return d
@property
def colore(self):
colors = ['darkred', 'red', 'yellow', 'orange', '#90c90d', 'green']
c = int(self.comprensibile)
return colors[c]
@property
def radius(self):
return int(self.stabilita) * 70
@property
def explaination(self):
return """
Ricevuto con %s
Aggiunto il %s da %s
""" % (
self.tipo_radio.nome,
self.created,
self.author.display_name,
)
@property
def edit_link(self):
return reverse('admin:rxmapp_rapportoricezione_change', kwargs=dict(object_id=self.id))
def __str__(self):
return "<Rapporto %s del %s>" % (self.pk, self.created)