class AssetType(models.Model): """Stores all asset types""" asset_type = models.CharField(unique=True, max_length=50) created_at = models.DateTimeField(auto_now_add=True, editable=False) last_modified = models.DateTimeField(auto_now=True, editable=False) asset_sub_category = models.ForeignKey(AssetSubCategory, on_delete=models.PROTECT) has_specs = models.BooleanField(default=False) objects = CaseInsensitiveManager() def clean(self): if not self.asset_sub_category: raise ValidationError('Sub category is required') self.asset_type = self.asset_type.title() def save(self, *args, **kwargs): self.full_clean() super().save(*args, **kwargs) class Meta: verbose_name = "Asset Type" ordering = ['-id'] def __str__(self): return self.asset_type
class AssetSubCategory(models.Model): """Stores all asset sub categories""" sub_category_name = models.CharField(unique=True, max_length=40) created_at = models.DateTimeField(auto_now_add=True, editable=False) last_modified = models.DateTimeField(auto_now_add=True, editable=False) asset_category = models.ForeignKey(AssetCategory, on_delete=models.PROTECT) objects = CaseInsensitiveManager() def clean(self): if not self.asset_category: raise ValidationError('Category is required') self.sub_category_name = self.sub_category_name.title() def save(self, *args, **kwargs): self.full_clean() super().save(*args, **kwargs) class Meta: verbose_name_plural = 'Asset SubCategories' ordering = ['-id'] def __str__(self): return self.sub_category_name
class AssetMake(models.Model): """ stores all asset makes """ make_label = models.CharField(unique=True, max_length=40, verbose_name="Asset Make") created_at = models.DateTimeField(auto_now_add=True, editable=False) last_modified_at = models.DateTimeField(auto_now=True, editable=False) asset_type = models.ForeignKey(AssetType, on_delete=models.PROTECT) objects = CaseInsensitiveManager() def clean(self): if not self.asset_type: raise ValidationError('Type is required') self.make_label = self.make_label.title() def save(self, *args, **kwargs): self.full_clean() super().save(*args, **kwargs) class Meta: verbose_name = "Asset Make" ordering = ['-id'] def __str__(self): return self.make_label
class AndelaCentre(models.Model): name = models.CharField(max_length=25, unique=True) country = models.ForeignKey('Country', on_delete=models.PROTECT, null=True) created_at = models.DateTimeField(auto_now_add=True, editable=False) last_modified = models.DateTimeField(auto_now=True, editable=False) objects = CaseInsensitiveManager() class Meta: verbose_name_plural = 'Andela Centres' def __str__(self): return self.name
class AssetModelNumber(models.Model): model_number = models.CharField(unique=True, max_length=100) created_at = models.DateTimeField(auto_now_add=True, editable=False) last_modified = models.DateTimeField(auto_now=True, editable=False) make_label = models.ForeignKey(AssetMake, null=True, on_delete=models.PROTECT, verbose_name="Asset Make") objects = CaseInsensitiveManager() def clean(self): self.model_number = self.model_number.upper() def save(self, *args, **kwargs): self.full_clean() super().save(*args, **kwargs) class Meta: verbose_name = "Asset Model Number" ordering = ['-id'] def __str__(self): return self.model_number
class Asset(models.Model): """Stores all assets""" uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False) asset_code = models.CharField(unique=True, null=True, blank=True, max_length=50) serial_number = models.CharField(unique=True, null=True, blank=True, max_length=50) created_at = models.DateTimeField(auto_now_add=True, editable=False) asset_location = models.ForeignKey('AndelaCentre', blank=True, null=True, on_delete=models.PROTECT) purchase_date = models.DateField(validators=[validate_date], null=True, blank=True) last_modified = models.DateTimeField(auto_now=True, editable=False) assigned_to = models.ForeignKey('AssetAssignee', blank=True, editable=False, null=True, on_delete=models.PROTECT) model_number = models.ForeignKey(AssetModelNumber, null=True, on_delete=models.PROTECT) current_status = models.CharField(editable=False, max_length=50) notes = models.TextField(editable=False, default=" ") specs = models.ForeignKey(AssetSpecs, blank=True, null=True, on_delete=models.PROTECT) verified = models.BooleanField(default=True) objects = CaseInsensitiveManager() def clean(self): if not self.asset_code and not self.serial_number: raise ValidationError( ('Please provide either the serial number, asset code or both.' ), code='required', ) if self.serial_number is None: self.asset_code = self.asset_code.upper() elif self.asset_code is None: self.serial_number = self.serial_number.upper() else: self.asset_code = self.asset_code.upper() self.serial_number = self.serial_number.upper() def save(self, *args, **kwargs): """ Validate either asset code, serial number are provided and an existing status is given """ self.full_clean() try: super().save(*args, **kwargs) except Exception as e: logger.warning(str(e)) else: self._save_initial_asset_status() def _save_initial_asset_status(self): existing_status = AssetStatus.objects.filter(asset=self) if not existing_status: AssetStatus.objects.create(asset=self, current_status=constants.AVAILABLE) self.current_status = constants.AVAILABLE self.save() def __str__(self): return '{}, {}, {}'.format(self.asset_code, self.serial_number, self.model_number) class Meta: ordering = ['-id'] unique_together = ("asset_code", "serial_number")