Skip to content

Commit 5db79cf

Browse files
authored
feat: add OC task & bhf fix (#1917)
1 parent e354b00 commit 5db79cf

File tree

6 files changed

+536
-17
lines changed

6 files changed

+536
-17
lines changed

CodeListLibrary_project/clinicalcode/api/views/GenericEntity.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
@swagger_auto_schema(method='post', auto_schema=None)
2727
@api_view(['POST'])
2828
@permission_classes([IsAuthenticated])
29-
def create_generic_entity(request):
29+
def create_generic_entity(request):
3030
"""
3131
Create a generic entity from request body, must be formatted in terms
3232
of a specific layout and is validated against it
@@ -40,13 +40,28 @@ def create_generic_entity(request):
4040
status=status.HTTP_403_FORBIDDEN
4141
)
4242

43+
params = { key: value for key, value in request.query_params.items() }
44+
publish_immediately = params.pop('publish', False)
45+
if publish_immediately and not request.user.is_superuser:
46+
return Response(
47+
data={
48+
'message': 'Permission denied'
49+
},
50+
content_type='json',
51+
status=status.HTTP_403_FORBIDDEN
52+
)
53+
4354
form = api_utils.validate_api_create_update_form(
4455
request, method=constants.FORM_METHODS.CREATE.value
4556
)
4657
if isinstance(form, Response):
4758
return form
4859

49-
entity = api_utils.create_update_from_api_form(request, form)
60+
entity = api_utils.create_update_from_api_form(
61+
request,
62+
form,
63+
publish_immediately=publish_immediately
64+
)
5065
if isinstance(entity, Response):
5166
return entity
5267

@@ -87,14 +102,29 @@ def update_generic_entity(request):
87102
content_type='json',
88103
status=status.HTTP_403_FORBIDDEN
89104
)
105+
106+
params = { key: value for key, value in request.query_params.items() }
107+
publish_immediately = params.pop('publish', False)
108+
if publish_immediately and not request.user.is_superuser:
109+
return Response(
110+
data={
111+
'message': 'Permission denied'
112+
},
113+
content_type='json',
114+
status=status.HTTP_403_FORBIDDEN
115+
)
90116

91117
form = api_utils.validate_api_create_update_form(
92118
request, method=constants.FORM_METHODS.UPDATE.value
93119
)
94120
if isinstance(form, Response):
95121
return form
96122

97-
entity = api_utils.create_update_from_api_form(request, form)
123+
entity = api_utils.create_update_from_api_form(
124+
request,
125+
form,
126+
publish_immediately=publish_immediately
127+
)
98128
if isinstance(entity, Response):
99129
return entity
100130

CodeListLibrary_project/clinicalcode/entity_utils/api_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,21 +1353,26 @@ def validate_api_create_update_form(request, method):
13531353

13541354
return form
13551355

1356-
def create_update_from_api_form(request, form):
1356+
def create_update_from_api_form(request, form, publish_immediately=False):
13571357
"""
13581358
Create or updates an entity from an entity form dict
13591359
13601360
Args:
13611361
request (HTTPContext): Request context
13621362
form (dict): Dict containing entity information
1363+
publish_immediately (bool): Whether to publish instantly, note that you need to perform permission checks beforehand
13631364
13641365
Returns:
13651366
Created/Updated entity if validation succeeds, otherwise returns
13661367
400 response
13671368
"""
13681369
form_errors = []
13691370
entity = create_utils.create_or_update_entity_from_form(
1370-
request, form, form_errors)
1371+
request,
1372+
form,
1373+
form_errors,
1374+
publish_immediately=publish_immediately
1375+
)
13711376
if entity is None:
13721377
return Response(
13731378
data={

CodeListLibrary_project/clinicalcode/entity_utils/create_utils.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from ..models.CodingSystem import CodingSystem
2222
from ..models.Organisation import Organisation, OrganisationAuthority
2323
from ..models.GenericEntity import GenericEntity
24+
from ..models.PublishedGenericEntity import PublishedGenericEntity
2425
from ..models.ConceptCodeAttribute import ConceptCodeAttribute
2526

2627
from . import (
@@ -1439,7 +1440,13 @@ def compute_brand_context(request, form_data, form_entity=None):
14391440
return related_brands
14401441

14411442
@transaction.atomic
1442-
def create_or_update_entity_from_form(request, form, errors=[], override_dirty=False):
1443+
def create_or_update_entity_from_form(
1444+
request,
1445+
form,
1446+
errors=[],
1447+
override_dirty=False,
1448+
publish_immediately=False
1449+
):
14431450
"""
14441451
Used to create or update entities - this method assumes you have
14451452
previously validated the content of the form using the validate_entity_form method
@@ -1448,15 +1455,16 @@ def create_or_update_entity_from_form(request, form, errors=[], override_dirty=F
14481455
request (RequestContext): the request context of the form
14491456
form (dict): a dict containing the validate_entity_form method result
14501457
override_dirty (boolean): overrides the is_dirty check for child entity creation
1451-
1458+
publish_immediately (boolean): Whether to publish instantly, note that you need to perform permission checks beforehand
1459+
14521460
Returns:
14531461
(GenericEntity|null) - null value is returned if this method fails
14541462
14551463
"""
14561464
user = request.user
14571465
if user is None:
14581466
return
1459-
1467+
14601468
form_method = form.get('method')
14611469
form_template = form.get('template')
14621470
form_data = form.get('data')
@@ -1534,6 +1542,9 @@ def create_or_update_entity_from_form(request, form, errors=[], override_dirty=F
15341542
# Create or update the entity
15351543
template_data['version'] = form_template.template_version
15361544
if form_method == constants.FORM_METHODS.CREATE:
1545+
if publish_immediately:
1546+
metadata['publish_status'] = constants.APPROVAL_STATUS.APPROVED.value
1547+
15371548
entity = GenericEntity.objects.create(
15381549
**metadata,
15391550
template=template_instance,
@@ -1572,7 +1583,7 @@ def create_or_update_entity_from_form(request, form, errors=[], override_dirty=F
15721583
entity.template_version = form_template.template_version
15731584
entity.template_data = template_data
15741585
entity.updated = make_aware(datetime.now())
1575-
entity.publish_status = constants.APPROVAL_STATUS.ANY.value
1586+
entity.publish_status = constants.APPROVAL_STATUS.ANY.value if not publish_immediately else constants.APPROVAL_STATUS.APPROVED.value
15761587
entity.updated_by = user
15771588
entity.brands = related_brands
15781589
entity.save()
@@ -1584,6 +1595,22 @@ def create_or_update_entity_from_form(request, form, errors=[], override_dirty=F
15841595
for instance in instances:
15851596
setattr(instance, field, entity)
15861597
instance.save_without_historical_record()
1598+
1599+
if publish_immediately:
1600+
PublishedGenericEntity.objects.update_or_create(
1601+
entity_id=entity.id,
1602+
entity_history_id=entity.history.latest().history_id,
1603+
defaults={
1604+
'moderator_id': 1,
1605+
'approval_status': constants.APPROVAL_STATUS.APPROVED.value,
1606+
},
1607+
create_defaults={
1608+
'moderator_id': 1,
1609+
'created_by_id': entity.created_by.id,
1610+
'approval_status': constants.APPROVAL_STATUS.APPROVED.value,
1611+
}
1612+
)
1613+
15871614
except IntegrityError as e:
15881615
msg = 'Data integrity error when submitting form'
15891616
errors.append(msg)

0 commit comments

Comments
 (0)