pdc-api 0.4.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pdc_api/__init__.py +37 -0
- pdc_api/_base.py +6 -0
- pdc_api/_generated.py +873 -0
- pdc_api/client.py +332 -0
- pdc_api/errors.py +61 -0
- pdc_api/models.py +136 -0
- pdc_api/permission_grants.py +307 -0
- pdc_api/py.typed +0 -0
- pdc_api/seeding/__init__.py +0 -0
- pdc_api/seeding/keycloak_admin.py +179 -0
- pdc_api/seeding/pdc_entities.py +711 -0
- pdc_api/testing.py +314 -0
- pdc_api/token.py +69 -0
- pdc_api/transport.py +100 -0
- pdc_api-0.4.1.dist-info/METADATA +276 -0
- pdc_api-0.4.1.dist-info/RECORD +18 -0
- pdc_api-0.4.1.dist-info/WHEEL +4 -0
- pdc_api-0.4.1.dist-info/licenses/LICENSE +661 -0
|
@@ -0,0 +1,711 @@
|
|
|
1
|
+
import uuid
|
|
2
|
+
from dataclasses import dataclass, field
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
import requests
|
|
6
|
+
from faker import Faker
|
|
7
|
+
|
|
8
|
+
from pdc_api.client import (
|
|
9
|
+
create_changemaker_field_value,
|
|
10
|
+
create_changemaker_field_value_batch,
|
|
11
|
+
create_changemaker_source,
|
|
12
|
+
create_initiative,
|
|
13
|
+
create_initiative_field_value,
|
|
14
|
+
get_bundle_page,
|
|
15
|
+
get_changemaker_field_values,
|
|
16
|
+
get_organization_by_keycloak_id,
|
|
17
|
+
link_changemaker_to_proposal,
|
|
18
|
+
)
|
|
19
|
+
from pdc_api.errors import PDCNotFoundError
|
|
20
|
+
from pdc_api.models import (
|
|
21
|
+
BaseField,
|
|
22
|
+
ChangemakerBundle,
|
|
23
|
+
ChangemakerFieldValue,
|
|
24
|
+
ChangemakerFieldValueBatch,
|
|
25
|
+
DataProvider,
|
|
26
|
+
Funder,
|
|
27
|
+
Initiative,
|
|
28
|
+
InitiativeFieldValue,
|
|
29
|
+
Opportunity,
|
|
30
|
+
Organization,
|
|
31
|
+
Proposal,
|
|
32
|
+
ProposalVersion,
|
|
33
|
+
ShallowChangemaker,
|
|
34
|
+
Source,
|
|
35
|
+
)
|
|
36
|
+
from pdc_api.seeding.keycloak_admin import KeycloakOrganization
|
|
37
|
+
from pdc_api.transport import APISession
|
|
38
|
+
|
|
39
|
+
fake = Faker()
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass
|
|
43
|
+
class SeededApplicationForm:
|
|
44
|
+
id: int
|
|
45
|
+
opportunity_id: int
|
|
46
|
+
fields: list[dict[str, Any]] = field(default_factory=list)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass
|
|
50
|
+
class SeededProposalBundle:
|
|
51
|
+
changemaker: ShallowChangemaker | None
|
|
52
|
+
funder: Funder
|
|
53
|
+
opportunity: Opportunity
|
|
54
|
+
source: Source
|
|
55
|
+
application_form: SeededApplicationForm
|
|
56
|
+
proposal: Proposal
|
|
57
|
+
proposal_version: ProposalVersion
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@dataclass
|
|
61
|
+
class SeededChangemakerWithProposals:
|
|
62
|
+
changemaker: ShallowChangemaker
|
|
63
|
+
bundles: list[SeededProposalBundle]
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
_PREFIX = "seed"
|
|
67
|
+
|
|
68
|
+
_PROPOSAL_FIELD_LABELS: tuple[tuple[str, str], ...] = (
|
|
69
|
+
("proposal_name", "Proposal Name"),
|
|
70
|
+
("proposal_summary", "Proposal Summary"),
|
|
71
|
+
("proposal_focus", "Proposal Focus"),
|
|
72
|
+
("proposal_date", "Proposal Date"),
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def make_proposal_field_seeds(*, include_numbers: bool = False) -> list[tuple[str, str, str]]:
|
|
77
|
+
if include_numbers:
|
|
78
|
+
# Numbers lead the text so they are the first digits a search sees; no thousands separators.
|
|
79
|
+
name = f"#{fake.random_int(min=100, max=9999)} {fake.catch_phrase()}"
|
|
80
|
+
summary = (
|
|
81
|
+
f"Requests ${fake.random_int(min=5_000, max=500_000)} "
|
|
82
|
+
f"over {fake.random_int(min=1, max=5)} years to serve "
|
|
83
|
+
f"{fake.random_int(min=50, max=5_000)} participants. "
|
|
84
|
+
f"{fake.sentence(nb_words=10)}"
|
|
85
|
+
)
|
|
86
|
+
else:
|
|
87
|
+
name = fake.catch_phrase()
|
|
88
|
+
summary = fake.paragraph(nb_sentences=2)
|
|
89
|
+
values_by_short_code = {
|
|
90
|
+
"proposal_name": name,
|
|
91
|
+
"proposal_summary": summary,
|
|
92
|
+
"proposal_focus": fake.bs().title(),
|
|
93
|
+
"proposal_date": fake.date_between(start_date="-3y", end_date="today").isoformat(),
|
|
94
|
+
}
|
|
95
|
+
return [(short_code, label, values_by_short_code[short_code]) for short_code, label in _PROPOSAL_FIELD_LABELS]
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def make_changemaker_name(*, include_numbers: bool = False) -> str:
|
|
99
|
+
if include_numbers:
|
|
100
|
+
suffix: str = fake.random_element(("Foundation", "Initiative", "Collective", "Project", "Alliance"))
|
|
101
|
+
return f"{fake.last_name()} {fake.random_int(min=100, max=9999)} {suffix}"
|
|
102
|
+
return fake.company()
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _json_object(response: requests.Response) -> dict[str, Any]:
|
|
106
|
+
payload: dict[str, Any] = response.json()
|
|
107
|
+
return payload
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _token(label: str) -> str:
|
|
111
|
+
return f"{_PREFIX}_{label}_{uuid.uuid4().hex[:8]}"
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def seed_base_field(
|
|
115
|
+
session: APISession,
|
|
116
|
+
*,
|
|
117
|
+
short_code: str | None = None,
|
|
118
|
+
label: str | None = None,
|
|
119
|
+
data_type: str = "string",
|
|
120
|
+
category: str = "project",
|
|
121
|
+
sensitivity_classification: str = "public",
|
|
122
|
+
) -> BaseField:
|
|
123
|
+
short_code = short_code or _token("field")
|
|
124
|
+
label = label or f"Test Field {short_code}"
|
|
125
|
+
response = session.put(
|
|
126
|
+
f"baseFields/{short_code}",
|
|
127
|
+
json={
|
|
128
|
+
"label": label,
|
|
129
|
+
"description": f"Integration test base field: {short_code}",
|
|
130
|
+
"dataType": data_type,
|
|
131
|
+
"category": category,
|
|
132
|
+
"sensitivityClassification": sensitivity_classification,
|
|
133
|
+
"valueRelevanceHours": None,
|
|
134
|
+
},
|
|
135
|
+
)
|
|
136
|
+
return BaseField.model_validate(response.json())
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
_FUNDER_NAME_SUFFIXES: tuple[str, ...] = (
|
|
140
|
+
"Foundation",
|
|
141
|
+
"Fund",
|
|
142
|
+
"Trust",
|
|
143
|
+
"Endowment",
|
|
144
|
+
"Philanthropies",
|
|
145
|
+
)
|
|
146
|
+
_COLLABORATIVE_NAME_SUFFIXES: tuple[str, ...] = (
|
|
147
|
+
"Collaborative",
|
|
148
|
+
"Funders Collaborative",
|
|
149
|
+
"Pooled Fund",
|
|
150
|
+
"Funder Alliance",
|
|
151
|
+
"Giving Circle",
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def make_funder_name(*, is_collaborative: bool = False) -> str:
|
|
156
|
+
suffixes = _COLLABORATIVE_NAME_SUFFIXES if is_collaborative else _FUNDER_NAME_SUFFIXES
|
|
157
|
+
return f"{fake.last_name()} {fake.random_element(suffixes)}"
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def seed_funder(
|
|
161
|
+
session: APISession,
|
|
162
|
+
*,
|
|
163
|
+
short_code: str | None = None,
|
|
164
|
+
name: str | None = None,
|
|
165
|
+
is_collaborative: bool = False,
|
|
166
|
+
keycloak_organization_id: str | None = None,
|
|
167
|
+
) -> Funder:
|
|
168
|
+
short_code = short_code or _token("funder")
|
|
169
|
+
name = name or make_funder_name(is_collaborative=is_collaborative)
|
|
170
|
+
body: dict[str, Any] = {"name": name, "isCollaborative": is_collaborative}
|
|
171
|
+
if keycloak_organization_id is not None:
|
|
172
|
+
body["keycloakOrganizationId"] = keycloak_organization_id
|
|
173
|
+
response = session.put(f"funders/{short_code}", json=body)
|
|
174
|
+
return Funder.model_validate(response.json())
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def seed_funder_collaborative_member(
|
|
178
|
+
session: APISession,
|
|
179
|
+
*,
|
|
180
|
+
collaborative: Funder,
|
|
181
|
+
member: Funder,
|
|
182
|
+
) -> dict[str, Any]:
|
|
183
|
+
response = session.post(
|
|
184
|
+
f"funders/{collaborative.short_code}/members/{member.short_code}",
|
|
185
|
+
)
|
|
186
|
+
return _json_object(response)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
@dataclass
|
|
190
|
+
class SeededFunderCollaborative:
|
|
191
|
+
collaborative: Funder
|
|
192
|
+
members: list[Funder]
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def seed_funder_collaborative_with_members(
|
|
196
|
+
session: APISession,
|
|
197
|
+
*,
|
|
198
|
+
member_count: int,
|
|
199
|
+
name: str | None = None,
|
|
200
|
+
) -> SeededFunderCollaborative:
|
|
201
|
+
collaborative = seed_funder(session, name=name, is_collaborative=True)
|
|
202
|
+
members = [seed_funder(session, is_collaborative=False) for _ in range(member_count)]
|
|
203
|
+
for member in members:
|
|
204
|
+
seed_funder_collaborative_member(session, collaborative=collaborative, member=member)
|
|
205
|
+
return SeededFunderCollaborative(collaborative=collaborative, members=members)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
@dataclass
|
|
209
|
+
class SeededFunderVariety:
|
|
210
|
+
standalones: list[Funder]
|
|
211
|
+
collaborative: SeededFunderCollaborative
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def seed_funder_variety(
|
|
215
|
+
session: APISession,
|
|
216
|
+
*,
|
|
217
|
+
standalone_count: int = 6,
|
|
218
|
+
collaborative_member_count: int = 3,
|
|
219
|
+
) -> SeededFunderVariety:
|
|
220
|
+
standalones = [seed_funder(session, is_collaborative=False) for _ in range(standalone_count)]
|
|
221
|
+
collaborative = seed_funder_collaborative_with_members(session, member_count=collaborative_member_count)
|
|
222
|
+
return SeededFunderVariety(standalones=standalones, collaborative=collaborative)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def seed_changemaker(
|
|
226
|
+
session: APISession,
|
|
227
|
+
*,
|
|
228
|
+
name: str | None = None,
|
|
229
|
+
tax_id: str | None = None,
|
|
230
|
+
keycloak_organization_id: str | None = None,
|
|
231
|
+
) -> ShallowChangemaker:
|
|
232
|
+
name = name or make_changemaker_name()
|
|
233
|
+
# PDC has no changemaker upsert, so a caller-supplied tax_id is looked up first to stay idempotent.
|
|
234
|
+
if tax_id is not None:
|
|
235
|
+
existing = _find_changemaker_by_tax_id(session, tax_id)
|
|
236
|
+
if existing is not None:
|
|
237
|
+
return existing
|
|
238
|
+
else:
|
|
239
|
+
tax_id = f"XX-{uuid.uuid4().hex[:7]}"
|
|
240
|
+
body: dict[str, Any] = {"name": name, "taxId": tax_id}
|
|
241
|
+
if keycloak_organization_id is not None:
|
|
242
|
+
body["keycloakOrganizationId"] = keycloak_organization_id
|
|
243
|
+
response = session.post("changemakers", json=body)
|
|
244
|
+
return ShallowChangemaker.model_validate(response.json())
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def _find_changemaker_by_tax_id(session: APISession, tax_id: str) -> ShallowChangemaker | None:
|
|
248
|
+
# PDC's _content search matches names only, so page through every changemaker.
|
|
249
|
+
page = 1
|
|
250
|
+
page_size = 100
|
|
251
|
+
while True:
|
|
252
|
+
bundle = get_bundle_page(session, ChangemakerBundle, page=page, count_per_page=page_size)
|
|
253
|
+
for entry in bundle.entries:
|
|
254
|
+
if entry.tax_id == tax_id:
|
|
255
|
+
return entry
|
|
256
|
+
if not bundle.entries or page * page_size >= bundle.total:
|
|
257
|
+
return None
|
|
258
|
+
page += 1
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def seed_opportunity(
|
|
262
|
+
session: APISession,
|
|
263
|
+
*,
|
|
264
|
+
funder: Funder | None = None,
|
|
265
|
+
title: str | None = None,
|
|
266
|
+
) -> Opportunity:
|
|
267
|
+
if funder is None:
|
|
268
|
+
funder = seed_funder(session)
|
|
269
|
+
title = title or f"{fake.catch_phrase()} Grant"
|
|
270
|
+
response = session.post(
|
|
271
|
+
"opportunities",
|
|
272
|
+
json={"title": title, "funderShortCode": funder.short_code},
|
|
273
|
+
)
|
|
274
|
+
return Opportunity.model_validate(response.json())
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def seed_data_provider(
|
|
278
|
+
session: APISession,
|
|
279
|
+
*,
|
|
280
|
+
short_code: str | None = None,
|
|
281
|
+
name: str | None = None,
|
|
282
|
+
keycloak_organization_id: str | None = None,
|
|
283
|
+
) -> DataProvider:
|
|
284
|
+
short_code = short_code or _token("dp")
|
|
285
|
+
name = name or f"{fake.company()} Data"
|
|
286
|
+
body: dict[str, Any] = {"name": name}
|
|
287
|
+
if keycloak_organization_id is not None:
|
|
288
|
+
body["keycloakOrganizationId"] = keycloak_organization_id
|
|
289
|
+
response = session.put(f"dataProviders/{short_code}", json=body)
|
|
290
|
+
return DataProvider.model_validate(response.json())
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def _keycloak_alias_slug(keycloak_org: KeycloakOrganization) -> str:
|
|
294
|
+
# Keycloak aliases are kebab-case; PDC short codes are snake_case.
|
|
295
|
+
return keycloak_org.alias.replace("-", "_")
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def _find_organization(session: APISession, *, keycloak_org: KeycloakOrganization) -> Organization | None:
|
|
299
|
+
try:
|
|
300
|
+
return get_organization_by_keycloak_id(session, keycloak_id=keycloak_org.id)
|
|
301
|
+
except PDCNotFoundError:
|
|
302
|
+
return None
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def ensure_funder_for_keycloak_org(
|
|
306
|
+
session: APISession,
|
|
307
|
+
*,
|
|
308
|
+
keycloak_org: KeycloakOrganization,
|
|
309
|
+
name: str,
|
|
310
|
+
is_collaborative: bool = False,
|
|
311
|
+
) -> Funder:
|
|
312
|
+
org = _find_organization(session, keycloak_org=keycloak_org)
|
|
313
|
+
if org is not None and org.funder is not None:
|
|
314
|
+
return org.funder
|
|
315
|
+
return seed_funder(
|
|
316
|
+
session,
|
|
317
|
+
short_code=f"{_keycloak_alias_slug(keycloak_org)}_funder",
|
|
318
|
+
name=name,
|
|
319
|
+
is_collaborative=is_collaborative,
|
|
320
|
+
keycloak_organization_id=keycloak_org.id,
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def ensure_changemaker_for_keycloak_org(
|
|
325
|
+
session: APISession,
|
|
326
|
+
*,
|
|
327
|
+
keycloak_org: KeycloakOrganization,
|
|
328
|
+
name: str,
|
|
329
|
+
) -> ShallowChangemaker:
|
|
330
|
+
org = _find_organization(session, keycloak_org=keycloak_org)
|
|
331
|
+
if org is not None and org.changemaker is not None:
|
|
332
|
+
return org.changemaker
|
|
333
|
+
return seed_changemaker(session, name=name, keycloak_organization_id=keycloak_org.id)
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def ensure_data_provider_for_keycloak_org(
|
|
337
|
+
session: APISession,
|
|
338
|
+
*,
|
|
339
|
+
keycloak_org: KeycloakOrganization,
|
|
340
|
+
name: str,
|
|
341
|
+
) -> DataProvider:
|
|
342
|
+
org = _find_organization(session, keycloak_org=keycloak_org)
|
|
343
|
+
if org is not None and org.data_provider is not None:
|
|
344
|
+
return org.data_provider
|
|
345
|
+
return seed_data_provider(
|
|
346
|
+
session,
|
|
347
|
+
short_code=f"{_keycloak_alias_slug(keycloak_org)}_data_provider",
|
|
348
|
+
name=name,
|
|
349
|
+
keycloak_organization_id=keycloak_org.id,
|
|
350
|
+
)
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def seed_fiscal_sponsorship(
|
|
354
|
+
session: APISession,
|
|
355
|
+
*,
|
|
356
|
+
sponsee: ShallowChangemaker,
|
|
357
|
+
sponsor: ShallowChangemaker,
|
|
358
|
+
not_after: str | None = None,
|
|
359
|
+
) -> dict[str, Any]:
|
|
360
|
+
body: dict[str, Any] = {}
|
|
361
|
+
if not_after is not None:
|
|
362
|
+
body["notAfter"] = not_after
|
|
363
|
+
response = session.put(
|
|
364
|
+
f"changemakers/{sponsee.id}/fiscalSponsors/{sponsor.id}",
|
|
365
|
+
json=body,
|
|
366
|
+
)
|
|
367
|
+
return _json_object(response)
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
def seed_funder_collaborative_invitation(
|
|
371
|
+
session: APISession,
|
|
372
|
+
*,
|
|
373
|
+
collaborative: Funder,
|
|
374
|
+
invited: Funder,
|
|
375
|
+
) -> dict[str, Any]:
|
|
376
|
+
response = session.post(
|
|
377
|
+
f"funders/{collaborative.short_code}/invitations/sent/{invited.short_code}",
|
|
378
|
+
# PDC rejects a missing body even though no fields are required.
|
|
379
|
+
json={},
|
|
380
|
+
)
|
|
381
|
+
return _json_object(response)
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def respond_to_funder_collaborative_invitation(
|
|
385
|
+
session: APISession,
|
|
386
|
+
*,
|
|
387
|
+
collaborative: Funder,
|
|
388
|
+
invited: Funder,
|
|
389
|
+
status: str,
|
|
390
|
+
) -> dict[str, Any]:
|
|
391
|
+
response = session.patch(
|
|
392
|
+
f"funders/{invited.short_code}/invitations/received/{collaborative.short_code}",
|
|
393
|
+
json={"invitationStatus": status},
|
|
394
|
+
)
|
|
395
|
+
return _json_object(response)
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
def seed_source(
|
|
399
|
+
session: APISession,
|
|
400
|
+
*,
|
|
401
|
+
label: str | None = None,
|
|
402
|
+
data_provider_short_code: str | None = None,
|
|
403
|
+
funder_short_code: str | None = None,
|
|
404
|
+
changemaker_id: int | None = None,
|
|
405
|
+
) -> Source:
|
|
406
|
+
label = label or f"{fake.catch_phrase()} Source"
|
|
407
|
+
if changemaker_id is not None:
|
|
408
|
+
return create_changemaker_source(session, changemaker_id=changemaker_id, label=label)
|
|
409
|
+
payload: dict[str, Any] = {"label": label}
|
|
410
|
+
if data_provider_short_code:
|
|
411
|
+
payload["dataProviderShortCode"] = data_provider_short_code
|
|
412
|
+
elif funder_short_code:
|
|
413
|
+
payload["funderShortCode"] = funder_short_code
|
|
414
|
+
else:
|
|
415
|
+
funder = seed_funder(session)
|
|
416
|
+
payload["funderShortCode"] = funder.short_code
|
|
417
|
+
response = session.post("sources", json=payload)
|
|
418
|
+
return Source.model_validate(response.json())
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
def seed_initiative(session: APISession, *, changemaker_id: int, title: str | None = None) -> Initiative:
|
|
422
|
+
return create_initiative(session, changemaker_id=changemaker_id, title=title or fake.catch_phrase())
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
def seed_initiative_field_value(
|
|
426
|
+
session: APISession,
|
|
427
|
+
*,
|
|
428
|
+
initiative_id: int,
|
|
429
|
+
base_field_short_code: str,
|
|
430
|
+
source: Source,
|
|
431
|
+
value: str,
|
|
432
|
+
) -> InitiativeFieldValue:
|
|
433
|
+
return create_initiative_field_value(
|
|
434
|
+
session,
|
|
435
|
+
initiative_id=initiative_id,
|
|
436
|
+
base_field_short_code=base_field_short_code,
|
|
437
|
+
value=value,
|
|
438
|
+
source_id=source.id,
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def seed_application_form(
|
|
443
|
+
session: APISession,
|
|
444
|
+
*,
|
|
445
|
+
opportunity: Opportunity | None = None,
|
|
446
|
+
base_field_short_codes: list[str] | None = None,
|
|
447
|
+
name: str | None = None,
|
|
448
|
+
) -> SeededApplicationForm:
|
|
449
|
+
if opportunity is None:
|
|
450
|
+
opportunity = seed_opportunity(session)
|
|
451
|
+
fields = []
|
|
452
|
+
if base_field_short_codes:
|
|
453
|
+
fields = [
|
|
454
|
+
{
|
|
455
|
+
"baseFieldShortCode": sc,
|
|
456
|
+
"position": i + 1,
|
|
457
|
+
"label": f"Field {sc}",
|
|
458
|
+
"instructions": None,
|
|
459
|
+
"inputType": None,
|
|
460
|
+
}
|
|
461
|
+
for i, sc in enumerate(base_field_short_codes)
|
|
462
|
+
]
|
|
463
|
+
response = session.post(
|
|
464
|
+
"applicationForms",
|
|
465
|
+
json={
|
|
466
|
+
"opportunityId": opportunity.id,
|
|
467
|
+
"name": name or f"Test Application Form {uuid.uuid4().hex[:8]}",
|
|
468
|
+
"fields": fields,
|
|
469
|
+
},
|
|
470
|
+
)
|
|
471
|
+
data = response.json()
|
|
472
|
+
return SeededApplicationForm(
|
|
473
|
+
id=data["id"],
|
|
474
|
+
opportunity_id=opportunity.id,
|
|
475
|
+
fields=data.get("fields", []),
|
|
476
|
+
)
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
def seed_proposal(
|
|
480
|
+
session: APISession,
|
|
481
|
+
*,
|
|
482
|
+
opportunity: Opportunity | None = None,
|
|
483
|
+
external_id: str | None = None,
|
|
484
|
+
) -> Proposal:
|
|
485
|
+
if opportunity is None:
|
|
486
|
+
opportunity = seed_opportunity(session)
|
|
487
|
+
external_id = external_id or _token("proposal")
|
|
488
|
+
response = session.post(
|
|
489
|
+
"proposals",
|
|
490
|
+
json={
|
|
491
|
+
"opportunityId": opportunity.id,
|
|
492
|
+
"externalId": external_id,
|
|
493
|
+
},
|
|
494
|
+
)
|
|
495
|
+
return Proposal.model_validate(response.json())
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
def seed_proposal_version(
|
|
499
|
+
session: APISession,
|
|
500
|
+
*,
|
|
501
|
+
proposal: Proposal,
|
|
502
|
+
source: Source,
|
|
503
|
+
application_form: SeededApplicationForm,
|
|
504
|
+
field_values: list[dict[str, Any]] | None = None,
|
|
505
|
+
) -> ProposalVersion:
|
|
506
|
+
response = session.post(
|
|
507
|
+
"proposalVersions",
|
|
508
|
+
json={
|
|
509
|
+
"proposalId": proposal.id,
|
|
510
|
+
"sourceId": source.id,
|
|
511
|
+
"applicationFormId": application_form.id,
|
|
512
|
+
"fieldValues": field_values or [],
|
|
513
|
+
},
|
|
514
|
+
)
|
|
515
|
+
return ProposalVersion.model_validate(response.json())
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
_CHANGEMAKER_PROFILE_FIELDS: tuple[tuple[str, str, str], ...] = (
|
|
519
|
+
("organization_legal_name", "Legal name", "string"),
|
|
520
|
+
("organization_website", "Website", "url"),
|
|
521
|
+
("organization_mission_statement", "Mission statement", "string"),
|
|
522
|
+
("organization_street_address_1", "Street address", "string"),
|
|
523
|
+
("organization_city", "City", "string"),
|
|
524
|
+
("organization_state_province", "State / province", "string"),
|
|
525
|
+
("organization_postal_code", "Postal code", "string"),
|
|
526
|
+
("organization_country", "Country", "string"),
|
|
527
|
+
)
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
def make_changemaker_profile_field_seeds(*, name: str) -> list[tuple[str, str, str, str]]:
|
|
531
|
+
values_by_short_code = {
|
|
532
|
+
"organization_legal_name": f"{name} Inc.",
|
|
533
|
+
"organization_website": f"https://www.{fake.domain_name()}",
|
|
534
|
+
"organization_mission_statement": fake.paragraph(nb_sentences=2),
|
|
535
|
+
"organization_street_address_1": fake.street_address(),
|
|
536
|
+
"organization_city": fake.city(),
|
|
537
|
+
"organization_state_province": fake.state(),
|
|
538
|
+
"organization_postal_code": fake.postcode(),
|
|
539
|
+
"organization_country": "United States",
|
|
540
|
+
}
|
|
541
|
+
return [
|
|
542
|
+
(short_code, label, data_type, values_by_short_code[short_code])
|
|
543
|
+
for short_code, label, data_type in _CHANGEMAKER_PROFILE_FIELDS
|
|
544
|
+
]
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
@dataclass
|
|
548
|
+
class SeededChangemakerProfile:
|
|
549
|
+
changemaker: ShallowChangemaker
|
|
550
|
+
source: Source
|
|
551
|
+
batch: ChangemakerFieldValueBatch
|
|
552
|
+
field_values: list[ChangemakerFieldValue]
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
def seed_changemaker_profile_fields(
|
|
556
|
+
session: APISession,
|
|
557
|
+
*,
|
|
558
|
+
changemaker: ShallowChangemaker,
|
|
559
|
+
data_provider: DataProvider,
|
|
560
|
+
source_label: str | None = None,
|
|
561
|
+
notes: str | None = "Seeded organization profile import",
|
|
562
|
+
) -> SeededChangemakerProfile | None:
|
|
563
|
+
existing = get_changemaker_field_values(session, changemaker_id=changemaker.id)
|
|
564
|
+
if any(field_value.batch is not None for field_value in existing):
|
|
565
|
+
return None
|
|
566
|
+
field_seeds = make_changemaker_profile_field_seeds(name=changemaker.name)
|
|
567
|
+
for short_code, label, data_type, _ in field_seeds:
|
|
568
|
+
seed_base_field(session, short_code=short_code, label=label, data_type=data_type, category="organization")
|
|
569
|
+
source = seed_source(
|
|
570
|
+
session,
|
|
571
|
+
label=source_label or f"{data_provider.name} nonprofit profiles",
|
|
572
|
+
data_provider_short_code=data_provider.short_code,
|
|
573
|
+
)
|
|
574
|
+
batch = create_changemaker_field_value_batch(session, source_id=source.id, notes=notes)
|
|
575
|
+
field_values = [
|
|
576
|
+
create_changemaker_field_value(
|
|
577
|
+
session,
|
|
578
|
+
changemaker_id=changemaker.id,
|
|
579
|
+
base_field_short_code=short_code,
|
|
580
|
+
batch_id=batch.id,
|
|
581
|
+
value=value,
|
|
582
|
+
)
|
|
583
|
+
for short_code, _, _, value in field_seeds
|
|
584
|
+
]
|
|
585
|
+
return SeededChangemakerProfile(changemaker=changemaker, source=source, batch=batch, field_values=field_values)
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
def _seed_linked_proposal(
|
|
589
|
+
session: APISession,
|
|
590
|
+
*,
|
|
591
|
+
changemaker: ShallowChangemaker,
|
|
592
|
+
funder: Funder,
|
|
593
|
+
opportunity: Opportunity,
|
|
594
|
+
source: Source,
|
|
595
|
+
application_form: SeededApplicationForm,
|
|
596
|
+
field_seeds: list[tuple[str, str, str]],
|
|
597
|
+
) -> SeededProposalBundle:
|
|
598
|
+
form_field_id_by_short_code = {
|
|
599
|
+
form_field["baseFieldShortCode"]: form_field["id"] for form_field in application_form.fields
|
|
600
|
+
}
|
|
601
|
+
proposal = seed_proposal(session, opportunity=opportunity)
|
|
602
|
+
proposal_version = seed_proposal_version(
|
|
603
|
+
session,
|
|
604
|
+
proposal=proposal,
|
|
605
|
+
source=source,
|
|
606
|
+
application_form=application_form,
|
|
607
|
+
field_values=[
|
|
608
|
+
{
|
|
609
|
+
"applicationFormFieldId": form_field_id_by_short_code[short_code],
|
|
610
|
+
"position": position,
|
|
611
|
+
"value": value,
|
|
612
|
+
"goodAsOf": None,
|
|
613
|
+
}
|
|
614
|
+
for position, (short_code, _, value) in enumerate(field_seeds, start=1)
|
|
615
|
+
],
|
|
616
|
+
)
|
|
617
|
+
link_changemaker_to_proposal(session, changemaker_id=changemaker.id, proposal_id=proposal.id)
|
|
618
|
+
return SeededProposalBundle(
|
|
619
|
+
changemaker=changemaker,
|
|
620
|
+
funder=funder,
|
|
621
|
+
opportunity=opportunity,
|
|
622
|
+
source=source,
|
|
623
|
+
application_form=application_form,
|
|
624
|
+
proposal=proposal,
|
|
625
|
+
proposal_version=proposal_version,
|
|
626
|
+
)
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
def seed_proposal_bundle(
|
|
630
|
+
session: APISession,
|
|
631
|
+
*,
|
|
632
|
+
field_seeds: list[tuple[str, str, str]] | None = None,
|
|
633
|
+
changemaker_name: str | None = None,
|
|
634
|
+
field_sensitivity_classification: str = "public",
|
|
635
|
+
) -> SeededProposalBundle:
|
|
636
|
+
# Base fields upsert by short_code across the whole stack, so a non-public sensitivity needs unique short codes.
|
|
637
|
+
if field_seeds is None:
|
|
638
|
+
field_seeds = make_proposal_field_seeds()
|
|
639
|
+
seeded_fields = [
|
|
640
|
+
seed_base_field(
|
|
641
|
+
session, short_code=short_code, label=label, sensitivity_classification=field_sensitivity_classification
|
|
642
|
+
)
|
|
643
|
+
for short_code, label, _ in field_seeds
|
|
644
|
+
]
|
|
645
|
+
funder = seed_funder(session)
|
|
646
|
+
opportunity = seed_opportunity(session, funder=funder)
|
|
647
|
+
return _seed_linked_proposal(
|
|
648
|
+
session,
|
|
649
|
+
changemaker=seed_changemaker(session, name=changemaker_name),
|
|
650
|
+
funder=funder,
|
|
651
|
+
opportunity=opportunity,
|
|
652
|
+
source=seed_source(session, funder_short_code=funder.short_code),
|
|
653
|
+
application_form=seed_application_form(
|
|
654
|
+
session, opportunity=opportunity, base_field_short_codes=[bf.short_code for bf in seeded_fields]
|
|
655
|
+
),
|
|
656
|
+
field_seeds=field_seeds,
|
|
657
|
+
)
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
def seed_changemaker_with_proposals(
|
|
661
|
+
session: APISession,
|
|
662
|
+
*,
|
|
663
|
+
proposal_count: int,
|
|
664
|
+
changemaker_name: str | None = None,
|
|
665
|
+
changemaker: ShallowChangemaker | None = None,
|
|
666
|
+
funder: Funder | None = None,
|
|
667
|
+
) -> SeededChangemakerWithProposals:
|
|
668
|
+
if changemaker is None:
|
|
669
|
+
changemaker = seed_changemaker(session, name=changemaker_name)
|
|
670
|
+
seeded_fields = [
|
|
671
|
+
seed_base_field(session, short_code=short_code, label=label)
|
|
672
|
+
for short_code, label, _ in make_proposal_field_seeds()
|
|
673
|
+
]
|
|
674
|
+
if funder is None:
|
|
675
|
+
funder = seed_funder(session)
|
|
676
|
+
opportunity = seed_opportunity(session, funder=funder)
|
|
677
|
+
source = seed_source(session, funder_short_code=funder.short_code)
|
|
678
|
+
application_form = seed_application_form(
|
|
679
|
+
session, opportunity=opportunity, base_field_short_codes=[bf.short_code for bf in seeded_fields]
|
|
680
|
+
)
|
|
681
|
+
bundles = [
|
|
682
|
+
_seed_linked_proposal(
|
|
683
|
+
session,
|
|
684
|
+
changemaker=changemaker,
|
|
685
|
+
funder=funder,
|
|
686
|
+
opportunity=opportunity,
|
|
687
|
+
source=source,
|
|
688
|
+
application_form=application_form,
|
|
689
|
+
field_seeds=make_proposal_field_seeds(include_numbers=i % 2 == 0),
|
|
690
|
+
)
|
|
691
|
+
for i in range(proposal_count)
|
|
692
|
+
]
|
|
693
|
+
return SeededChangemakerWithProposals(changemaker=changemaker, bundles=bundles)
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
def seed_proposal_corpus(
|
|
697
|
+
session: APISession,
|
|
698
|
+
*,
|
|
699
|
+
size: int = 4,
|
|
700
|
+
) -> list[SeededProposalBundle]:
|
|
701
|
+
bundles: list[SeededProposalBundle] = []
|
|
702
|
+
for i in range(size):
|
|
703
|
+
include_numbers = i % 2 == 0
|
|
704
|
+
bundles.append(
|
|
705
|
+
seed_proposal_bundle(
|
|
706
|
+
session,
|
|
707
|
+
field_seeds=make_proposal_field_seeds(include_numbers=include_numbers),
|
|
708
|
+
changemaker_name=make_changemaker_name(include_numbers=include_numbers),
|
|
709
|
+
)
|
|
710
|
+
)
|
|
711
|
+
return bundles
|