aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/models.py
blob: ca33ed82095fc14035200ce9bfa08b0993d2a7a3 (plain)
1
2
3
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
##############################################################################
# Copyright (c) 2018 Sawyer Bergeron, Parker Berberian, and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################


from django.contrib.auth.models import User
from django.db import models
from django.core.exceptions import PermissionDenied, ValidationError
from django.shortcuts import get_object_or_404
from django.contrib.postgres.fields import JSONField
from django.http import HttpResponseNotFound
from django.urls import reverse
from django.utils import timezone

import json
import uuid
import yaml
import re

from booking.models import Booking
from account.models import Downtime, UserProfile
from dashboard.utils import AbstractModelQuery



class LabManagerTracker:

    @classmethod
    def get(cls, lab_name, token):
        """
        Get a LabManager.

        Takes in a lab name (from a url path)
        returns a lab manager instance for that lab, if it exists
        Also checks that the given API token is correct
        """
        try:
            lab = Lab.objects.get(name=lab_name)
        except Exception:
            raise PermissionDenied("Lab not found")
        if lab.api_token == token:
            return LabManager(lab)
        raise PermissionDenied("Lab not authorized")


class LabManager:
    """
    Handles all lab REST calls.

    handles jobs, inventory, status, etc
    may need to create helper classes
    """

    def __init__(self, lab):
        self.lab = lab

    def get_downtime(self):
        return Downtime.objects.filter(start__lt=timezone.now(), end__gt=timezone.now(), lab=self.lab)

    def get_downtime_json(self):
        downtime = self.get_downtime().first()  # should only be one item in queryset
        if downtime:
            return {
                "is_down": True,
                "start": downtime.start,
                "end": downtime.end,
                "description": downtime.description
            }
        return {"is_down": False}

    def create_downtime(self, form):
        """
        Create a downtime event.

        Takes in a dictionary that describes the model.
        {
          "start": utc timestamp
          "end": utc timestamp
          "description": human text (optional)
        }
        For timestamp structure, https://docs.djangoproject.com/en/2.2/ref/forms/fields/#datetimefield
        """
        Downtime.objects.create(
            start=form.cleaned_data['start'],
            end=form.cleaned_data['end'],
            description=form.cleaned_data['description'],
            lab=self.lab
        )
        return self.get_downtime_json()

    def get_profile(self):
        prof = {}
        prof['name'] = self.lab.name
        prof['contact'] = {
            "phone": self.lab.contact_phone,
            "email": self.lab.contact_email
        }
        prof['host_count'] = [{
            "type": profile.name,
            "count": len(profile.get_resources(lab=self.lab))}
            for profile in ResourceProfile.objects.filter(labs=self.lab)]
        return prof

    def format_user(self, userprofile):
        return {
            "id": userprofile.user.id,
            "username": userprofile.user.username,
            "email": userprofile.email_addr,
            "first_name": userprofile.user.first_name,
            "last_name": userprofile.user.last_name,
            "company": userprofile.company
        }

    def get_users(self):
        userlist = [self.format_user(profile) for profile in UserProfile.objects.select_related("user").all()]

        return json.dumps({"users": userlist})

    def get_user(self, user_id):
        user = User.objects.get(pk=user_id)

        profile = get_object_or_404(UserProfile, user=user)

        return json.dumps(self.format_user(profile))

    def get_status(self):
        return {"status": self.lab.status}

    def set_status(self, payload):
        {}

class APILog(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    call_time = models.DateTimeField(auto_now=True)
    method = models.CharField(null=True, max_length=6)
    endpoint = models.CharField(null=True, max_length=300)
    ip_addr = models.GenericIPAddressField(protocol="both", null=True, unpack_ipv4=False)
    body = JSONField(null=True)

    def __str__(self):
        return "Call to {} at {} by {}".format(
            self.endpoint,
            self.call_time,
            self.user.username
        )


class AutomationAPIManager:
    @staticmethod
    def serialize_booking(booking):
        sbook = {}
        sbook['id'] = booking.pk
        sbook['owner'] = booking.owner.username
        sbook['collaborators'] = [user.username for user in booking.collaborators.all()]
        sbook['start'] = booking.start
        sbook['end'] = booking.end
        sbook['lab'] = AutomationAPIManager.serialize_lab(booking.lab)
        sbook['purpose'] = booking.purpose
        return sbook

    @staticmethod
    def serialize_lab(lab):
        slab = {}
        slab['id'] = lab.pk
        slab['name'] = lab.name
        return slab

    @staticmethod
    def serialize_userprofile(up):
        sup = {}
        sup['id'] = up.pk
        sup['username'] = up.user.username
        return sup

# Needs to exist for migrations
def get_task_uuid():
    pass