aboutsummaryrefslogtreecommitdiffstats
path: root/src/workflow/booking_workflow.py
blob: ef89804460b37b3f96a7dfff8b8884ebc9ed8472 (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.
# Copyright (c) 2020 Sawyer Bergeron, Sean Smith, 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.utils import timezone

from datetime import timedelta

from booking.models import Booking
from workflow.models import WorkflowStep, AbstractSelectOrCreate
from workflow.forms import ResourceSelectorForm, BookingMetaForm, OPNFVSelectForm
from resource_inventory.models import OPNFVConfig, ResourceTemplate
from django.db.models import Q


"""
subclassing notes:
    subclasses have to define the following class attributes:
        self.repo_key: main output of step, where the selected/created single selector
            result is placed at the end
        self.confirm_key:
"""


class Abstract_Resource_Select(AbstractSelectOrCreate):
    form = ResourceSelectorForm
    template = 'dashboard/genericselect.html'
    title = "Select Resource"
    description = "Select a resource template to use for your deployment"
    short_title = "pod select"

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.select_repo_key = self.repo.SELECTED_RESOURCE_TEMPLATE
        self.confirm_key = self.workflow_type

    def alert_bundle_missing(self):
        self.set_invalid("Please select a valid resource template")

    def get_form_queryset(self):
        user = self.repo_get(self.repo.SESSION_USER)
        return ResourceTemplate.objects.filter((Q(owner=user) | Q(public=True)))

    def get_page_context(self):
        return {
            'select_type': 'resource',
            'select_type_title': 'Resource template',
            'addable_type_num': 1
        }

    def put_confirm_info(self, bundle):
        confirm_dict = self.repo_get(self.repo.CONFIRMATION)
        if self.confirm_key not in confirm_dict:
            confirm_dict[self.confirm_key] = {}
        confirm_dict[self.confirm_key]["Resource Template"] = bundle.name
        self.repo_put(self.repo.CONFIRMATION, confirm_dict)


class Booking_Resource_Select(Abstract_Resource_Select):
    workflow_type = "booking"


class OPNFV_EnablePicker(object):
    pass


class OPNFV_Select(AbstractSelectOrCreate, OPNFV_EnablePicker):
    title = "Choose an OPNFV Config"
    description = "Choose or create a description of how you want to deploy OPNFV"
    short_title = "opnfv config"
    form = OPNFVSelectForm
    enabled = False

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.select_repo_key = self.repo.SELECTED_OPNFV_CONFIG
        self.confirm_key = "booking"

    def alert_bundle_missing(self):
        self.set_invalid("Please select a valid OPNFV config")

    def get_form_queryset(self):
        cb = self.repo_get(self.repo.SELECTED_CONFIG_BUNDLE)
        qs = OPNFVConfig.objects.filter(bundle=cb)
        return qs

    def put_confirm_info(self, config):
        confirm_dict = self.repo_get(self.repo.CONFIRMATION)
        if self.confirm_key not in confirm_dict:
            confirm_dict[self.confirm_key] = {}
        confirm_dict[self.confirm_key]["OPNFV Configuration"] = config.name
        self.repo_put(self.repo.CONFIRMATION, confirm_dict)

    def get_page_context(self):
        return {
            'select_type': 'opnfv',
            'select_type_title': 'OPNFV Config',
            'addable_type_num': 4
        }


class Booking_Meta(WorkflowStep):
    template = 'booking/steps/booking_meta.html'
    title = "Extra Info"
    description = "Tell us how long you want your booking, what it is for, and who else should have access to it"
    short_title = "booking info"

    def get_context(self):
        context = super(Booking_Meta, self).get_context()
        initial = {}
        default = []
        try:
            models = self.repo_get(self.repo.BOOKING_MODELS, {})
            booking = models.get("booking")
            if booking:
                initial['purpose'] = booking.purpose
                initial['project'] = booking.project
                initial['length'] = (booking.end - booking.start).days
            info = self.repo_get(self.repo.BOOKING_INFO_FILE, False)
            if info:
                initial['info_file'] = info
            users = models.get("collaborators", [])
            for user in users:
                default.append(user.userprofile)
        except Exception:
            pass

        owner = self.repo_get(self.repo.SESSION_USER)

        context['form'] = BookingMetaForm(initial=initial, user_initial=default, owner=owner)
        return context

    def post(self, post_data, user):
        form = BookingMetaForm(data=post_data, owner=user)

        forms = self.repo_get(self.repo.BOOKING_FORMS, {})

        forms["meta_form"] = form
        self.repo_put(self.repo.BOOKING_FORMS, forms)

        if form.is_valid():
            models = self.repo_get(self.repo.BOOKING_MODELS, {})
            if "booking" not in models:
                models['booking'] = Booking()
            models['collaborators'] = []
            confirm = self.repo_get(self.repo.CONFIRMATION)
            if "booking" not in confirm:
                confirm['booking'] = {}

            models['booking'].start = timezone.now()
            models['booking'].end = timezone.now() + timedelta(days=int(form.cleaned_data['length']))
            models['booking'].purpose = form.cleaned_data['purpose']
            models['booking'].project = form.cleaned_data['project']
            for key in ['length', 'project', 'purpose']:
                confirm['booking'][key] = form.cleaned_data[key]

            if form.cleaned_data["deploy_opnfv"]:
                self.repo_get(self.repo.SESSION_MANAGER).set_step_statuses(OPNFV_EnablePicker, desired_enabled=True)
            else:
                self.repo_get(self.repo.SESSION_MANAGER).set_step_statuses(OPNFV_EnablePicker, desired_enabled=False)

            userprofile_list = form.cleaned_data['users']
            confirm['booking']['collaborators'] = []
            for userprofile in userprofile_list:
                models['collaborators'].append(userprofile.user)
                confirm['booking']['collaborators'].append(userprofile.user.username)

            info_file = form.cleaned_data.get("info_file", False)
            if info_file:
                self.repo_put(self.repo.BOOKING_INFO_FILE, info_file)

            self.repo_put(self.repo.BOOKING_MODELS, models)
            self.repo_put(self.repo.CONFIRMATION, confirm)
            self.set_valid("Step Completed")
        else:
            self.set_invalid("Please complete the fields highlighted in red to continue")