Skip to content

Instantly share code, notes, and snippets.

@pmclanahan
Created June 14, 2013 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmclanahan/5781498 to your computer and use it in GitHub Desktop.
Save pmclanahan/5781498 to your computer and use it in GitHub Desktop.
From @dpoirier: Here's a stab at a script to fix up our Basket data given a set of email-token mappings.
# Draft script to fix the token mappings in the Basket database,
# given a list of valid email-token mappings
from django.db.models import Q
from news.models import Subscriber
# Let's assume we have the data in the form of a dictionary,
# where each key is an email address and its value is the corresponding
# token (or we can get it into that format).
# For an example, make up a dummy set of data
data = {
'user1@example.com': 'c5586048-a640-4bb8-be63-3a8b06add858',
'user2@example.com': '360f7283-d9db-4ecc-ae32-4082ef4b7d81',
}
# Let's just delete any existing records with either the email or token,
# then create one with the right mapping.
email_query = Q(email__in=data.keys())
token_query = Q(token__in=data.values())
Subscriber.objects.filter(email_query|token_query).delete()
# That was one query.
# Now create the right mappings.
# If we have Django 1.4 or later, we can do it in one more query.
if hasattr(Subscriber.objects, 'bulk_create'):
Subscriber.objects.bulk_create(
Subscriber(email=email, token=token)
for email, token in data.iteritems()
)
else:
# We'll have to do it one at a time.
for email, token in data.iteritems():
Subscriber.objects.create(email=email, token=token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment