Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import six
  2. from django.core import exceptions
  3. from oscar.apps.offer.models import Range, Condition, Benefit
  4. def _class_path(klass):
  5. return '%s.%s' % (klass.__module__, klass.__name__)
  6. def create_range(range_class):
  7. """
  8. Create a custom range instance from the passed range class
  9. This function creates the appropriate database record for this custom
  10. range, including setting the class path for the custom proxy class.
  11. """
  12. if not hasattr(range_class, 'name'):
  13. raise exceptions.ValidationError(
  14. "A custom range must have a name attribute")
  15. # Ensure range name is text (not ugettext wrapper)
  16. if range_class.name.__class__.__name__ == '__proxy__':
  17. raise exceptions.ValidationError(
  18. "Custom ranges must have text names (not ugettext proxies)")
  19. # In Django versions further than 1.6 it will be update_or_create
  20. # https://docs.djangoproject.com/en/dev/ref/models/querysets/#update-or-create # noqa
  21. values = {
  22. 'name': range_class.name,
  23. 'proxy_class': _class_path(range_class),
  24. }
  25. try:
  26. obj = Range.objects.get(**values)
  27. except Range.DoesNotExist:
  28. obj = Range(**values)
  29. else:
  30. # Using iteritems because the range could potentially be rather big
  31. for key, value in six.iteritems(values):
  32. setattr(obj, key, value)
  33. obj.save()
  34. return obj
  35. def create_condition(condition_class):
  36. """
  37. Create a custom condition instance
  38. """
  39. return Condition.objects.create(
  40. proxy_class=_class_path(condition_class))
  41. def create_benefit(benefit_class):
  42. """
  43. Create a custom benefit instance
  44. """
  45. # The custom benefit_class must override __str__ and description to
  46. # avoid a recursion error
  47. if benefit_class.description is Benefit.description:
  48. raise RuntimeError("Your custom benefit must implement its own "
  49. "'description' property")
  50. return Benefit.objects.create(
  51. proxy_class=_class_path(benefit_class))