models.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from django.contrib.contenttypes.models import ContentType
  2. from django.db import models
  3. from django.http import HttpResponse
  4. from django.template import Template, Context
  5. from dcim.models import Site
  6. GRAPH_TYPE_INTERFACE = 100
  7. GRAPH_TYPE_PROVIDER = 200
  8. GRAPH_TYPE_SITE = 300
  9. GRAPH_TYPE_CHOICES = (
  10. (GRAPH_TYPE_INTERFACE, 'Interface'),
  11. (GRAPH_TYPE_PROVIDER, 'Provider'),
  12. (GRAPH_TYPE_SITE, 'Site'),
  13. )
  14. EXPORTTEMPLATE_MODELS = [
  15. 'site', 'rack', 'device', 'consoleport', 'powerport', 'interfaceconnection',
  16. 'aggregate', 'prefix', 'ipaddress', 'vlan',
  17. 'provider', 'circuit'
  18. ]
  19. class Graph(models.Model):
  20. type = models.PositiveSmallIntegerField(choices=GRAPH_TYPE_CHOICES)
  21. weight = models.PositiveSmallIntegerField(default=1000)
  22. name = models.CharField(max_length=100, verbose_name='Name')
  23. source = models.CharField(max_length=500, verbose_name='Source URL')
  24. link = models.URLField(verbose_name='Link URL', blank=True)
  25. class Meta:
  26. ordering = ['type', 'weight', 'name']
  27. def __unicode__(self):
  28. return self.name
  29. def embed_url(self, obj):
  30. template = Template(self.source)
  31. return template.render(Context({'obj': obj}))
  32. class ExportTemplate(models.Model):
  33. content_type = models.ForeignKey(ContentType, limit_choices_to={'model__in': EXPORTTEMPLATE_MODELS})
  34. name = models.CharField(max_length=200)
  35. template_code = models.TextField()
  36. mime_type = models.CharField(max_length=15, blank=True)
  37. file_extension = models.CharField(max_length=15, blank=True)
  38. class Meta:
  39. ordering = ['content_type', 'name']
  40. unique_together = [
  41. ['content_type', 'name']
  42. ]
  43. def __unicode__(self):
  44. return "{}: {}".format(self.content_type, self.name)
  45. def to_response(self, context_dict, filename):
  46. """
  47. Render the template to an HTTP response, delivered as a named file attachment
  48. """
  49. template = Template(self.template_code)
  50. mime_type = 'text/plain' if not self.mime_type else self.mime_type
  51. response = HttpResponse(
  52. template.render(Context(context_dict)),
  53. content_type=mime_type
  54. )
  55. if self.file_extension:
  56. filename += '.{}'.format(self.file_extension)
  57. response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
  58. return response
  59. class TopologyMap(models.Model):
  60. name = models.CharField(max_length=50, unique=True)
  61. slug = models.SlugField(unique=True)
  62. site = models.ForeignKey(Site, related_name='topology_maps', blank=True, null=True)
  63. device_patterns = models.TextField(help_text="Identify devices to include in the diagram using regular expressions,"
  64. "one per line. Each line will result in a new tier of the drawing. "
  65. "Separate multiple regexes on a line using commas. Devices will be "
  66. "rendered in the order they are defined.")
  67. description = models.CharField(max_length=100, blank=True)
  68. class Meta:
  69. ordering = ['name']
  70. def __unicode__(self):
  71. return self.name
  72. @property
  73. def device_sets(self):
  74. if not self.device_patterns:
  75. return None
  76. return [line.strip() for line in self.device_patterns.split('\n')]