Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

generic.py 6.9KB

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