models.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 à internet'),
  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. floor = models.PositiveIntegerField(
  34. 'étage',
  35. blank=True, null=True)
  36. floor_total = models.PositiveIntegerField(
  37. "mombre d'étages",
  38. blank=True, null=True)
  39. orientations = CommaSeparatedCharField(
  40. blank=True, null=True, max_length=100)
  41. roof = models.BooleanField(
  42. 'accès au toît',
  43. default=False)
  44. comment = models.TextField(
  45. 'commentaire',
  46. blank=True, null=True)
  47. privacy_name = models.BooleanField(
  48. 'nom/pseudo public',
  49. default=False)
  50. privacy_email = models.BooleanField(
  51. 'email public',
  52. default=False)
  53. privacy_coordinates = models.BooleanField(
  54. 'coordonnées GPS publiques',
  55. default=True)
  56. privacy_place_details = models.BooleanField(
  57. 'étage/orientations publiques',
  58. default=True)
  59. privacy_comment = models.BooleanField(
  60. 'commentaire public',
  61. default=False)
  62. date = models.DateTimeField(auto_now_add=True)
  63. STATUS_TOSTUDY = 'TOSTUDY'
  64. STATUS_TOCONNECT = 'TOCONNECT'
  65. STATUS_CONNECTED = 'CONNECTED'
  66. STATUS_WONTCONNECT = 'WONTCONNECT'
  67. CONNECTABILITY = (
  68. (STATUS_TOSTUDY, 'à étudier'),
  69. (STATUS_TOCONNECT, 'à connecter'),
  70. (STATUS_CONNECTED, 'connecté'),
  71. (STATUS_WONTCONNECT, 'pas connectable'),
  72. )
  73. status = models.CharField(
  74. blank=True,
  75. null=True,
  76. max_length=250,
  77. choices=CONNECTABILITY,
  78. default=STATUS_TOSTUDY)
  79. class Meta:
  80. managed = True
  81. db_table = 'contribs'
  82. verbose_name = 'contribution'
  83. PRIVACY_MAP = {
  84. 'name': 'privacy_name',
  85. 'comment': 'privacy_comment',
  86. 'floor': 'privacy_place_details',
  87. 'floor_total': 'privacy_place_details',
  88. 'orientations': 'privacy_place_details',
  89. 'roof': 'privacy_place_details',
  90. 'angles': 'privacy_place_details',
  91. }
  92. PUBLIC_FIELDS = set(PRIVACY_MAP.keys())
  93. def __str__(self):
  94. return '#{} {}'.format(self.pk, self.name)
  95. @property
  96. def angles(self):
  97. """Return a list of (start, stop) angles from cardinal orientations
  98. """
  99. # Cleanup
  100. if self.orientations is None:
  101. return []
  102. orientations = [o for o in self.orientations if o in ANGLES.keys()]
  103. # Hack to make leaflet-semicircle happy (drawing a full circle only
  104. # works with (0, 360))
  105. if len(orientations) == 8:
  106. return [[0, 360]]
  107. angles = [ANGLES[orientation] for orientation in self.orientations]
  108. angles.sort(key=lambda i: i[0]) # sort by x
  109. return merge_intervals(angles)
  110. def is_public(self):
  111. return self.privacy_coordinates
  112. def _may_be_public(self, field):
  113. return field in self.PUBLIC_FIELDS
  114. def _is_public(self, field):
  115. return getattr(self, self.PRIVACY_MAP[field])
  116. def get_public_field(self, field):
  117. """ Gets safely an attribute in its public form (if any)
  118. :param field: The field name
  119. :return: the field value, or None, if the field is private
  120. """
  121. if self._may_be_public(field) and self._is_public(field):
  122. v = getattr(self, field)
  123. if hasattr(v, '__call__'):
  124. return v()
  125. else:
  126. return v
  127. else:
  128. return None