-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup_objects.py
236 lines (227 loc) · 8.91 KB
/
setup_objects.py
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
from django.conf import settings
from django.contrib.auth.models import Group
from bag_transfer.lib.RAC_CMD import (add2grp, add_org, add_user,
set_server_password)
from bag_transfer.models import (AcceptBagItVersion, AcceptSerialization,
BagItProfile, BagItProfileBagInfo,
BagItProfileBagInfoValues, ManifestsAllowed,
Organization, User)
from bag_transfer.rights.models import (RecordType, RightsStatement,
RightsStatementCopyright,
RightsStatementOther,
RightsStatementRightsGranted)
orgs = Organization.objects.all()
org_ids = []
DEFAULT_ORGS = [
{"name": "Archival Repository", "acquisition_type": "donation"},
{"name": "Donor Organization", "acquisition_type": "deposit"},
]
DEFAULT_USERS = [
{
"username": "admin",
"password": "password",
"first_name": "System",
"last_name": "Administrator",
"superuser": True,
"staff": True,
"org_admin": True,
"org": "Archival Repository",
},
{
"username": "donor",
"password": "password",
"first_name": "Donor",
"last_name": "Representative",
"superuser": False,
"staff": False,
"org_admin": True,
"org": "Donor Organization",
},
{
"username": "manager",
"password": "password",
"first_name": "Managing",
"last_name": "Archivist",
"superuser": False,
"staff": True,
"org_admin": True,
"groups": ["managing_archivists"],
"org": "Archival Repository",
},
{
"username": "appraiser",
"password": "password",
"first_name": "Appraisal",
"last_name": "Archivist",
"superuser": False,
"staff": True,
"groups": ["appraisal_archivists"],
"org": "Archival Repository",
},
{
"username": "accessioner",
"password": "password",
"first_name": "Accessioning",
"last_name": "Archivist",
"superuser": False,
"staff": True,
"groups": ["accessioning_archivists"],
"org": "Archival Repository",
},
]
print("Creating record types")
for type in [
"administrative records",
"board materials",
"grant records",
"communications and publications",
"annual reports",
]:
RecordType.objects.get_or_create(name=type)
print("Creating organizations")
if len(orgs) == 0:
for org in DEFAULT_ORGS:
new_org = Organization.objects.create(
name=org["name"], acquisition_type=org["acquisition_type"]
)
transfer_org = Organization.objects.get(name="Archival Repository")
print("Creating BagIt Profile for {}".format(new_org))
profile = BagItProfile.objects.create(
source_organization=transfer_org,
external_description="Test BagIt Profile",
contact_email="[email protected]",
organization=new_org
)
AcceptSerialization.objects.create(
name="application/zip", bagit_profile=profile
)
AcceptSerialization.objects.create(
name="application/x-tar", bagit_profile=profile
)
AcceptSerialization.objects.create(
name="application/x-gzip", bagit_profile=profile
)
ManifestsAllowed.objects.create(name="sha256", bagit_profile=profile)
ManifestsAllowed.objects.create(name="sha512", bagit_profile=profile)
AcceptBagItVersion.objects.create(name="0.97", bagit_profile=profile)
for field in [
"external_identifier",
"internal_sender_description",
"title",
"date_start",
"date_end",
"bagging_date",
"payload_oxum",
"source_organization",
"record_type",
]:
BagItProfileBagInfo.objects.create(
bagit_profile=profile, field=field, required=True, repeatable=False
)
for field in ["record_creators", "language"]:
BagItProfileBagInfo.objects.create(
bagit_profile=profile, field=field, required=True, repeatable=True
)
for field in ["bag_count", "bag_group_identifier"]:
BagItProfileBagInfo.objects.create(
bagit_profile=profile, field=field, required=False, repeatable=False
)
source_organization = BagItProfileBagInfo.objects.get(
field="source_organization", bagit_profile=profile
)
BagItProfileBagInfoValues.objects.create(
bagit_profile_baginfo=source_organization, name=new_org.name
)
record_type = BagItProfileBagInfo.objects.get(
field="record_type", bagit_profile=profile
)
for name in [
"administrative records",
"board materials",
"grant records",
"communications and publications",
"annual reports",
]:
BagItProfileBagInfoValues.objects.create(
bagit_profile_baginfo=record_type,
name=name)
print("Creating Rights Statements for {}".format(new_org))
copyright_statement = RightsStatement.objects.create(
organization=new_org, rights_basis="Copyright",
)
for record_type in RecordType.objects.all():
copyright_statement.applies_to_type.add(record_type)
donor_statement = RightsStatement.objects.create(
organization=new_org, rights_basis="Other",
)
for record_type in RecordType.objects.all():
donor_statement.applies_to_type.add(record_type)
RightsStatementCopyright.objects.create(
rights_statement=copyright_statement,
copyright_status="copyrighted",
copyright_end_date_period=40,
copyright_note="Work for hire, under copyright until 40 years after creation.",
)
RightsStatementOther.objects.create(
rights_statement=donor_statement,
other_rights_basis="Donor",
other_rights_end_date_period=10,
other_rights_note="Records embargoed for 10 years after date of creation.",
)
RightsStatementRightsGranted.objects.create(
rights_statement=copyright_statement,
act="publish",
end_date_period=40,
rights_granted_note="Records under copyright may not be republished without permission from the copyright holder.",
restriction="disallow",
)
RightsStatementRightsGranted.objects.create(
rights_statement=donor_statement,
act="disseminate",
start_date_period=10,
rights_granted_note="Records are open for research after expiration of embargo period.",
restriction="allow",
)
RightsStatementRightsGranted.objects.create(
rights_statement=donor_statement,
act="disseminate",
end_date_period=10,
rights_granted_note="Records are open for research after expiration of embargo period.",
restriction="disallow",
)
else:
for org in Organization.objects.all():
add_org(org.machine_name)
for user in DEFAULT_USERS:
if User.objects.filter(username=user["username"]).exists():
user = User.objects.get(username=user["username"])
# Re-adds linux system users based on users in database
if add_user(user.username):
if not settings.S3_USE:
add2grp(user.organization.machine_name, user.username)
# Resets the user's password to "password".
# If you don't want this behavior, you can comment out this line,
# but be aware that in a Docker environment doing so will mean that
# you need to manually set a user's password in Aurora before you are
# able to SFTP records into the container with that username and password.
if not settings.COGNITO_USE:
set_server_password(user.username, "password")
else:
new_user = User.objects.create_user(
user["username"],
first_name=user["first_name"],
last_name=user["last_name"],
email=f"{user['username']}@example.org",
is_superuser=user["superuser"],
is_staff=user["staff"],
is_org_admin=user.get("org_admin", False),
organization=Organization.objects.get(name=user["org"]),
)
new_user.set_password(user["password"])
if "groups" in user:
for group in user["groups"]:
g = Group.objects.get_or_create(name=group)[0]
new_user.groups.add(g)
if not settings.COGNITO_USE:
set_server_password(user["username"], user["password"])
new_user.save()