models.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from hashlib import sha1
  2. from django.db import models
  3. from glitchtip.base_models import CreatedModel
  4. class Release(CreatedModel):
  5. organization = models.ForeignKey(
  6. "organizations_ext.Organization", on_delete=models.CASCADE
  7. )
  8. projects = models.ManyToManyField("projects.Project", related_name="releases")
  9. version = models.CharField(max_length=255)
  10. ref = models.CharField(
  11. max_length=255, null=True, blank=True, help_text="May be branch or tag name"
  12. )
  13. url = models.URLField(null=True, blank=True)
  14. released = models.DateTimeField(null=True, blank=True)
  15. data = models.JSONField(default=dict)
  16. owner = models.ForeignKey(
  17. "users.User",
  18. null=True,
  19. blank=True,
  20. on_delete=models.SET_NULL,
  21. help_text="Release manager - the person initiating the release",
  22. )
  23. commit_count = models.PositiveSmallIntegerField(default=0)
  24. # last commit - not implemented
  25. # authors - not implemented
  26. deploy_count = models.PositiveSmallIntegerField(default=0)
  27. # last_deploy - not implemented
  28. files = models.ManyToManyField("files.File", through="ReleaseFile")
  29. class Meta:
  30. unique_together = ("organization", "version")
  31. class ReleaseProject(models.Model):
  32. """Through model may be used to store cached event counts in the future"""
  33. project = models.ForeignKey("projects.Project", on_delete=models.CASCADE)
  34. release = models.ForeignKey(Release, on_delete=models.CASCADE)
  35. class Meta:
  36. unique_together = ("project", "release")
  37. class ReleaseFile(CreatedModel):
  38. release = models.ForeignKey(Release, on_delete=models.CASCADE)
  39. file = models.ForeignKey("files.File", on_delete=models.CASCADE)
  40. ident = models.CharField(max_length=40)
  41. name = models.TextField()
  42. class Meta:
  43. unique_together = (("release", "file"), ("release", "ident"))
  44. def save(self, *args, **kwargs):
  45. if not self.ident:
  46. self.ident = type(self).get_ident(self.name)
  47. return super().save(*args, **kwargs)
  48. @classmethod
  49. def get_ident(cls, name, dist=None):
  50. if dist is not None:
  51. dist_name = name + "\x00\x00" + dist
  52. return sha1(dist_name.encode()).hexdigest()
  53. return sha1(name.encode()).hexdigest()