summaryrefslogtreecommitdiffstats
path: root/django/engagementmanager/models.py
blob: 0b84172b3f3d51ff9d12f0b1f092de40ef50581c (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
#
# ============LICENSE_START==========================================
# org.onap.vvp/engagementmgr
# ===================================================================
# Copyright © 2017 AT&T Intellectual Property. All rights reserved.
# ===================================================================
#
# Unless otherwise specified, all software contained herein is licensed
# under the Apache License, Version 2.0 (the “License”);
# you may not use this software except in compliance with the License.
# You may obtain a copy of the License at
#
#             http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
#
# Unless otherwise specified, all documentation contained herein is licensed
# under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
# you may not use this documentation except in compliance with the License.
# You may obtain a copy of the License at
#
#             https://creativecommons.org/licenses/by/4.0/
#
# Unless required by applicable law or agreed to in writing, documentation
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ============LICENSE_END============================================
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.
from __future__ import unicode_literals
import uuid
from django.contrib.auth.models import User
from django.db import models
from django.db.models.deletion import SET_NULL
from django.db.models.signals import post_save
from django.utils import timezone
from django.utils.timezone import timedelta
from engagementmanager.service.logging_service import LoggingServiceFactory
from engagementmanager.utils.constants import EngagementStage, ActivityType, \
    NextStepType, CheckListState, CheckListCategory, CheckListDecisionValue, \
    CheckListLineType, \
    RecentEngagementActionType, NextStepState


logger = LoggingServiceFactory.get_logger()


class Role(models.Model):
    uuid = models.CharField(default=uuid.uuid4, max_length=36, unique=True)
    name = models.CharField(max_length=36, unique=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "ice_role"


class Vendor(models.Model):
    uuid = models.CharField(default=uuid.uuid4, max_length=36, unique=True)
    name = models.CharField(max_length=100, unique=True)
    public = models.BooleanField()

    def __str__(self):
        return self.name

    class Meta:
        db_table = "ice_vendor"


class CustomUser(User):
    activation_token = models.CharField(max_length=128, unique=True, null=True)
    activation_token_create_time = models.DateTimeField(
        'activation_token_create_time', default=timezone.now, null=True)
    temp_password = models.CharField(
        max_length=256, null=True, blank=True, default=None)

    def __str__(self):
        return self.email

    class Meta:
        db_table = "ice_custom_user"


class IceUserProfile(models.Model):
    user = models.OneToOneField(
        CustomUser, null=False, on_delete=models.CASCADE)
    uuid = models.CharField(default=uuid.uuid4, max_length=36, unique=True)
    company = models.ForeignKey(Vendor, on_delete=models.PROTECT, null=True)
    phone_number = models.CharField(max_length=30)
    full_name = models.CharField(max_length=30)
    role = models.ForeignKey(Role, on_delete=models.PROTECT, null=True)
    # although number of expected users<10^5 we prefer to index it
    email = models.EmailField('email', unique=True, db_index=True)
    create_time = models.DateTimeField('creation time', default=timezone.now)
    ssh_public_key = models.CharField(
        'ssh_public_key', max_length=1024, null=True, blank=True)
    regular_email_updates = models.BooleanField(default=False)
    email_updates_on_every_notification = models.BooleanField(default=True)
    email_updates_daily_digest = models.BooleanField(default=False)
    is_service_provider_contact = models.BooleanField(default=False)
    rgwa_access_key = models.CharField(
        max_length=1024, null=True, blank=True, unique=True)
    rgwa_secret_key = models.CharField(
        max_length=1024, null=True, blank=True, unique=True)
    slack_handle = models.CharField(
        max_length=64, null=True, blank=True, default=None)

    def __str__(self):
        return self.full_name + " - " + self.email

    class Meta:
        db_table = "ice_user_profile"


def create_user_profile(sender, instance, created, **kwargs):
    if created:
        user = CustomUser.objects.get(username=instance)
        profile, created = IceUserProfile.objects.get_or_create(
            user=user, email=instance)


post_save.connect(create_user_profile, sender=CustomUser)


# Represents Deployment Target Cloud Version. For example name=AIC version=2.0
class DeploymentTarget(models.Model):
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, primary_key=True)
    name = models.CharField(max_length=45)
    version = models.CharField(max_length=100)
    weight = models.IntegerField(default=1)
    ui_visibility = models.BooleanField(default=True)

    def __str__(self):
        return self.name + ", Version: " + self.version

    class Meta:
        db_table = "ice_deployment_target"
        unique_together = (("name", "version"),)


# Represents ECOMPRelease
class ECOMPRelease(models.Model):
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, primary_key=True)
    name = models.CharField(max_length=45)
    weight = models.IntegerField(default=1)
    ui_visibility = models.BooleanField(default=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "ice_ecomp_release"


# Represents Deployment Target site, for example: Willows. It is connected
# to deployment target version
class DeploymentTargetSite(models.Model):
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, primary_key=True)
    name = models.CharField(max_length=45)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "ice_deployment_target_site"


def get_default_target_completion_date():
    return timezone.now() + timedelta(days=16)


class Engagement(models.Model):
    # no need to index this since index already exists thanks to it PK
    # characteristics
    uuid = models.CharField(
        default=uuid.uuid4, max_length=64, primary_key=True)
    engagement_team = models.ManyToManyField(
        IceUserProfile, related_name='members')
    creator = models.ForeignKey(
        IceUserProfile,
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name='Engagement_creator')
    contact_user = models.ForeignKey(
        IceUserProfile,
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name='Engagement_contact_user')
    engagement_manual_id = models.CharField(
        max_length=36,
        null=False,
        blank=False,
        default=-1,
        db_index=True)  # index in favor of dashboard search
    progress = models.IntegerField(default=0)
    target_completion_date = models.DateField(
        null=True, blank=True, default=get_default_target_completion_date)
    engagement_stage = models.CharField(
        max_length=15,
        default=EngagementStage.Intake.name,
        choices=EngagementStage.choices(),
        db_index=True)
    # Can be: Intake, Active, Validated, Completed @UndefinedVariable
    create_time = models.DateTimeField('creation time', default=timezone.now)
    peer_reviewer = models.ForeignKey(
        IceUserProfile,
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name='Engagement_peer_reviewer')
    reviewer = models.ForeignKey(
        IceUserProfile,
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name='Engagement_el_reviewer')
    starred_engagement = models.ManyToManyField(
        IceUserProfile, default=None, blank=True)
    heat_validated_time = models.DateTimeField(
        'heat validated time', null=True, blank=True)
    image_scan_time = models.DateTimeField(
        'image scan time', null=True, blank=True)
    aic_instantiation_time = models.DateTimeField(
        'aic instantiation time', null=True, blank=True)
    asdc_onboarding_time = models.DateTimeField(
        'asdc onboarding time', null=True, blank=True)
    started_state_time = models.DateTimeField(
        'started state time', null=True, blank=True)
    intake_time = models.DateTimeField('intake time', null=True, blank=True)
    active_time = models.DateTimeField('active time', null=True, blank=True)
    validated_time = models.DateTimeField(
        'validated time', null=True, blank=True)
    completed_time = models.DateTimeField(
        'completed time', null=True, blank=True)
    archive_reason = models.TextField(default=None, null=True)
    archived_time = models.DateTimeField(
        'archived time', null=True, blank=True)
    is_with_files = models.BooleanField(default=False)

    def __str__(self):
        return " uuid: " + str(self.uuid)

    class Meta:
        db_table = "ice_engagement"


class EngagementStatus(models.Model):
    uuid = models.CharField(
        default=uuid.uuid4, max_length=64, primary_key=True)
    engagement = models.ForeignKey(Engagement, on_delete=models.PROTECT)
    description = models.CharField(max_length=256)
    creator = models.ForeignKey(
        IceUserProfile,
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name='status_creator')
    create_time = models.DateTimeField(default=timezone.now)
    update_time = models.DateTimeField(default=timezone.now)

    class Meta:
        db_table = "ice_engagement_status"


class Activity(models.Model):
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, unique=True, primary_key=True)
    create_time = models.DateTimeField(default=timezone.now)
    description = models.CharField(max_length=512)
    is_notification = models.BooleanField(default=False)
    engagement = models.ForeignKey(
        Engagement, on_delete=models.PROTECT, db_index=True, null=True)
    activity_type = models.CharField(
        max_length=36, choices=ActivityType.choices())
    activity_owner = models.ForeignKey(IceUserProfile, null=True, blank=True)
    metadata = models.CharField(max_length=1024)

    def __str__(self):
        return 'Activity created at ' + str(self.create_time) \
            + ', Description: ' + self.description + \
            ', Notification:' + str(self.is_notification) + \
            ', ActivityType=' + str(self.activity_type)

    class Meta:
        ordering = ['-create_time']
        db_table = "ice_activity"
        # index in favor of pullRecentActivities
        index_together = ["engagement", "activity_owner"]


class Notification(models.Model):
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, unique=True, primary_key=True)
    user = models.ForeignKey(IceUserProfile, on_delete=models.CASCADE)
    is_sent = models.BooleanField(default=False)
    is_read = models.BooleanField(default=False)
    activity = models.ForeignKey(Activity, on_delete=models.CASCADE, null=True)

    def __str__(self):
        return str(self.user) + ' ' + str(self.is_sent) + \
            ' ' + str(self.is_read)

    class Meta:
        db_table = "ice_notification"
        # index in favor of Notification screen (pull_recent_notifications,
        # num_of_notifications_for_user)
        index_together = ["user", "is_read"]


class Feedback(models.Model):
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, unique=True, primary_key=True)
    create_time = models.DateTimeField(default=timezone.now)
    user = models.ForeignKey(IceUserProfile, null=False)
    description = models.TextField('feedback_description')

    def __str__(self):
        return 'Feedback created at ' + \
            str(self.create_time) + ' ' + str(self.user) + \
            ', Description: ' + self.description

    class Meta:
        db_table = "ice_feedback"
        # index in favor of Notification screen (pull_recent_notifications,
        # num_of_notifications_for_user)
        index_together = ["user"]


class ApplicationServiceInfrastructure(models.Model):
    name = models.CharField(max_length=100, unique=True)
    uuid = models.CharField(default=uuid.uuid4, max_length=36, unique=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "ice_application_service_infrastructure"
        unique_together = (('name', 'uuid'),)


class VF(models.Model):
    # in favor of dashboard search by keyword
    name = models.CharField(max_length=100, db_index=True)
    # in favor of dashboard search by keyword
    version = models.CharField(max_length=100, db_index=True, null=True)
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, unique=True, primary_key=True)
    # index in favor of getRecentEng, getStarredEng
    engagement = models.OneToOneField(
        Engagement, null=False, blank=False, default=-1, db_index=True)
    deployment_target = models.ForeignKey(
        DeploymentTarget, on_delete=models.SET_NULL, null=True, blank=True)
    ecomp_release = models.ForeignKey(ECOMPRelease, null=True, blank=False)
    deployment_target_sites = models.ManyToManyField(
        DeploymentTargetSite,
        default=None,
        blank=True,
        related_name='DeployTarget_sites')
    is_service_provider_internal = models.BooleanField(default=False)
    vendor = models.ForeignKey(Vendor, on_delete=models.PROTECT)
    git_repo_url = models.CharField(max_length=512, blank=False, default=-1)
    target_lab_entry_date = models.DateField(
        'target_lab_entry_date', null=False)

    def jenkins_job_name(self):
        if not self.engagement.engagement_manual_id:
            raise ValueError(
                "engagement_manual_id (%s) is not valid for jenkins job name" %
                self.engagement.engagement_manual_id)
        return "{self.name}_{self.engagement.engagement_manual_id}".format(
            self=self)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "ice_vf"


class VFC(models.Model):
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, unique=True, primary_key=True)
    vf = models.ForeignKey(VF, on_delete=models.PROTECT, db_index=True)
    # in favor of dashboard search by keyword
    name = models.CharField(max_length=100, db_index=True)
    external_ref_id = models.CharField(max_length=20, default='')
    ice_mandated = models.BooleanField(default=False)
    company = models.ForeignKey(Vendor, on_delete=SET_NULL, null=True)
    create_time = models.DateTimeField('creation time', default=timezone.now)
    creator = models.ForeignKey(
        IceUserProfile,
        on_delete=models.PROTECT,
        null=True,
        blank=True,
        related_name='Vfc_creator')

    def __str__(self):
        return self.name

    class Meta:
        db_table = "ice_vfc"


class RecentEngagement(models.Model):
    uuid = models.CharField(
        default=uuid.uuid4, max_length=64, primary_key=True)
    vf = models.ForeignKey(VF, on_delete=models.CASCADE)
    user_uuid = models.CharField(max_length=64)
    action_type = models.CharField(
        max_length=36, choices=RecentEngagementActionType.choices())
    last_update = models.DateTimeField('update time', default=timezone.now)

    def __str__(self):
        return " uuid: " + str(self.uuid)

    class Meta:
        db_table = "ice_recent_engagement"

# _________________________ C H E C K - L I S T __________________________


class ChecklistTemplate(models.Model):  # Reference Table
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, primary_key=True)
    name = models.CharField('template name', max_length=255)
    category = models.CharField(
        max_length=36,
        choices=CheckListCategory.choices(),
        default=CheckListCategory.overall.name)  # @UndefinedVariable
    version = models.IntegerField('template version')
    create_time = models.DateTimeField('creation time', default=timezone.now)
    update_time = models.DateTimeField(
        'last update time', null=True, blank=True)

    def __str__(self):
        return self.name + ' ' + self.category

    class Meta:
        db_table = "ice_checklist_template"


class ChecklistSection(models.Model):  # Reference Table
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, primary_key=True)
    name = models.CharField('section name', max_length=255)
    weight = models.FloatField('checklist weight')
    description = models.TextField('section description')
    validation_instructions = models.TextField(
        'section validation instructions')
    create_time = models.DateTimeField('creation time', default=timezone.now)
    update_time = models.DateTimeField(
        'last update time', null=True, blank=True)
    parent_section = models.ForeignKey("self", null=True, blank=True)
    template = models.ForeignKey(
        ChecklistTemplate, on_delete=models.PROTECT, null=False, blank=False)

    def __str__(self):
        return self.name + ' ' + self.description

    class Meta:
        db_table = "ice_checklist_section"


class ChecklistLineItem(models.Model):  # Reference Table
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, primary_key=True)
    name = models.CharField('line name', max_length=255)
    weight = models.FloatField('line weight')
    description = models.TextField('line description')
    line_type = models.CharField(
        max_length=36,
        choices=CheckListLineType.choices(),
        default=CheckListLineType.auto.name)  # @UndefinedVariable
    validation_instructions = models.TextField('line validation instructions')
    create_time = models.DateTimeField('creation time', default=timezone.now)
    update_time = models.DateTimeField(
        'last update time', null=True, blank=True)
    template = models.ForeignKey(
        ChecklistTemplate, on_delete=models.CASCADE, null=False, blank=False)
    section = models.ForeignKey(
        ChecklistSection, on_delete=models.CASCADE, null=False, blank=False)

    def save(self, *args, **kwargs):
        if (self.template != self.section.template is not None):
            raise ValueError(
                "ChecklistLineItem can't be saved/updated \
                since the template " +
                self.template.name +
                " is not equal to its section's template " +
                self.section.template.name)
        super(ChecklistLineItem, self).save(*args, **kwargs)

    def __str__(self):
        return self.name + ' ' + self.description

    class Meta:
        db_table = "ice_checklist_line_item"


class Checklist(models.Model):
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, primary_key=True)
    name = models.CharField('checklist name', max_length=255)
    state = models.CharField(max_length=36, choices=CheckListState.choices(
    ), default=CheckListState.pending.name)  # @UndefinedVariable
    # On each validation cycle this counter is increased
    validation_cycle = models.IntegerField('validation cycle')
    weight = models.FloatField('checklist weight', default=0)
    associated_files = models.TextField('list of files from gitlab')
    create_time = models.DateTimeField('creation time', default=timezone.now)
    update_time = models.DateTimeField(
        'last update time', null=True, blank=True)
    # index in favor of get StarredEngagements (especially for admin and EL)
    engagement = models.ForeignKey(
        Engagement, on_delete=models.CASCADE, db_index=True)
    template = models.ForeignKey(ChecklistTemplate, on_delete=models.PROTECT)
    # The EL that opened the modal.
    creator = models.ForeignKey(
        IceUserProfile,
        on_delete=models.CASCADE,
        related_name='checklist_creator')
    # The user who currently validates the checklist
    owner = models.ForeignKey(
        IceUserProfile,
        on_delete=models.CASCADE,
        related_name='checklist_owner')

    def __str__(self):
        return self.name + ' ' + self.state

    class Meta:
        db_table = "ice_checklist"


class NextStep(models.Model):
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, primary_key=True)
    create_time = models.DateTimeField('creation time', default=timezone.now)
    creator = models.ForeignKey(
        IceUserProfile,
        on_delete=models.PROTECT,
        related_name="NextStep_creator")
    last_update_time = models.DateTimeField(
        'last update time', default=timezone.now)
    last_updater = models.ForeignKey(
        IceUserProfile,
        on_delete=models.PROTECT,
        null=True,
        related_name="NextStep_last_updater")
    # Can be: Modified, Added, Completed, Denied
    last_update_type = models.CharField(max_length=15, default='Added')
    position = models.IntegerField()
    description = models.TextField()
    # Can be: Incomplete, Completed
    state = models.CharField(max_length=15, choices=NextStepState.choices())
    # Can be: Intake, Active, Validated, Completed. This field is used to
    # correlate between the engagement stage to the next step stage in favor
    # of UI.
    engagement_stage = models.CharField(max_length=15)
    engagement = models.ForeignKey(
        Engagement, on_delete=models.PROTECT, null=True, blank=True)
    owner = models.ForeignKey(
        IceUserProfile, on_delete=models.PROTECT, null=True, blank=True)
    next_step_type = models.CharField(
        max_length=36,
        choices=NextStepType.choices(),
        default=NextStepType.user_defined.name)  # @UndefinedVariable
    files = models.TextField('list of files', null=True)
    assignees = models.ManyToManyField(
        IceUserProfile, related_name='assignees')
    due_date = models.DateField('due_date', null=True)

    def __str__(self):
        return self.engagement_stage + ' ' + self.state + \
            ' ' + self.description

    class Meta:
        db_table = "ice_next_step"
        verbose_name_plural = 'Next steps'
        # index in favor of nextstep_service.get_next_steps
        index_together = ["engagement_stage", "owner"]


class ChecklistDecision(models.Model):
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, primary_key=True)
    review_value = models.CharField(
        max_length=36,
        choices=CheckListDecisionValue.choices())
    peer_review_value = models.CharField(
        max_length=36, choices=CheckListDecisionValue.choices())
    create_time = models.DateTimeField('creation time', default=timezone.now)
    update_time = models.DateTimeField(
        'last update time', null=True, blank=True)
    checklist = models.ForeignKey(Checklist, on_delete=models.CASCADE)
    template = models.ForeignKey(ChecklistTemplate, on_delete=models.CASCADE)
    lineitem = models.ForeignKey(ChecklistLineItem, on_delete=models.CASCADE)

    def save(self, *args, **kwargs):
        if (self.template != self.checklist.template is not None):
            raise ValueError(
                "ChecklistDecision can't be saved/updated \
                since the template " +
                self.template.name +
                " is not equal to its checklist's template " +
                self.checklist.template.name)
        if (self.template != self.lineitem.section.template is not None):
            raise ValueError(
                "ChecklistDecision can't be saved/updated \
                since the template " +
                self.template.name +
                " is not equal to its lineitem/section's template " +
                self.lineitem.section.template)
        if (self.checklist.template != self.lineitem.section.template
                is not None):
            raise ValueError(
                "ChecklistDecision can't be saved/updated since \
                its checklist's template " +
                self.checklist.template +
                " is not equal to its lineitem/section's template " +
                self.lineitem.section.template)
        super(ChecklistDecision, self).save(*args, **kwargs)

    def __str__(self):
        return 'decision:' + self.uuid + ' ' + self.template.name + \
            ' ' + self.review_value + ' ' + self.peer_review_value

    class Meta:
        db_table = "ice_checklist_decision"


class ChecklistAuditLog(models.Model):
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, primary_key=True)
    category = models.CharField(max_length=255)
    description = models.TextField()
    create_time = models.DateTimeField('creation time', default=timezone.now)
    update_time = models.DateTimeField(
        'last update time', null=True, blank=True)
    creator = models.ForeignKey(IceUserProfile)
    # should be None if decisions is populated
    checklist = models.ForeignKey(
        Checklist, on_delete=models.CASCADE, null=True, blank=True)
    # should be None if checklist is populated
    decision = models.ForeignKey(
        ChecklistDecision, on_delete=models.CASCADE, null=True, blank=True)

    def save(self, *args, **kwargs):
        if (self.checklist is not None and self.decision is not None):
            raise ValueError(
                "ChecklistAuditLog can't be attached \
                to both checklist and decision. Please \
                remove one of them and retry the operation")
        super(ChecklistAuditLog, self).save(*args, **kwargs)

    def __str__(self):
        return self.category + ' ' + self.description

    class Meta:
        db_table = "ice_checklist_audit_log"


class Invitation(models.Model):
    uuid = models.CharField(
        default=uuid.uuid4, max_length=36, primary_key=True)
    engagement_uuid = models.CharField(
        max_length=64, null=False, blank=False, db_index=True)
    invited_by_user_uuid = models.CharField(
        max_length=64, null=False, blank=False, db_index=True)
    email = models.CharField(max_length=255, null=False, blank=False)
    invitation_token = models.CharField(
        max_length=1024,
        null=False,
        blank=False,
        db_index=True)  # index in favor of signup
    accepted = models.BooleanField(default=False)
    create_time = models.DateTimeField(
        'invitation creation time', default=timezone.now)

    def __str__(self):
        return "Invite from " + self.invited_by_user_uuid + " to " + \
            self.email + " for joining engagement " + self.engagement_uuid

    class Meta:
        db_table = "ice_invitation"
        unique_together = (("engagement_uuid", "email"),)