Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_add_component(self):
out = StringIO()
self.addCleanup(out.close)
components = copy.deepcopy(DEFCON_COMPONENTS)
component = components['production']
with self.settings(DEFCON_COMPONENTS=components):
management.call_command("loadcomponents", stdout=out)
self.assertIn("Created %s" % component["name"], out.getvalue())
component_model = models.Component.objects.get(id="production")
self.assertEqual(component_model.name, component["name"])
self.assertEqual(component_model.description,
component["description"])
self.assertEqual(component_model.link, component["link"])
self.assertEqual(component_model.contact, component["contact"])
"""Admin classes for defcon.status."""
from django.contrib import admin
from defcon.status import models
@admin.register(models.Component)
class ComponentAdmin(admin.ModelAdmin):
"""Admin for Component."""
list_display = ('name', 'link', 'contact')
@admin.register(models.Plugin)
class PluginAdmin(admin.ModelAdmin):
"""Admin for Plugin."""
list_display = ('name', 'link')
@admin.register(models.PluginInstance)
class PluginInstanceAdmin(admin.ModelAdmin):
"""Admin for PluginInstance."""
def handle(self, *args, **options):
"""Run the command."""
existing_components = set(
models.Component.objects.all().values_list('id', flat=True))
updated_components = set()
components = sorted(settings.DEFCON_COMPONENTS.items())
for cid, component in components:
self.add_component(cid, component)
updated_components.add(cid)
removed_components = existing_components - updated_components
for cid in removed_components:
models.Component.objects.filter(id=cid).delete()
self.stdout.write('Removed %s' % cid)
"""Serializers for defcon.status."""
from rest_framework import serializers
from defcon.status import models
# Models for the full API.
class ComponentSerializer(serializers.HyperlinkedModelSerializer):
"""Serializer for Component."""
class Meta:
"""Configuration."""
model = models.Component
fields = '__all__'
class ComponentSimpleSerializer(ComponentSerializer):
"""Serializer for Component."""
class Meta(ComponentSerializer.Meta):
"""Configuration."""
fields = ['name', 'contact', 'link', 'defcon', 'description']
class PluginSerializer(serializers.HyperlinkedModelSerializer):
"""Serializer for Plugin."""
class Meta:
def status(request, component_id):
"""Display a specific component."""
component = shortcuts.get_object_or_404(models.Component, pk=component_id)
base_url = "{0}://{1}".format(request.scheme, request.get_host())
return {'component': component, 'base_url': base_url}
from defcon.status import models
from defcon.status import serializers
class DefConViewSet(viewsets.ReadOnlyModelViewSet):
"""API view for Component with expanded statuses."""
queryset = models.Component.objects.all()
serializer_class = serializers.ComponentFullSerializer
class SimpleViewSet(viewsets.ReadOnlyModelViewSet):
"""API view for Component with expanded statuses."""
queryset = models.Component.objects.all()
serializer_class = serializers.ComponentSimpleSerializer
class ComponentViewSet(viewsets.ModelViewSet):
"""API view for Component."""
queryset = models.Component.objects.all()
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
serializer_class = serializers.ComponentSerializer
class PluginViewSet(viewsets.ModelViewSet):
"""API view for Plugin."""
queryset = models.Plugin.objects.all()
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)