Ejemplo n.º 1
0
    def _setupargs(self,
                   content,
                   record=None,
                   pha_spec=False,
                   pha=None,
                   label='testing',
                   creator=None,
                   external_id=None,
                   mime_type='application/xml'):
        self.content = content
        self.mime_type = mime_type
        self.record = record
        self.pha = pha
        self.local_external_id = external_id
        if self.pha:
            self.external_id = Document.prepare_external_id(
                external_id, self.pha, pha_spec, record)
        else:
            self.external_id = None

        if not pha_spec:
            self.pha = None

        self.label = label
        self.creator = creator
Ejemplo n.º 2
0
def _get_prefs_doc(account, pha):
    from indivo.models import Document
    from indivo.views.documents.document import _get_document
    prepared_id = Document.prepare_external_id(USER_PREFS_EXTID % account.id,
                                               pha,
                                               pha_specific=True,
                                               record_specific=False)
    return _get_document(pha=pha, external_id=prepared_id)
Ejemplo n.º 3
0
    def test_prepare_external_id(self):
        valid_args = {('abc', self.app, False, True): '%s/%s'%(self.app.email, 'abc'),
                      ('bcd', self.app, True, True): '%s/INTERNAL/%s'%(self.app.email, 'bcd'),
                      ('cde', self.app, True, False): '%s/NORECORD/%s'%(self.app.email, 'cde'),
                      }
        
        invalid_args = {('def', None): ValueError,
                        ('efg', self.app, False, False): ValueError,
                        }

        # Make sure ids are generated properly
        for args, correct_id in valid_args.iteritems():
            self.assertEqual(Document.prepare_external_id(*args), correct_id)

        # Make sure invalid args throw errors
        for args, error in invalid_args.iteritems():
            self.assertRaises(error, Document.prepare_external_id, *args)
Ejemplo n.º 4
0
    def _setupargs(self, content, record=None, pha_spec=False, 
                 pha=None, label='testing', creator=None, external_id=None, mime_type='application/xml'):
        self.content = content
        self.mime_type = mime_type
        self.record = record
        self.pha = pha
        self.local_external_id = external_id
        if self.pha:
            self.external_id = Document.prepare_external_id(external_id, self.pha, pha_spec, record)
        else:
            self.external_id = None

        if not pha_spec:
            self.pha = None

        self.label = label
        self.creator = creator
Ejemplo n.º 5
0
    def _setupargs(self, content, record=None, pha_spec=False, 
                 pha=None, label='testing', creator=None, external_id=None):
        self.content = content
        self.record = record
        self.pha = pha
        self.local_external_id = external_id
        if self.pha:
            self.external_id = Document.prepare_external_id(external_id, self.pha, pha_spec, record)
        else:
            self.external_id = None

        if not pha_spec:
            self.pha = None

        self.label = label
        self.creator = creator
        self.size = len(self.content)

        md = hashlib.sha256()
        md.update(self.content)
        self.digest = md.hexdigest()
Ejemplo n.º 6
0
    def test_prepare_external_id(self):
        valid_args = {
            ('abc', self.app, False, True):
            '%s/%s' % (self.app.email, 'abc'),
            ('bcd', self.app, True, True):
            '%s/INTERNAL/%s' % (self.app.email, 'bcd'),
            ('cde', self.app, True, False):
            '%s/NORECORD/%s' % (self.app.email, 'cde'),
        }

        invalid_args = {
            ('def', None): ValueError,
            ('efg', self.app, False, False): ValueError,
        }

        # Make sure ids are generated properly
        for args, correct_id in valid_args.iteritems():
            self.assertEqual(Document.prepare_external_id(*args), correct_id)

        # Make sure invalid args throw errors
        for args, error in invalid_args.iteritems():
            self.assertRaises(error, Document.prepare_external_id, *args)
Ejemplo n.º 7
0
    def handle(self, *args, **options):
        if len(args) == 0:
            raise CommandError('No files listed for import')

        if (options.get('externalid') or options.get('replaces')) and len(args) > 1:
            raise CommandError('Only one file can be specified with the externalid or replaces options')

        for filename in args:
            if not os.path.isfile(filename):
                raise CommandError('Cannot locate file [%s]', filename)

        params = {}

        if options['mimetype']:
            mimetype = params['mime_type'] = options['mimetype']
        else:
            mimetype = params['mime_type'] = 'application/xml'

        if options['replaces']:
            olddoc = Document.objects.filter(pk=options['replaces'])
            if len(olddoc) == 0:
                raise CommandError('Cannot locate document to be replaced [%s]', options['replaces'])
            if options['verbosity'] > 1:
                print "Replacing document: ", filename
            content = open(args[0], 'r').read()
            olddoc.replace(content, mimetype)

        else:
            if options['record']:
                records = Record.objects.filter(id=options['record'])
                if len(records) == 0:
                    records = Record.objects.filter(external_id=options['record'])
                if len(records) == 0:
                    raise CommandError('Cannot locate record [%s], You must provide a record.id or a record.external_id', options['record'])
                assert len(records) == 1
                params['record'] = records[0]
            else:
                raise CommandError('You must provide a record parameters [indivo id or external id] to attach this document to')

            if options['externalid']:
                if not options['pha']:
                    raise CommandError('You can only specify the external_id if you also specify the PHA')

                pha = None
                phas = PHA.objects.filter(name=options['pha'])
                if len(phas) == 0:
                    raise CommandError('Cannot locate pha [%s], I have %s', options['pha'], PHA.objects.all().value_list('name'))
                assert len(phas) == 1
                pha = phas[0]
                params['external_id'] = Document.prepare_external_id(options['externalid'], pha)

            if options['creator']:
                creators = Principal.objects.filter(id=options['creator'])
                if len(creators) == 0:
                    creators = Principal.objects.filter(email=options['creator'])
                if len(creators) == 0:
                    raise CommandError('The creator you provided cannot be located in the principals table using the id or email')
                assert len(records) == 1
                params['creator'] = creators[0]

        count = 0
        for filename in args:
            if options['verbosity'] > 1:
                print "Loading document: ", filename
            params['content'] = open(filename, 'r').read()
            Document.objects.create(**params)
            count += 1
        if options['verbosity'] > 1:
            print count, "documents loaded ", filename
Ejemplo n.º 8
0
def _get_prefs_doc(account, pha):
    from indivo.models import Document
    from indivo.views.documents.document import _get_document
    prepared_id = Document.prepare_external_id(USER_PREFS_EXTID%account.id, pha, pha_specific=True, record_specific=False)
    return _get_document(pha=pha, external_id=prepared_id)