sites.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. from django.contrib.contenttypes.fields import GenericRelation
  2. from django.core.exceptions import ValidationError
  3. from django.db import models
  4. from django.urls import reverse
  5. from mptt.models import TreeForeignKey
  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.utils import extras_features
  11. from netbox.models import NestedGroupModel, PrimaryModel
  12. from utilities.fields import NaturalOrderingField
  13. from utilities.querysets import RestrictedQuerySet
  14. __all__ = (
  15. 'Location',
  16. 'Region',
  17. 'Site',
  18. 'SiteGroup',
  19. )
  20. #
  21. # Regions
  22. #
  23. @extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
  24. class Region(NestedGroupModel):
  25. """
  26. A region represents a geographic collection of sites. For example, you might create regions representing countries,
  27. states, and/or cities. Regions are recursively nested into a hierarchy: all sites belonging to a child region are
  28. also considered to be members of its parent and ancestor region(s).
  29. """
  30. parent = TreeForeignKey(
  31. to='self',
  32. on_delete=models.CASCADE,
  33. related_name='children',
  34. blank=True,
  35. null=True,
  36. db_index=True
  37. )
  38. name = models.CharField(
  39. max_length=100
  40. )
  41. slug = models.SlugField(
  42. max_length=100
  43. )
  44. description = models.CharField(
  45. max_length=200,
  46. blank=True
  47. )
  48. # Generic relations
  49. vlan_groups = GenericRelation(
  50. to='ipam.VLANGroup',
  51. content_type_field='scope_type',
  52. object_id_field='scope_id',
  53. related_query_name='region'
  54. )
  55. contacts = GenericRelation(
  56. to='tenancy.ContactAssignment'
  57. )
  58. class Meta:
  59. unique_together = (
  60. ('parent', 'name'),
  61. ('parent', 'slug'),
  62. )
  63. def get_absolute_url(self):
  64. return reverse('dcim:region', args=[self.pk])
  65. def get_site_count(self):
  66. return Site.objects.filter(
  67. Q(region=self) |
  68. Q(region__in=self.get_descendants())
  69. ).count()
  70. #
  71. # Site groups
  72. #
  73. @extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
  74. class SiteGroup(NestedGroupModel):
  75. """
  76. A site group is an arbitrary grouping of sites. For example, you might have corporate sites and customer sites; and
  77. within corporate sites you might distinguish between offices and data centers. Like regions, site groups can be
  78. nested recursively to form a hierarchy.
  79. """
  80. parent = TreeForeignKey(
  81. to='self',
  82. on_delete=models.CASCADE,
  83. related_name='children',
  84. blank=True,
  85. null=True,
  86. db_index=True
  87. )
  88. name = models.CharField(
  89. max_length=100
  90. )
  91. slug = models.SlugField(
  92. max_length=100
  93. )
  94. description = models.CharField(
  95. max_length=200,
  96. blank=True
  97. )
  98. # Generic relations
  99. vlan_groups = GenericRelation(
  100. to='ipam.VLANGroup',
  101. content_type_field='scope_type',
  102. object_id_field='scope_id',
  103. related_query_name='site_group'
  104. )
  105. contacts = GenericRelation(
  106. to='tenancy.ContactAssignment'
  107. )
  108. class Meta:
  109. unique_together = (
  110. ('parent', 'name'),
  111. ('parent', 'slug'),
  112. )
  113. def get_absolute_url(self):
  114. return reverse('dcim:sitegroup', args=[self.pk])
  115. def get_site_count(self):
  116. return Site.objects.filter(
  117. Q(group=self) |
  118. Q(group__in=self.get_descendants())
  119. ).count()
  120. #
  121. # Sites
  122. #
  123. @extras_features('custom_fields', 'custom_links', 'export_templates', 'tags', 'webhooks')
  124. class Site(PrimaryModel):
  125. """
  126. A Site represents a geographic location within a network; typically a building or campus. The optional facility
  127. field can be used to include an external designation, such as a data center name (e.g. Equinix SV6).
  128. """
  129. name = models.CharField(
  130. max_length=100,
  131. unique=True
  132. )
  133. _name = NaturalOrderingField(
  134. target_field='name',
  135. max_length=100,
  136. blank=True
  137. )
  138. slug = models.SlugField(
  139. max_length=100,
  140. unique=True
  141. )
  142. status = models.CharField(
  143. max_length=50,
  144. choices=SiteStatusChoices,
  145. default=SiteStatusChoices.STATUS_ACTIVE
  146. )
  147. region = models.ForeignKey(
  148. to='dcim.Region',
  149. on_delete=models.SET_NULL,
  150. related_name='sites',
  151. blank=True,
  152. null=True
  153. )
  154. group = models.ForeignKey(
  155. to='dcim.SiteGroup',
  156. on_delete=models.SET_NULL,
  157. related_name='sites',
  158. blank=True,
  159. null=True
  160. )
  161. tenant = models.ForeignKey(
  162. to='tenancy.Tenant',
  163. on_delete=models.PROTECT,
  164. related_name='sites',
  165. blank=True,
  166. null=True
  167. )
  168. facility = models.CharField(
  169. max_length=50,
  170. blank=True,
  171. help_text='Local facility ID or description'
  172. )
  173. time_zone = TimeZoneField(
  174. blank=True
  175. )
  176. description = models.CharField(
  177. max_length=200,
  178. blank=True
  179. )
  180. physical_address = models.CharField(
  181. max_length=200,
  182. blank=True
  183. )
  184. shipping_address = models.CharField(
  185. max_length=200,
  186. blank=True
  187. )
  188. latitude = models.DecimalField(
  189. max_digits=8,
  190. decimal_places=6,
  191. blank=True,
  192. null=True,
  193. help_text='GPS coordinate (latitude)'
  194. )
  195. longitude = models.DecimalField(
  196. max_digits=9,
  197. decimal_places=6,
  198. blank=True,
  199. null=True,
  200. help_text='GPS coordinate (longitude)'
  201. )
  202. contact_name = models.CharField(
  203. max_length=50,
  204. blank=True
  205. )
  206. contact_phone = models.CharField(
  207. max_length=20,
  208. blank=True
  209. )
  210. contact_email = models.EmailField(
  211. blank=True,
  212. verbose_name='Contact E-mail'
  213. )
  214. comments = models.TextField(
  215. blank=True
  216. )
  217. # Generic relations
  218. vlan_groups = GenericRelation(
  219. to='ipam.VLANGroup',
  220. content_type_field='scope_type',
  221. object_id_field='scope_id',
  222. related_query_name='site'
  223. )
  224. contacts = GenericRelation(
  225. to='tenancy.ContactAssignment'
  226. )
  227. images = GenericRelation(
  228. to='extras.ImageAttachment'
  229. )
  230. objects = RestrictedQuerySet.as_manager()
  231. clone_fields = [
  232. 'status', 'region', 'group', 'tenant', 'facility', 'time_zone', 'description', 'physical_address',
  233. 'shipping_address', 'latitude', 'longitude', 'contact_name', 'contact_phone', 'contact_email',
  234. ]
  235. class Meta:
  236. ordering = ('_name',)
  237. def __str__(self):
  238. return self.name
  239. def get_absolute_url(self):
  240. return reverse('dcim:site', args=[self.pk])
  241. def get_status_class(self):
  242. return SiteStatusChoices.CSS_CLASSES.get(self.status)
  243. #
  244. # Locations
  245. #
  246. @extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')
  247. class Location(NestedGroupModel):
  248. """
  249. A Location represents a subgroup of Racks and/or Devices within a Site. A Location may represent a building within a
  250. site, or a room within a building, for example.
  251. """
  252. name = models.CharField(
  253. max_length=100
  254. )
  255. slug = models.SlugField(
  256. max_length=100
  257. )
  258. site = models.ForeignKey(
  259. to='dcim.Site',
  260. on_delete=models.CASCADE,
  261. related_name='locations'
  262. )
  263. parent = TreeForeignKey(
  264. to='self',
  265. on_delete=models.CASCADE,
  266. related_name='children',
  267. blank=True,
  268. null=True,
  269. db_index=True
  270. )
  271. tenant = models.ForeignKey(
  272. to='tenancy.Tenant',
  273. on_delete=models.PROTECT,
  274. related_name='locations',
  275. blank=True,
  276. null=True
  277. )
  278. description = models.CharField(
  279. max_length=200,
  280. blank=True
  281. )
  282. # Generic relations
  283. vlan_groups = GenericRelation(
  284. to='ipam.VLANGroup',
  285. content_type_field='scope_type',
  286. object_id_field='scope_id',
  287. related_query_name='location'
  288. )
  289. contacts = GenericRelation(
  290. to='tenancy.ContactAssignment'
  291. )
  292. images = GenericRelation(
  293. to='extras.ImageAttachment'
  294. )
  295. clone_fields = ['site', 'parent', 'description']
  296. class Meta:
  297. ordering = ['site', 'name']
  298. unique_together = ([
  299. ('site', 'parent', 'name'),
  300. ('site', 'parent', 'slug'),
  301. ])
  302. def get_absolute_url(self):
  303. return reverse('dcim:location', args=[self.pk])
  304. def clean(self):
  305. super().clean()
  306. # Parent Location (if any) must belong to the same Site
  307. if self.parent and self.parent.site != self.site:
  308. raise ValidationError(f"Parent location ({self.parent}) must belong to the same site ({self.site})")