2012-12-11 22:49:20 +01:00
|
|
|
from optparse import make_option
|
|
|
|
from django.core.management.base import BaseCommand
|
2012-12-15 15:28:22 +01:00
|
|
|
from zephyr.models import Realm, UserProfile, Message, UserMessage
|
2012-12-13 17:15:06 +01:00
|
|
|
from zephyr.lib.time import datetime_to_timestamp, timestamp_to_datetime
|
2012-12-11 22:49:20 +01:00
|
|
|
import simplejson
|
|
|
|
|
|
|
|
def dump():
|
|
|
|
pointers = []
|
|
|
|
for u in UserProfile.objects.select_related("user__email").all():
|
2012-12-13 17:15:06 +01:00
|
|
|
pointer = u.pointer
|
|
|
|
if pointer != -1:
|
|
|
|
pub_date = Message.objects.get(id=pointer).pub_date
|
|
|
|
pointers.append((u.user.email, datetime_to_timestamp(pub_date)))
|
|
|
|
else:
|
|
|
|
pointers.append((u.user.email, -1))
|
2012-12-11 22:49:20 +01:00
|
|
|
file("dumped-pointers", "w").write(simplejson.dumps(pointers) + "\n")
|
|
|
|
|
|
|
|
def restore(change):
|
2012-12-13 17:15:06 +01:00
|
|
|
for (email, timestamp) in simplejson.loads(file("dumped-pointers").read()):
|
2012-12-14 00:55:06 +01:00
|
|
|
try:
|
|
|
|
u = UserProfile.objects.get(user__email__iexact=email)
|
|
|
|
except UserProfile.DoesNotExist:
|
|
|
|
print "Skipping...", email
|
|
|
|
continue
|
2012-12-13 17:15:06 +01:00
|
|
|
if timestamp == -1:
|
|
|
|
pointer = -1
|
|
|
|
else:
|
2012-12-14 00:55:06 +01:00
|
|
|
try:
|
2012-12-15 15:28:22 +01:00
|
|
|
pointer = UserMessage.objects.filter(user_profile=u,
|
|
|
|
message__pub_date__gte=timestamp_to_datetime(timestamp)).order_by("message")[0].message_id
|
2012-12-14 00:55:06 +01:00
|
|
|
except IndexError:
|
|
|
|
print "Alert...", email, timestamp
|
|
|
|
continue
|
2012-12-11 22:49:20 +01:00
|
|
|
if change:
|
|
|
|
u.pointer = pointer
|
|
|
|
u.save()
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
option_list = BaseCommand.option_list + (
|
|
|
|
make_option('--restore', default=False, action='store_true'),
|
|
|
|
make_option('--dry-run', '-n', default=False, action='store_true'),)
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
if options["restore"]:
|
|
|
|
restore(change=not options['dry_run'])
|
|
|
|
else:
|
|
|
|
dump()
|