2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2012-12-11 23:14:32 +01:00
|
|
|
from optparse import make_option
|
|
|
|
from django.core.management.base import BaseCommand
|
2013-03-28 20:20:31 +01:00
|
|
|
from zephyr.models import Realm, UserActivity, get_client, \
|
|
|
|
get_user_profile_by_email
|
2012-12-11 23:14:32 +01:00
|
|
|
import simplejson
|
2013-01-10 22:49:07 +01:00
|
|
|
from zephyr.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
|
2012-12-11 23:14:32 +01:00
|
|
|
|
|
|
|
def dump():
|
|
|
|
pointers = []
|
2013-03-28 20:43:34 +01:00
|
|
|
for activity in UserActivity.objects.select_related("user_profile__email",
|
2012-12-11 23:14:32 +01:00
|
|
|
"client__name").all():
|
2013-03-28 20:43:34 +01:00
|
|
|
pointers.append((activity.user_profile.email, activity.client.name,
|
2012-12-11 23:14:32 +01:00
|
|
|
activity.query, activity.count,
|
|
|
|
datetime_to_timestamp(activity.last_visit)))
|
|
|
|
file("dumped-activity", "w").write(simplejson.dumps(pointers) + "\n")
|
|
|
|
|
|
|
|
def restore(change):
|
|
|
|
for (email, client_name, query, count, timestamp) in simplejson.loads(file("dumped-activity").read()):
|
2013-03-28 20:20:31 +01:00
|
|
|
user_profile = get_user_profile_by_email(email)
|
2012-12-11 23:14:32 +01:00
|
|
|
client = get_client(client_name)
|
|
|
|
last_visit = timestamp_to_datetime(timestamp)
|
|
|
|
print "%s: activity for %s,%s" % (email, client_name, query)
|
|
|
|
if change:
|
|
|
|
activity, created = UserActivity.objects.get_or_create(user_profile=user_profile,
|
|
|
|
query=query, client=client,
|
|
|
|
defaults={"last_visit": last_visit,
|
|
|
|
"count": count})
|
|
|
|
if not created:
|
|
|
|
activity.count += count
|
|
|
|
activity.last_visit = max(last_visit, activity.last_visit)
|
|
|
|
activity.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()
|