-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hl 970 open ahjo case #2519
Merged
Merged
Hl 970 open ahjo case #2519
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7674209
feat: add utility function for encoding multipart/form-data
rikuke 49f3c3d
feat: open a case in Ahjo via Ahjo Rest API
rikuke 02a9c95
fix: typo
rikuke 87e9ac6
fix: code style/imports
rikuke 0db7fca
fix: change to f-string for consistency
rikuke a631a6f
fix: remove non-pythonic injection of requests module
rikuke 38220e6
fix: remove unneeded form data request implementation
rikuke 180622c
fix: clarify error handling
rikuke e1caf4e
fix: missing default value breaks tests in pipelines
rikuke 93e852b
fix: failing ahjo_connector test
rikuke fa75ac5
fix: strange fail on CI pipeline Sonarcloud test
rikuke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import uuid | ||
from datetime import datetime | ||
from typing import List, Union | ||
|
||
from django.conf import settings | ||
from django.urls import reverse | ||
|
||
from applications.models import Application, Attachment | ||
from common.utils import hash_file | ||
from users.models import User | ||
|
||
|
||
def _prepare_top_level_dict(application: Application, case_records: List[dict]) -> dict: | ||
"""Prepare the dictionary that is sent to Ahjo""" | ||
application_date = application.created_at.isoformat() | ||
application_year = application.created_at.year | ||
title = f"Avustuksen myöntäminen, työllisyyspalvelut, \ | ||
työnantajan Helsinki-lisä vuonna {application.created_at.year}, \ | ||
työnantaja {application.company_name}" | ||
|
||
case_dict = { | ||
"Title": title, | ||
"Acquired": application_date, | ||
"ClassificationCode": "02 05 01 00", | ||
"ClassificationTitle": "Kunnan myöntämät avustukset", | ||
"Language": "fi", | ||
"PublicityClass": "Julkinen", | ||
"InternalTitle": f"Avustuksen myöntäminen, työllisyyspalvelut, \ | ||
työnantajan Helsinki-lisä vuonna {application_year}, \ | ||
työnantaja {application.company_name}", | ||
"Subjects": [ | ||
{"Subject": "Helsinki-lisät", "Scheme": "hki-yhpa"}, | ||
{"Subject": "kunnan myöntämät avustukset", "Scheme": "hki-yhpa"}, | ||
{"Subject": "työnantajat", "Scheme": "hki-yhpa"}, | ||
{"Subject": "työllisyydenhoito"}, | ||
], | ||
"PersonalData": "Sisältää erityisiä henkilötietoja", | ||
"Reference": application.application_number, | ||
"Records": case_records, | ||
"Agents": [ | ||
{ | ||
"Role": "sender_initiator", | ||
"CorporateName": application.company.name, | ||
"ContactPerson": application.contact_person, | ||
"Type": "ExterOnal", | ||
"Email": application.company_contact_person_email, | ||
"AddressStreet": application.company.street_address, | ||
"AddressPostalCode": application.company.postcode, | ||
"AddressCity": application.company.city, | ||
} | ||
], | ||
} | ||
return case_dict | ||
|
||
|
||
def _prepare_record_document_dict(attachment: Attachment) -> dict: | ||
"""Prepare a documents dict for a record""" | ||
# If were running in mock mode, use the local file URI | ||
file_url = reverse("ahjo_attachment_url", kwargs={"uuid": attachment.id}) | ||
hash_value = hash_file(attachment.attachment_file) | ||
return { | ||
"FileName": f"{attachment.attachment_file.name}", | ||
"FormatName": f"{attachment.content_type}", | ||
"HashAlgorithm": "sha256", | ||
"HashValue": hash_value, | ||
"FileURI": f"{settings.API_BASE_URL}{file_url}", | ||
} | ||
|
||
|
||
def _prepare_record( | ||
record_title: str, | ||
record_type: str, | ||
acquired: datetime, | ||
reference: Union[int, uuid.UUID], | ||
documents: List[dict], | ||
handler: User, | ||
publicity_class: str = "Salassa pidettävä", | ||
): | ||
"""Prepare a single record dict for Ahjo.""" | ||
|
||
return { | ||
"Title": record_title, | ||
"Type": record_type, | ||
"Acquired": acquired, | ||
"PublicityClass": publicity_class, | ||
"SecurityReasons": ["JulkL (621/1999) 24.1 § 25 k"], | ||
"Language": "fi", | ||
"PersonalData": "Sisältää erityisiä henkilötietoja", | ||
"Reference": str(reference), | ||
"Documents": documents, | ||
"Agents": [ | ||
{ | ||
"Role": "mainCreator", | ||
"Name": f"{handler.last_name}, {handler.first_name}", | ||
"ID": handler.ad_username, | ||
} | ||
], | ||
} | ||
|
||
|
||
def _prepare_case_records(application: Application) -> List[dict]: | ||
"""Prepare the list of case records""" | ||
case_records = [] | ||
handler = application.calculation.handler | ||
main_document_record = _prepare_record( | ||
"Hakemus", | ||
"hakemus", | ||
application.created_at.isoformat(), | ||
application.application_number, | ||
[], # TODO Pdf version of the application goes here with prepare_record() | ||
handler, | ||
) | ||
|
||
case_records.append(main_document_record) | ||
|
||
for attachment in application.attachments.all(): | ||
document_record = _prepare_record( | ||
"Hakemuksen Liite", | ||
"liite", | ||
attachment.created_at.isoformat(), | ||
attachment.id, | ||
[_prepare_record_document_dict(attachment)], | ||
handler, | ||
) | ||
case_records.append(document_record) | ||
|
||
return case_records | ||
|
||
|
||
def prepare_open_case_payload(application: Application) -> dict: | ||
"Prepare the complete dictionary payload that is sent to Ahjo" | ||
case_records = _prepare_case_records(application) | ||
payload = _prepare_top_level_dict(application, case_records) | ||
return payload |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So many parameters. Maybe using them named would make easier to know what's what?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try to make it more readable.