Skip to content

Commit

Permalink
Merge pull request #284 from Q-Niranjan/17.0-1.2
Browse files Browse the repository at this point in the history
 Added JSON formatter for upcoming social registry API response.
  • Loading branch information
shibu-narayanan authored Jan 21, 2025
2 parents 29e739d + 0e20398 commit 6a9ca72
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
2 changes: 1 addition & 1 deletion g2p_social_registry_importer/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"security/ir.model.access.csv",
"views/fetch_social_registry_beneficiary_views.xml",
],
"external_dependencies": {"python": ["camel_converter"]},
"external_dependencies": {"python": ["jq"]},
"application": True,
"auto_install": False,
"installable": True,
Expand Down
62 changes: 62 additions & 0 deletions g2p_social_registry_importer/data/search_criteria.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,68 @@
<field name="import_registrant_without_id">False</field>
<field name="target_registry">group</field>
<field name="target_program" />
<field name="output_mapping">{

"name": .name,
"is_group": .isGroup,
"given_name": .givenName,
"family_name": .familyName,
"group_membership_ids": (
if .groupMembershipIds? then
[
.groupMembershipIds[] |
{
"name": .individual.name,
"kind": .kind.name,
"given_name": .individual.givenName,
"family_name": .individual.familyName,
"addl_name": .individual.addlName,
"email": .individual.email,
"address": .individual.address,
"registration_date": .individual.registrationDate,
"birth_place": .individual.birthPlace,
"birthdate": .individual.birthdate,
"create_date": .individual.createDate,
"write_date": .individual.writeDate,
"reg_ids": (
if .individual.regIds? then
[.individual.regIds[] | {id_type: .idTypeAsStr, value: .value}]
else
[]
end
),
"phone_number_ids": (
if .individual.phoneNumberIds? then
[.individual.phoneNumberIds[] | {phone_no: .phoneNo, phone_sanitized: .phoneSanitized, date_collected: .dateCollected, disabled: .disabled}]
else
[]
end
)
}
]
else
[]
end
),
"reg_ids": (
if .regIds? then
[.regIds[] | {id_type: .idTypeAsStr, value: .value}]
else
[]
end
),
"phone_number_ids": (
if .phoneNumberIds? then
[.phoneNumberIds[] | {phone_no: .phoneNo, phone_sanitized: .phoneSanitized, date_collected: .dateCollected, disabled: .disabled}]
else
[]
end
)


}
</field>

<field name="query">{
getRegistrants(limit:2){
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import uuid
from datetime import datetime, timezone

import jq
import requests
from camel_converter import dict_to_snake

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
Expand Down Expand Up @@ -39,6 +39,7 @@ class G2PFetchSocialRegistryBeneficiary(models.Model):
query = fields.Text(
required=True,
)
output_mapping = fields.Text(required=True)

last_sync_date = fields.Datetime(string="Last synced on", required=False)

Expand Down Expand Up @@ -147,6 +148,18 @@ def onchange_target_registry(self):
for rec in self:
rec.target_program = None

@api.constrains("output_mapping")
def constraint_json_fields(self):
for rec in self:
if rec.output_mapping:
try:
# Validate the JSON Query (jq expression)
jq.compile(rec.output_mapping)
except ValueError as ve:
raise ValidationError(
_("The provided value for 'Output Mapping' is not a valid jq expression.")
) from ve

def get_data_source_paths(self):
self.ensure_one()

Expand Down Expand Up @@ -333,8 +346,8 @@ def get_partner_and_clean_identifier(self, identifiers):
return partner_id, clean_identifiers

def get_individual_data(self, record):
vals = dict_to_snake(record)
return vals
mapped_json = jq.first(self.output_mapping, record)
return mapped_json

def get_member_kind(self, data):
# TODO: Kind will be in List
Expand Down Expand Up @@ -547,7 +560,7 @@ def process_registrants_async(self, registrants, count):
main_job.delay()

def fetch_social_registry_beneficiary(self):
self.write({"job_status": "running"})
self.write({"job_status": "running", "start_datetime": fields.Datetime.now()})

config_parameters = self.env["ir.config_parameter"].sudo()
today_isoformat = datetime.now(timezone.utc).isoformat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<field name="target_registry" />
<field name="target_program" />
<field name="query" />
<field name="output_mapping" />
<field name="last_sync_date" />
<field
name="imported_registrant_ids"
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# generated from manifests external_dependencies
base45
camel_converter
cose
formio-data
jq
pdfkit
python-barcode
python-dateutil
Expand Down

0 comments on commit 6a9ca72

Please sign in to comment.