-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #895 from kobotoolbox/daily-counter-null-xform
Create null xform instances for DailyXFormSubmissionCounter
- Loading branch information
Showing
7 changed files
with
140 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
onadata/apps/logger/migrations/0028_add_user_to_daily_submission_counters.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
from django.db.models import Subquery, deletion, OuterRef | ||
|
||
|
||
def add_user_to_daily_submission_counter(apps, schema_editor): | ||
DailyXFormSubmissionCounter = apps.get_model('logger', 'DailyXFormSubmissionCounter') | ||
# delete any counters where xform and user are None, since we can't associate them with a user | ||
DailyXFormSubmissionCounter.objects.filter(xform=None, user=None).delete() | ||
# add the user to the counter, based on the xform user | ||
DailyXFormSubmissionCounter.objects.all().exclude(xform=None).update( | ||
user=Subquery( | ||
DailyXFormSubmissionCounter.objects.filter(pk=OuterRef('pk')).values('xform__user')[:1] | ||
), | ||
) | ||
|
||
|
||
def delete_null_xform_daily_counters(apps, schema_editor): | ||
DailyXFormSubmissionCounter = apps.get_model('logger', 'DailyXFormSubmissionCounter') | ||
# to migrate backwards, we need to delete any null xform instances | ||
DailyXFormSubmissionCounter.objects.filter(xform=None).delete() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('logger', '0027_on_delete_cascade_monthlyxformsubmissioncounter'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='DailyXFormSubmissionCounter', | ||
name='user', | ||
field=models.ForeignKey('auth.User', related_name='daily_users', null=True, on_delete=models.CASCADE), | ||
), | ||
migrations.RunPython( | ||
add_user_to_daily_submission_counter, | ||
migrations.RunPython.noop, | ||
), | ||
migrations.AlterField( | ||
model_name='dailyxformsubmissioncounter', | ||
name='xform', | ||
field=models.ForeignKey(null=True, on_delete=deletion.CASCADE, | ||
related_name='daily_counters', to='logger.xform'), | ||
), | ||
migrations.RunPython( | ||
migrations.RunPython.noop, | ||
delete_null_xform_daily_counters, | ||
), | ||
migrations.AlterUniqueTogether( | ||
name='dailyxformsubmissioncounter', | ||
unique_together=set(), | ||
), | ||
migrations.AddIndex( | ||
model_name='dailyxformsubmissioncounter', | ||
index=models.Index(fields=['date', 'user'], name='logger_dail_date_f738ed_idx'), | ||
), | ||
migrations.AddConstraint( | ||
model_name='dailyxformsubmissioncounter', | ||
constraint=models.UniqueConstraint(fields=('date', 'user', 'xform'), name='daily_unique_with_xform'), | ||
), | ||
migrations.AddConstraint( | ||
model_name='dailyxformsubmissioncounter', | ||
constraint=models.UniqueConstraint(condition=models.Q(('xform', None)), fields=('date', 'user'), | ||
name='daily_unique_without_xform'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 34 additions & 5 deletions
39
onadata/apps/logger/models/daily_xform_submission_counter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,47 @@ | ||
# coding: utf-8 | ||
import datetime | ||
|
||
from django.contrib.auth.models import User | ||
from django.db import models | ||
|
||
from onadata.apps.logger.models.xform import XForm | ||
from django.db.models import UniqueConstraint, Q, F | ||
|
||
|
||
class DailyXFormSubmissionCounter(models.Model): | ||
date = models.DateField() | ||
user = models.ForeignKey(User, related_name='daily_counts', null=True, on_delete=models.CASCADE) | ||
xform = models.ForeignKey( | ||
XForm, related_name='daily_counters', on_delete=models.CASCADE | ||
'logger.XForm', related_name='daily_counters', null=True, on_delete=models.CASCADE | ||
) | ||
counter = models.IntegerField(default=0) | ||
|
||
class Meta: | ||
unique_together = (('date', 'xform'),) | ||
constraints = [ | ||
UniqueConstraint(fields=['date', 'user', 'xform'], | ||
name='daily_unique_with_xform'), | ||
UniqueConstraint(fields=['date', 'user'], | ||
condition=Q(xform=None), | ||
name='daily_unique_without_xform') | ||
] | ||
indexes = [ | ||
models.Index(fields=('date', 'user')), | ||
] | ||
|
||
@classmethod | ||
def update_catch_all_counter_on_delete(cls, sender, instance, **kwargs): | ||
daily_counters = cls.objects.filter( | ||
xform_id=instance.pk, counter__gte=1 | ||
) | ||
|
||
for daily_counter in daily_counters: | ||
criteria = dict( | ||
date=daily_counter.date, | ||
user=daily_counter.user, | ||
xform=None, | ||
) | ||
# make sure an instance exists with `xform = NULL` | ||
cls.objects.get_or_create(**criteria) | ||
# add the count for the project being deleted to the null-xform | ||
# instance, atomically! | ||
cls.objects.filter(**criteria).update( | ||
counter=F('counter') + daily_counter.counter | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters