Exemplo n.º 1
0
 def test_random_string(self):
     # test default length
     self.assertEqual(len(su.random_string()), 8)
     # test length setting
     for length in xrange(5,10):
         self.assertEqual(len(su.random_string(length)), length)
     # test invalid lengths
     for length in (-2, -1, 0):
         self.assertRaises(ValueError, su.random_string, length)
     # test uniqueness - could theoretically fail, but very unlikely
     testlist = (su.random_string() for unused in xrange(10))
     self.assertEqual(len(set(testlist)), 10)
     # test default charset
     for c in su.random_string(10000):
         self.assertNotIn(c, '~!@#$%^&*()_+`-={}|[]\\:";\'<>?,./')
     # test charset - could theoretically fail, but very unlikely
     charset = 'ABC'
     abc = su.random_string(100, charset)
     self.assertEqual(set(abc), {'A', 'B', 'C'})
Exemplo n.º 2
0
Arquivo: views.py Projeto: pgwthf/TSB
def backup(request, action=None, file_id=None):
    '''
    Backup and restore
    Using django dumpdata was too slow and took too much RAM, so backups are
    now made using pg_dump (straight from postgresql)
TODO: run this asynchronously
TODO: use subprocess properly using args, PIPE, etc
    '''
    message = ''
    if not file_id and action == 'make_backup':
        backup_folder = '/tmp/'
        filename = 'backup_{}_{}.gz'.format(
                        datetime.date.today().isoformat(), random_string(4))
        command = 'pg_dump -U postgres -Fc django_TSB > {}{}'.format(
                                                    backup_folder, filename)
        subprocess.call(command, shell=True)

#        outfile = open(filename, 'w')
#        models = get_models()
#        call_command('dumpdata', *models, format='json', indent=0, 
#                                                                stdout=outfile)
#        outfile.close()
#        message = {'backup': True, 'text': 'Backup \"{}\" created'.format(
#                                                                    filename)}

    file_list = {}
    for filename in glob.glob('/tmp/backup_????-??-??_*.gz'):
        file_list[filename[-7:-3]] = filename
#    file_list = {}
#    for filename in glob.glob('backup_????-??-??_*.json'):
#        file_list[filename[-9:-5]] = filename

    if file_id:
        if action == 'restore':
            message = {'restore': 'confirmed', 'filename': file_list[file_id], 
                                                    'file_id': file_id}
        elif action == 'restore_confirmed':
            message = {'restore': True, 'text': 
                       'Restore not implemented yet, use pg_restore manually'}
#TODO: implement restore
#        command = 'pg_restore -U postgres -Fc {}'.format(filename)

#            call_command('loaddata', filename)
#            message = {'restore': True, 'text': 'Data from {} restored'.format(
#                                                                    filename)}
        else:
            raise ValueError('action {} is invalid'.format(action))
    return render(request, 'backup.html', 
            {'file_list': file_list,
             'message': message})
Exemplo n.º 3
0
Arquivo: models.py Projeto: pgwthf/TSB
    def _copy(cls, instance):
        '''
        Copy <instance> to a new instance with a unique name.

        It is a deep copy for all associated methods, entries and exits are 
        copied too.
        '''
        fields = model_to_dict(instance, exclude=['id'])
        fields['pool'] = instance.pool
        fields['group'] = instance.group
        newname = fields['name'][:19] + random_string(1)
        fields['name'] = newname
        new_system = cls.objects.create(**fields)
        Method.copy_all(instance, new_system)
        return new_system