Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <arapgodsmack@gmail.com>
  • Loading branch information
KipSigei committed Aug 15, 2023
1 parent 241f19c commit cb23637
Showing 1 changed file with 30 additions and 35 deletions.
65 changes: 30 additions & 35 deletions onadata/apps/logger/models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,41 +256,36 @@ def update_xform_submission_count(instance_id, created):
.get(pk=instance_id)
)

# Check if the instance exists
if instance:
# Instance exists, update submission count if it's a new submission
if created:
cursor = connection.cursor()
sql = (
"UPDATE logger_xform SET "
"num_of_submissions = num_of_submissions + 1, "
"last_submission_time = %s "
"WHERE id = %s"
)
params = [instance.date_created, instance.xform_id]

# update user profile.num_of_submissions
cursor.execute(sql, params)
sql = (
"UPDATE main_userprofile SET "
"num_of_submissions = num_of_submissions + 1 "
"WHERE user_id = %s"
)
cursor.execute(sql, [instance.xform.user_id])

# Track submissions made today
_update_submission_count_for_today(instance.xform_id)

safe_delete(f"{XFORM_DATA_VERSIONS}{instance.xform_id}")
safe_delete(f"{DATAVIEW_COUNT}{instance.xform_id}")
safe_delete(f"{XFORM_COUNT}{instance.xform_id}")
# Clear project cache
from onadata.apps.logger.models.xform import clear_project_cache

clear_project_cache(instance.xform.project_id)

# No need to retry, exit
return
# Check if the instance exists, update submission count if new submission
if created and instance:
cursor = connection.cursor()
sql = (
"UPDATE logger_xform SET "
"num_of_submissions = num_of_submissions + 1, "
"last_submission_time = %s "
"WHERE id = %s"
)
params = [instance.date_created, instance.xform_id]

# update user profile.num_of_submissions
cursor.execute(sql, params)
sql = (
"UPDATE main_userprofile SET "
"num_of_submissions = num_of_submissions + 1 "
"WHERE user_id = %s"
)
cursor.execute(sql, [instance.xform.user_id])

# Track submissions made today
_update_submission_count_for_today(instance.xform_id)

safe_delete(f"{XFORM_DATA_VERSIONS}{instance.xform_id}")
safe_delete(f"{DATAVIEW_COUNT}{instance.xform_id}")
safe_delete(f"{XFORM_COUNT}{instance.xform_id}")
# Clear project cache
from onadata.apps.logger.models.xform import clear_project_cache

clear_project_cache(instance.xform.project_id)

except Instance.DoesNotExist as e:
# In case of synchronous execution (not a Celery task),
Expand Down

0 comments on commit cb23637

Please sign in to comment.