Esempio n. 1
0
File: models.py Progetto: Arpaso/ETS
 def updata_data(cls, data):
     """Deserializes data and saves them"""
     
     #Decompress field names
     data = decompress_json(data)
     #Deserialize them
     objects = serializers.deserialize('json', data)
     #And save
     for wrapper in objects:
         wrapper.save()
     
     cls(serialized_data=data).save()
Esempio n. 2
0
File: utils.py Progetto: Arpaso/ETS
def import_file(f):
    """Reads file, decompresses serialized data,deserializes it and saves objects"""
    #File is supposed to be small (< 4Mb)

    data = decompress_json(f.read())
    total = 0

    for obj in serializers.deserialize("json", data, parse_float=decimal.Decimal):
        if "LogEntry" == obj.object._meta.object_name:
            if not LogEntry.objects.filter(content_type__id=ContentType.objects.get_for_model(ets_models.Waybill).pk,
                                           action_time=obj.object.action_time,
                                           user=obj.object.user,
                                           object_id=obj.object.object_id).exists():
                obj.object.pk = None
                models.Model.save_base(obj.object, raw=True, force_insert=True)
        else:    
            obj.save()
        total += 1

    return total
Esempio n. 3
0
    def handle(self, file_name=None, dir_name=None, *args, **options):
        
        verbosity = int(options.get('verbosity', 1))
        
        if verbosity >= 2:
            print "Importing file --> ", file_name.encode('utf-8')

        if file_name:
            try:
                with open(file_name) as f:
                    total = import_file(f)
                    if verbosity >= 2:
                        print "Totally saved objects --> ", total
                
            except TypeError:
                raise CommandError("Wrong file argument. It must be proper file name instead of %s" % file_name)
            exit()

        root = Tk()
        root.withdraw()

        if dir_name and os.path.exists(dir_name):
            dir_name = os.path.abspath(dir_name)
        else:
            dir_name = BASE_DIR
    
        options = {
            'initialdir': dir_name,
            'title': "Please choose file with initial data",
            'filetypes': FILETYPES,
        }
    
        try:
            initialfile = (i for i in os.listdir(dir_name) if i.endswith(".data")).next()
            initialfile = os.path.join(dir_name, initialfile)
            if os.path.isfile(initialfile):
                options['initialfile'] = initialfile
                # ext = os.path.splitext(initialfile)[1]
                # index = (n for n, i in enumerate(FILETYPES) if i[1] == ext).next()
                # if index:
                #     item = FILETYPES.pop(index)
                #     FILETYPES.insert(0, item)
                #     options['filetypes'] = FILETYPES
        except StopIteration:
            pass

        data_file = askopenfilename(**options)
        
        if data_file:
            ext = os.path.splitext(data_file)[1]
            if ext == ".data":
                file_decompressed = tempfile.NamedTemporaryFile(suffix=".json", delete=False)
                with open(os.path.normpath(data_file), 'r') as f:
                    data = decompress_json(f.read())
                    if not data:
                        showerror(TITLE, "Decompression is failed. Wrong data in %" % data_file)
                        exit()
                    file_decompressed.write(data)
                    file_decompressed.close()
                    data_file = file_decompressed.name

            # addition = {}
            # if platform.system() == "Windows":
            #     addition['shell'] = True
            output = StringIO.StringIO()
            errors = StringIO.StringIO()
        
            call_command('loaddata', data_file, stdout=output, stderr=errors)
            
            if errors.getvalue():
                showerror(" ".join([TITLE, "error"]), errors.getvalue())

            if output.getvalue():
                showinfo(TITLE, output.getvalue())
            
            output.close()
            errors.close()