models.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from __future__ import unicode_literals
  2. from django.db import models
  3. from .fields import CommaSeparatedCharField
  4. class Contrib(models.Model):
  5. CONTRIB_CONNECT = 'connect'
  6. CONTRIB_SHARE = 'share'
  7. id = models.AutoField(primary_key=True, blank=False, null=False)
  8. name = models.CharField(
  9. 'Nom / Pseudo',
  10. max_length=30)
  11. contrib_type = models.CharField(
  12. 'Type de contribution',
  13. max_length=10, choices=(
  14. (CONTRIB_CONNECT, 'Me raccorder au réseau expérimental'),
  15. (CONTRIB_SHARE, 'Partager une partie de ma connexion')
  16. ))
  17. latitude = models.FloatField(blank=True, null=True)
  18. longitude = models.FloatField(blank=True, null=True)
  19. phone = models.CharField(
  20. 'Téléphone',
  21. max_length=30, blank=True, default='')
  22. email = models.EmailField(blank=True)
  23. access_type = models.CharField(
  24. 'Type de connexion',
  25. max_length=10, blank=True, choices=(
  26. ('vdsl', 'ADSL'),
  27. ('vdsl', 'VDSL'),
  28. ('fiber', 'Fibre optique'),
  29. ('cable', 'Coaxial (FTTLA)'),
  30. ))
  31. connect_local = models.NullBooleanField(
  32. 'Accès internet',
  33. default=False, null=True)
  34. connect_internet = models.NullBooleanField(
  35. 'Services locaux',
  36. default=False, null=True)
  37. bandwidth = models.FloatField(
  38. 'débit total',
  39. blank=True, null=True)
  40. share_part = models.FloatField(
  41. 'débit partagé',
  42. blank=True, null=True)
  43. floor = models.PositiveIntegerField(
  44. 'étage',
  45. blank=True, null=True)
  46. floor_total = models.PositiveIntegerField(
  47. "mombre d'étages",
  48. blank=True, null=True)
  49. orientations = CommaSeparatedCharField(
  50. blank=True, null=True, max_length=100)
  51. roof = models.BooleanField(
  52. 'accès au toît',
  53. default=False)
  54. comment = models.TextField(
  55. 'commentaire',
  56. blank=True, null=True)
  57. privacy_name = models.BooleanField(
  58. 'nom/pseudo public',
  59. default=False)
  60. privacy_email = models.BooleanField(
  61. 'email public',
  62. default=False)
  63. privacy_coordinates = models.BooleanField(
  64. 'coordonnées GPS publiques',
  65. default=True)
  66. privacy_place_details = models.BooleanField(
  67. 'étage/orientations publiques',
  68. default=True)
  69. privacy_comment = models.BooleanField(
  70. 'commentaire public',
  71. default=False)
  72. date = models.DateTimeField(auto_now_add=True)
  73. class Meta:
  74. managed = True
  75. db_table = 'contribs'
  76. verbose_name = 'contribution'
  77. PRIVACY_MAP = {
  78. 'name': 'privacy_name',
  79. 'comment': 'privacey_comment',
  80. 'floor': 'privacy_place_details',
  81. 'floor_total': 'privacy_place_details',
  82. 'orientations': 'privacy_place_details',
  83. 'roof': 'privacy_place_details',
  84. }
  85. PUBLIC_FIELDS = set(PRIVACY_MAP.keys())
  86. def __str__(self):
  87. return '#{} {}'.format(self.pk, self.name)
  88. def is_public(self):
  89. return not self.privacy_coordinates
  90. def _may_be_public(self, field):
  91. return field in self.PUBLIC_FIELDS
  92. def _is_public(self, field):
  93. return getattr(self, self.PRIVACY_MAP[field])
  94. def get_public_field(self, field):
  95. """ Gets safely an attribute in its public form (if any)
  96. :param field: The field name
  97. :return: the field value, or None, if the field is private
  98. """
  99. if self._may_be_public(field) and self._is_public(field):
  100. return getattr(self, field)
  101. else:
  102. return None