Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'editor': 200, 'reviewer': 200, 'reviewer': 200, 'api': 200, 'user': 200, 'anonymous': 403
}
}
class OptionSetTests(TestListViewsetMixin, TestRetrieveViewsetMixin, DomainTestCase):
instances = OptionSet.objects.all()
url_names = {
'viewset': 'internal-domain:optionset'
}
class ConditionTests(TestListViewsetMixin, TestRetrieveViewsetMixin, DomainTestCase):
instances = Condition.objects.all()
url_names = {
'viewset': 'internal-domain:condition'
}
class DomainExportTests(TestExportViewMixin, DomainTestCase):
url_names = {
'list_view': 'domain',
'export_view': 'domain_export'
}
export_formats = ('xml', 'html', 'rtf', 'csv')
class DomainImportTests(TestImportViewMixin, TestCase):
entity.parent = parent
entity.uri_prefix = uri.split('/domain/')[0]
entity.key = uri.split('/')[-1]
entity.comment = entity_node[get_ns_tag('dc:comment', nsmap)]
entity.is_collection = entity_node['is_collection'] == 'True'
entity.save()
if hasattr(entity_node, 'verbosename'):
import_verbose_name(entity_node.verbosename, entity)
if hasattr(entity_node, 'conditions'):
for condition_node in entity_node.conditions.iterchildren():
try:
condition_uri = condition_node.get(get_ns_tag('dc:uri', nsmap))
condition = Condition.objects.get(uri=condition_uri)
entity.conditions.add(condition)
except Condition.DoesNotExist:
pass
if hasattr(entity_node, 'children'):
for child_node in entity_node.children.iterchildren():
if child_node.tag == 'entity':
import_attribute_entity(child_node, nsmap, parent=entity)
else:
import_attribute(child_node, nsmap, parent=entity)
from django_filters.rest_framework import DjangoFilterBackend
from rdmo.core.permissions import HasModelPermission
from rdmo.core.views import ChoicesViewSet
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from .models import Condition
from .serializers.v1 import ConditionIndexSerializer, ConditionSerializer
class ConditionViewSet(ModelViewSet):
permission_classes = (HasModelPermission, )
queryset = Condition.objects.all()
serializer_class = ConditionSerializer
filter_backends = (DjangoFilterBackend,)
filterset_fields = (
'uri',
'key',
'source',
'relation',
'target_text',
'target_option'
)
@action(detail=False)
def index(self, request):
queryset = self.get_queryset()
serializer = ConditionIndexSerializer(queryset, many=True)
try:
TaskUniqueKeyValidator(task).validate()
except ValidationError as e:
log.info('Task not saving "%s" due to validation error (%s).', element['uri'], e)
pass
else:
log.info('Task saving to "%s".', element['uri'])
task.save()
task.sites.add(Site.objects.get_current())
task.conditions.clear()
if element['conditions'] is not None:
for condition in element['conditions']:
try:
task.conditions.add(Condition.objects.get(uri=condition))
except Condition.DoesNotExist:
pass
def import_condition(element):
try:
condition = Condition.objects.get(uri=element['uri'])
except Condition.DoesNotExist:
log.info('Condition not in db. Created with uri %s.', element['uri'])
condition = Condition()
condition.uri_prefix = element['uri_prefix'] or ''
condition.key = element['key'] or ''
condition.comment = element['comment'] or ''
condition.source = None
if element['source']:
try:
condition.source = Attribute.objects.get(uri=element['source'])
except Attribute.DoesNotExist:
pass
condition.relation = element['relation']
import_verbose_name(attribute_node.verbosename, attribute)
if hasattr(attribute_node, 'optionsets'):
for optionset_node in attribute_node.optionsets.iterchildren():
try:
optionset_uri = optionset_node.get(get_ns_tag('dc:uri', nsmap))
optionset = OptionSet.objects.get(uri=optionset_uri)
attribute.optionsets.add(optionset)
except OptionSet.DoesNotExist:
pass
if hasattr(attribute_node, 'conditions'):
for condition_node in attribute_node.conditions.iterchildren():
try:
condition_uri = condition_node.get(get_ns_tag('dc:uri', nsmap))
condition = Condition.objects.get(uri=condition_uri)
attribute.conditions.add(condition)
except Condition.DoesNotExist:
pass
# TODO: finalize timeframe
timeframe = TimeFrame()
timeframe_node = task_node.find('timeframe')
timeframe.start_attribute = timeframe_node.find('start_attribute').get(get_ns_tag('dc:uri', nsmap))
timeframe.end_attribute = timeframe_node.find('end_attribute').get(get_ns_tag('dc:uri', nsmap))
timeframe.days_before = get_value_from_treenode(timeframe_node, 'days_before')
timeframe.days_after = get_value_from_treenode(timeframe_node, 'days_after')
# log.info(nsmap)
# log.info(str(start_attribute) + " - " + str(end_attribute) + " - " + str(days_before) + " - " + str(days_after))
# timeframe.save()
if hasattr(task_node, 'conditions'):
for condition_node in task_node.find('condition').findall('conditions'):
try:
condition_uri = get_uri(condition_node, nsmap)
condition = Condition.objects.get(uri=condition_uri)
task.conditions.add(condition)
except Condition.DoesNotExist:
pass
set_lang_field(questionset, 'verbose_name_plural', element, lang_code, lang_field)
try:
QuestionSetUniquePathValidator(questionset).validate()
except ValidationError as e:
log.info('QuestionSet not saving "%s" due to validation error (%s).', element['uri'], e)
pass
else:
log.info('QuestionSet saving to "%s".', element['uri'])
questionset.save()
questionset.conditions.clear()
if element['conditions'] is not None:
for condition in element['conditions']:
try:
questionset.conditions.add(Condition.objects.get(uri=condition))
except Condition.DoesNotExist:
pass
question.unit = element['unit'] or ''
try:
QuestionUniquePathValidator(question).validate()
except ValidationError as e:
log.info('Question not saving "%s" due to validation error (%s).', element['uri'], e)
pass
else:
log.info('Question saving to "%s".', element['uri'])
question.save()
question.conditions.clear()
if element['conditions'] is not None:
for condition in element['conditions']:
try:
question.conditions.add(Condition.objects.get(uri=condition))
except Condition.DoesNotExist:
pass
question.optionsets.clear()
if element['optionsets'] is not None:
for condition in element['optionsets']:
try:
question.optionsets.add(OptionSet.objects.get(uri=condition))
except OptionSet.DoesNotExist:
pass
def render(self, project, snapshot=None):
# # get list of conditions
conditions = {}
for condition in Condition.objects.all():
conditions[condition.key] = condition.resolve(project, snapshot)
# get all values for this snapshot and put them in a dict labled by the values attibute path
values = {}
for value in project.values.filter(snapshot=snapshot):
if value.attribute:
attribute_path = value.attribute.path
set_index = value.set_index
# create entry for this values attribute in the values_dict
if attribute_path not in values:
values[attribute_path] = []
# add this value to the values
try:
values[attribute_path][set_index].append(value)