def load_part_from_ice(self, registry_id): update = Update.load_update() user_email = update.mod_by.email try: ice = IceApi(auth=HmacAuth(key_id=settings.ICE_KEY_ID, username=user_email), verify_ssl_cert=settings.VERIFY_ICE_CERT) ice.timeout = settings.ICE_REQUEST_TIMEOUT self.entry = ice.get_entry(registry_id) self.entry.url = ''.join(( ice.base_url, '/entry/', str(self.entry.id), )) except Exception: logger.exception( 'Exception loading part %(part_id)s from ICE for user ' '%(user_email)s' % { 'part_id': registry_id, 'user_email': user_email, }) raise ValidationError( _('Failed to load strain %(uuid)s from ICE'), code='ice failure', params={"uuid": registry_id}, )
def search_strain(request): """ Autocomplete delegates to ICE search API. """ auth = HmacAuth(key_id=settings.ICE_KEY_ID, username=request.user.email) ice = IceApi(auth=auth, verify_ssl_cert=settings.ICE_VERIFY_CERT) ice.timeout = settings.ICE_REQUEST_TIMEOUT term = request.GET.get("term", "") results = ice.search(term) return JsonResponse({"rows": results})
def create_ice_connection(user_token): """Creates an instance of the ICE API using common settings.""" # Use getattr to load settings without raising AttributeError key_id = getattr(settings, "ICE_KEY_ID", None) url = getattr(settings, "ICE_URL", None) verify = getattr(settings, "ICE_VERIFY_CERT", False) timeout = getattr(settings, "ICE_REQUEST_TIMEOUT", None) if key_id and url: try: auth = HmacAuth(key_id=key_id, username=user_token) ice = IceApi(auth=auth, base_url=url, verify_ssl_cert=verify) if timeout: ice.timeout = timeout ice.write_enabled = True return ice except Exception as e: logger.error("Failed to create ICE connection: %s", e) return None
def handle(self, *args, **options): # get the username argument username = options['username'] # get ICE authentication configured via EDD's config files (secrets.env and / or # settings/local.py) auth = HmacAuth(key_id=settings.ICE_KEY_ID, username=username) ice = IceApi(auth, verify_ssl_cert=settings.VERIFY_ICE_CERT) ice.timeout = settings.ICE_REQUEST_TIMEOUT try: print('Contacting ICE at %s' % ice.base_url) part_id = options.get('part_id') if part_id: print('Requesting part "%s"' % part_id) entry = ice.get_entry(part_id) if entry: print('Found the part!') else: print('Part "%s" was not found in ICE' % part_id) else: entries_results_page = ice.search_entries() if entries_results_page: self.stdout.write( 'Successfully searched ICE for entries.\nICE reports %(total_entries)d ' 'total entries and returned the first page of %(returned_entries)d' % { 'total_entries': entries_results_page.total_result_count, 'returned_entries': entries_results_page.current_result_count, }) else: print( "No known error occurred, but also didn't find any entries in ICE. If " "user %s has any ICE entries visible to him/her, you might want to " "look into this." % username) except Exception as e: raise CommandError(e)
def handle(self, *args, **options): # get the username argument username = options["username"] # get ICE authentication configured via EDD's config files (secrets.env and / or # settings/local.py) auth = HmacAuth(key_id=settings.ICE_KEY_ID, username=username) ice = IceApi(auth, verify_ssl_cert=settings.ICE_VERIFY_CERT) ice.timeout = settings.ICE_REQUEST_TIMEOUT try: self.stdout.write("Contacting ICE at %s" % ice.base_url) part_id = options.get("part_id") if part_id: self.stdout.write('Requesting part "%s"' % part_id) entry = ice.get_entry(part_id) if entry: self.stdout.write("Found the part!") else: self.stdout.write(f'Part "{part_id}" was not found in ICE') else: search_term = options.get("term", "") if options.get("advanced", False): self.stdout.write( f'Searching ICE for term "{search_term}" (advanced search)' ) results_page = ice.search_entries(search_term) self.print_advanced_search(results_page, username) else: self.stdout.write( f'Searching ICE for term "{search_term}" ' f"(simple UI-facing search)") entry_info = ice.search(search_term) self.print_simple_search(entry_info) except Exception as e: raise CommandError(e)
def search_ice_as_action(self, request, queryset): # intentionally throw error when multiple users selected user = queryset.get() context = self.admin_site.each_context(request) auth = HmacAuth(key_id=settings.ICE_KEY_ID, username=user.email) ice = IceApi(auth=auth, verify_ssl_cert=settings.ICE_VERIFY_CERT) ice.timeout = settings.ICE_REQUEST_TIMEOUT results = [] term = request.POST.get("term", None) if term is not None: try: results = ice.search(term) except IceApiException: self.message_user( request, "Failed to execute search in ICE, check the ICE logs.", messages.ERROR, ) context.update(ice=ice.base_url, results=results, impersonate=user) return render(request, "admin/strain_impersonate_search.html", context=context)
def load_part_from_ice(self, registry_id): if self.existing_entry is not None: return self.existing_entry # using the Update to get the correct user for the search update = Update.load_update() user_email = update.mod_by.email try: auth = HmacAuth(key_id=settings.ICE_KEY_ID, username=user_email) verify = getattr(settings, "ICE_VERIFY_CERT", True) ice = IceApi(auth=auth, verify_ssl_cert=verify) ice.timeout = settings.ICE_REQUEST_TIMEOUT entry = ice.get_entry(registry_id) return entry except Exception as e: raise ValidationError( _("Failed to load strain %(uuid)s from ICE for user %(user)s"), code="ice failure", params={ "user": user_email, "uuid": registry_id }, ) from e
def load_part_from_ice(self, registry_id): update = Update.load_update() user_email = update.mod_by.email try: ice = IceApi( auth=HmacAuth(key_id=settings.ICE_KEY_ID, username=user_email), verify_ssl_cert=settings.ICE_VERIFY_CERT, ) ice.timeout = settings.ICE_REQUEST_TIMEOUT self.entry = ice.get_entry(registry_id) self.entry.url = f"{ice.base_url}/entry/{self.entry.id}" except Exception: logger.exception( "Exception loading part %(part_id)s from ICE for user " "%(user_email)s" % { "part_id": registry_id, "user_email": user_email }) raise ValidationError( _("Failed to load strain %(uuid)s from ICE"), code="ice failure", params={"uuid": registry_id}, )