You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generic.py 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import six
  2. import json
  3. from django import forms
  4. from django.core import validators
  5. from django.core.exceptions import ValidationError
  6. from django.utils.encoding import smart_str
  7. from django.contrib import messages
  8. from django.http import HttpResponseRedirect, HttpResponse
  9. from django.utils.translation import ugettext_lazy as _
  10. from django.views.generic.base import View
  11. import phonenumbers
  12. from oscar.core.phonenumber import PhoneNumber
  13. from six.moves import map
  14. class PostActionMixin(object):
  15. """
  16. Simple mixin to forward POST request that contain a key 'action'
  17. onto a method of form "do_{action}".
  18. This only works with DetailView
  19. """
  20. def post(self, request, *args, **kwargs):
  21. if 'action' in self.request.POST:
  22. model = self.get_object()
  23. # The do_* method is required to do what it needs to with the model
  24. # it is passed, and then to assign the HTTP response to
  25. # self.response.
  26. method_name = "do_%s" % self.request.POST['action'].lower()
  27. if hasattr(self, method_name):
  28. getattr(self, method_name)(model)
  29. return self.response
  30. else:
  31. messages.error(request, _("Invalid form submission"))
  32. return super(PostActionMixin, self).post(request, *args, **kwargs)
  33. class BulkEditMixin(object):
  34. """
  35. Mixin for views that have a bulk editing facility. This is normally in the
  36. form of tabular data where each row has a checkbox. The UI allows a number
  37. of rows to be selected and then some 'action' to be performed on them.
  38. """
  39. action_param = 'action'
  40. # Permitted methods that can be used to act on the selected objects
  41. actions = None
  42. current_view = None
  43. checkbox_object_name = None
  44. def get_checkbox_object_name(self):
  45. if self.checkbox_object_name:
  46. return self.checkbox_object_name
  47. return smart_str(self.model._meta.object_name.lower())
  48. def get_error_url(self, request):
  49. return request.META.get('HTTP_REFERER', '.')
  50. def get_success_url(self, request):
  51. return request.META.get('HTTP_REFERER', '.')
  52. def post(self, request, *args, **kwargs):
  53. # Dynamic dispatch pattern - we forward POST requests onto a method
  54. # designated by the 'action' parameter. The action has to be in a
  55. # whitelist to avoid security issues.
  56. action = request.POST.get(self.action_param, '').lower()
  57. if not self.actions or action not in self.actions:
  58. messages.error(self.request, _("Invalid action"))
  59. return HttpResponseRedirect(self.get_error_url(request))
  60. ids = request.POST.getlist(
  61. 'selected_%s' % self.get_checkbox_object_name())
  62. ids = list(map(int, ids))
  63. if not ids:
  64. messages.error(
  65. self.request,
  66. _("You need to select some %ss")
  67. % self.get_checkbox_object_name())
  68. return HttpResponseRedirect(self.get_error_url(request))
  69. objects = self.get_objects(ids)
  70. return getattr(self, action)(request, objects)
  71. def get_objects(self, ids):
  72. object_dict = self.get_object_dict(ids)
  73. # Rearrange back into the original order
  74. return [object_dict[id] for id in ids]
  75. def get_object_dict(self, ids):
  76. return self.get_queryset().in_bulk(ids)
  77. class ObjectLookupView(View):
  78. """Base view for json lookup for objects"""
  79. def get_query_set(self):
  80. return self.model.objects.all()
  81. def format_object(self, obj):
  82. return {
  83. 'id': obj.pk,
  84. 'text': six.text_type(obj),
  85. }
  86. def initial_filter(self, qs, value):
  87. return qs.filter(pk__in=value.split(','))
  88. def lookup_filter(self, qs, term):
  89. return qs
  90. def paginate(self, qs, page, page_limit):
  91. total = qs.count()
  92. start = (page - 1) * page_limit
  93. stop = start + page_limit
  94. qs = qs[start:stop]
  95. return qs, (page_limit * page < total)
  96. def get_args(self):
  97. GET = self.request.GET
  98. return (GET.get('initial', None),
  99. GET.get('q', None),
  100. int(GET.get('page', 1)),
  101. int(GET.get('page_limit', 20)))
  102. def get(self, request):
  103. self.request = request
  104. qs = self.get_query_set()
  105. initial, q, page, page_limit = self.get_args()
  106. if initial:
  107. qs = self.initial_filter(qs, initial)
  108. more = False
  109. else:
  110. if q:
  111. qs = self.lookup_filter(qs, q)
  112. qs, more = self.paginate(qs, page, page_limit)
  113. return HttpResponse(json.dumps({
  114. 'results': map(self.format_object, qs),
  115. 'more': more,
  116. }), mimetype='application/json')
  117. class PhoneNumberMixin(object):
  118. """
  119. Validation mixin for forms with a phone number, and optionally a country.
  120. It tries to validate the phone number, and on failure tries to validate it
  121. using a hint (the country provided), and treating it as a local number.
  122. It looks for ``self.country``, or ``self.fields['country'].queryset``
  123. """
  124. phone_number = forms.CharField(max_length=32, required=False)
  125. def clean_phone_number(self):
  126. number = self.cleaned_data['phone_number']
  127. # empty
  128. if number in validators.EMPTY_VALUES:
  129. return None
  130. # Check for an international phone format
  131. try:
  132. phone_number = PhoneNumber.from_string(number)
  133. except phonenumbers.NumberParseException:
  134. # Try hinting with the shipping country
  135. if hasattr(self.instance, 'country'):
  136. country = self.instance.country
  137. elif hasattr(self.fields.get('country'), 'queryset'):
  138. country = self.fields['country'].queryset[0]
  139. else:
  140. country = None
  141. if not country:
  142. # There is no shipping country, not a valid international
  143. # number
  144. raise ValidationError(
  145. _(u'This is not a valid international phone format.'))
  146. country = self.cleaned_data.get('country', country)
  147. region_code = country.iso_3166_1_a2
  148. # The PhoneNumber class does not allow specifying
  149. # the region. So we drop down to the underlying phonenumbers
  150. # library, which luckily allows parsing into a PhoneNumber
  151. # instance
  152. try:
  153. phone_number = PhoneNumber.from_string(number,
  154. region=region_code)
  155. if not phone_number.is_valid():
  156. raise ValidationError(
  157. _(u'This is not a valid local phone format for %s.')
  158. % country)
  159. except phonenumbers.NumberParseException:
  160. # Not a valid local or international phone number
  161. raise ValidationError(
  162. _(u'This is not a valid local or international phone'
  163. u' format.'))
  164. return phone_number