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

check num of submission when count 0 #962

Merged
merged 5 commits into from
Mar 21, 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
10 changes: 6 additions & 4 deletions onadata/apps/logger/models/xform.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,15 +813,17 @@ def soft_delete(self):
soft_deletion_time = datetime.now()
deletion_suffix = soft_deletion_time.strftime('-deleted-at-%s')
self.deleted_at = soft_deletion_time
self.id_string = self.id_string + deletion_suffix
self.sms_id_string = self.sms_id_string + deletion_suffix
self.id_string += deletion_suffix
self.sms_id_string += deletion_suffix
self.save()

def submission_count(self, force_update=False):
if self.num_of_submissions == 0 or force_update:
count = self.instances.filter(deleted_at__isnull=True).count()
self.num_of_submissions = count
self.save(update_fields=['num_of_submissions'])

if count != self.num_of_submissions:
self.num_of_submissions = count
self.save(update_fields=['num_of_submissions'])
return self.num_of_submissions
submission_count.short_description = ugettext_lazy("Submission Count")

Expand Down
17 changes: 14 additions & 3 deletions onadata/libs/serializers/xform_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
XFORM_DATA_VERSIONS,
XFORM_LINKED_DATAVIEWS,
XFORM_METADATA_CACHE,
XFORM_PERMISSIONS_CACHE)
XFORM_PERMISSIONS_CACHE,
DATAVIEW_COUNT)
from onadata.libs.utils.common_tags import GROUP_DELIMETER_TAG
from onadata.libs.utils.decorators import check_obj
from onadata.libs.utils.viewer_tools import (EnketoError, enketo_url,
Expand Down Expand Up @@ -168,6 +169,16 @@ def get_data_views(self, obj):
return data_views
return []

def get_num_of_submissions(self, obj):
if obj:
key = '{}{}'.format(DATAVIEW_COUNT, obj.pk)
count = cache.get(key)
if count:
return count
count = obj.submission_count()
cache.set(key, count)
return count


class XFormBaseSerializer(XFormMixin, serializers.HyperlinkedModelSerializer):
formid = serializers.ReadOnlyField(source='id')
Expand All @@ -192,7 +203,7 @@ class XFormBaseSerializer(XFormMixin, serializers.HyperlinkedModelSerializer):
users = serializers.SerializerMethodField()
enketo_url = serializers.SerializerMethodField()
enketo_preview_url = serializers.SerializerMethodField()
num_of_submissions = serializers.ReadOnlyField()
num_of_submissions = serializers.SerializerMethodField()
data_views = serializers.SerializerMethodField()

class Meta:
Expand Down Expand Up @@ -229,7 +240,7 @@ class XFormSerializer(XFormMixin, serializers.HyperlinkedModelSerializer):
users = serializers.SerializerMethodField()
enketo_url = serializers.SerializerMethodField()
enketo_preview_url = serializers.SerializerMethodField()
num_of_submissions = serializers.ReadOnlyField()
num_of_submissions = serializers.SerializerMethodField()
form_versions = serializers.SerializerMethodField()
data_views = serializers.SerializerMethodField()

Expand Down