2 from django.conf import settings
3 from django.contrib.auth.models import User
4 from django.db import models
5 from django.urls import reverse
6 from django.utils.translation import ugettext_lazy as _
9 class Service(models.Model):
10 name = models.CharField(_('name'), max_length=255)
11 url = models.URLField(_('URL'), blank=True)
12 key = models.CharField(_('key'), max_length=255, blank=True)
13 uses_ssh = models.BooleanField(_('uses SSH'), default=False)
15 for_all = models.BooleanField(_('for all'), default=False)
16 description = models.TextField(_('description'), blank=True)
17 icon = models.FileField(_('icon'), blank=True, upload_to='service/icon')
19 groups = models.ManyToManyField('auth.Group', verbose_name=_('groups'), blank=True)
20 users = models.ManyToManyField(settings.AUTH_USER_MODEL, verbose_name=_('users'), blank=True)
24 verbose_name = _('service')
25 verbose_name_plural = _('services')
30 def get_absolute_url(self):
31 return reverse('service_detail', args=[self.pk])
33 def save(self, *args, **kwargs):
35 self.key = secrets.token_urlsafe()
36 return super().save(*args, **kwargs)
39 return User.objects.filter(
40 models.Q(service=self) |
41 models.Q(groups__service=self)
45 def for_user(cls, user):
46 return cls.objects.filter(
47 models.Q(for_all=True) | models.Q(users=user) | models.Q(groups__user=user)
51 class Hook(models.Model):
52 service = models.ForeignKey(Service, on_delete=models.CASCADE)
53 email = models.CharField(max_length=255)