Skip to content

Commit

Permalink
Merge pull request #191 from mkumar-02/patch-1
Browse files Browse the repository at this point in the history
Updated cron job method
  • Loading branch information
shibu-narayanan authored Sep 27, 2024
2 parents 4c33626 + f968205 commit ef49e77
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion g2p_encryption_keymanager/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"depends": [
"g2p_encryption",
],
"external_dependencies": {"python": ["cryptography<37", "jwcrypto", "python-jose"]},
"external_dependencies": {"python": ["cryptography>36,<37", "jwcrypto", "python-jose"]},
"data": [
"views/encryption_provider.xml",
"data/default_provider.xml",
Expand Down
7 changes: 7 additions & 0 deletions 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
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
2 changes: 1 addition & 1 deletion g2p_openid_vci/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"g2p_registry_base",
"g2p_encryption",
],
"external_dependencies": {"python": ["cryptography<37", "python-jose", "jq", "PyLD"]},
"external_dependencies": {"python": ["cryptography>36,<37", "python-jose", "jq", "PyLD"]},
"data": [
"security/ir.model.access.csv",
"views/vci_issuers.xml",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# generated from manifests external_dependencies
PyLD
boto3<=1.15.18
cryptography<37
cryptography>36,<37
extendable-pydantic
jq
jwcrypto
Expand Down

0 comments on commit ef49e77

Please sign in to comment.