def storage_match_up_media_job(job): logging.info('storage_match_up_media started for %s' % job) jobinfo = JobInfo.objects.get(id=job.arg) arg = simplejson.loads(jobinfo.arg) logging.info('Matching up media with arguments %s', arg) storage = Storage.objects.get(id=arg['storage']) collection = Collection.objects.get(id=arg['collection']) allow_multiple_use = arg.get('allow_multiple_use') jobinfo.update_status('Analyzing available files') count = -1 for count, (record, filename) in enumerate( match_up_media(storage, collection, allow_multiple_use)): id = os.path.splitext(os.path.split(filename)[1])[0] mimetype = mimetypes.guess_type(filename)[0] or \ 'application/octet-stream' Media.objects.create(record=record, name=id, storage=storage, url=filename, mimetype=mimetype) if count % 100 == 0: jobinfo.update_status('Created %s media objects' % (count + 1)) logging.info('storage_match_up_media complete: %s' % job) jobinfo.complete( 'Complete', '%s files were matched up with existing records.' % (count + 1))
def storage_match_up_media_job(job): logging.info('storage_match_up_media started for %s' % job) jobinfo = JobInfo.objects.get(id=job.arg) arg = json.loads(jobinfo.arg) logging.info('Matching up media with arguments %s', arg) storage = Storage.objects.get(id=arg['storage']) collection = Collection.objects.get(id=arg['collection']) jobinfo.update_status('Analyzing available files') count = -1 for count, (record, filename) in enumerate(match_up_media(storage, collection)): id = os.path.splitext(os.path.split(filename)[1])[0] mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream' media = Media.objects.create(record=record, name=id, storage=storage, url=filename, mimetype=mimetype) if (count % 100 == 0): jobinfo.update_status('Created %s media objects' % (count + 1)) logging.info('storage_match_up_media complete: %s' % job) jobinfo.complete('Complete', '%s files were matched up with existing records.' % (count + 1))
def match_up_files(request): available_storage = get_list_or_404( filter_by_access(request.user, Storage, manage=True).order_by('title').values_list( 'id', 'title')) available_collections = get_list_or_404( filter_by_access(request.user, Collection, manage=True)) class MatchUpForm(forms.Form): collection = forms.ChoiceField(choices=( (c.id, c.title) for c in sorted(available_collections, key=lambda c: c.title))) storage = forms.ChoiceField(choices=available_storage) if request.method == 'POST': form = MatchUpForm(request.POST) if form.is_valid(): collection = get_object_or_404( filter_by_access(request.user, Collection.objects.filter( id=form.cleaned_data['collection']), manage=True)) storage = get_object_or_404( filter_by_access( request.user, Storage.objects.filter(id=form.cleaned_data['storage']), manage=True)) matches = match_up_media(storage, collection) for record, filename in matches: id = os.path.splitext(os.path.split(filename)[1])[0] mimetype = mimetypes.guess_type( filename)[0] or 'application/octet-stream' media = Media.objects.create(record=record, name=id, storage=storage, url=filename, mimetype=mimetype) request.user.message_set.create( message='%s files were matched up with existing records.' % len(matches)) return HttpResponseRedirect( '%s?collection=%s&storage=%s' % (reverse('storage-match-up-files'), collection.id, storage.id)) else: form = MatchUpForm(request.GET) return render_to_response('storage_match_up_files.html', { 'form': form, }, context_instance=RequestContext(request))
def test_connect_files(self): r1 = self.create_record('id_1') r2 = self.create_record('id_2') r3 = self.create_record('id_3') Media.objects.create(record=r1, storage=self.storage, url='id_1.txt') matches = match_up_media(self.storage, self.collection) self.assertEqual(1, len(matches)) match = matches[0] record, file_url = match self.assertEqual(r2, record) self.assertEqual('id_2.txt', file_url)
def storage_match_up_media(self, storage_id, collection_id, allow_multiple_use): storage = Storage.objects.get(id=storage_id) collection = Collection.objects.get(id=collection_id) count = -1 created = [] for count, (record, filename) in enumerate( match_up_media(storage, collection, allow_multiple_use)): identifier = os.path.splitext(os.path.split(filename)[1])[0] mimetype = mimetypes.guess_type(filename)[0] or \ 'application/octet-stream' Media.objects.create( record=record, name=identifier, storage=storage, url=filename, mimetype=mimetype, ) created.append({ 'id': identifier, 'record': record.id, 'file': filename, 'mimetype': mimetype, }) if count % 100 == 99: self.update_state(state='PROGRESS', meta={ 'count': count + 1, }) attachment = get_attachment(self) with open(attachment, 'w') as report: json.dump(created, report, indent=2) return { 'collection': collection_id, 'storage': storage_id, 'allow_multiple_use': allow_multiple_use, 'count': count + 1, 'attachment': attachment, }
def match_up_files(request): available_storage = get_list_or_404(filter_by_access(request.user, Storage, manage=True).order_by('title').values_list('id', 'title')) available_collections = get_list_or_404(filter_by_access(request.user, Collection, manage=True)) class MatchUpForm(forms.Form): collection = forms.ChoiceField(choices=((c.id, c.title) for c in sorted(available_collections, key=lambda c: c.title))) storage = forms.ChoiceField(choices=available_storage) if request.method == 'POST': form = MatchUpForm(request.POST) if form.is_valid(): collection = get_object_or_404(filter_by_access(request.user, Collection.objects.filter(id=form.cleaned_data['collection']), manage=True)) storage = get_object_or_404(filter_by_access(request.user, Storage.objects.filter(id=form.cleaned_data['storage']), manage=True)) matches = match_up_media(storage, collection) for record, filename in matches: id = os.path.splitext(os.path.split(filename)[1])[0] mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream' media = Media.objects.create(record=record, name=id, storage=storage, url=filename, mimetype=mimetype) request.user.message_set.create(message='%s files were matched up with existing records.' % len(matches)) return HttpResponseRedirect('%s?collection=%s&storage=%s' % ( reverse('storage-match-up-files'), collection.id, storage.id )) else: form = MatchUpForm(request.GET) return render_to_response('storage_match_up_files.html', {'form': form, }, context_instance=RequestContext(request))