Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def import_attribute(attribute_node, nsmap, parent=None):
uri = attribute_node[get_ns_tag('dc:uri', nsmap)].text
try:
attribute = Attribute.objects.get(uri=uri)
except Attribute.DoesNotExist:
attribute = Attribute()
attribute.parent = parent
attribute.uri_prefix = uri.split('/domain/')[0]
attribute.key = uri.split('/')[-1]
attribute.comment = attribute_node[get_ns_tag('dc:comment', nsmap)]
attribute.is_collection = attribute_node['is_collection'] == 'True'
attribute.value_type = attribute_node['value_type']
attribute.unit = attribute_node['unit']
attribute.save()
if hasattr(attribute_node, 'range'):
import_verbose_name(attribute_node.range, attribute)
if hasattr(attribute_node, 'verbosename'):
import_verbose_name(attribute_node.verbosename, attribute)
def import_attribute(attribute_node, nsmap, parent=None):
uri = attribute_node[get_ns_tag('dc:uri', nsmap)].text
try:
attribute = Attribute.objects.get(uri=uri)
except Attribute.DoesNotExist:
attribute = Attribute()
attribute.parent = parent
attribute.uri_prefix = uri.split('/domain/')[0]
attribute.key = uri.split('/')[-1]
attribute.comment = attribute_node[get_ns_tag('dc:comment', nsmap)]
attribute.is_collection = attribute_node['is_collection'] == 'True'
attribute.value_type = attribute_node['value_type']
attribute.unit = attribute_node['unit']
attribute.save()
if hasattr(attribute_node, 'range'):
import_verbose_name(attribute_node.range, attribute)
if hasattr(attribute_node, 'verbosename'):
import_verbose_name(attribute_node.verbosename, attribute)
blank=True,
verbose_name=_('Text (quaternary)'),
help_text=_('The text for this task in the quaternary language.')
)
text_lang5 = models.TextField(
blank=True,
verbose_name=_('Text (quinary)'),
help_text=_('The text for this task in the quinary language.')
)
start_attribute = models.ForeignKey(
Attribute, blank=True, null=True, on_delete=models.SET_NULL, related_name='+',
verbose_name=_('Start date attribute'),
help_text=_('The attribute that is setting the start date for this task.')
)
end_attribute = models.ForeignKey(
Attribute, blank=True, null=True, on_delete=models.SET_NULL, related_name='+',
verbose_name=_('End date attribute'),
help_text=_('The attribute that is setting the end date for this task (optional, if no end date attribute is given, the start date attribute sets also the end date).')
)
days_before = models.IntegerField(
blank=True, null=True,
verbose_name=_('Days before'),
help_text=_('Additional days before the start date.')
)
days_after = models.IntegerField(
blank=True, null=True,
verbose_name=_('Days after'),
help_text=_('Additional days after the end date.')
)
conditions = models.ManyToManyField(
Condition, blank=True,
verbose_name=_('Conditions'),
question = Question()
try:
question.questionset = QuestionSet.objects.get(uri=element['questionset'])
except QuestionSet.DoesNotExist:
log.info('QuestionSet not in db. Skipping.')
return
question.uri_prefix = element['uri_prefix'] or ''
question.key = element['key'] or ''
question.comment = element['comment'] or ''
if element['attribute']:
try:
question.attribute = Attribute.objects.get(uri=element['attribute'])
except Attribute.DoesNotExist:
pass
question.is_collection = element['is_collection']
question.order = element['order']
for lang_code, lang_string, lang_field in get_languages():
set_lang_field(question, 'text', element, lang_code, lang_field)
set_lang_field(question, 'help', element, lang_code, lang_field)
set_lang_field(question, 'verbose_name', element, lang_code, lang_field)
set_lang_field(question, 'verbose_name_plural', element, lang_code, lang_field)
question.widget_type = element['widget_type'] or ''
question.value_type = element['value_type'] or ''
question.maximum = element['maximum']
question.minimum = element['minimum']
question.step = element['step']
def get_id_attribute(self, obj):
try:
return {'id': obj.children.get(key='id').pk}
except Attribute.DoesNotExist:
return None
from django.contrib import admin
from .models import Attribute
class AttributeAdmin(admin.ModelAdmin):
list_display = ('uri', )
search_fields = ('uri', )
readonly_fields = ('uri', 'path')
admin.site.register(Attribute, AttributeAdmin)
from django_filters.rest_framework import DjangoFilterBackend
from rdmo.core.permissions import HasModelPermission
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from .models import Attribute
from .serializers.v1 import AttributeSerializer, NestedAttributeSerializer
class AttributeViewSet(ModelViewSet):
permission_classes = (HasModelPermission, )
queryset = Attribute.objects.order_by('path')
serializer_class = AttributeSerializer
filter_backends = (DjangoFilterBackend,)
filterset_fields = (
'uri',
'path',
'key',
'parent'
)
@action(detail=False)
def nested(self, request):
queryset = Attribute.objects.get_cached_trees()
serializer = NestedAttributeSerializer(queryset, many=True)
return Response(serializer.data)
def render_to_response(self, context, **response_kwargs):
format = self.kwargs.get('format')
if format == 'xml':
queryset = Attribute.objects.get_cached_trees()
serializer = ExportSerializer(queryset, many=True)
xmldata = XMLRenderer().render(serializer.data)
response = HttpResponse(prettify_xml(xmldata), content_type="application/xml")
response['Content-Disposition'] = 'filename="domain.xml"'
return response
elif format[:3] == 'csv':
if format == 'csvcomma':
delimiter = ','
else:
delimiter = ';'
rows = []
for attribute in context['attributes']:
rows.append((
attribute.key,
attribute.comment,
attribute.uri