2 from django.conf import settings
3 from django.contrib.auth.models import User
4 from django.db import models
5 from django.utils.translation import ugettext_lazy as _
8 class Service(models.Model):
9 name = models.CharField(_('name'), max_length=255)
10 url = models.URLField(_('URL'), blank=True)
11 key = models.CharField(_('key'), max_length=255, blank=True)
12 uses_ssh = models.BooleanField(_('uses SSH'), default=False)
13 groups = models.ManyToManyField('auth.Group', verbose_name=_('groups'), blank=True)
14 users = models.ManyToManyField(settings.AUTH_USER_MODEL, verbose_name=_('users'), blank=True)
18 verbose_name = _('service')
19 verbose_name_plural = _('services')
24 def save(self, *args, **kwargs):
26 self.key = secrets.token_urlsafe()
27 return super().save(*args, **kwargs)
30 return User.objects.filter(
31 models.Q(service=self) |
32 models.Q(groups__service=self)
36 def for_user(cls, user):
37 return cls.objects.filter(models.Q(users=user) | models.Q(groups__user=user))
40 class Hook(models.Model):
41 service = models.ForeignKey(Service, on_delete=models.CASCADE)
42 email = models.CharField(max_length=255)