models.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. class Meta:
  75. managed = True
  76. db_table = 'contribs'
  77. verbose_name = 'contribution'
  78. PRIVACY_MAP = {
  79. 'name': 'privacy_name',
  80. 'comment': 'privacey_comment',
  81. 'floor': 'privacy_place_details',
  82. 'floor_total': 'privacy_place_details',
  83. 'orientations': 'privacy_place_details',
  84. 'roof': 'privacy_place_details',
  85. }
  86. PUBLIC_FIELDS = set(PRIVACY_MAP.keys())
  87. def __str__(self):
  88. return '#{} {}'.format(self.pk, self.name)
  89. def is_public(self):
  90. return not self.privacy_coordinates
  91. def _may_be_public(self, field):
  92. return field in self.PUBLIC_FIELDS
  93. def _is_public(self, field):
  94. return getattr(self, self.PRIVACY_MAP[field])
  95. def get_public_field(self, field):
  96. """ Gets safely an attribute in its public form (if any)
  97. :param field: The field name
  98. :return: the field value, or None, if the field is private
  99. """
  100. if self._may_be_public(field) and self._is_public(field):
  101. return getattr(self, field)
  102. else:
  103. return None