From 00659738cb8fe8d6418d8ef5f945a14ce5acd044 Mon Sep 17 00:00:00 2001 From: Nikita Karetnikov Date: Sun, 12 Aug 2012 06:37:00 +0400 Subject: Custom manage.py command for synchronization --- gstudio/management/commands/sync-instances.py | 112 ++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 gstudio/management/commands/sync-instances.py (limited to 'gstudio/management/commands') diff --git a/gstudio/management/commands/sync-instances.py b/gstudio/management/commands/sync-instances.py new file mode 100644 index 00000000..632e9ef2 --- /dev/null +++ b/gstudio/management/commands/sync-instances.py @@ -0,0 +1,112 @@ +# Copyright (c) 2012 Free Software Foundation + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.core.management.base import BaseCommand +from optparse import make_option + +import re +import sys +from datetime import datetime + +from objectapp.models import Gbobject +from objectapp.models import Objecttype +from objectapp.models import Attributetype +from objectapp.models import Relationtype +from objectapp.models import ObjectDoesNotExist + +# from gstudio.xmlrpc.metaweblog import class_checker +from gstudio import models as gstmodels +from objectapp import models as objmodels +import inspect + +from xmlrpclib import DateTime +from xmlrpclib import ServerProxy + +from django.db.utils import IntegrityError + +class Command(BaseCommand): + """Gets all Gbobjects from a specified server""" + option_list = BaseCommand.option_list + ( + make_option("--server", action="store", type="string", + dest="server", help="Specify an ip"),) + + def handle(self, *args, **options): + def class_checker(m): + """Returns a dict which contains all classes of the m module""" + res = {} + for name, obj in inspect.getmembers(m): + if inspect.isclass(obj) and obj.__module__ == m.__name__: + res[name] = obj + return res + + def parse(module=None, instance=None, id=None): + """Parses and saves instances""" + try: + instances = srv.metaWeblog.show_instance(module, instance, id) + + if module == "objectapp.models": + module = objmodels + + if module == "gstudio.models": + module = gstmodels + + for i in instances: + pattern = "^(\d{4})(\d{2})(\d{2}).(\d{2}).(\d{2}).(\d{2})$" + + if "_tags_cache" in i: + del i["_tags_cache"] + + if "_state" in i: + del i["_state"] + + if "_altnames_cache" in i: + del i["_altnames_cache"] + + if "_mptt_cached_fields" in i: + del i["_mptt_cached_fields"] + + def group(value): + return value.group(1, 2, 3, 4, 5, 6) + + def str_to_int(string): + return [int(x) for x in string] + + for key in i.keys(): + # Weird check for DateTime objects + if "make_comparable" in dir(i[key]): + dt = DateTime().make_comparable(i[key])[1] + dt = str_to_int(group(re.search(pattern, dt))) + + i[key] = datetime(*dt) + + class_checker(module)[instance](**i).save() + + except (ObjectDoesNotExist, IntegrityError): + sys.stderr.write("Object matching query does not exist\n") + + except ValueError: + sys.stderr.write("Object already exists\n") + + server = options["server"] + srv = ServerProxy(server, allow_none=True) + + objcc = class_checker(objmodels) + gstcc = class_checker(gstmodels) + + for i in objcc.keys(): + parse("objectapp.models", i) + + for i in gstcc.keys(): + parse("gstudio.models", i) -- cgit v1.2.3-70-g09d2