diff --git a/label_studio/projects/api.py b/label_studio/projects/api.py index 48f76bbf3083..91a44daf359f 100644 --- a/label_studio/projects/api.py +++ b/label_studio/projects/api.py @@ -786,30 +786,36 @@ def perform_create(self, serializer): self.request.user.active_organization, project, WebhookAction.TASKS_CREATED, [instance] ) return instance + + +def read_templates_and_groups(): + annotation_templates_dir = find_dir('annotation_templates') + configs = [] + for config_file in pathlib.Path(annotation_templates_dir).glob('**/*.yml'): + config = read_yaml(config_file) + if settings.VERSION_EDITION == 'Community': + if settings.VERSION_EDITION.lower() != config.get('type', 'community'): + continue + if config.get('image', '').startswith('/static') and settings.HOSTNAME: + # if hostname set manually, create full image urls + config['image'] = settings.HOSTNAME + config['image'] + configs.append(config) + template_groups_file = find_file(os.path.join('annotation_templates', 'groups.txt')) + with open(template_groups_file, encoding='utf-8') as f: + groups = f.read().splitlines() + logger.debug(f'{len(configs)} templates found.') + return {'templates': configs, 'groups': groups} class TemplateListAPI(generics.ListAPIView): parser_classes = (JSONParser, FormParser, MultiPartParser) permission_required = all_permissions.projects_view swagger_schema = None + # load this once in memory for performance + templates_and_groups = read_templates_and_groups() def list(self, request, *args, **kwargs): - annotation_templates_dir = find_dir('annotation_templates') - configs = [] - for config_file in pathlib.Path(annotation_templates_dir).glob('**/*.yml'): - config = read_yaml(config_file) - if settings.VERSION_EDITION == 'Community': - if settings.VERSION_EDITION.lower() != config.get('type', 'community'): - continue - if config.get('image', '').startswith('/static') and settings.HOSTNAME: - # if hostname set manually, create full image urls - config['image'] = settings.HOSTNAME + config['image'] - configs.append(config) - template_groups_file = find_file(os.path.join('annotation_templates', 'groups.txt')) - with open(template_groups_file, encoding='utf-8') as f: - groups = f.read().splitlines() - logger.debug(f'{len(configs)} templates found.') - return Response({'templates': configs, 'groups': groups}) + return Response(self.templates_and_groups) class ProjectSampleTask(generics.RetrieveAPIView):