示例#1
0
文件: views.py 项目: Arpaso/ETS
    def form_valid(self, form):
        _file = form.cleaned_data['file']

        total = import_file(_file)
        
        messages.add_message(self.request, messages.INFO, 
                             _('File has been imported successfully. Totally saved objects --> %s' % total))
        
        return self.get(self.request)
示例#2
0
    def test_export_waybills_command(self):
        """Tests offliner.management.commands.export_waybills.Command"""
        
        output = StringIO.StringIO()

        call_command('export_waybills', user="******", passwd="dispatcher", stdout=output)
        output.seek(0)
        
        total = import_file(output)
        
        self.assertEqual(total, 2)
示例#3
0
 def test_export_compas_command(self):
     """Tests ets.management.commands.export_compas.Command"""
     
     output = StringIO.StringIO()
     
     #Try with not existed COMPAS station argument
     call_command('export_compas', compas='******', compress=True, stdout=output)
     output.seek(0)
     
     total = import_file(output)
     
     self.assertEqual(total, 14)
示例#4
0
    def test_export_waybills_dispach_sign(self):
        waybill = ets.models.Waybill.objects.get(pk="ISBX00211A")

        self.client.login(username='******', password='******')
        response = self.client.post(reverse('waybill_finalize_dispatch', kwargs={'waybill_pk': waybill.pk,}))
        self.assertEqual(response.status_code, 200)

        output = StringIO.StringIO()
        
        call_command('export_waybills', user="******", passwd="dispatcher", stdout=output)
        output.seek(0)
        
        total = import_file(output)
        
        self.assertEqual(total, 5)
示例#5
0
文件: import_file.py 项目: Arpaso/ETS
    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()