models.py 2.3 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
2
from django.contrib.auth.models import AbstractUser, Group
from django.db import models
Lukas Burgey's avatar
Lukas Burgey committed
3
from django.forms import ValidationError
Lukas Burgey's avatar
Lukas Burgey committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33


class User(AbstractUser):
    sub = models.CharField(max_length=150, blank=True, null=True)
    password = models.CharField(max_length=150, blank=True, null=True)


def construct_user(user_info):
    return User(
            sub=user_info['sub'],
            name=user_info['name'],
            first_name=user_info['given_name'],
            last_name=user_info['family_name'],
            email=user_info['email'],
            username=user_info['email'],
            )


class Site(models.Model):
    name = models.CharField(max_length=150, unique=True)
    description = models.TextField(max_length=300, blank=True)

    def __str__(self):
        return self.name


class Service(models.Model):
    name = models.CharField(max_length=150, unique=True)
    description = models.TextField(max_length=300, blank=True)
    site = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
34
35
            Site,
            related_name='services')
Lukas Burgey's avatar
Lukas Burgey committed
36
    groups = models.ManyToManyField(
Lukas Burgey's avatar
Lukas Burgey committed
37
38
39
            Group,
            related_name='services',
            blank=True)
Lukas Burgey's avatar
Lukas Burgey committed
40
41
42
43
44
45
46
47
48

    def __str__(self):
        return self.name + '@' + self.site.name


class SSHPublicKey(models.Model):
    name = models.CharField(max_length=150, unique=True)
    key = models.TextField(max_length=1000)
    user = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
49
50
            User,
            related_name='ssh_keys')
Lukas Burgey's avatar
Lukas Burgey committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64

    def __str__(self):
        return self.name


class State(object):
    def __init__(self, user):

        if user.is_authenticated:
            self.services = Service.objects.filter(
                    groups__user=user
                    )
        else:
            self.services = []
Lukas Burgey's avatar
Lukas Burgey committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86


class Deployment(models.Model):
    user = models.ForeignKey(
            User,
            related_name='deployments',
            on_delete=models.CASCADE,
            )
    service = models.ForeignKey(
            Service,
            related_name='deployments',
            on_delete=models.CASCADE,
            )
    # SET_NULL: we allow credentials to be deleted after deployment
    ssh_keys = models.ManyToManyField(
            SSHPublicKey,
            related_name='deployments',
            blank=True,
            )

    def __str__(self):
        return str(self.user) + '@' + str(self.service)