Created
September 9, 2015 08:56
-
-
Save maracuja/f26b51b767173bade4af to your computer and use it in GitHub Desktop.
Member Duplicates Removal
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
def get_membership(code): | |
memberships = Membership.objects.filter(code=code) | |
if memberships.count() > 1: | |
print '-------- code %s has %s memberships' % (code, memberships.count(),) | |
membership = None | |
for m in memberships: | |
if m.customermembership_set.count() > 0: | |
print '------------ membership %s is linked to customer memberships' % m.id | |
membership = m | |
else: | |
m.delete() | |
print '------------ deleting duplicate membership' | |
if membership is not None: | |
return membership | |
elif memberships.count() == 1: | |
print '-------- code %s has 1 membership' % code | |
return memberships[0] | |
print '-------- creating new membership: %s' % code | |
return Membership(code=code) | |
def duplicates(): | |
from django.db import connections | |
from django.db.models import Count | |
cursor = connections['default'].cursor() | |
cursor.execute('this is an aggregate sql statement that gets the number of duplicates') | |
for row in cursor.fetchall(): | |
if row[1] > 1: | |
yield row | |
for dupe in duplicates(): | |
print '---- %s' % dupe[0] | |
get_membership(dupe[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment