diff options
author | Nikita Karetnikov <nikita.karetnikov@gmail.com> | 2012-08-12 06:37:00 +0400 |
---|---|---|
committer | Nikita Karetnikov <nikita.karetnikov@gmail.com> | 2012-08-12 06:37:00 +0400 |
commit | 00659738cb8fe8d6418d8ef5f945a14ce5acd044 (patch) | |
tree | 3b81593c593582ecfd44abb7fda8b8bdb139d5ea /gstudio/management/commands/sync-instances.py | |
parent | 98613a76a3ddb585b2d930cbe556eca895f37bfa (diff) | |
download | gnowsys-00659738cb8fe8d6418d8ef5f945a14ce5acd044.tar.gz |
Custom manage.py command for synchronization
Diffstat (limited to 'gstudio/management/commands/sync-instances.py')
-rw-r--r-- | gstudio/management/commands/sync-instances.py | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/gstudio/management/commands/sync-instances.py b/gstudio/management/commands/sync-instances.py new file mode 100644 index 0000000..632e9ef --- /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 <http://www.gnu.org/licenses/>. + +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) |