models.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.db import models
  4. from django.utils import timezone
  5. from .fields import CommaSeparatedCharField
  6. from .utils import add_one_year, ANGLES, merge_intervals
  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=30, blank=True, default='')
  25. email = models.EmailField(blank=True)
  26. access_type = models.CharField(
  27. 'Type de connexion',
  28. max_length=10, blank=True, choices=(
  29. ('vdsl', 'ADSL'),
  30. ('vdsl', 'VDSL'),
  31. ('fiber', 'Fibre optique'),
  32. ('cable', 'Coaxial (FTTLA)'),
  33. ))
  34. floor = models.PositiveIntegerField(
  35. 'étage',
  36. blank=True, null=True)
  37. floor_total = models.PositiveIntegerField(
  38. "mombre d'étages",
  39. blank=True, null=True)
  40. orientations = CommaSeparatedCharField(
  41. blank=True, null=True, max_length=100)
  42. roof = models.BooleanField(
  43. 'accès au toît',
  44. default=False)
  45. comment = models.TextField(
  46. 'commentaire',
  47. blank=True, null=True)
  48. privacy_name = models.BooleanField(
  49. 'nom/pseudo public',
  50. default=False)
  51. privacy_email = models.BooleanField(
  52. 'email public',
  53. default=False)
  54. privacy_coordinates = models.BooleanField(
  55. 'coordonnées GPS publiques',
  56. default=True)
  57. privacy_place_details = models.BooleanField(
  58. 'étage/orientations publiques',
  59. default=True)
  60. privacy_comment = models.BooleanField(
  61. 'commentaire public',
  62. default=False)
  63. date = models.DateTimeField(
  64. "date d'enregistrement",
  65. auto_now_add=True)
  66. expiration_date = models.DateTimeField(
  67. "date d'expiration",
  68. null=True, blank=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 orientations]
  114. angles.sort(key=lambda i: i[0]) # sort by x
  115. return merge_intervals(angles)
  116. def get_postponed_expiration_date(self, from_date):
  117. """ Computes the new expiration date
  118. :param from_date: reference datetime frow where we add our extra delay.
  119. """
  120. return add_one_year(from_date)
  121. def clean(self):
  122. # usefull only for data imported from bottle version
  123. if not self.date:
  124. self.date = timezone.now()
  125. if not self.expiration_date:
  126. self.expiration_date = self.get_postponed_expiration_date(
  127. self.date)
  128. def save(self, *args, **kwargs):
  129. if not self.pk: # New instance
  130. self.date = timezone.now()
  131. self.expiration_date = self.get_postponed_expiration_date(
  132. self.date)
  133. super().save(*args, **kwargs)
  134. def is_public(self):
  135. return self.privacy_coordinates
  136. def _may_be_public(self, field):
  137. return field in self.PUBLIC_FIELDS
  138. def _is_public(self, field):
  139. return getattr(self, self.PRIVACY_MAP[field])
  140. def get_public_field(self, field):
  141. """ Gets safely an attribute in its public form (if any)
  142. :param field: The field name
  143. :return: the field value, or None, if the field is private
  144. """
  145. if self._may_be_public(field) and self._is_public(field):
  146. v = getattr(self, field)
  147. if hasattr(v, '__call__'):
  148. return v()
  149. else:
  150. return v
  151. else:
  152. return None