mappa-ricezione-radio/rxmap/rxmapp/models.py

88 lines
2.5 KiB
Python
Raw Normal View History

2020-03-02 01:49:44 +01:00
from django.db import models
2020-03-07 23:57:51 +01:00
from django.urls import reverse
2020-03-02 01:49:44 +01:00
from datetime import datetime
from django.contrib.auth.models import AbstractUser
# Create your models here.
class User(AbstractUser):
2020-03-07 19:42:59 +01:00
@property
def display_name(self):
2020-03-08 01:01:55 +01:00
name = "%s %s" % (self.first_name, self.last_name)
2020-03-07 19:42:59 +01:00
if name:
return name
return self.username
2020-03-02 01:49:44 +01:00
class TipoRadio(models.Model):
2020-03-07 19:22:25 +01:00
class Meta:
2020-03-08 01:01:55 +01:00
verbose_name = "tipo radio"
verbose_name_plural = "tipi radio"
2020-03-02 01:49:44 +01:00
nome = models.CharField(max_length=64, unique=True)
descrizione = models.CharField(max_length=1000)
2020-03-07 19:22:25 +01:00
def __str__(self):
return self.nome
2020-03-02 01:49:44 +01:00
class RapportoRicezione(models.Model):
class Meta:
2020-03-03 18:35:27 +01:00
verbose_name = "rapporto ricezione"
2020-03-08 01:01:55 +01:00
verbose_name_plural = "rapporti ricezione"
2020-03-02 01:49:44 +01:00
2020-03-03 18:35:27 +01:00
author = models.ForeignKey(User, related_name="rapporti", on_delete=models.PROTECT)
tipo_radio = models.ForeignKey(
TipoRadio, on_delete=models.PROTECT, related_name="rapporti"
)
2020-03-02 01:49:44 +01:00
2020-03-07 19:16:49 +01:00
created = models.DateField(default=datetime.now)
2020-03-02 01:49:44 +01:00
lat = models.FloatField()
lng = models.FloatField()
2020-03-08 01:02:10 +01:00
indirizzo = models.CharField(default='', max_length=400,
help_text='Indirizzo indicativo. Usato solo per descrizione. '
'Per piazzare il marcatore vengono usate solo le coordinate')
2020-03-02 01:49:44 +01:00
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("_")}
2020-03-08 01:02:10 +01:00
d["id"] = self.id
2020-03-02 01:49:44 +01:00
d["colore"] = self.colore
d["radius"] = self.radius
d["explaination"] = self.explaination
return d
@property
def colore(self):
2020-03-08 01:01:55 +01:00
colors = ["darkred", "red", "yellow", "orange", "#90c90d", "green"]
2020-03-03 18:44:22 +01:00
c = int(self.comprensibile)
2020-03-08 00:02:00 +01:00
return colors[c]
2020-03-02 01:49:44 +01:00
@property
def radius(self):
2020-03-03 18:44:22 +01:00
return int(self.stabilita) * 70
2020-03-02 01:49:44 +01:00
@property
def explaination(self):
return """
Ricevuto con %s
Aggiunto il %s da %s
""" % (
self.tipo_radio.nome,
2020-03-07 19:16:49 +01:00
self.created,
2020-03-07 19:42:59 +01:00
self.author.display_name,
2020-03-02 01:49:44 +01:00
)
2020-03-07 23:57:51 +01:00
@property
def edit_link(self):
2020-03-08 01:02:10 +01:00
return reverse(
"admin:rxmapp_rapportoricezione_change", kwargs=dict(object_id=self.id)
)
2020-03-07 23:57:51 +01:00
2020-03-02 01:49:44 +01:00
def __str__(self):
2020-03-08 01:02:10 +01:00
if self.indirizzo:
return self.indirizzo
2020-03-07 19:16:49 +01:00
return "<Rapporto %s del %s>" % (self.pk, self.created)