Repository navigation
Expand file tree
/
Copy pathdynamo_models.py
More file actions
5701 lines (4621 loc) · 213 KB
/
Copy pathdynamo_models.py
File metadata and controls
5701 lines (4621 loc) · 213 KB
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
# Copyright The Linux Foundation and each contributor to CommunityBridge.
# SPDX-License-Identifier: MIT
"""
Easily access CLA models backed by DynamoDB using pynamodb.
"""
import base64
import datetime
import os
import re
import time
import uuid
from datetime import timezone
from typing import Optional, List
from dateutil.parser import parse as parsedatestring
import dateutil.parser
from pynamodb import attributes
from pynamodb.attributes import (
UTCDateTimeAttribute,
UnicodeSetAttribute,
UnicodeAttribute,
BooleanAttribute,
NumberAttribute,
ListAttribute,
JSONAttribute,
MapAttribute,
)
from pynamodb.expressions.condition import Condition
from pynamodb.indexes import GlobalSecondaryIndex, AllProjection
from pynamodb.models import Model
import cla
from cla.models import model_interfaces, key_value_store_interface, DoesNotExist
from cla.models.event_types import EventType
from cla.models.model_interfaces import User, Signature, ProjectCLAGroup, Repository, Gerrit
from cla.models.model_utils import is_uuidv4
from cla.project_service import ProjectService
stage = os.environ.get("STAGE", "")
cla_logo_url = os.environ.get("CLA_BUCKET_LOGO_URL", "")
def create_database():
"""
Named "create_database" instead of "create_tables" because create_database
is expected to exist in all database storage wrappers.
"""
tables = [
RepositoryModel,
ProjectModel,
SignatureModel,
CompanyModel,
UserModel,
StoreModel,
GitHubOrgModel,
GerritModel,
EventModel,
CCLAAllowlistRequestModel,
APILogModel,
]
# Create all required tables.
for table in tables:
# Wait blocks until table is created.
table.create_table(wait=True)
def delete_database():
"""
Named "delete_database" instead of "delete_tables" because delete_database
is expected to exist in all database storage wrappers.
WARNING: This will delete all existing table data.
"""
tables = [
RepositoryModel,
ProjectModel,
SignatureModel,
CompanyModel,
UserModel,
StoreModel,
GitHubOrgModel,
GerritModel,
CCLAAllowlistRequestModel,
APILogModel,
]
# Delete all existing tables.
for table in tables:
if table.exists():
table.delete_table()
class GitHubUserIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying users by GitHub ID.
"""
class Meta:
"""Meta class for GitHub User index."""
index_name = "github-id-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
user_github_id = NumberAttribute(hash_key=True)
class SignatureProjectExternalIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying signatures by project external ID
"""
class Meta:
""" Meta class for Signature Project External Index """
index_name = "project-signature-external-id-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index
signature_project_external_id = UnicodeAttribute(hash_key=True)
class SignatureCompanySignatoryIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying signatures by signature company signatory ID
"""
class Meta:
""" Meta class for Signature Company Signatory Index """
index_name = "signature-company-signatory-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
signature_company_signatory_id = UnicodeAttribute(hash_key=True)
class SignatureProjectReferenceIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying signatures by project reference ID
"""
class Meta:
""" Meta class for Signature Project Reference Index """
index_name = "signature-project-reference-index"
write_capacity_units = 10
read_capacity_units = 10
projection = AllProjection()
signature_project_id = UnicodeAttribute(hash_key=True)
signature_reference_id = UnicodeAttribute(range_key=True)
class SignatureCompanyInitialManagerIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying signatures by signature company initial manager ID
"""
class Meta:
""" Meta class for Signature Company Initial Manager Index """
index_name = "signature-company-initial-manager-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
signature_company_initial_manager_id = UnicodeAttribute(hash_key=True)
class GitHubUsernameIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying users by github username.
"""
class Meta:
index_name = "github-username-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
# This attribute is the hash key for the index.
user_github_username = UnicodeAttribute(hash_key=True)
class GitLabIDIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying users by github username.
"""
class Meta:
index_name = "gitlab-id-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
# This attribute is the hash key for the index.
user_gitlab_id = UnicodeAttribute(hash_key=True)
class GitLabUsernameIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying users by github username.
"""
class Meta:
index_name = "gitlab-username-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
# This attribute is the hash key for the index.
user_gitlab_username = UnicodeAttribute(hash_key=True)
class LFUsernameIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying users by LF Username.
"""
class Meta:
"""Meta class for LF Username index."""
index_name = "lf-username-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
lf_username = UnicodeAttribute(hash_key=True)
class LFEmailIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying users by LF Emails.
"""
class Meta:
"""Meta class for LF Email index."""
index_name = "lf-email-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
lf_email = UnicodeAttribute(hash_key=True)
class ProjectRepositoryIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying repositories by project ID.
"""
class Meta:
"""Meta class for project repository index."""
index_name = "project-repository-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
repository_project_id = UnicodeAttribute(hash_key=True)
class ProjectSFIDRepositoryIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying repositories by project ID.
"""
class Meta:
"""Meta class for project repository index."""
index_name = "project-sfid-repository-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
project_sfid = UnicodeAttribute(hash_key=True)
class ExternalRepositoryIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying repositories by external ID.
"""
class Meta:
"""Meta class for external ID repository index."""
index_name = "external-repository-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
repository_external_id = UnicodeAttribute(hash_key=True)
class SFDCRepositoryIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying repositories by external ID.
"""
class Meta:
"""Meta class for external ID repository index."""
index_name = "sfdc-repository-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
repository_sfdc_id = UnicodeAttribute(hash_key=True)
class ExternalProjectIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying projects by external ID.
"""
class Meta:
"""Meta class for external ID project index."""
index_name = "external-project-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
project_external_id = UnicodeAttribute(hash_key=True)
class ProjectNameIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying projects by name.
"""
class Meta:
"""Meta class for external ID project index."""
index_name = "project-name-search-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
project_name = UnicodeAttribute(hash_key=True)
class ProjectNameLowerIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying projects by name.
"""
class Meta:
"""Meta class for external ID project index."""
index_name = "project-name-lower-search-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
project_name_lower = UnicodeAttribute(hash_key=True)
class ProjectFoundationIDIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying projects by name.
"""
class Meta:
"""Meta class for external ID project index."""
index_name = "foundation-sfid-project-name-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
foundation_sfid = UnicodeAttribute(hash_key=True)
project_name = UnicodeAttribute(range_key=True)
class CompanyNameIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying companies by name.
"""
class Meta:
"""Meta class for company name index."""
index_name = "company-name-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
company_name = UnicodeAttribute(hash_key=True)
class SigningEntityNameIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying companies by the signing entity name.
"""
class Meta:
"""Meta class for company name index."""
index_name = "company-signing-entity-name-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
signing_entity_name = UnicodeAttribute(hash_key=True)
class ExternalCompanyIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying companies by external ID.
"""
class Meta:
"""Meta class for external ID company index."""
index_name = "external-company-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
company_external_id = UnicodeAttribute(hash_key=True)
class GithubOrgSFIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying github organizations by a Salesforce ID.
"""
class Meta:
"""Meta class for external ID github org index."""
index_name = "github-org-sfid-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
organization_sfid = UnicodeAttribute(hash_key=True)
class GitlabOrgSFIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying gitlab organizations by a Salesforce ID.
"""
class Meta:
"""Meta class for external ID github org index."""
index_name = "gitlab-org-sfid-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
organization_sfid = UnicodeAttribute(hash_key=True)
class GitlabOrgProjectSfidOrganizationNameIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying gitlab organizations by a Project sfid and
Organization Name.
"""
class Meta:
"""Meta class for external ID github org index."""
index_name = "gitlab-project-sfid-organization-name-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
project_sfid = UnicodeAttribute(hash_key=True)
organization_name = UnicodeAttribute(range_key=True)
class GitlabOrganizationNameLowerIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying gitlab organizations by Organization Name.
"""
class Meta:
"""Meta class for external ID github org index."""
index_name = "gitlab-organization-name-lower-search-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
organization_name_lower = UnicodeAttribute(hash_key=True)
class OrganizationNameLowerSearchIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying organizations by Organization Name.
"""
class Meta:
"""Meta class for external ID github org index."""
index_name = "organization-name-lower-search-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
organization_name_lower = UnicodeAttribute(hash_key=True)
class GitlabExternalGroupIDIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying gitlab organizations by group ID
"""
class Meta:
"""Meta class for external ID for gitlab group id index"""
index_name = "gitlab-external-group-id-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
external_gitlab_group_id = NumberAttribute(hash_key=True)
class GerritProjectIDIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying gerrit's by the project ID
"""
class Meta:
"""Meta class for external ID github org index."""
index_name = "gerrit-project-id-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
project_id = UnicodeAttribute(hash_key=True)
class GerritProjectSFIDIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying gerrit's by the project SFID
"""
class Meta:
"""Meta class for external ID github org index."""
index_name = "gerrit-project-sfid-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
project_sfid = UnicodeAttribute(hash_key=True)
class ProjectSignatureIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying signatures by project ID.
"""
class Meta:
"""Meta class for reference Signature index."""
index_name = "project-signature-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
signature_project_id = UnicodeAttribute(hash_key=True)
class ReferenceSignatureIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying signatures by reference.
"""
class Meta:
"""Meta class for reference Signature index."""
index_name = "reference-signature-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
# All attributes are projected - not sure if this is necessary.
projection = AllProjection()
# This attribute is the hash key for the index.
signature_reference_id = UnicodeAttribute(hash_key=True)
class RequestedCompanyIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying company invites with a company ID.
"""
class Meta:
"""Meta class for external ID company index."""
index_name = "requested-company-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
requested_company_id = UnicodeAttribute(hash_key=True)
class EventTypeIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying events with an event type
"""
class Meta:
"""Meta class for event type index."""
index_name = "event-type-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
event_type = UnicodeAttribute(hash_key=True)
class EventUserIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying events by user ID.
"""
class Meta:
"""Meta class for user ID index"""
index_name = "user-id-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
user_id_index = UnicodeAttribute(hash_key=True)
class GithubUserExternalIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying users by a user external ID.
"""
class Meta:
"""Meta class for github user external ID index"""
index_name = "github-user-external-id-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
user_external_id = UnicodeAttribute(hash_key=True)
class FoundationSfidIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying mapping of cla-groups and projects by foundation_sfid
"""
class Meta:
"""Meta class for project-cla-groups foundation_sfid index"""
index_name = "foundation-sfid-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
foundation_sfid = UnicodeAttribute(hash_key=True)
class CLAGroupIDIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying by cla-group-id
"""
class Meta:
"""Meta class for cla-groups-projects cla-group-id index"""
index_name = "cla-group-id-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
cla_group_id = UnicodeAttribute(hash_key=True)
class CompanyIDProjectIDIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index for querying by company-id
"""
class Meta:
""" Meta class for ccla-allowlist-requests company-id-project-id-index """
index_name = "company-id-project-id-index"
write_capacity_units = int(cla.conf["DYNAMO_WRITE_UNITS"])
read_capacity_units = int(cla.conf["DYNAMO_READ_UNITS"])
projection = AllProjection()
company_id = UnicodeAttribute(hash_key=True)
project_id = UnicodeAttribute(range_key=True)
# LG: patched class
class DateTimeAttribute(UTCDateTimeAttribute):
"""
We need to patch deserialize, see https://pynamodb.readthedocs.io/en/stable/upgrading.html#no-longer-parsing-date-time-strings-leniently
This fails for ProjectModel.date_created having '2022-11-21T10:31:31Z' instead of strictly expected '2022-08-25T16:26:04.000000+0000'
"""
def deserialize(self, value):
try:
return self._fast_parse_utc_date_string(value)
except (TypeError, ValueError, AttributeError):
return parsedatestring(value)
# LG: patched class
class PatchedUnicodeSetAttribute(UnicodeSetAttribute):
"""
In attribute value we can have:
- set of strings "SS": {"SS":["id1","id2"]} - this is expected by pynamodb
- list of strings "LS": {"L":[{"S": "id"},{"S":"id2"}] - this is what golang saves
- NULL: {"NULL":true}
"""
def get_value(self, value):
# if self.attr_type not in value:
if not value:
return set()
if self.attr_type == 'SS' and 'L' in value:
value = {'SS':list(map(lambda x: x['S'], value['L']))}
return super(PatchedUnicodeSetAttribute, self).get_value(value)
def deserialize(self, value):
if not value:
return set()
return set(value)
class BaseModel(Model):
"""
Base pynamodb model used for all CLA models.
"""
date_created = DateTimeAttribute(default=datetime.datetime.utcnow)
date_modified = DateTimeAttribute(default=datetime.datetime.utcnow)
version = UnicodeAttribute(default="v1") # Schema version.
def __iter__(self):
"""Used to convert model to dict for JSON-serialized string."""
for name, attr in self.get_attributes().items():
if isinstance(attr, ListAttribute):
if attr is None or getattr(self, name) is None:
yield name, None
else:
values = attr.serialize(getattr(self, name))
if len(values) < 1:
yield name, []
else:
key = list(values[0].keys())[0]
yield name, [value[key] for value in values]
else:
yield name, attr.serialize(getattr(self, name))
def get_version(self):
return self.version
def get_date_created(self):
return self.date_created
def get_date_modified(self):
return self.date_modified
def set_version(self, version):
self.version = version
def set_date_created(self, date_created):
self.date_created = date_created
def set_date_modified(self, date_modified):
self.date_modified = date_modified
class DocumentTabModel(MapAttribute):
"""
Represents a document tab in the document model.
"""
document_tab_type = UnicodeAttribute(default="text")
document_tab_id = UnicodeAttribute(null=True)
document_tab_name = UnicodeAttribute(null=True)
document_tab_page = NumberAttribute(default=1)
document_tab_position_x = NumberAttribute(null=True)
document_tab_position_y = NumberAttribute(null=True)
document_tab_width = NumberAttribute(default=200)
document_tab_height = NumberAttribute(default=20)
document_tab_is_locked = BooleanAttribute(default=False)
document_tab_is_required = BooleanAttribute(default=True)
document_tab_anchor_string = UnicodeAttribute(default=None, null=True)
document_tab_anchor_ignore_if_not_present = BooleanAttribute(default=True)
document_tab_anchor_x_offset = NumberAttribute(null=True)
document_tab_anchor_y_offset = NumberAttribute(null=True)
class DocumentTab(model_interfaces.DocumentTab):
"""
ORM-agnostic wrapper for the DynamoDB DocumentTab model.
"""
def __init__(
self, # pylint: disable=too-many-arguments
document_tab_type=None,
document_tab_id=None,
document_tab_name=None,
document_tab_page=None,
document_tab_position_x=None,
document_tab_position_y=None,
document_tab_width=None,
document_tab_height=None,
document_tab_is_locked=False,
document_tab_is_required=True,
document_tab_anchor_string=None,
document_tab_anchor_ignore_if_not_present=True,
document_tab_anchor_x_offset=None,
document_tab_anchor_y_offset=None,
):
super().__init__()
self.model = DocumentTabModel()
self.model.document_tab_id = document_tab_id
self.model.document_tab_name = document_tab_name
# x,y coordinates are None when anchor x,y offsets are supplied.
if document_tab_position_x is not None:
self.model.document_tab_position_x = document_tab_position_x
if document_tab_position_y is not None:
self.model.document_tab_position_y = document_tab_position_y
# Use defaults if None is provided for the following attributes.
if document_tab_type is not None:
self.model.document_tab_type = document_tab_type
if document_tab_page is not None:
self.model.document_major_version = document_tab_page
if document_tab_width is not None:
self.model.document_tab_width = document_tab_width
if document_tab_height is not None:
self.model.document_tab_height = document_tab_height
self.model.document_tab_is_locked = document_tab_is_locked
self.model.document_tab_is_required = document_tab_is_required
# Anchor string properties
if document_tab_anchor_string is not None:
self.model.document_tab_anchor_string = document_tab_anchor_string
self.model.document_tab_anchor_ignore_if_not_present = document_tab_anchor_ignore_if_not_present
if document_tab_anchor_x_offset is not None:
self.model.document_tab_anchor_x_offset = document_tab_anchor_x_offset
if document_tab_anchor_y_offset is not None:
self.model.document_tab_anchor_y_offset = document_tab_anchor_y_offset
def to_dict(self):
return {
"document_tab_type": self.model.document_tab_type,
"document_tab_id": self.model.document_tab_id,
"document_tab_name": self.model.document_tab_name,
"document_tab_page": self.model.document_tab_page,
"document_tab_position_x": self.model.document_tab_position_x,
"document_tab_position_y": self.model.document_tab_position_y,
"document_tab_width": self.model.document_tab_width,
"document_tab_height": self.model.document_tab_height,
"document_tab_is_locked": self.model.document_tab_is_locked,
"document_tab_is_required": self.model.document_tab_is_required,
"document_tab_anchor_string": self.model.document_tab_anchor_string,
"document_tab_anchor_ignore_if_not_present": self.model.document_tab_anchor_ignore_if_not_present,
"document_tab_anchor_x_offset": self.model.document_tab_anchor_x_offset,
"document_tab_anchor_y_offset": self.model.document_tab_anchor_y_offset,
}
def get_document_tab_type(self):
return self.model.document_tab_type
def get_document_tab_id(self):
return self.model.document_tab_id
def get_document_tab_name(self):
return self.model.document_tab_name
def get_document_tab_page(self):
return self.model.document_tab_page
def get_document_tab_position_x(self):
return self.model.document_tab_position_x
def get_document_tab_position_y(self):
return self.model.document_tab_position_y
def get_document_tab_width(self):
return self.model.document_tab_width
def get_document_tab_height(self):
return self.model.document_tab_height
def get_document_tab_is_locked(self):
return self.model.document_tab_is_locked
def get_document_tab_anchor_string(self):
return self.model.document_tab_anchor_string
def get_document_tab_anchor_ignore_if_not_present(self):
return self.model.document_tab_anchor_ignore_if_not_present
def get_document_tab_anchor_x_offset(self):
return self.model.document_tab_anchor_x_offset
def get_document_tab_anchor_y_offset(self):
return self.model.document_tab_anchor_y_offset
def set_document_tab_type(self, tab_type):
self.model.document_tab_type = tab_type
def set_document_tab_id(self, tab_id):
self.model.document_tab_id = tab_id
def set_document_tab_name(self, tab_name):
self.model.document_tab_name = tab_name
def set_document_tab_page(self, tab_page):
self.model.document_tab_page = tab_page
def set_document_tab_position_x(self, tab_position_x):
self.model.document_tab_position_x = tab_position_x
def set_document_tab_position_y(self, tab_position_y):
self.model.document_tab_position_y = tab_position_y
def set_document_tab_width(self, tab_width):
self.model.document_tab_width = tab_width
def set_document_tab_height(self, tab_height):
self.model.document_tab_height = tab_height
def set_document_tab_is_locked(self, is_locked):
self.model.document_tab_is_locked = is_locked
def set_document_tab_anchor_string(self, document_tab_anchor_string):
self.model.document_tab_anchor_string = document_tab_anchor_string
def set_document_tab_anchor_ignore_if_not_present(self, document_tab_anchor_ignore_if_not_present):
self.model.document_tab_anchor_ignore_if_not_present = document_tab_anchor_ignore_if_not_present
def set_document_tab_anchor_x_offset(self, document_tab_anchor_x_offset):
self.model.document_tab_anchor_x_offset = document_tab_anchor_x_offset
def set_document_tab_anchor_y_offset(self, document_tab_anchor_y_offset):
self.model.document_tab_anchor_y_offset = document_tab_anchor_y_offset
class DocumentModel(MapAttribute):
"""
Represents a document in the project model.
"""
document_name = UnicodeAttribute(null=True)
document_file_id = UnicodeAttribute(null=True)
document_content_type = UnicodeAttribute(null=True) # pdf, url+pdf, storage+pdf, etc
document_content = UnicodeAttribute(null=True) # None if using storage service.
document_major_version = NumberAttribute(default=1)
document_minor_version = NumberAttribute(default=0)
document_author_name = UnicodeAttribute(null=True)
# LG: now we can use DateTimeAttribute - because pynamodb was updated
# document_creation_date = UnicodeAttribute(null=True)
document_creation_date = DateTimeAttribute(null=True)
document_preamble = UnicodeAttribute(null=True)
document_legal_entity_name = UnicodeAttribute(null=True)
document_s3_url = UnicodeAttribute(null=True)
document_tabs = ListAttribute(of=DocumentTabModel, default=list)
class Document(model_interfaces.Document):
"""
ORM-agnostic wrapper for the DynamoDB Document model.