models.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # -*- coding: utf-8 -*-
  2. from django.db import models
  3. from polymorphic import PolymorphicModel
  4. from coin.offers.models import OfferSubscription
  5. """
  6. Implementation note : Configuration is a PolymorphicModel.
  7. The childs of Configuration are the differents models to store
  8. technical informations of a subscibtion.
  9. To add a new configuration backend, you have to create a new app with a model
  10. which inherit from Configuration.
  11. Your model can implement Meta verbose_name to have human readable name and a
  12. url_namespace variable to specify the url namespace used by this model.
  13. """
  14. class Configuration(PolymorphicModel):
  15. offersubscription = models.OneToOneField(OfferSubscription, blank=True,
  16. null=True,
  17. related_name='configuration',
  18. verbose_name='Abonnement')
  19. @staticmethod
  20. def get_configurations_choices_list():
  21. """
  22. Génère automatiquement la liste de choix possibles de configurations
  23. en fonction des classes enfants de Configuration
  24. """
  25. return tuple((x().__class__.__name__,x()._meta.verbose_name)
  26. for x in Configuration.__subclasses__())
  27. def model_name(self):
  28. return self.__class__.__name__
  29. model_name.short_description = 'Nom du modèle'
  30. def configuration_type_name(self):
  31. return self._meta.verbose_name
  32. configuration_type_name.short_description = 'Type'
  33. def get_absolute_url(self):
  34. """
  35. Renvoi l'URL d'accès à la page "details" de l'objet
  36. Une url doit être nommée "details"
  37. """
  38. from django.core.urlresolvers import reverse
  39. return reverse('%s:details' % self.get_url_namespace(),
  40. args=[str(self.id)])
  41. def get_url_namespace(self):
  42. """
  43. Renvoi le namespace utilisé par la configuration. Utilise en priorité
  44. celui définit dans la classe enfant dans url_namespace sinon
  45. par défaut utilise le nom de la classe en minuscule
  46. """
  47. if self.url_namespace:
  48. return self.url_namespace
  49. else:
  50. return self.model_name().lower()