models.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.db import models
  4. from .fields import CommaSeparatedCharField
  5. from .utils import ANGLES, merge_intervals
  6. class Contrib(models.Model):
  7. CONTRIB_CONNECT = 'connect'
  8. CONTRIB_SHARE = 'share'
  9. id = models.AutoField(primary_key=True, blank=False, null=False)
  10. name = models.CharField(
  11. 'Nom / Pseudo',
  12. max_length=30)
  13. contrib_type = models.CharField(
  14. 'Type de contribution',
  15. max_length=10, choices=(
  16. (CONTRIB_CONNECT, 'Me raccorder au réseau expérimental'),
  17. (CONTRIB_SHARE, 'Partager une partie de ma connexion')
  18. ), default=None)
  19. latitude = models.FloatField()
  20. longitude = models.FloatField()
  21. phone = models.CharField(
  22. 'Téléphone',
  23. max_length=30, blank=True, default='')
  24. email = models.EmailField(blank=True)
  25. access_type = models.CharField(
  26. 'Type de connexion',
  27. max_length=10, blank=True, choices=(
  28. ('vdsl', 'ADSL'),
  29. ('vdsl', 'VDSL'),
  30. ('fiber', 'Fibre optique'),
  31. ('cable', 'Coaxial (FTTLA)'),
  32. ))
  33. connect_local = models.NullBooleanField(
  34. 'Accès internet',
  35. default=False, null=True)
  36. connect_internet = models.NullBooleanField(
  37. 'Services locaux',
  38. default=False, null=True)
  39. floor = models.PositiveIntegerField(
  40. 'étage',
  41. blank=True, null=True)
  42. floor_total = models.PositiveIntegerField(
  43. "mombre d'étages",
  44. blank=True, null=True)
  45. orientations = CommaSeparatedCharField(
  46. blank=True, null=True, max_length=100)
  47. roof = models.BooleanField(
  48. 'accès au toît',
  49. default=False)
  50. comment = models.TextField(
  51. 'commentaire',
  52. blank=True, null=True)
  53. privacy_name = models.BooleanField(
  54. 'nom/pseudo public',
  55. default=False)
  56. privacy_email = models.BooleanField(
  57. 'email public',
  58. default=False)
  59. privacy_coordinates = models.BooleanField(
  60. 'coordonnées GPS publiques',
  61. default=True)
  62. privacy_place_details = models.BooleanField(
  63. 'étage/orientations publiques',
  64. default=True)
  65. privacy_comment = models.BooleanField(
  66. 'commentaire public',
  67. default=False)
  68. date = models.DateTimeField(auto_now_add=True)
  69. STATUS_TOSTUDY = 'TOSTUDY'
  70. STATUS_TOCONNECT = 'TOCONNECT'
  71. STATUS_CONNECTED = 'CONNECTED'
  72. STATUS_WONTCONNECT = 'WONTCONNECT'
  73. CONNECTABILITY = (
  74. (STATUS_TOSTUDY, 'à étudier'),
  75. (STATUS_TOCONNECT, 'à connecter'),
  76. (STATUS_CONNECTED, 'connecté'),
  77. (STATUS_WONTCONNECT, 'pas connectable'),
  78. )
  79. status = models.CharField(
  80. blank=True,
  81. null=True,
  82. max_length=250,
  83. choices=CONNECTABILITY,
  84. default=STATUS_TOSTUDY)
  85. class Meta:
  86. managed = True
  87. db_table = 'contribs'
  88. verbose_name = 'contribution'
  89. PRIVACY_MAP = {
  90. 'name': 'privacy_name',
  91. 'comment': 'privacy_comment',
  92. 'floor': 'privacy_place_details',
  93. 'floor_total': 'privacy_place_details',
  94. 'orientations': 'privacy_place_details',
  95. 'roof': 'privacy_place_details',
  96. 'angles': 'privacy_place_details',
  97. }
  98. PUBLIC_FIELDS = set(PRIVACY_MAP.keys())
  99. def __str__(self):
  100. return '#{} {}'.format(self.pk, self.name)
  101. @property
  102. def angles(self):
  103. """Return a list of (start, stop) angles from cardinal orientations
  104. """
  105. # Cleanup
  106. if self.orientations is None:
  107. return []
  108. orientations = [o for o in self.orientations if o in ANGLES.keys()]
  109. # Hack to make leaflet-semicircle happy (drawing a full circle only
  110. # works with (0, 360))
  111. if len(orientations) == 8:
  112. return [[0, 360]]
  113. angles = [ANGLES[orientation] for orientation in self.orientations]
  114. angles.sort(key=lambda i: i[0]) # sort by x
  115. return merge_intervals(angles)
  116. def is_public(self):
  117. return self.privacy_coordinates
  118. def _may_be_public(self, field):
  119. return field in self.PUBLIC_FIELDS
  120. def _is_public(self, field):
  121. return getattr(self, self.PRIVACY_MAP[field])
  122. def get_public_field(self, field):
  123. """ Gets safely an attribute in its public form (if any)
  124. :param field: The field name
  125. :return: the field value, or None, if the field is private
  126. """
  127. if self._may_be_public(field) and self._is_public(field):
  128. v = getattr(self, field)
  129. if hasattr(v, '__call__'):
  130. return v()
  131. else:
  132. return v
  133. else:
  134. return None