from django.db import models from django.conf import settings import uuid def getRandom(): return str(uuid.uuid4().hex) class Organization(models.Model): name = models.CharField("Organization Name", max_length=300) users = models.ManyToManyField(settings.AUTH_USER_MODEL) def __str__(self): return self.name class Network(models.Model): name = models.CharField("Common Name", max_length=64, blank=True, null=True) extip = models.CharField("External/Public IP", max_length=15) intip = models.CharField("Internal/Private IP", max_length=15) organizations = models.ManyToManyField(Organization) def __str__(self): return ("%s" % self.intip + (" (%s)" % self.name if self.name else "")) class Model(models.Model): name = models.CharField("Model Name", max_length=100, unique=True) extname = models.CharField("Manufacturer Model Name", max_length=100) config = models.TextField("OpenWRT Compile Config", blank=True, null=True) firmware = models.DateTimeField("Firmware Modification Date", auto_now=True) wwan = models.BooleanField("Supports WWAN (External WiFi)", default=False) def __str__(self): return self.name class Wifi(models.Model): serial = models.CharField("Device Serial Number", max_length=12, unique=True) organization = models.ForeignKey(Organization, on_delete=models.CASCADE) ssid = models.CharField("WiFi SSID", max_length=32) bssid = models.CharField("WiFi BSSID", max_length=64, blank=True, null=True) key = models.CharField("WiFi key", max_length=32) ip = models.CharField("Configuration IP", max_length=15, blank=True, null=True) user = models.CharField("Configuration User Name", max_length=32, blank=True, null=True) password = models.CharField("Configuration Password", max_length=32, blank=True, null=True) def __str__(this): return "%s (%s)" % (this.serial, this.ssid) class Device(models.Model): serial = models.CharField("Device Serial Number", max_length=12, unique=True) name = models.CharField("Common Name", max_length=100, default="", blank=True, null=True) model = models.ForeignKey(Model, on_delete=models.CASCADE) organization = models.ForeignKey(Organization, on_delete=models.CASCADE) network = models.ForeignKey(Network, on_delete=models.SET_NULL, blank=True, null=True) wifi = models.ManyToManyField(Wifi, blank=True) curip = models.CharField("Current IP Address", max_length=15, blank=True, null=True) lasttime = models.DateTimeField("Last Received IP", blank=True, null=True, editable=False) lastbeat = models.DateTimeField("Last Received Timestamp", blank=True, null=True, editable=False) secret = models.CharField("Secret", default=getRandom, max_length=128) password = models.CharField("Device Password", default=getRandom, max_length=128) vpnconfig = models.TextField("VPN Configuration", blank=True, null=True, editable=False) firmware = models.DateTimeField("Firmware Creation Time", blank=True, null=True, editable=False) update = models.BooleanField("Trigger Firmware Update", default=False) reboot = models.BooleanField("Trigger Reboot", default=False) changed = models.DateTimeField("Last Change of Device Config") def __str__(self): return "%s%s" % (self.serial, " (%s)" % self.curip if self.curip else "")