sites.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. from django.contrib.contenttypes.fields import GenericRelation
  2. from django.db import models
  3. from django.urls import reverse
  4. from mptt.models import MPTTModel, TreeForeignKey
  5. from taggit.managers import TaggableManager
  6. from timezone_field import TimeZoneField
  7. from dcim.choices import *
  8. from dcim.constants import *
  9. from dcim.fields import ASNField
  10. from extras.models import ChangeLoggedModel, CustomFieldModel, ObjectChange, TaggedItem
  11. from extras.utils import extras_features
  12. from utilities.fields import NaturalOrderingField
  13. from utilities.querysets import RestrictedQuerySet
  14. from utilities.mptt import TreeManager
  15. from utilities.utils import serialize_object
  16. __all__ = (
  17. 'Region',
  18. 'Site',
  19. )
  20. #
  21. # Regions
  22. #
  23. @extras_features('export_templates', 'webhooks')
  24. class Region(MPTTModel, ChangeLoggedModel):
  25. """
  26. Sites can be grouped within geographic Regions.
  27. """
  28. parent = TreeForeignKey(
  29. to='self',
  30. on_delete=models.CASCADE,
  31. related_name='children',
  32. blank=True,
  33. null=True,
  34. db_index=True
  35. )
  36. name = models.CharField(
  37. max_length=100,
  38. unique=True
  39. )
  40. slug = models.SlugField(
  41. max_length=100,
  42. unique=True
  43. )
  44. description = models.CharField(
  45. max_length=200,
  46. blank=True
  47. )
  48. objects = TreeManager()
  49. csv_headers = ['name', 'slug', 'parent', 'description']
  50. class MPTTMeta:
  51. order_insertion_by = ['name']
  52. def __str__(self):
  53. return self.name
  54. def get_absolute_url(self):
  55. return "{}?region={}".format(reverse('dcim:site_list'), self.slug)
  56. def to_csv(self):
  57. return (
  58. self.name,
  59. self.slug,
  60. self.parent.name if self.parent else None,
  61. self.description,
  62. )
  63. def get_site_count(self):
  64. return Site.objects.filter(
  65. Q(region=self) |
  66. Q(region__in=self.get_descendants())
  67. ).count()
  68. def to_objectchange(self, action):
  69. # Remove MPTT-internal fields
  70. return ObjectChange(
  71. changed_object=self,
  72. object_repr=str(self),
  73. action=action,
  74. object_data=serialize_object(self, exclude=['level', 'lft', 'rght', 'tree_id'])
  75. )
  76. #
  77. # Sites
  78. #
  79. @extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
  80. class Site(ChangeLoggedModel, CustomFieldModel):
  81. """
  82. A Site represents a geographic location within a network; typically a building or campus. The optional facility
  83. field can be used to include an external designation, such as a data center name (e.g. Equinix SV6).
  84. """
  85. name = models.CharField(
  86. max_length=100,
  87. unique=True
  88. )
  89. _name = NaturalOrderingField(
  90. target_field='name',
  91. max_length=100,
  92. blank=True
  93. )
  94. slug = models.SlugField(
  95. max_length=100,
  96. unique=True
  97. )
  98. status = models.CharField(
  99. max_length=50,
  100. choices=SiteStatusChoices,
  101. default=SiteStatusChoices.STATUS_ACTIVE
  102. )
  103. region = models.ForeignKey(
  104. to='dcim.Region',
  105. on_delete=models.SET_NULL,
  106. related_name='sites',
  107. blank=True,
  108. null=True
  109. )
  110. tenant = models.ForeignKey(
  111. to='tenancy.Tenant',
  112. on_delete=models.PROTECT,
  113. related_name='sites',
  114. blank=True,
  115. null=True
  116. )
  117. facility = models.CharField(
  118. max_length=50,
  119. blank=True,
  120. help_text='Local facility ID or description'
  121. )
  122. asn = ASNField(
  123. blank=True,
  124. null=True,
  125. verbose_name='ASN',
  126. help_text='32-bit autonomous system number'
  127. )
  128. time_zone = TimeZoneField(
  129. blank=True
  130. )
  131. description = models.CharField(
  132. max_length=200,
  133. blank=True
  134. )
  135. physical_address = models.CharField(
  136. max_length=200,
  137. blank=True
  138. )
  139. shipping_address = models.CharField(
  140. max_length=200,
  141. blank=True
  142. )
  143. latitude = models.DecimalField(
  144. max_digits=8,
  145. decimal_places=6,
  146. blank=True,
  147. null=True,
  148. help_text='GPS coordinate (latitude)'
  149. )
  150. longitude = models.DecimalField(
  151. max_digits=9,
  152. decimal_places=6,
  153. blank=True,
  154. null=True,
  155. help_text='GPS coordinate (longitude)'
  156. )
  157. contact_name = models.CharField(
  158. max_length=50,
  159. blank=True
  160. )
  161. contact_phone = models.CharField(
  162. max_length=20,
  163. blank=True
  164. )
  165. contact_email = models.EmailField(
  166. blank=True,
  167. verbose_name='Contact E-mail'
  168. )
  169. comments = models.TextField(
  170. blank=True
  171. )
  172. images = GenericRelation(
  173. to='extras.ImageAttachment'
  174. )
  175. tags = TaggableManager(through=TaggedItem)
  176. objects = RestrictedQuerySet.as_manager()
  177. csv_headers = [
  178. 'name', 'slug', 'status', 'region', 'tenant', 'facility', 'asn', 'time_zone', 'description', 'physical_address',
  179. 'shipping_address', 'latitude', 'longitude', 'contact_name', 'contact_phone', 'contact_email', 'comments',
  180. ]
  181. clone_fields = [
  182. 'status', 'region', 'tenant', 'facility', 'asn', 'time_zone', 'description', 'physical_address',
  183. 'shipping_address', 'latitude', 'longitude', 'contact_name', 'contact_phone', 'contact_email',
  184. ]
  185. class Meta:
  186. ordering = ('_name',)
  187. def __str__(self):
  188. return self.name
  189. def get_absolute_url(self):
  190. return reverse('dcim:site', args=[self.slug])
  191. def to_csv(self):
  192. return (
  193. self.name,
  194. self.slug,
  195. self.get_status_display(),
  196. self.region.name if self.region else None,
  197. self.tenant.name if self.tenant else None,
  198. self.facility,
  199. self.asn,
  200. self.time_zone,
  201. self.description,
  202. self.physical_address,
  203. self.shipping_address,
  204. self.latitude,
  205. self.longitude,
  206. self.contact_name,
  207. self.contact_phone,
  208. self.contact_email,
  209. self.comments,
  210. )
  211. def get_status_class(self):
  212. return SiteStatusChoices.CSS_CLASSES.get(self.status)