2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2012-12-11 22:49:20 +01:00
|
|
|
from optparse import make_option
|
|
|
|
from django.core.management.base import BaseCommand
|
2013-06-27 20:03:51 +02:00
|
|
|
from zephyr.models import UserProfile, Message, UserMessage, \
|
2013-03-28 20:20:31 +01:00
|
|
|
get_user_profile_by_email
|
2013-01-10 22:49:07 +01:00
|
|
|
from zephyr.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
|
2013-06-18 23:55:55 +02:00
|
|
|
import ujson
|
2012-12-11 22:49:20 +01:00
|
|
|
|
|
|
|
def dump():
|
|
|
|
pointers = []
|
2013-03-28 20:43:34 +01:00
|
|
|
for u in UserProfile.objects.all():
|
2012-12-13 17:15:06 +01:00
|
|
|
pointer = u.pointer
|
|
|
|
if pointer != -1:
|
|
|
|
pub_date = Message.objects.get(id=pointer).pub_date
|
2013-03-28 20:43:34 +01:00
|
|
|
pointers.append((u.email, datetime_to_timestamp(pub_date)))
|
2012-12-13 17:15:06 +01:00
|
|
|
else:
|
2013-03-28 20:43:34 +01:00
|
|
|
pointers.append((u.email, -1))
|
2013-06-18 23:55:55 +02:00
|
|
|
file("dumped-pointers", "w").write(ujson.dumps(pointers) + "\n")
|
2012-12-11 22:49:20 +01:00
|
|
|
|
|
|
|
def restore(change):
|
2013-06-18 23:55:55 +02:00
|
|
|
for (email, timestamp) in ujson.loads(file("dumped-pointers").read()):
|
2012-12-14 00:55:06 +01:00
|
|
|
try:
|
2013-03-28 20:20:31 +01:00
|
|
|
u = get_user_profile_by_email(email)
|
2012-12-14 00:55:06 +01:00
|
|
|
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()
|