Skip to content
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

XML submission checksum #1122

Merged
merged 3 commits into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions onadata/apps/logger/migrations/0039_auto_20170909_2052.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2017-09-10 00:52
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('logger', '0038_auto_20170828_1718'),
]

operations = [
migrations.AddField(
model_name='instance',
name='checksum',
field=models.CharField(blank=True, max_length=64, null=True),
),
migrations.AddField(
model_name='instancehistory',
name='checksum',
field=models.CharField(blank=True, max_length=64, null=True),
),
]
3 changes: 3 additions & 0 deletions onadata/apps/logger/models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ class Instance(models.Model, InstanceBaseClass):
media_count = models.PositiveIntegerField(_("Received Media Attachments"),
null=True,
default=0)
checksum = models.CharField(max_length=64, null=True, blank=True)

tags = TaggableManager()

Expand Down Expand Up @@ -528,6 +529,8 @@ class Meta:
date_modified = models.DateTimeField(auto_now=True)
submission_date = models.DateTimeField(null=True, default=None)
geom = models.GeometryCollectionField(null=True)
checksum = models.CharField(max_length=64, null=True, blank=True)

objects = models.GeoManager()

@property
Expand Down
29 changes: 19 additions & 10 deletions onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from django.utils import timezone
from django.utils.encoding import DjangoUnicodeDecodeError
from django.utils.translation import ugettext as _
from hashlib import sha256
from modilabs.utils.subprocess_timeout import ProcessTimedOut
from multidb.pinning import use_master

Expand Down Expand Up @@ -53,15 +54,16 @@
re.DOTALL)


def _get_instance(xml, new_uuid, submitted_by, status, xform):
def _get_instance(xml, new_uuid, submitted_by, status, xform, checksum):
history = None
instance = None
# check if its an edit submission
old_uuid = get_deprecated_uuid_from_xml(xml)
if old_uuid:
instance = Instance.objects.filter(uuid=old_uuid).first()
instance = Instance.objects.filter(uuid=old_uuid,
xform_id=xform.pk).first()
history = InstanceHistory.objects.filter(
xform_instance__xform=xform,
xform_instance__xform_id=xform.pk,
uuid=new_uuid).only('xform_instance').first()

if instance:
Expand All @@ -70,6 +72,7 @@ def _get_instance(xml, new_uuid, submitted_by, status, xform):

last_edited = timezone.now()
InstanceHistory.objects.create(
checksum=instance.checksum,
xml=instance.xml,
xform_instance=instance,
uuid=old_uuid,
Expand All @@ -79,6 +82,7 @@ def _get_instance(xml, new_uuid, submitted_by, status, xform):
instance.xml = xml
instance.last_edited = last_edited
instance.uuid = new_uuid
instance.checksum = checksum
instance.save()

# call webhooks
Expand All @@ -88,7 +92,8 @@ def _get_instance(xml, new_uuid, submitted_by, status, xform):
if old_uuid is None or (instance is None and history is None):
# new submission
instance = Instance.objects.create(
xml=xml, user=submitted_by, status=status, xform=xform)
xml=xml, user=submitted_by, status=status, xform=xform,
checksum=checksum)
return instance


Expand Down Expand Up @@ -218,11 +223,12 @@ def save_attachments(xform, instance, media_files):


def save_submission(xform, xml, media_files, new_uuid, submitted_by, status,
date_created_override):
date_created_override, checksum):
if not date_created_override:
date_created_override = get_submission_date_from_xml(xml)

instance = _get_instance(xml, new_uuid, submitted_by, status, xform)
instance = _get_instance(xml, new_uuid, submitted_by, status, xform,
checksum)
save_attachments(xform, instance, media_files)

# override date created if required
Expand Down Expand Up @@ -279,10 +285,11 @@ def create_instance(username,
xml = xml_file.read()
xform = get_xform_from_submission(xml, username, uuid)
check_submission_permissions(request, xform)
checksum = sha256(xml).hexdigest()

new_uuid = get_uuid_from_xml(xml)
filtered_instances = get_filtered_instances(
Q(xml=xml) | Q(uuid=new_uuid), xform_id=xform.pk)
Q(checksum=checksum) | Q(uuid=new_uuid), xform_id=xform.pk)
existing_instance = filtered_instances.first()
if existing_instance and \
(new_uuid or existing_instance.xform.has_start_time):
Expand All @@ -297,7 +304,7 @@ def create_instance(username,
# has already been submitted for that user.
return DuplicateInstance()

# get new and depracated uuid's
# get new and deprecated UUIDs
history = InstanceHistory.objects.filter(
xform_instance__xform_id=xform.pk,
uuid=new_uuid).only('xform_instance').first()
Expand All @@ -315,9 +322,11 @@ def create_instance(username,
with transaction.atomic():
instance = save_submission(xform, xml, media_files, new_uuid,
submitted_by, status,
date_created_override)
date_created_override, checksum)
except IntegrityError:
instance = Instance.objects.filter(xml=xml, xform__id=xform.pk).first()
instance = Instance.objects.filter(
Q(checksum=checksum) | Q(uuid=new_uuid),
xform_id=xform.pk).first()

if instance:
attachment_names = [
Expand Down