6e945ce0c74da27065e5f002b57ac844f7ad91ff
[cas.git] / src / services / models.py
1 from django.conf import settings
2 from django.contrib.auth.models import User
3 from django.db import models
4
5
6 class Service(models.Model):
7     name = models.CharField(max_length=255)
8     url = models.URLField(blank=True)
9     key = models.CharField(max_length=255, blank=True)
10     uses_ssh = models.BooleanField(default=False)
11     groups = models.ManyToManyField('auth.Group', blank=True)
12     users = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True)
13
14     class Meta:
15         ordering = ('name', )
16
17     def __str__(self):
18         return self.name
19
20     def all_users(self):
21         return User.objects.filter(
22                 models.Q(service=self) |
23                 models.Q(groups__service=self)
24             ).distinct()
25
26     @classmethod
27     def for_user(cls, user):
28         return cls.objects.filter(models.Q(users=user) | models.Q(groups__user=user))
29
30
31 class Hook(models.Model):
32     service = models.ForeignKey(Service, on_delete=models.CASCADE)
33     email = models.CharField(max_length=255)