Skip to content

Commit

Permalink
Merge branch '17.0-develop' of https://github.com/openg2p/openg2p-reg…
Browse files Browse the repository at this point in the history
…istry into 17.0-develop

Signed-off-by: Lalith Kota <[email protected]>
  • Loading branch information
lalithkota committed Oct 17, 2024
2 parents 32b8fbd + ff779ec commit 408abac
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
5 changes: 5 additions & 0 deletions g2p_auth_oidc/i18n/g2p_auth_oidc.pot
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ msgstr ""
msgid "Denies user signup (invitation only)"
msgstr ""

#. module: g2p_auth_oidc
#: model:ir.model.fields,field_description:g2p_auth_oidc.field_auth_oauth_provider__enable_pkce
msgid "Enable Pkce"
msgstr ""

#. module: g2p_auth_oidc
#: model:ir.model.fields,field_description:g2p_auth_oidc.field_auth_oauth_provider__extra_authorize_params
msgid "Extra Authorize Params"
Expand Down
8 changes: 7 additions & 1 deletion g2p_odk_importer/i18n/g2p_odk_importer.pot
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ msgstr ""
msgid "Form ID"
msgstr ""

#. module: g2p_odk_importer
#. odoo-python
#: code:addons/g2p_odk_importer/models/odk_client.py:0
#, python-format
msgid "Future records cannot be fetched before the regular import occurs."
msgstr ""

#. module: g2p_odk_importer
#: model:ir.model.fields.selection,name:g2p_odk_importer.selection__odk_import__target_registry__group
msgid "Group"
Expand Down Expand Up @@ -258,7 +265,6 @@ msgstr ""
#: code:addons/g2p_odk_importer/models/odk_import.py:0
#: code:addons/g2p_odk_importer/models/odk_import.py:0
#: code:addons/g2p_odk_importer/models/odk_import.py:0
#: code:addons/g2p_odk_importer/models/odk_import.py:0
#, python-format
msgid "Please configure the ODK."
msgstr ""
Expand Down
20 changes: 18 additions & 2 deletions g2p_odk_importer/models/odk_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
from datetime import datetime

import jq
import pytz
import requests
from dateutil import parser

from odoo import _
from odoo.exceptions import ValidationError
from odoo.exceptions import UserError, ValidationError

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -304,7 +305,7 @@ def download_attachment(self, base_url, project_id, form_id, instance_id, filena
return response.content

# Fetch Record using Instance ID
def import_record_by_instance_id(self, instance_id):
def import_record_by_instance_id(self, instance_id, last_sync_timestamp=None):
url = (
f"{self.base_url}/v1/projects/{self.project_id}/forms/{self.form_id}.svc/"
f"Submissions('{instance_id}')"
Expand All @@ -325,8 +326,23 @@ def import_record_by_instance_id(self, instance_id):
raise ValidationError(f"Failed to parse response by using instance ID: {e}") from e

_logger.info(f"ODK RAW DATA by instance ID %s {instance_id} {data}")

if last_sync_timestamp:
last_sync_time = pytz.UTC.localize(last_sync_timestamp)
else:
last_sync_time = None

try:
for member in data["value"]:
submission_date_str = member.get("__system", {}).get("submissionDate")
if submission_date_str:
# Parse submissionDate to a timezone-aware datetime object
submission_date = parser.isoparse(submission_date_str)
if last_sync_time and last_sync_time < submission_date:
raise UserError(
_("Future records cannot be fetched before the regular import occurs.")
)

mapped_json = jq.first(self.json_formatter, member)
if self.target_registry == "individual":
mapped_json.update({"is_registrant": True, "is_group": False})
Expand Down
26 changes: 4 additions & 22 deletions g2p_odk_importer/models/odk_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ def fetch_record_by_instance_id(self):
config.json_formatter,
)
client.login()

imported = client.import_record_by_instance_id(instance_id=config.instance_id)
imported = client.import_record_by_instance_id(
instance_id=config.instance_id, last_sync_timestamp=config.last_sync_time
)
if "form_updated" in imported:
message = "ODK form records is imported successfully."
types = "success"
Expand Down Expand Up @@ -171,25 +172,6 @@ def import_records(self):
},
}

def import_records_by_cron(self, _id):
config = self.env["odk.config"].browse(_id)
if not config.base_url:
raise UserError(_("Please configure the ODK."))
client = ODKClient(
self.env,
config.id,
config.base_url,
config.username,
config.password,
config.project,
config.form_id,
self.target_registry,
self.json_formatter,
)
client.login()
client.import_delta_records(last_sync_timestamp=config.last_sync_time)
config.update({"last_sync_time": fields.Datetime.now()})

def odk_import_action_trigger(self):
for rec in self:
if rec.job_status == "draft" or rec.job_status == "completed":
Expand All @@ -204,7 +186,7 @@ def odk_import_action_trigger(self):
"interval_type": "minutes",
"model_id": self.env["ir.model"].search([("model", "=", "odk.import")]).id,
"state": "code",
"code": "model.import_records_by_cron(" + str(rec.id) + ")",
"code": "model.browse(" + str(rec.id) + ").import_records()",
"doall": False,
"numbercall": -1,
}
Expand Down

0 comments on commit 408abac

Please sign in to comment.