aboutsummaryrefslogtreecommitdiffstats
path: root/lib/orm/models.py
blob: 9b4f99ceaa66324978ebe7434cb1ac9c53b02acf (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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
#
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
#
# Security Response Tool Implementation
#
# Copyright (C) 2017       Wind River Systems
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from __future__ import unicode_literals

from django.db import models, IntegrityError, DataError
from django.db import transaction

from django.core import validators
from django.conf import settings
import django.db.models.signals

from users.models import SrtUser

import sys
import os
import re
from signal import SIGUSR1
from datetime import datetime
import json

import logging
logger = logging.getLogger("srt")

# quick development/debugging support
from srtgui.api import _log

# Sqlite support

if 'sqlite' in settings.DATABASES['default']['ENGINE']:
    from django.db import OperationalError
    from time import sleep

    _base_save = models.Model.save
    def save(self, *args, **kwargs):
        while True:
            try:
                with transaction.atomic():
                    return _base_save(self, *args, **kwargs)
            except OperationalError as err:
                if 'database is locked' in str(err):
                    logger.warning("%s, model: %s, args: %s, kwargs: %s",
                                   err, self.__class__, args, kwargs)
                    sleep(0.5)
                    continue
                raise

    models.Model.save = save

    # HACK: Monkey patch Django to fix 'database is locked' issue

    from django.db.models.query import QuerySet
    _base_insert = QuerySet._insert
    def _insert(self,  *args, **kwargs):
        with transaction.atomic(using=self.db, savepoint=False):
            return _base_insert(self, *args, **kwargs)
    QuerySet._insert = _insert

    from django.utils import six
    def _create_object_from_params(self, lookup, params):
        """
        Tries to create an object using passed params.
        Used by get_or_create and update_or_create
        """
        try:
            obj = self.create(**params)
            return obj, True
        except (IntegrityError, DataError):
            exc_info = sys.exc_info()
            try:
                return self.get(**lookup), False
            except self.model.DoesNotExist:
                pass
            six.reraise(*exc_info)

    QuerySet._create_object_from_params = _create_object_from_params

    # end of HACK

class GitURLValidator(validators.URLValidator):
    regex = re.compile(
        r'^(?:ssh|git|http|ftp)s?://'  # http:// or https://
        r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'  # domain...
        r'localhost|'  # localhost...
        r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|'  # ...or ipv4
        r'\[?[A-F0-9]*:[A-F0-9:]+\]?)'  # ...or ipv6
        r'(?::\d+)?'  # optional port
        r'(?:/?|[/?]\S+)$', re.IGNORECASE)

def GitURLField(**kwargs):
    r = models.URLField(**kwargs)
    for i in range(len(r.validators)):
        if isinstance(r.validators[i], validators.URLValidator):
            r.validators[i] = GitURLValidator()
    return r

# Core Classes

# Helper class to common mappings
class SRTool():

    # Global date format
    DATE_FORMAT = '%Y-%m-%d'
    DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'

    # SRTool Priority
    UNDEFINED = 0
    LOW = 1
    MEDIUM = 2
    HIGH = 3
    CRITICAL = 4
    PRIORITY_ERROR = 99
    SRT_PRIORITY = (
        (UNDEFINED, 'Undefined'),
        (LOW, 'Low'),
        (MEDIUM, 'Medium'),
        (HIGH, 'High'),
        (CRITICAL, 'Critical'),
    )
    @staticmethod
    def priority_text(index):
        if (0 > index) or (index >= len(SRTool.SRT_PRIORITY)):
            return 'PRIORITY_ERROR'
        return SRTool.SRT_PRIORITY[index][1]
    @staticmethod
    def priority_index(value):
        for item in SRTool.SRT_PRIORITY:
            if value == item[1]:
                return item[0]
        return SRTool.PRIORITY_ERROR

    # SRTool Severity (same integer values as prority)
    SRT_SEVERITY = (
        (UNDEFINED, 'UNDEFINED'),
        (LOW, 'LOW'),
        (MEDIUM, 'MEDIUM'),
        (HIGH, 'HIGH'),
        (CRITICAL, 'CRITICAL'),
    )
    @staticmethod
    def severity_text(index):
        if (0 > index) or (index >= len(SRTool.SRT_SEVERITY)):
            return 'SEVERITY_ERROR'
        return SRTool.SRT_SEVERITY[index][1]
    @staticmethod
    def severity_index(value):
        for item in SRTool.SRT_SEVERITY:
            if value == item[1]:
                return item[0]
        return SRTool.PRIORITY_ERROR

    # SRTool Status
    HISTORICAL = 0
    NEW = 1
    NEW_RESERVED = 2
    INVESTIGATE = 3
    VULNERABLE = 4
    NOT_VULNERABLE = 5
    NEW_INACTIVE = 6
    INVESTIGATE_INACTIVE = 7
    VULNERABLE_INACTIVE = 8
    NOT_VULNERABLE_INACTIVE = 9
    STATUS_ERROR = 99
    SRT_STATUS = (
        (HISTORICAL, 'Historical'),
        (NEW, 'New'),
        (NEW_RESERVED, 'New-Reserved'),
        (INVESTIGATE, 'Investigate'),
        (VULNERABLE, 'Vulnerable'),
        (NOT_VULNERABLE, 'Not Vulnerable'),
        (NEW_INACTIVE, '(New)'),
        (INVESTIGATE_INACTIVE, '(Investigate)'),
        (VULNERABLE_INACTIVE, '(Vulnerable)'),
        (NOT_VULNERABLE_INACTIVE, '(Not Vulnerable)'),
    )
    @staticmethod
    def status_text(index):
        if (0 > index) or (index >= len(SRTool.SRT_STATUS)):
            return 'STATUS_ERROR'
        return SRTool.SRT_STATUS[index][1]
    @staticmethod
    def status_index(value):
        for item in SRTool.SRT_STATUS:
            if value == item[1]:
                return item[0]
        return SRTool.STATUS_ERROR
    @staticmethod
    def status_to_inactive(value):
        if SRTool.NEW == value:
            return SRTool.NEW_INACTIVE
        elif SRTool.INVESTIGATE == value:
            return SRTool.INVESTIGATE_INACTIVE
        elif SRTool.VULNERABLE == value:
            return SRTool.VULNERABLE_INACTIVE
        elif SRTool.NOT_VULNERABLE == value:
            return SRTool.NOT_VULNERABLE_INACTIVE
        else:
            return value
    @staticmethod
    def status_to_active(value):
        if SRTool.NEW_INACTIVE == value:
            return SRTool.NEW
        elif SRTool.INVESTIGATE_INACTIVE == value:
            return SRTool.INVESTIGATE
        elif SRTool.VULNERABLE_INACTIVE == value:
            return SRTool.VULNERABLE
        elif SRTool.NOT_VULNERABLE_INACTIVE == value:
            return SRTool.NOT_VULNERABLE
        else:
            return value

    OPEN = 0
    CLOSED = 1
    FIXED = 2
    NOT_FIX = 3
    OUTCOME_ERROR = 4
    SRT_OUTCOME = (
        (OPEN, 'Open'),
        (CLOSED, 'Closed (Not Vulnerable)'),
        (FIXED, 'Closed (Fixed)'),
        (NOT_FIX, "Closed (Won't Fix)"),
    )
    @staticmethod
    def outcome_text(index):
        if (0 > index) or (index >= len(SRTool.SRT_OUTCOME)):
            return "OUTCOME_ERROR"
        return SRTool.SRT_OUTCOME[index][1]
    @staticmethod
    def outcome_index(value):
        for item in SRTool.SRT_OUTCOME:
            if value == item[1]:
                return item[0]
        return SRTool.OUTCOME_ERROR

    # Publish state
    PUBLISH_UNPUBLISHED = 0
    PUBLISH_NOPUBLISH = 1
    PUBLISH_PUBLISHED = 2
    PUBLISH_REQUEST = 3
    PUBLISH_UPDATE = 4
    PUBLISH_SUBMITTED = 5
    PUBLISH_ERROR = 99
    SRT_PUBLISH_STATE = (
        (PUBLISH_UNPUBLISHED, 'Unpublished'),
        (PUBLISH_NOPUBLISH, 'Not to be Published'),
        (PUBLISH_PUBLISHED, 'Published'),
        (PUBLISH_REQUEST, 'Publish Request (New)'),
        (PUBLISH_UPDATE, 'Publish Request (Update)'),
        (PUBLISH_SUBMITTED, 'Publish Submitted'),
    )
    @staticmethod
    def publish_text(index):
        if (0 > index) or (index >= len(SRTool.SRT_PUBLISH_STATE)):
            return SRTool.SRT_PUBLISH_STATE[SRTool.PUBLISH_ERROR][1]
        return 'PUBLISH_ERROR'
    @staticmethod
    def publish_index(value):
        for item in SRTool.SRT_PUBLISH_STATE:
            if value == item[1]:
                return item[0]
        return SRTool.PUBLISH_ERROR

    # Normalize displayed dates
    @staticmethod
    def date_ymd_text(value):
        if isinstance(value,datetime):
            return(value.strftime("%Y-%m-%d"))
        return(value)

    # Extract dictionary tag values
    @staticmethod
    def get_dict_tag(tag,dict_str,default=None):
        dict = json.loads(dict_str)
        if tag in dict:
            return dict[tag]
        return default


# Helper class to format and track updates
# Enforce strict formatting and content to enable reporting, change filtering, pretty printing
class Update():
    # General history prefix format (type,source,semicolon-joined changes):
    #   UPDATE(User):Priority(%s,%s);Tag();Status(%s,%s) {helpful text}
    #   CREATE(Defect): {Created from defect ABCD-1234}
    # Update report check strings: 'UPDATE(','Priority(','Status('

    # General update label
    UPDATE_STR = "UPDATE(%s):"
    CREATE_STR = "CREATE(%s):"
    UPDATE_PREFIX_STR = "UPDATE("
    CREATE_PREFIX_STR = "CREATE("

    # Update sources
    SOURCE_USER = "User"
    SOURCE_TRIAGE = "Triage"
    SOURCE_CVE = "CVE"
    SOURCE_DEFECT = "Defect"

    # Update labels (no string overlaps allowed)
    NEW_NAME = "New_Name(%s,%s)"
    PRIORITY = "Priority(%s,%s)"
    STATUS = "Status(%s,%s)"
    SEVERITY_V3 = "Severity_V3(%s,%s)"
    SEVERITY_V2 = "Severity_V2(%s,%s)"
    OUTCOME = "Outcome(%s,%s)"
    RELEASE = "Release(%s,%s)"
    DESCRIPTION = "Description()"
    LASTMODIFIEDDATE = "LastModifiedDate(%s,%s)"
    NOTE = "User_Note()"
    PRIVATE_NOTE = "Private_Note()"
    TAG = "Tag()"
    PUBLISH_STATE = "Publish_State(%s,%s)"
    PUBLISH_DATE = "Publish_Date(%s,%s)"
    AFFECTED_COMPONENT = "Affected_Component(%s,%s)"
    ACKNOWLEDGE_DATE = "AcknowledgeDate(%s,%s)"
    ATTACH_CVE = "Attach_CVE(%s)"
    DETACH_CVE = "Detach_CVE(%s)"
    ATTACH_VUL = "Attach_Vulnerability(%s)"
    DETACH_VUL = "Detach_Vulnerability(%s)"
    ATTACH_INV = "Attach_Investigration(%s)"
    DETACH_INV = "Detach_Investigration(%s)"
    ATTACH_DEV = "Attach_Defect(%s)"
    DETACH_DEV = "Detach_Defect(%s)"
    ATTACH_DOC = "Attach_Document(%s)"
    DETACH_DOC = "Detach_Document(%s)"
    ATTACH_USER_NOTIFY = "Attach_User_Notify(%s)"
    DETACH_USER_NOTIFY = "Detach_User_Notify(%s)"
    ATTACH_ACCESS = "Attach_Access(%s)"
    DETACH_ACCESS = "Detach_Access(%s)"
    ATTACH_PRODUCT = "Attach_Product(%s)"
    DETACH_PRODUCT = "Detach_Product(%s)"
    MARK_NEW = "Mark_New(%s)"
    MARK_UPDATED = "Mark_Updated(%s)"
    MARK_PREFIX = "Mark_"
    MARK_NEW_PREFIX = "Mark_New"
    MARK_UPDATED_PREFIX = "Mark_Updated"
    MARK_UNMARK = "Mark_Unmark()"

    # Update Report list
    UPDATE_CHECK_LIST = (
        PRIORITY,
        STATUS,
        SEVERITY_V3,
        SEVERITY_V2,
        RELEASE,
        MARK_NEW,
        MARK_UPDATED,
    )

    #Any matching string for the period indicates reportable change
    @staticmethod
    def get_check_list():
        check_list = []
        for check in UPDATE_CHECK_LIST:
            simple_check = re.sub(r'(.*', '(', check)
            check_list.append(simple_check)
        return(check_list)

class SrtSetting(models.Model):
    name = models.CharField(max_length=63)
    helptext = models.TextField()
    value = models.CharField(max_length=255)

    def __str__(self):
        return "Setting %s = %s" % (self.name, self.value)

    @staticmethod
    def get_setting(key,default):
        try:
            return(SrtSetting.objects.get(name=key).value)
        except:
            return(default)
    @staticmethod
    def set_setting(key,value):
        obj,created = SrtSetting.objects.get_or_create(name=key)
        obj.value = value
        obj.save()


class HelpText(models.Model):
    VARIABLE = 0
    HELPTEXT_AREA = ((VARIABLE, 'variable'), )

    area = models.IntegerField(choices=HELPTEXT_AREA)
    key = models.CharField(max_length=100)
    text = models.TextField()


#UPDATE_FREQUENCY: 0 = every minute, 1 = every hour, 2 = every day, 3 = every week, 4 = every month, 5 = every year
class DataSource(models.Model):
    search_allowed_fields = ['key', 'name', 'description', 'init', 'update', 'lookup']

    #UPDATE FREQUENCT
    MINUTELY = 0
    HOURLY = 1
    DAILY = 2
    WEEKLY = 3
    MONTHLY = 4
    ONDEMAND = 5
    ONSTARTUP = 6
    FREQUENCY = (
        (MINUTELY, 'Minute'),
        (HOURLY, 'Hourly'),
        (DAILY, 'Daily'),
        (WEEKLY, 'Weekly'),
        (MONTHLY, 'Monthly'),
        (ONDEMAND, 'OnDemand'),
        (ONSTARTUP, 'OnStartup'),
    )

    # Global date format
    DATE_FORMAT = '%Y-%m-%d'
    DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'

    # Metadata
    LOOKUP_MISSING = 'LOOKUP-MISSING'
    PREVIEW_SOURCE = 'PREVIEW-SOURCE'

    key =  models.CharField(max_length=20)
    data =  models.CharField(max_length=20)
    source =  models.CharField(max_length=20)
    name =  models.CharField(max_length=20)
    description = models.TextField(blank=True)
    attributes = models.TextField(blank=True)
    cve_filter =  models.CharField(max_length=20)
    init = models.TextField(blank=True)
    update = models.TextField(blank=True)
    lookup = models.TextField(blank=True)
    update_frequency = models.IntegerField(choices=FREQUENCY, default=DAILY)
    loaded = models.BooleanField(default=False)
    lastModifiedDate = models.CharField(max_length=50, blank=True)
    lastUpdatedDate = models.CharField(max_length=50, blank=True)
    update_time = models.CharField(max_length=50, blank=True)

    def get_frequency_text(self):
        return DataSource.FREQUENCY[int(self.update_frequency)][1]

class CweTable(models.Model):
    search_allowed_fields = ['name', 'href', 'description', 'summary']
    name =  models.CharField(max_length=40)
    href =  models.TextField(blank=True)
    summary =  models.TextField(blank=True)
    description = models.TextField(blank=True)
    vulnerable_count = models.IntegerField(default=0)
    found = models.BooleanField(default=False)


class Cve(models.Model):
    search_allowed_fields = ['name', 'description', 'publishedDate',
                             'lastModifiedDate', 'comments', 'comments_private', 'tags', 'packages']

    # SRTool Priority
    UNDEFINED = 0
    LOW = 1
    MEDIUM = 2
    HIGH = 3
    CRITICAL = 4
    PRIORITY_ERROR = 5
    PRIORITY = (
        (UNDEFINED, 'Undefined'),
        (LOW, 'Low'),
        (MEDIUM, 'Medium'),
        (HIGH, 'High'),
        (CRITICAL, 'Critical'),
        (PRIORITY_ERROR, 'PRIORITY_ERROR'),
    )

    # WR Status
    HISTORICAL = 0
    NEW = 1
    NEW_RESERVED = 2
    INVESTIGATE = 3
    VULNERABLE = 4
    NOT_VULNERABLE = 5
    STATUS = (
        (HISTORICAL, 'Historical'),
        (NEW, 'New'),
        (NEW_RESERVED, 'New-Reserved'),
        (INVESTIGATE, 'Investigate'),
        (VULNERABLE, 'Vulnerable'),
        (NOT_VULNERABLE, 'Not Vulnerable'),
    )

    # Publish state
    PUBLISH_UNPUBLISHED = 0
    PUBLISH_NOPUBLISH = 1
    PUBLISH_PUBLISHED = 2
    PUBLISH_REQUEST = 3
    PUBLISH_UPDATE = 4
    PUBLISH_SUBMITTED = 5
    PUBLISH_STATE = (
        (PUBLISH_UNPUBLISHED, 'Unpublished'),
        (PUBLISH_NOPUBLISH, 'Not to be Published'),
        (PUBLISH_PUBLISHED, 'Published'),
        (PUBLISH_REQUEST, 'Publish Request (New)'),
        (PUBLISH_UPDATE, 'Publish Request (Update)'),
        (PUBLISH_SUBMITTED, 'Publish Submitted'),
    )

    # CPE item list
    CPE_LIST_KEY = 0    # entry is <[/]component|and|or> tag or '|' delimited list
    CPE_LIST_VULNERABLE = 0
    CPE_LIST_CPE23 = 1
    CPE_LIST_CPE22 = 2
    CPE_LIST_VERSIONEND = 3

    name = models.CharField(max_length=50)
    name_sort = models.CharField(max_length=50)

    priority = models.IntegerField(default=0)
    status = models.IntegerField(choices=STATUS, default=NEW)
    comments = models.TextField(blank=True)
    comments_private = models.TextField(blank=True)
    tags = models.TextField(blank=True, default='', null=True)

    cve_data_type = models.CharField(max_length=100, blank=True)
    cve_data_format = models.CharField(max_length=50, blank=True)
    cve_data_version = models.CharField(max_length=50, blank=True)

    public = models.BooleanField(default=True)
    publish_state = models.IntegerField(choices=PUBLISH_STATE, default=PUBLISH_UNPUBLISHED)
    publish_date = models.CharField(max_length=50, blank=True)
    acknowledge_date = models.DateTimeField(null=True)

    description = models.TextField(blank=True)
    publishedDate = models.CharField(max_length=50, blank=True)
    lastModifiedDate = models.CharField(max_length=50, blank=True)

    recommend = models.IntegerField(default=0)
    recommend_list = models.TextField(blank=True)

    cvssV3_baseScore = models.CharField(max_length=50, blank=True)
    cvssV3_baseSeverity = models.CharField(max_length=50, blank=True)

    cvssV2_baseScore = models.CharField(max_length=50, blank=True)
    cvssV2_severity = models.CharField(max_length=50, blank=True)

    # AKA Affected Components
    packages = models.TextField(blank=True)

    score_date = models.DateField(null=True, blank=True)
    srt_updated = models.DateTimeField(auto_now=True, null=True)
    srt_created = models.DateTimeField(auto_now_add=True, null=True)

    @property
    def get_priority_text(self):
        return SRTool.priority_text(self.priority)
    @property
    def get_status_text(self):
        return SRTool.status_text(self.status)
    @property
    def get_publish_text(self):
        return Cve.PUBLISH_STATE[int(self.publish_state)][1]
    @property
    def is_local(self):
        try:
            CveLocal.objects.get(name=self.name)
            return True
        except:
            return False
    @property
    def get_publishset_state(self):
        try:
            obj = PublishSet.objects.get(cve=self)
            return obj.state_text
        except:
            return PublishSet.PUBLISH_SET_STATE[PublishSet.PUBLISH_SET_NONE][1]
    @property
    def get_public_comments(self):
        the_comments = self.comments.strip()
        the_packages = self.packages.strip()
        if not the_comments or not the_packages:
            return '%s%s' % (the_comments,the_packages)
        if the_comments == the_packages:
            return the_comments
        return '%s' % (the_comments)

class CveDetail():
    # CPE item list
    CPE_LIST_KEY = 0    # entry is <[/]component|and|or> tag or '|' delimited list
    CPE_LIST_VULNERABLE = 0
    CPE_LIST_CPE23 = 1
    CPE_LIST_CPE22 = 2
    CPE_LIST_VERSIONEND = 3

    name = ''

    cve_data_type = ''
    cve_data_format = ''
    cve_data_version = ''

    description = ''
    publishedDate = ''
    acknowledge_date = ''
    lastModifiedDate = ''
    url_title = ''
    url = ''

    recommend = ''
    recommend_list = ''

    cpe_list= ''
    ref_list= ''

    cvssV3_baseScore = ''
    cvssV3_baseSeverity = ''
    cvssV3_vectorString = ''
    cvssV3_exploitabilityScore = ''
    cvssV3_impactScore = ''
    cvssV3_attackVector = ''
    cvssV3_attackComplexity = ''
    cvssV3_privilegesRequired = ''
    cvssV3_userInteraction = ''
    cvssV3_scope = ''
    cvssV3_confidentialityImpact = ''
    cvssV3_integrityImpact = ''
    cvssV3_availabilityImpact = ''

    cvssV2_baseScore = ''
    cvssV2_severity = ''
    cvssV2_vectorString = ''
    cvssV2_exploitabilityScore = ''
    cvssV2_impactScore = ''
    cvssV2_accessVector = ''
    cvssV2_accessComplexity = ''
    cvssV2_authentication = ''
    cvssV2_confidentialityImpact = ''
    cvssV2_integrityImpact = ''

    def get_cpe_list(self):
        cpe_array = []
        for cpe in self.cpe_list.split('|'):
            cpe_array.append(cpe.split(','))
        return cpe_array
    def get_ref_list(self):
        ref_array = []
        for ref in self.ref_list.split('|'):
            ref_array.append(ref.split('\t'))
        return ref_array

# Local full Cve class, based on "Cve"
class CveLocal(models.Model):

    # CPE item list
    CPE_LIST_KEY = 0    # entry is <[/]component|and|or> tag or '|' delimited list
    CPE_LIST_VULNERABLE = 0
    CPE_LIST_CPE23 = 1
    CPE_LIST_CPE22 = 2
    CPE_LIST_VERSIONEND = 3

    name = models.CharField(max_length=50)

    cve_data_type = models.CharField(max_length=100, blank=True)
    cve_data_format = models.CharField(max_length=50, blank=True)
    cve_data_version = models.CharField(max_length=50, blank=True)

    description = models.TextField(blank=True)
    publishedDate = models.CharField(max_length=50, blank=True)
    lastModifiedDate = models.CharField(max_length=50, blank=True)
    url = models.TextField(blank=True)
    url_title = models.TextField(default='Link')

    recommend = models.IntegerField(default=0)
    recommend_list = models.TextField(blank=True)

    cpe_list= models.TextField(blank=True)
    ref_list= ''

    cvssV3_baseScore = models.CharField(max_length=50, blank=True)
    cvssV3_baseSeverity = models.CharField(max_length=50, blank=True)
    cvssV3_vectorString = models.TextField(blank=True)
    cvssV3_exploitabilityScore = models.CharField(max_length=50, blank=True)
    cvssV3_impactScore = models.CharField(max_length=50, blank=True)
    cvssV3_attackVector = models.CharField(max_length=50, blank=True)
    cvssV3_attackComplexity = models.CharField(max_length=50, blank=True)
    cvssV3_privilegesRequired = models.CharField(max_length=50, blank=True)
    cvssV3_userInteraction = models.CharField(max_length=50, blank=True)
    cvssV3_scope = models.CharField(max_length=50, blank=True)
    cvssV3_confidentialityImpact = models.CharField(max_length=50, blank=True)
    cvssV3_integrityImpact = models.CharField(max_length=50, blank=True)
    cvssV3_availabilityImpact = models.CharField(max_length=50, blank=True)

    cvssV2_baseScore = models.CharField(max_length=50, blank=True)
    cvssV2_severity = models.CharField(max_length=50, blank=True)
    cvssV2_vectorString = models.TextField(blank=True)
    cvssV2_exploitabilityScore = models.CharField(max_length=50, blank=True)
    cvssV2_impactScore = models.CharField(max_length=50, blank=True)
    cvssV2_accessVector = models.CharField(max_length=50, blank=True)
    cvssV2_accessComplexity = models.CharField(max_length=50, blank=True)
    cvssV2_authentication = models.CharField(max_length=50, blank=True)
    cvssV2_confidentialityImpact = models.CharField(max_length=50, blank=True)
    cvssV2_integrityImpact = models.CharField(max_length=50, blank=True)

    @staticmethod
    def new_cve_name():
        current_cve_index,create = SrtSetting.objects.get_or_create(name='current_cve_index')
        if create:
            index = 100
        else:
            index = int(current_cve_index.value) + 1
        current_cve_index.value = str(index)
        current_cve_index.save()
        this_year = datetime.today().strftime('%Y')
        return "SRTCVE-%s-%d" % (this_year,index)


# Map of all sources for the given CVE
class CveSource(models.Model):
    cve = models.ForeignKey(Cve,related_name="cve_parent",blank=True, null=True,on_delete=models.CASCADE,)
    datasource = models.ForeignKey(DataSource,related_name="cve_datasource", blank=True, null=True,on_delete=models.CASCADE,)

class CveHistory(models.Model):
    search_allowed_fields = ['cve__name', 'comment', 'date', 'author']
    cve = models.ForeignKey(Cve,related_name="cve_history",default=None, null=True, on_delete=models.CASCADE,)
    comment = models.TextField(blank=True)
    date = models.DateField(null=True, blank=True)
    author = models.TextField(blank=True)

# CPE mapping for CVE

class CpeTable(models.Model):
    search_allowed_fields = ['vulnerable', 'cpeMatchString', 'cpe23Uri']
    vulnerable = models.BooleanField(default='False')
    cpeMatchString = models.TextField(blank=True)
    cpe23Uri = models.TextField(blank=True)
    versionEndIncluding = models.TextField(blank=True)

class CpeToCve(models.Model):
    cpe = models.ForeignKey(CpeTable,related_name="cpe2cve",on_delete=models.CASCADE,)
    cve = models.ForeignKey(Cve,related_name="cve2cpe",on_delete=models.CASCADE,)

# Package Mapping (SRT-CPE)
# NOTE: normally, transient computed data would not be kept in a record
#       (e.g. vulnerability/investigation/defect counts per package)
# However, (a) the size of the table is relatively small, (b) the
# counts can be in the thousands, and (c) that is too much for doing
# in template script because it results in huge page rendering delays
class Package(models.Model):
    search_allowed_fields = ['name', 'realname', 'invalidname']
    # Package filter Status
    FOR = 0
    AGAINST = 1
    MODE = (
        (FOR, 'For'),
        (AGAINST, 'Against'),
    )

    mode = models.IntegerField(choices=MODE, default=FOR)
    name = models.CharField(max_length=50, blank=True)
    realname = models.CharField(max_length=50, blank=True)
    invalidname = models.TextField(blank=True)
    weight = models.IntegerField(default=0)
    # computed count data
    cve_count = models.IntegerField(default=0)
    vulnerability_count = models.IntegerField(default=0)
    investigation_count = models.IntegerField(default=0)
    defect_count = models.IntegerField(default=0)
    @property
    def get_mode_text(self):
        return Package.MODE[int(self.mode)][1]
    @staticmethod
    def update_computed_counts(package_name=None):
        # A 'None' indicates all packages
#        _log("update_computed_counts0:%s" % package_name)
        if package_name:
            package_list = Package.objects.filter(name=package_name)
        else:
            package_list = Package.objects.all()
#        _log("update_computed_counts:p:%s" % len(package_list))
        for package in package_list:
            try:
                state = "p"
                package.cve_count = 0
                package.vulnerability_count = 0
                package.investigation_count = 0
                package.defect_count = 0
#                _log("update_computed_counts2:c:%s" % len(package.package2cve.all()))
                for pc in package.package2cve.all():
                    cve = pc.cve
                    package.cve_count += 1
                    for cv in cve.cve_to_vulnerability.all():
                        vulnerability = cv.vulnerability
                        package.vulnerability_count += 1
                        for vi in vulnerability.vulnerability2investigation.all():
                            package.investigation_count += 1
                            for id in vi.investigation.investigation_to_defect.all():
                                package.defect_count += 1
                package.save()
            except Exception as e:
                _log("ERROR:update_computed_counts:p=%s,state=%s,e=%s" % (package.name,state,e))


# NOTE: move 'NullBooleanField' to 'BooleanField' with 'null-True' >= Django 2.1
class PackageToCve(models.Model):
    package = models.ForeignKey(Package,related_name="package2cve",on_delete=models.CASCADE,)
    cve = models.ForeignKey(Cve,related_name="cve2package",on_delete=models.CASCADE,)
    applicable = models.NullBooleanField(default=True, null=True)


# CPE Filtering
class CpeFilter(models.Model):
    search_allowed_fields = ['key_prime', 'key_sub']
    UNDECIDED = 0
    INCLUDE = 1
    EXCLUDE = 2
    MANUAL = 3
    STATUS = (
        (UNDECIDED, 'Undecided'),
        (INCLUDE, 'Include'),
        (EXCLUDE, 'Exclude'),
        (MANUAL, 'Manual'),
    )

    key_prime =  models.CharField(max_length=40)
    key_sub =  models.CharField(max_length=40)
    status = models.IntegerField(choices=STATUS, default=UNDECIDED)
    automatic = models.BooleanField(default='False')

    class Meta:
        unique_together = ('key_prime', 'key_sub', )

    @property
    def get_name(self):
        return "%s:%s" % (self.key_prime,self.key_sub)
    @property
    def get_status_text(self):
        return CpeFilter.STATUS[int(self.status)][1]


# CVE/CWE Mapping

class CveToCwe(models.Model):
    cve = models.ForeignKey(Cve,related_name="cve2cwe",on_delete=models.CASCADE,)
    cwe = models.ForeignKey(CweTable,related_name="cwe2cve",on_delete=models.CASCADE,)

class CveReference(models.Model):
    cve = models.ForeignKey(Cve,related_name="references",on_delete=models.CASCADE,)
    hyperlink = models.CharField(max_length=100, null=True)
    resource = models.CharField(max_length=100, null=True)
    type = models.CharField(max_length=100, null=True)
    source = models.CharField(max_length=100, null=True)
    name = models.CharField(max_length=100, null=True)
    datasource = models.ForeignKey(DataSource,related_name="source_references", blank=True, null=True,on_delete=models.CASCADE,)

# PRODUCT

class Product(models.Model):
    search_allowed_fields = ['key', 'name', 'version', 'profile']

    order = models.IntegerField(default=0)
    key =  models.CharField(max_length=40)
    name =  models.CharField(max_length=40)
    version =  models.CharField(max_length=40)
    profile =  models.CharField(max_length=40)
    cpe =  models.CharField(max_length=40)
    defect_tags =  models.TextField(blank=True, default='')
    product_tags =  models.TextField(blank=True, default='')

    class Meta:
        unique_together = ('name', 'version', 'profile', )
    @property
    def long_name(self):
        long_name = '%s %s %s' % (self.name,self.version,self.profile)
        return long_name.strip()
    def get_defect_tag(self,tag,default=None):
        return SRTool.get_dict_tag(tag,self.defect_tags,default)
    def get_product_tag(self,tag,default=None):
        return SRTool.get_dict_tag(tag,self.product_tags,default)
    def get_defect_str(self):
        return self.defect_tags.replace('"','')
    def get_product_str(self):
        return self.product_tags.replace('"','')

# VULNERABILITY

# Company-level Vulnerablility Record
class Vulnerability(models.Model):
    search_allowed_fields = ['name', 'comments', 'comments_private', 'tags']

    HISTORICAL = 0
    NEW = 1
    NEW_RESERVED = 2
    INVESTIGATE = 3
    VULNERABLE = 4
    NOT_VULNERABLE = 5
    STATUS = (
        (HISTORICAL, 'Historical'),
        (NEW, 'New'),
        (NEW_RESERVED, 'New-Reserved'),
        (INVESTIGATE, 'Investigate'),
        (VULNERABLE, 'Vulnerable'),
        (NOT_VULNERABLE, 'Not Vulnerable'),
    )

    OPEN = 0
    CLOSED = 1
    FIXED = 2
    NOT_FIX = 3
    OUTCOME = (
        (OPEN, 'Open'),
        (CLOSED, 'Closed (Not Vulnerable)'),
        (FIXED, 'Closed (Fixed)'),
        (NOT_FIX, "Closed (Won't Fix)"),
    )

    # SRTool Priority
    UNDEFINED = 0
    LOW = 1
    MEDIUM = 2
    HIGH = 3
    CRITICAL = 4
    PRIORITY_ERROR = 5
    PRIORITY = (
        (UNDEFINED, 'Undefined'),
        (LOW, 'Low'),
        (MEDIUM, 'Medium'),
        (HIGH, 'High'),
        (CRITICAL, 'Critical'),
        (PRIORITY_ERROR, 'PRIORITY_ERROR'),
    )

    name = models.CharField(max_length=50)
    cve_primary_name = models.CharField(max_length=50, default='')
    description = models.TextField(blank=True, default='')

    public = models.BooleanField(default=True)
    comments = models.TextField(blank=True, default='')
    comments_private = models.TextField(blank=True, default='')
    tags = models.TextField(blank=True, default='')

    status = models.IntegerField(choices=STATUS, default=INVESTIGATE)
    outcome = models.IntegerField(choices=OUTCOME, default=OPEN)
    priority = models.IntegerField(choices=PRIORITY, default=LOW)

    # AKA Affected Components
    packages = models.TextField(blank=True)

    srt_updated = models.DateTimeField(auto_now=True, null=True)
    srt_created = models.DateTimeField(auto_now_add=True, null=True)

    @property
    def get_priority_text(self):
        return SRTool.priority_text(self.priority)
    @property
    def get_status_text(self):
        return SRTool.status_text(self.status)
    @property
    def get_outcome_text(self):
        return SRTool.outcome_text(self.outcome)
        return Vulnerability.OUTCOME[int(self.outcome)][1]
    @property
    def get_long_name(self):
        if self.cve_primary_name:
            return "%s (%s)" % (self.name,self.cve_primary_name)
        return "%s" % (self.name)
    @staticmethod
    def new_vulnerability_name():
        # get next vulnerability name atomically
        # FIXME ???
        if True:
            current_vulnerability_index,create = SrtSetting.objects.get_or_create(name='current_vulnerability_index')
            if create:
                index = 100
            else:
                index = int(current_vulnerability_index.value) +1
            current_vulnerability_index.value = str(index)
            current_vulnerability_index.save()
        else:
            try:
                with transaction.atomic():
                    current_vulnerability_index,create = SrtSetting.objects.get_or_create(name='current_vulnerability_index')
                    if create:
                        index = 100
                    else:
                        index = int(current_vulnerability_index.value) +1
                    current_vulnerability_index.value = str(index)
                    current_vulnerability_index.save()
            except IntegrityError:
                print("Error in new_vulnerability_name")
                raise
        return "VUL-%05d" % index
    @property
    def investigation_list(self):
        return VulnerabilityToInvestigation.objects.filter(vulnerability_id=self.id).order_by('investigation__product__order')

class VulnerabilityComments(models.Model):
    vulnerability = models.ForeignKey(Vulnerability,related_name="vulnerability_comments",on_delete=models.CASCADE,)
    comment = models.TextField(blank=True)
    date = models.DateField(null=True, blank=True)
    author = models.TextField(blank=True)

class VulnerabilityHistory(models.Model):
    search_allowed_fields = ['vulnerability__name', 'comment', 'date', 'author']
    vulnerability = models.ForeignKey(Vulnerability,related_name="vulnerability_history",on_delete=models.CASCADE,)
    comment = models.TextField(blank=True)
    date = models.DateField(null=True, blank=True)
    author = models.TextField(blank=True)

class VulnerabilityUploads(models.Model):
    vulnerability = models.ForeignKey(Vulnerability,related_name="vulnerability_uploads",on_delete=models.CASCADE,)
    description = models.TextField(blank=True)
    path = models.TextField(blank=True)
    size = models.IntegerField(default=0)
    date = models.DateField(null=True, blank=True)
    author = models.TextField(blank=True)

class CveToVulnerablility(models.Model):
    vulnerability = models.ForeignKey(Vulnerability,related_name="vulnerability_to_cve",on_delete=models.CASCADE,)
    cve = models.ForeignKey(Cve,related_name="cve_to_vulnerability",on_delete=models.CASCADE,)

# Defects

# Defect Record

class Defect(models.Model):
    search_allowed_fields = ['name', 'summary', 'release_version'] #, 'product']

    #Issue Type,Key,Summary,Priority,Status,Resolution,Publish To OLS,Fix Version
    #Bug,LIN10-2031,Security Advisory - libvorbis - CVE-2017-14633,P3,Closed,Fixed,Reviewed - Publish,10.17.41.3

    # Defect/SRTool Priority
    DEFECT_UNDEFINED = 0
    DEFECT_LOW = 1
    DEFECT_MEDIUM = 2
    DEFECT_HIGH = 3
    DEFECT_CRITICAL = 4
    DEFECT_PRIORITY_ERROR = 5
    DEFECT_PRIORITY = (
        (DEFECT_UNDEFINED, 'Undefined'),
        (DEFECT_LOW, 'Low'),
        (DEFECT_MEDIUM, 'Medium'),
        (DEFECT_HIGH, 'High'),
        (DEFECT_CRITICAL, 'Critical'),
        (DEFECT_PRIORITY_ERROR, 'PRIORITY_ERROR'),
    )

    DEFECT_STATUS_OPEN = 0
    DEFECT_STATUS_IN_PROGRESS = 1
    DEFECT_STATUS_ON_HOLD = 2
    DEFECT_STATUS_CHECKED_IN = 3
    DEFECT_STATUS_RESOLVED = 4
    DEFECT_STATUS_CLOSED = 5
    DEFECT_STATUS = (
        (DEFECT_STATUS_OPEN, 'Open'),
        (DEFECT_STATUS_IN_PROGRESS, 'In progress'),
        (DEFECT_STATUS_ON_HOLD, 'On Hold'),
        (DEFECT_STATUS_CHECKED_IN, 'Checked In'),
        (DEFECT_STATUS_RESOLVED, 'Resolved'),
        (DEFECT_STATUS_CLOSED, 'Closed'),
    )

    DEFECT_UNRESOLVED = 0
    DEFECT_RESOLVED = 1
    DEFECT_FIXED = 2
    DEFECT_WILL_NOT_FIX = 3
    DEFECT_WITHDRAWN = 4
    DEFECT_REJECTED = 5
    DEFECT_DUPLICATE = 6
    DEFECT_NOT_APPLICABLE = 7
    DEFECT_REPLACED_BY_REQUIREMENT = 8
    DEFECT_CANNOT_REPRODUCE = 9
    DEFECT_DONE = 10
    DEFECT_RESOLUTION = (
        (DEFECT_UNRESOLVED, 'Unresolved'),
        (DEFECT_RESOLVED, 'Resolved'),
        (DEFECT_FIXED, 'Fixed'),
        (DEFECT_WILL_NOT_FIX, 'Won\'t Fix'),
        (DEFECT_WITHDRAWN, 'Withdrawn'),
        (DEFECT_REJECTED, 'Rejected'),
        (DEFECT_DUPLICATE, 'Duplicate'),
        (DEFECT_NOT_APPLICABLE, 'Not Applicable'),
        (DEFECT_REPLACED_BY_REQUIREMENT, 'Replaced By Requirement'),
        (DEFECT_CANNOT_REPRODUCE, 'Cannot Reproduce'),
        (DEFECT_DONE, 'Done'),
    )

    Components = (
        'BSP',
        'Kernel',
        'Toolchain',
        'Userspace',
        'BSP - Async',
        'Build & Config',
        'Documentation',
        'Test',
    )

    HISTORICAL = 0
    NEW = 1
    NEW_RESERVED = 2
    INVESTIGATE = 3
    VULNERABLE = 4
    NOT_VULNERABLE = 5
    SRT_STATUS = (
        (HISTORICAL, 'Historical'),
        (NEW, 'New'),
        (NEW_RESERVED, 'New-Reserved'),
        (INVESTIGATE, 'Investigate'),
        (VULNERABLE, 'Vulnerable'),
        (NOT_VULNERABLE, 'Not Vulnerable'),
    )

    OPEN = 0
    CLOSED = 1
    FIXED = 2
    NOT_FIX = 3
    SRT_OUTCOME = (
        (OPEN, 'Open'),
        (CLOSED, 'Closed (Not Vulnerable)'),
        (FIXED, 'Closed (Fixed)'),
        (NOT_FIX, "Closed (Won't Fix)"),
    )

    # SRTool Priority
    UNDEFINED = 0
    LOW = 1
    MEDIUM = 2
    HIGH = 3
    CRITICAL = 4
    PRIORITY_ERROR = 5
    SRT_PRIORITY = (
        (UNDEFINED, 'Undefined'),
        (LOW, 'Low'),
        (MEDIUM, 'Medium'),
        (HIGH, 'High'),
        (CRITICAL, 'Critical'),
        (PRIORITY_ERROR, 'PRIORITY_ERROR'),
    )

    name = models.CharField(max_length=50)
    summary = models.TextField(blank=True)
    url = models.TextField(blank=True)
    duplicate_of = models.CharField(max_length=50, blank=True, default='')

    # External defect specific values
    priority = models.IntegerField(choices=DEFECT_PRIORITY, default=DEFECT_LOW)
    status = models.IntegerField(choices=DEFECT_STATUS, default=DEFECT_STATUS_OPEN)
    resolution = models.IntegerField(choices=DEFECT_RESOLUTION, default=DEFECT_UNRESOLVED)
    # SRTool compatible values
    srt_priority = models.IntegerField(choices=SRT_PRIORITY, default=LOW)
    srt_status = models.IntegerField(choices=SRT_STATUS, default=INVESTIGATE)
    srt_outcome = models.IntegerField(choices=SRT_OUTCOME, default=OPEN)

    publish = models.TextField(blank=True)
    release_version = models.CharField(max_length=50)
    product = models.ForeignKey(Product,related_name="product_defect",on_delete=models.CASCADE,)
    date_created = models.CharField(max_length=50)
    date_updated = models.CharField(max_length=50)

    # AKA Affected Components
    packages = models.TextField(blank=True)

    srt_updated = models.DateTimeField(auto_now=True)

    # Methods
    @property
    def get_defect_priority_text(self):
        return Defect.DEFECT_PRIORITY[int(self.priority)][1]
    @property
    def get_defect_status_text(self):
        return Defect.DEFECT_STATUS[int(self.status)][1]
    @property
    def get_defect_resolution_text(self):
        return Defect.DEFECT_RESOLUTION[int(self.resolution)][1]
    @property
    def get_priority_text(self):
        return SRTool.priority_text(self.srt_priority)
    @property
    def get_status_text(self):
        return SRTool.status_text(self.srt_status)
    @property
    def get_outcome_text(self):
        return SRTool.outcome_text(self.srt_outcome)
    @property
    def get_date_created_text(self):
        return re.sub(r"T.*", "", self.date_created)
    @property
    def get_date_updated_text(self):
        return re.sub(r"T.*", "", self.date_updated)
    @property
    def get_long_name(self):
        if self.release_version:
            return "%s (%s)" % (self.name,self.release_version)
        return "%s" % (self.name)
    @property
    def get_cve_names(self):
        cve_list = []
        for di in InvestigationToDefect.objects.filter(defect = self):
            for i2v in VulnerabilityToInvestigation.objects.filter(investigation = di.investigation):
                for v2c in CveToVulnerablility.objects.filter(vulnerability = i2v.vulnerability):
                    cve_list.append(v2c.cve.name)
        return ','.join(cve_list)
    @property
    def get_cve_ids(self):
        cve_list = []
        for di in InvestigationToDefect.objects.filter(defect = self):
            for i2v in VulnerabilityToInvestigation.objects.filter(investigation = di.investigation):
                for v2c in CveToVulnerablility.objects.filter(vulnerability = i2v.vulnerability):
                    cve_list.append(str(v2c.cve.id))
        return ','.join(cve_list)
    @property
    def get_publishset_state(self):
        pub_list = []
        cve_list = self.get_cve_names
        if not cve_list:
            return PublishSet.PUBLISH_SET_STATE[PublishSet.PUBLISH_SET_NONE][1]
        for cve_name in cve_list.split(','):
            try:
                cve = Cve.objects.get(name = cve_name)
                pub_list.append(cve.get_publishset_state)
            except Exception as e:
                pass
        return ','.join(pub_list)

class DefectHistory(models.Model):
    search_allowed_fields = ['defect__name', 'comment', 'date', 'author']
    defect = models.ForeignKey(Defect,related_name="defect_history",on_delete=models.CASCADE,)
    comment = models.TextField(blank=True)
    date = models.DateField(null=True, blank=True)
    author = models.TextField(blank=True)


# INVESTIGATION

# Product-level Vulnerablility Investigation Record
class Investigation(models.Model):
    search_allowed_fields = ['name', 'comments', 'comments_private', 'tags']

    HISTORICAL = 0
    NEW = 1
    NEW_RESERVED = 2
    INVESTIGATE = 3
    VULNERABLE = 4
    NOT_VULNERABLE = 5
    STATUS = (
        (HISTORICAL, 'Historical'),
        (NEW, 'New'),
        (NEW_RESERVED, 'New-Reserved'),
        (INVESTIGATE, 'Investigate'),
        (VULNERABLE, 'Vulnerable'),
        (NOT_VULNERABLE, 'Not Vulnerable'),
    )

    OPEN = 0
    CLOSED = 1
    FIXED = 2
    NOT_FIX = 3
    OUTCOME = (
        (OPEN, 'Open'),
        (CLOSED, 'Closed (Not Vulnerable)'),
        (FIXED, 'Closed (Fixed)'),
        (NOT_FIX, "Closed (Won't Fix)"),
    )

    # SRTool Priority
    UNDEFINED = 0
    LOW = 1
    MEDIUM = 2
    HIGH = 3
    CRITICAL = 4
    PRIORITY_ERROR = 5
    PRIORITY = (
        (UNDEFINED, 'Undefined'),
        (LOW, 'Low'),
        (MEDIUM, 'Medium'),
        (HIGH, 'High'),
        (CRITICAL, 'Critical'),
        (PRIORITY_ERROR, 'PRIORITY_ERROR'),
    )

    name = models.CharField(max_length=50)
    vulnerability = models.ForeignKey(Vulnerability,related_name="vulnerability_investigation",on_delete=models.CASCADE,)
    product = models.ForeignKey(Product,related_name="product_investigation",on_delete=models.CASCADE,)

    public = models.BooleanField(default=True)
    comments = models.TextField(blank=True)
    comments_private = models.TextField(blank=True)
    tags = models.TextField(blank=True, default='')

    status = models.IntegerField(choices=STATUS, default=OPEN)
    outcome = models.IntegerField(choices=OUTCOME, default=INVESTIGATE)
    priority = models.IntegerField(choices=PRIORITY, default=LOW)

    # AKA Affected Components
    packages = models.TextField(blank=True)

    srt_updated = models.DateTimeField(auto_now=True, null=True)
    srt_created = models.DateTimeField(auto_now_add=True, null=True)

    # Methods
    @property
    def get_priority_text(self):
        return SRTool.priority_text(self.priority)
    @property
    def get_status_text(self):
        return SRTool.status_text(self.status)
    @property
    def get_outcome_text(self):
        return SRTool.outcome_text(self.outcome)
    @property
    def get_long_name(self):
        if self.vulnerability and self.vulnerability.cve_primary_name:
            return "%s (%s)" % (self.name,self.vulnerability.cve_primary_name.name)
        return "%s" % (self.name)
    @staticmethod
    def new_investigation_name():
        current_investigation_index,create = SrtSetting.objects.get_or_create(name='current_investigation_index')
        if create:
            index = 100
        else:
            index = int(current_investigation_index.value) + 1
        current_investigation_index.value = str(index)
        current_investigation_index.save()
        return "INV-%05d" % index

class InvestigationToDefect(models.Model):
    investigation = models.ForeignKey(Investigation,related_name="investigation_to_defect",on_delete=models.CASCADE,)
    defect = models.ForeignKey(Defect,related_name="defect_to_investigation",on_delete=models.CASCADE,)
    product = models.ForeignKey(Product,related_name="defect_to_product",on_delete=models.CASCADE,)

class InvestigationComments(models.Model):
    investigation = models.ForeignKey(Investigation,related_name="investigation_comments",on_delete=models.CASCADE,)
    comment = models.TextField(blank=True)
    date = models.DateField(null=True, blank=True)
    author = models.TextField(blank=True)

class InvestigationHistory(models.Model):
    search_allowed_fields = ['investigation__name', 'comment', 'date', 'author']
    investigation = models.ForeignKey(Investigation,related_name="investigation_history",on_delete=models.CASCADE,)
    comment = models.TextField(blank=True)
    date = models.DateField(null=True, blank=True)
    author = models.TextField(blank=True)

class InvestigationUploads(models.Model):
    investigation = models.ForeignKey(Investigation,related_name="investigation_uploads",on_delete=models.CASCADE,)
    description = models.TextField(blank=True)
    path = models.TextField(blank=True)
    size = models.IntegerField(default=0)
    date = models.DateField(null=True, blank=True)
    author = models.TextField(blank=True)

class VulnerabilityToInvestigation(models.Model):
    vulnerability = models.ForeignKey(Vulnerability,related_name="vulnerability2investigation",on_delete=models.CASCADE,)
    investigation = models.ForeignKey(Investigation,related_name="investigation2vulnerability",on_delete=models.CASCADE,)

class VulnerabilityAccess(models.Model):
    vulnerability = models.ForeignKey(Vulnerability,related_name="vulnerability_users",on_delete=models.CASCADE,)
    user = models.ForeignKey(SrtUser,related_name="vulnerability_user",on_delete=models.CASCADE,)

class VulnerabilityNotification(models.Model):
    vulnerability = models.ForeignKey(Vulnerability,related_name="vulnerability_notification",on_delete=models.CASCADE,)
    user = models.ForeignKey(SrtUser,related_name="vulnerability_notify",on_delete=models.CASCADE,)

class InvestigationAccess(models.Model):
    investigation = models.ForeignKey(Investigation,related_name="investigation_users",on_delete=models.CASCADE,)
    user = models.ForeignKey(SrtUser,related_name="investigation_user",on_delete=models.CASCADE,)

class InvestigationNotification(models.Model):
    investigation = models.ForeignKey(Investigation,related_name="investigation_notification",on_delete=models.CASCADE,)
    user = models.ForeignKey(SrtUser,related_name="investigation_notify",on_delete=models.CASCADE,)

# Items waiting for SRTool external publishing
class PublishPending(models.Model):
    cve = models.ForeignKey(Cve,related_name="publish_pending_cves",blank=True,null=True,on_delete=models.CASCADE,)
    vulnerability = models.ForeignKey(Vulnerability,related_name="publish_pending_vulnerabilities",blank=True,null=True,on_delete=models.CASCADE,)
    investigation = models.ForeignKey(Investigation,related_name="publish_pending_investigations",blank=True,null=True,on_delete=models.CASCADE,)
    date = models.DateField(null=True, blank=True)
    note = models.TextField(blank=True)

# ==== Support clases, meta classes ====

def _log_args(msg, *args, **kwargs):
    s = '%s:(' % msg
    if args:
        for a in args:
            s += '%s,' % a
    s += '),('
    if kwargs:
        for key, value in kwargs.items():
            s += '(%s=%s),' % (key,value)
    s += ')'
    _log(s)


# Action items waiting
class Notify(models.Model):
    search_allowed_fields = ['category','description','url']

    # SRTool Priority
    UNDEFINED = 0
    LOW = 1
    MEDIUM = 2
    HIGH = 3
    CRITICAL = 4
    PRIORITY_ERROR = 5
    PRIORITY = (
        (UNDEFINED, 'Undefined'),
        (LOW, 'Low'),
        (MEDIUM, 'Medium'),
        (HIGH, 'High'),
        (CRITICAL, 'Critical'),
        (PRIORITY_ERROR, 'PRIORITY_ERROR'),
    )

    category = models.CharField(max_length=50)
    description = models.TextField(blank=True)
    priority = models.IntegerField(default=0)
    url = models.TextField(blank=True)
    author = models.TextField(blank=True)
    srt_updated = models.DateTimeField(auto_now=True, null=True)
    srt_created = models.DateTimeField(auto_now_add=True, null=True)

    @property
    def get_priority_text(self):
        return Notify.PRIORITY[int(self.priority)][1]

# Access list for action items
class NotifyAccess(models.Model):
    notify = models.ForeignKey(Notify,related_name="todo2user",blank=True,null=True,on_delete=models.CASCADE,)
    user = models.ForeignKey(SrtUser,related_name="user2todo",blank=True,null=True,on_delete=models.CASCADE,)

# Predefined list of Notify categories
class NotifyCategories(models.Model):
    category = models.CharField(max_length=50)

class PublishSet(models.Model):
    search_allowed_fields = ['cve__name','cve__description','cve__status','cve__publishedDate','cve__lastModifiedDate']

    # Publish state
    PUBLISH_SET_NONE = 0
    PUBLISH_SET_NEW = 1
    PUBLISH_SET_MODIFIED = 2
    PUBLISH_SET_NEW_USER = 3
    PUBLISH_SET_MODIFIED_USER = 4
    PUBLISH_SET_ERROR = 5
    PUBLISH_SET_STATE = (
        (PUBLISH_SET_NONE, 'Skip'),
        (PUBLISH_SET_NEW, 'New'),
        (PUBLISH_SET_MODIFIED, 'Modified'),
        (PUBLISH_SET_NEW_USER, 'New_User'),
        (PUBLISH_SET_MODIFIED_USER, 'Modified_User'),
        (PUBLISH_SET_ERROR, 'PUBLISH_SET_ERROR'),
    )

    cve = models.ForeignKey(default=None, to='orm.cve', null=True, on_delete=models.CASCADE,)
    state = models.IntegerField(choices=PUBLISH_SET_STATE, default=PUBLISH_SET_NONE)
    reason = models.TextField(blank=True)

    @property
    def state_text(self):
        if (0 > self.state) or (self.state >= len(self.PUBLISH_SET_STATE)):
            return self.PUBLISH_SET_STATE[self.PUBLISH_SET_ERROR][1]
        return self.PUBLISH_SET_STATE[self.state][1]

# Error Log
class ErrorLog(models.Model):
    search_allowed_fields = ['description']

    # Severity
    INFO = 0
    WARNING = 1
    ERROR = 2
    SEVERITY = (
        (INFO, 'Info'),
        (WARNING, 'Warning'),
        (ERROR, 'Error'),
    )

    severity = models.IntegerField(default=0)
    description = models.TextField(blank=True)
    srt_created = models.DateTimeField(auto_now_add=True, null=True)

    @property
    def get_severity_text(self):
        return ErrorLog.SEVERITY[int(self.severity)][1]

#
# Database Cache Support
#

def invalidate_cache(**kwargs):
    from django.core.cache import cache
    try:
        cache.clear()
    except Exception as e:
        logger.warning("Problem with cache backend: Failed to clear cache: %s" % e)

def signal_runbuilds():
    """Send SIGUSR1 to runbuilds process"""
    try:
        with open(os.path.join(os.getenv('BUILDDIR', '.'),
                               '.runbuilds.pid')) as pidf:
            os.kill(int(pidf.read()), SIGUSR1)
    except FileNotFoundError:
        logger.info("Stopping existing runbuilds: no current process found")

django.db.models.signals.post_save.connect(invalidate_cache)
django.db.models.signals.post_delete.connect(invalidate_cache)
django.db.models.signals.m2m_changed.connect(invalidate_cache)