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

69 lines
1.8 KiB
Python
Raw Normal View History

2020-03-02 01:49:44 +01:00
from django.db import models
from datetime import datetime
from django.contrib.auth.models import AbstractUser
# Create your models here.
class User(AbstractUser):
pass
class TipoRadio(models.Model):
2020-03-03 18:35:27 +01:00
# tid = db.Column(db.Integer, primary_key=True, nullable=False)
2020-03-02 01:49:44 +01:00
nome = models.CharField(max_length=64, unique=True)
descrizione = models.CharField(max_length=1000)
2020-03-03 18:35:27 +01:00
# rapporti = db.relationship("RapportoRicezione")
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"
# verbose_plural_name = '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
date = 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["colore"] = self.colore
d["radius"] = self.radius
d["explaination"] = self.explaination
return d
@property
def colore(self):
c = self.comprensibile
if c > 3:
return "green"
if c > 1:
return "yellow"
return "red"
@property
def radius(self):
return self.stabilita * 70
@property
def explaination(self):
return """
Ricevuto con %s
Aggiunto il %s da %s
""" % (
self.tipo_radio.nome,
self.date,
self.author.username,
)
def __str__(self):
2020-03-03 18:35:27 +01:00
return "<Rapporto %s del %s>" % (self.pk, self.date)