models.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. STATUS_TOSTUDY = 'TOSTUDY'
  75. STATUS_TOCONNECT = 'TOCONNECT'
  76. STATUS_CONNECTED = 'CONNECTED'
  77. STATUS_WONTCONNECT = 'WONTCONNECT'
  78. CONNECTABILITY = (
  79. (STATUS_TOSTUDY,'à étudier'),
  80. (STATUS_TOCONNECT,'à connecter'),
  81. (STATUS_CONNECTED, 'connecté'),
  82. (STATUS_WONTCONNECT, 'pas connectable'),
  83. )
  84. status = models.CharField(
  85. blank=True,
  86. null=True,
  87. max_length= 250,
  88. choices=CONNECTABILITY,
  89. default=STATUS_TOSTUDY)
  90. class Meta:
  91. managed = True
  92. db_table = 'contribs'
  93. verbose_name = 'contribution'
  94. PRIVACY_MAP = {
  95. 'name': 'privacy_name',
  96. 'comment': 'privacy_comment',
  97. 'floor': 'privacy_place_details',
  98. 'floor_total': 'privacy_place_details',
  99. 'orientations': 'privacy_place_details',
  100. 'roof': 'privacy_place_details',
  101. }
  102. PUBLIC_FIELDS = set(PRIVACY_MAP.keys())
  103. def __str__(self):
  104. return '#{} {}'.format(self.pk, self.name)
  105. def is_public(self):
  106. return not self.privacy_coordinates
  107. def _may_be_public(self, field):
  108. return field in self.PUBLIC_FIELDS
  109. def _is_public(self, field):
  110. return getattr(self, self.PRIVACY_MAP[field])
  111. def get_public_field(self, field):
  112. """ Gets safely an attribute in its public form (if any)
  113. :param field: The field name
  114. :return: the field value, or None, if the field is private
  115. """
  116. if self._may_be_public(field) and self._is_public(field):
  117. return getattr(self, field)
  118. else:
  119. return None