models.py 3.9 KB

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