models.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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(auto_now_add=True)
  64. expiration_date = models.DateTimeField(
  65. "date d'expiration",
  66. null=True, blank=True)
  67. STATUS_TOSTUDY = 'TOSTUDY'
  68. STATUS_TOCONNECT = 'TOCONNECT'
  69. STATUS_CONNECTED = 'CONNECTED'
  70. STATUS_WONTCONNECT = 'WONTCONNECT'
  71. CONNECTABILITY = (
  72. (STATUS_TOSTUDY, 'à étudier'),
  73. (STATUS_TOCONNECT, 'à connecter'),
  74. (STATUS_CONNECTED, 'connecté'),
  75. (STATUS_WONTCONNECT, 'pas connectable'),
  76. )
  77. status = models.CharField(
  78. blank=True,
  79. null=True,
  80. max_length=250,
  81. choices=CONNECTABILITY,
  82. default=STATUS_TOSTUDY)
  83. class Meta:
  84. managed = True
  85. db_table = 'contribs'
  86. verbose_name = 'contribution'
  87. PRIVACY_MAP = {
  88. 'name': 'privacy_name',
  89. 'comment': 'privacy_comment',
  90. 'floor': 'privacy_place_details',
  91. 'floor_total': 'privacy_place_details',
  92. 'orientations': 'privacy_place_details',
  93. 'roof': 'privacy_place_details',
  94. 'angles': 'privacy_place_details',
  95. }
  96. PUBLIC_FIELDS = set(PRIVACY_MAP.keys())
  97. def __str__(self):
  98. return '#{} {}'.format(self.pk, self.name)
  99. @property
  100. def angles(self):
  101. """Return a list of (start, stop) angles from cardinal orientations
  102. """
  103. # Cleanup
  104. if self.orientations is None:
  105. return []
  106. orientations = [o for o in self.orientations if o in ANGLES.keys()]
  107. # Hack to make leaflet-semicircle happy (drawing a full circle only
  108. # works with (0, 360))
  109. if len(orientations) == 8:
  110. return [[0, 360]]
  111. angles = [ANGLES[orientation] for orientation in orientations]
  112. angles.sort(key=lambda i: i[0]) # sort by x
  113. return merge_intervals(angles)
  114. def get_postponed_expiration_date(self, from_date):
  115. """ Computes the new expiration date
  116. :param from_date: reference datetime frow where we add our extra delay.
  117. """
  118. return add_one_year(from_date)
  119. def clean(self):
  120. # usefull only for data imported from bottle version
  121. if not self.date:
  122. self.date = timezone.now()
  123. if not self.expiration_date:
  124. self.expiration_date = self.get_postponed_expiration_date(
  125. self.date)
  126. def save(self, *args, **kwargs):
  127. if not self.pk: # New instance
  128. self.date = timezone.now()
  129. self.expiration_date = self.get_postponed_expiration_date(
  130. self.date)
  131. super().save(*args, **kwargs)
  132. def is_public(self):
  133. return self.privacy_coordinates
  134. def _may_be_public(self, field):
  135. return field in self.PUBLIC_FIELDS
  136. def _is_public(self, field):
  137. return getattr(self, self.PRIVACY_MAP[field])
  138. def get_public_field(self, field):
  139. """ Gets safely an attribute in its public form (if any)
  140. :param field: The field name
  141. :return: the field value, or None, if the field is private
  142. """
  143. if self._may_be_public(field) and self._is_public(field):
  144. v = getattr(self, field)
  145. if hasattr(v, '__call__'):
  146. return v()
  147. else:
  148. return v
  149. else:
  150. return None