models.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. from urllib.parse import urlparse
  2. from uuid import uuid4
  3. from django.conf import settings
  4. from django.db import models
  5. from django.utils.text import slugify
  6. from django_extensions.db.fields import AutoSlugField
  7. from glitchtip.base_models import CreatedModel
  8. class Project(CreatedModel):
  9. """
  10. Projects are permission based namespaces which generally
  11. are the top level entry point for all data.
  12. """
  13. slug = AutoSlugField(populate_from=["name", "organization_id"], max_length=50)
  14. name = models.CharField(max_length=64)
  15. organization = models.ForeignKey(
  16. "organizations_ext.Organization",
  17. on_delete=models.CASCADE,
  18. related_name="projects",
  19. )
  20. platform = models.CharField(max_length=64, blank=True, null=True)
  21. first_event = models.DateTimeField(null=True)
  22. scrub_ip_addresses = models.BooleanField(
  23. default=True, help_text="Should project anonymize IP Addresses",
  24. )
  25. class Meta:
  26. unique_together = (("organization", "slug"),)
  27. def __str__(self):
  28. return self.name
  29. def save(self, *args, **kwargs):
  30. first = False
  31. if not self.pk:
  32. first = True
  33. super().save(*args, **kwargs)
  34. if first:
  35. ProjectKey.objects.create(project=self)
  36. @property
  37. def should_scrub_ip_addresses(self):
  38. """ Organization overrides project setting """
  39. return self.scrub_ip_addresses or self.organization.scrub_ip_addresses
  40. def slugify_function(self, content):
  41. """
  42. Make the slug the project name. Validate uniqueness with both name and org id.
  43. This works because when it runs on organization_id it returns an empty string.
  44. """
  45. if isinstance(content, str):
  46. return slugify(self.name)
  47. return ""
  48. class ProjectCounter(models.Model):
  49. """
  50. Counter for issue short IDs
  51. - Unique per project
  52. - Autoincrements on each new issue
  53. - Separate table for performance
  54. """
  55. project = models.OneToOneField(Project, on_delete=models.CASCADE)
  56. value = models.PositiveIntegerField()
  57. class ProjectKey(CreatedModel):
  58. """ Authentication key for a Project """
  59. project = models.ForeignKey(Project, on_delete=models.CASCADE)
  60. label = models.CharField(max_length=64, blank=True)
  61. public_key = models.UUIDField(default=uuid4, unique=True, editable=False)
  62. rate_limit_count = models.PositiveSmallIntegerField(blank=True, null=True)
  63. rate_limit_window = models.PositiveSmallIntegerField(blank=True, null=True)
  64. data = models.JSONField(blank=True, null=True)
  65. def __str__(self):
  66. return str(self.public_key)
  67. @classmethod
  68. def from_dsn(cls, dsn: str):
  69. urlparts = urlparse(dsn)
  70. public_key = urlparts.username
  71. project_id = urlparts.path.rsplit("/", 1)[-1]
  72. try:
  73. return ProjectKey.objects.get(public_key=public_key, project=project_id)
  74. except ValueError:
  75. # ValueError would come from a non-integer project_id,
  76. # which is obviously a DoesNotExist. We catch and rethrow this
  77. # so anything downstream expecting DoesNotExist works fine
  78. raise ProjectKey.DoesNotExist("ProjectKey matching query does not exist.")
  79. @property
  80. def public_key_hex(self):
  81. """ The public key without dashes """
  82. return self.public_key.hex
  83. def dsn(self):
  84. return self.get_dsn()
  85. def get_dsn(self):
  86. urlparts = settings.GLITCHTIP_URL
  87. # If we do not have a scheme or domain/hostname, dsn is never valid
  88. if not urlparts.netloc or not urlparts.scheme:
  89. return ""
  90. return "%s://%s@%s/%s" % (
  91. urlparts.scheme,
  92. self.public_key_hex,
  93. urlparts.netloc + urlparts.path,
  94. self.project_id,
  95. )
  96. def get_dsn_security(self):
  97. urlparts = settings.GLITCHTIP_URL
  98. if not urlparts.netloc or not urlparts.scheme:
  99. return ""
  100. return "%s://%s/api/%s/security/?glitchtip_key=%s" % (
  101. urlparts.scheme,
  102. urlparts.netloc + urlparts.path,
  103. self.project_id,
  104. self.public_key_hex,
  105. )