models.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import os
  4. from django.db import models
  5. from django.core.validators import RegexValidator
  6. import requests
  7. from coin.configuration.models import Configuration
  8. FDN_ELIGIBILITY_URL = 'https://vador.fdn.fr/souscription/eligibilite.cgi'
  9. def convert_bandwidth(value):
  10. """Convert bandwidth expression to Int
  11. """
  12. units = {'k': 1000, 'M': 1000000}
  13. if value[-1] not in units:
  14. raise ValueError('Incorrect bandwidth value: {}'.format(value))
  15. return int(value[:-1]) * units[value[-1]]
  16. class FDNWhiteLabelError(Exception):
  17. pass
  18. class FDNWhiteLabel(Configuration):
  19. class Meta:
  20. verbose_name = 'marque blanche FDN'
  21. # If Django's default pluralisation is not satisfactory
  22. # verbose_name_plural = 'very many DSL lines'
  23. # URL namespace associated to this configuration type, to build URLs
  24. # in various view. Should also be defined in urls.py. Here, we don't
  25. # define any view, so there's no need for an URL namespace.
  26. url_namespace = "fdn"
  27. phone_number = models.CharField(max_length=20,
  28. verbose_name='phone number',
  29. help_text='Phone number associated to the '
  30. 'DSL line')
  31. address = models.TextField(
  32. verbose_name='adresse postale', blank=True, null=True)
  33. postal_code = models.CharField(max_length=5, blank=True, null=True,
  34. validators=[
  35. RegexValidator(regex=r'^\d{5}$',
  36. message='Code postal '
  37. 'non valide.')
  38. ],
  39. verbose_name='code postal')
  40. city = models.CharField(max_length=200, blank=True, null=True,
  41. verbose_name='commune')
  42. ppp_login = models.CharField(max_length=200, blank=True, null=True,
  43. verbose_name='identifiant PPP')
  44. ppp_password = models.CharField(max_length=200, blank=True, null=True,
  45. verbose_name='mot de passe PPP')
  46. fdn_option = models.CharField(max_length=10, verbose_name='Option chez'
  47. ' FDN', blank=True, null=True)
  48. _fdn_bundled_line = True
  49. def is_compatible_option(self, option):
  50. if self._fdn_bundled_line:
  51. return 'opt1' in option['code_offre']
  52. else:
  53. return 'opt1' not in option['code_offre']
  54. def check_eligibility(self):
  55. """sends a request to FDN in order to get DSL eligibility status for
  56. that number and postal code, and returns the best option, as a tuple of
  57. offer code, and bandwidth, or raises an exception if no offer is
  58. available.
  59. TODO: add caching
  60. """
  61. if not self.phone_number or not self.postal_code:
  62. raise AttributeError('Phone number and postal code must be '
  63. 'defined')
  64. cert_file = os.path.join(os.getcwd(), 'fdnwhitelabel',
  65. 'cacert.crt')
  66. request_params = {'tel': self.phone_number,
  67. 'cp': self.postal_code,
  68. 'etape': 'eligibilite',
  69. 'tarif': 'blanche'}
  70. eligibility = requests\
  71. .get(FDN_ELIGIBILITY_URL, params=request_params,
  72. verify=cert_file).json()
  73. if 'erreur' in eligibility and eligibility['erreur']:
  74. raise FDNWhiteLabelError(eligibility['message'])
  75. offers = [o for o in eligibility['offres']
  76. if self.is_compatible_option(o)]
  77. best_offer = sorted(offers,
  78. key=lambda o: convert_bandwidth(o['debit']))[-1]
  79. return (best_offer['code_offre'], best_offer['debit'])
  80. def __unicode__(self):
  81. return self.phone_number
  82. def subnet_event(self):
  83. # Do something with self.ip_subnet.all() here.
  84. pass
  85. class FDNUnbundledWhiteLabel(FDNWhiteLabel):
  86. _fdn_bundled_line = True