Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_form_kwargs(self):
catalogs = Catalog.objects.filter_current_site().filter_group(self.request.user)
form_kwargs = super().get_form_kwargs()
form_kwargs.update({
'catalogs': catalogs
})
return form_kwargs
def import_catalog(element):
try:
catalog = Catalog.objects.get(uri=element['uri'])
except Catalog.DoesNotExist:
log.info('Catalog not in db. Created with uri %s.', element['uri'])
catalog = Catalog()
catalog.uri_prefix = element['uri_prefix'] or ''
catalog.key = element['key'] or ''
catalog.comment = element['comment'] or ''
catalog.order = element['order']
for lang_code, lang_string, lang_field in get_languages():
set_lang_field(catalog, 'title', element, lang_code, lang_field)
try:
CatalogUniqueKeyValidator(catalog).validate()
except ValidationError as e:
log.info('Catalog not saving "%s" due to validation error (%s).', element['uri'], e)
pass
else:
Site, on_delete=models.CASCADE,
verbose_name=_('Site'),
help_text=_('The site this project belongs to (in a multi site setup).')
)
title = models.CharField(
max_length=256,
verbose_name=_('Title'),
help_text=_('The title for this project.')
)
description = models.TextField(
blank=True,
verbose_name=_('Description'),
help_text=_('A description for this project (optional).')
)
catalog = models.ForeignKey(
Catalog, related_name='+', on_delete=models.SET_NULL, null=True,
verbose_name=_('Catalog'),
help_text=_('The catalog which will be used for this project.')
)
tasks = models.ManyToManyField(
Task, blank=True,
verbose_name=_('Tasks'),
help_text=_('The tasks that will be used for this project.')
)
views = models.ManyToManyField(
View, blank=True,
verbose_name=_('Views'),
help_text=_('The views that will be used for this project.')
)
class Meta:
ordering = ('title', )
QuestionSetSerializer,
QuestionSerializer,
CatalogIndexSerializer,
SectionIndexSerializer,
QuestionSetIndexSerializer,
QuestionIndexSerializer,
CatalogNestedSerializer,
SectionNestedSerializer,
QuestionSetNestedSerializer,
QuestionNestedSerializer
)
class CatalogViewSet(ModelViewSet):
permission_classes = (HasModelPermission, )
queryset = Catalog.objects.all()
serializer_class = CatalogSerializer
filter_backends = (DjangoFilterBackend,)
filter_fields = (
'uri',
'key'
)
@action(detail=True)
def nested(self, request, pk):
queryset = get_object_or_404(Catalog, pk=pk)
serializer = CatalogNestedSerializer(queryset)
return Response(serializer.data)
@action(detail=False)
def index(self, request):
def handle(self, *args, **options):
for obj in Condition.objects.all():
self._set_uri_prefix(obj, options['uri_prefix'])
for obj in OptionSet.objects.all():
self._set_uri_prefix(obj, options['uri_prefix'])
for obj in Option.objects.all():
self._set_uri_prefix(obj, options['uri_prefix'])
for obj in AttributeEntity.objects.all():
self._set_uri_prefix(obj, options['uri_prefix'])
for obj in Catalog.objects.all():
self._set_uri_prefix(obj, options['uri_prefix'])
for obj in Section.objects.all():
self._set_uri_prefix(obj, options['uri_prefix'])
for obj in Subsection.objects.all():
self._set_uri_prefix(obj, options['uri_prefix'])
for obj in QuestionEntity.objects.all():
self._set_uri_prefix(obj, options['uri_prefix'])
for obj in Task.objects.all():
self._set_uri_prefix(obj, options['uri_prefix'])
for obj in View.objects.all():
self._set_uri_prefix(obj, options['uri_prefix'])
def import_project(user, root):
ns_map = get_ns_map(root)
project = Project()
project.title = root.find('title').text or ''
project.description = root.find('description').text or ''
project.created = root.find('created').text
project.site = Site.objects.get_current()
catalog = get_uri(root.find('catalog'), ns_map)
try:
project.catalog = Catalog.objects.all().get(uri=catalog)
except Catalog.DoesNotExist:
log.info('Catalog not in db. Created with uri %s', catalog)
project.catalog = Catalog.objects.all().first()
project.save()
# add user to project
membership = Membership(project=project, user=user, role='owner')
membership.save()
tasks_node = root.find('tasks')
if tasks_node is not None:
for task_node in tasks_node.findall('task'):
try:
task_uri = get_uri(task_node, ns_map)
project.tasks.add(Task.objects.get(uri=task_uri))
def import_section(element):
try:
section = Section.objects.get(uri=element['uri'])
except Section.DoesNotExist:
log.info('Section not in db. Created with uri %s.', element['uri'])
section = Section()
try:
section.catalog = Catalog.objects.get(uri=element['catalog'])
except Catalog.DoesNotExist:
log.info('Catalog not in db. Skipping.')
return
section.uri_prefix = element['uri_prefix'] or ''
section.key = element['key'] or ''
section.comment = element['comment'] or ''
section.order = element['order']
for lang_code, lang_string, lang_field in get_languages():
set_lang_field(section, 'title', element, lang_code, lang_field)
try:
SectionUniquePathValidator(section).validate()
except ValidationError as e:
log.info('Section not saving "%s" due to validation error (%s).', element['uri'], e)
def nested(self, request, pk):
queryset = get_object_or_404(Catalog, pk=pk)
serializer = CatalogNestedSerializer(queryset)
return Response(serializer.data)
def first(self, request, pk=None):
try:
catalog = Catalog.objects.get(pk=request.GET.get('catalog'))
questionset = QuestionSet.objects.order_by_catalog(catalog).first()
serializer = self.get_serializer(questionset)
return Response(serializer.data)
except Catalog.DoesNotExist:
raise NotFound()
def first(self, request, pk=None):
try:
catalog = Catalog.objects.get(pk=request.GET.get('catalog'))
questionset = QuestionSet.objects.order_by_catalog(catalog).first()
serializer = self.get_serializer(questionset)
return Response(serializer.data)
except Catalog.DoesNotExist:
raise NotFound()