models.py 4.5 KB

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