mirror of
https://github.com/zulip/zulip.git
synced 2025-11-06 15:03:34 +00:00
The previous situation was bad for two reasons: (1) It had a lot of copies of the code, some of them missing pieces: UserProfile.objects.get(user__email__iexact=foo) This was in particular going to be inconvenient since we are dropping the __user part of that. (2) It didn't take advantage of our memcached caching. (imported from commit 2325795f288a7cf306cdae191f5d3080aac0651a)
49 lines
1.8 KiB
Python
Executable File
49 lines
1.8 KiB
Python
Executable File
from optparse import make_option
|
|
from django.core.management.base import BaseCommand
|
|
from zephyr.models import Realm, UserProfile, Message, UserMessage, \
|
|
get_user_profile_by_email
|
|
from zephyr.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
|
|
import simplejson
|
|
|
|
def dump():
|
|
pointers = []
|
|
for u in UserProfile.objects.select_related("user__email").all():
|
|
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))
|
|
file("dumped-pointers", "w").write(simplejson.dumps(pointers) + "\n")
|
|
|
|
def restore(change):
|
|
for (email, timestamp) in simplejson.loads(file("dumped-pointers").read()):
|
|
try:
|
|
u = get_user_profile_by_email(email)
|
|
except UserProfile.DoesNotExist:
|
|
print "Skipping...", email
|
|
continue
|
|
if timestamp == -1:
|
|
pointer = -1
|
|
else:
|
|
try:
|
|
pointer = UserMessage.objects.filter(user_profile=u,
|
|
message__pub_date__gte=timestamp_to_datetime(timestamp)).order_by("message")[0].message_id
|
|
except IndexError:
|
|
print "Alert...", email, timestamp
|
|
continue
|
|
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()
|