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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import re
  2. from django import forms
  3. from django.db.models import get_model, Q
  4. from django.utils.translation import ugettext_lazy as _
  5. Product = get_model('catalogue', 'Product')
  6. Range = get_model('offer', 'Range')
  7. class RangeForm(forms.ModelForm):
  8. class Meta:
  9. model = Range
  10. exclude = ('included_products', 'slug', 'excluded_products', 'classes',
  11. 'proxy_class')
  12. class RangeProductForm(forms.Form):
  13. query = forms.CharField(
  14. max_length=1024, label=_("Product SKUs or UPCs"),
  15. widget=forms.Textarea, required=False,
  16. help_text=_("You can paste in a selection of SKUs or UPCs"))
  17. file_upload = forms.FileField(
  18. label=_("File of SKUs or UPCs"), required=False, max_length=255,
  19. help_text=_('Either comma-separated, or one identifier per line'))
  20. def __init__(self, range, *args, **kwargs):
  21. self.range = range
  22. super(RangeProductForm, self).__init__(*args, **kwargs)
  23. def clean(self):
  24. clean_data = super(RangeProductForm, self).clean()
  25. if not clean_data.get('query') and not clean_data.get('file_upload'):
  26. raise forms.ValidationError(
  27. _("You must submit either a list of SKU/UPCs or a file"))
  28. return clean_data
  29. def clean_query(self):
  30. raw = self.cleaned_data['query']
  31. if not raw:
  32. return raw
  33. # Check that the search matches some products
  34. ids = set(re.compile(r'[\w-]+').findall(raw))
  35. products = self.range.included_products.all()
  36. existing_skus = set(products.values_list(
  37. 'stockrecords__partner_sku', flat=True))
  38. existing_upcs = set(products.values_list('upc', flat=True))
  39. existing_ids = existing_skus.union(existing_upcs)
  40. new_ids = ids - existing_ids
  41. if len(new_ids) == 0:
  42. raise forms.ValidationError(
  43. _("The products with SKUs or UPCs matching %s are already in this range") % (
  44. ', '.join(ids)))
  45. self.products = Product._default_manager.filter(
  46. Q(stockrecords__partner_sku__in=new_ids) |
  47. Q(upc__in=new_ids))
  48. if len(self.products) == 0:
  49. raise forms.ValidationError(
  50. _("No products exist with a SKU or UPC matching %s") % ", ".join(ids))
  51. found_skus = set(self.products.values_list(
  52. 'stockrecords__partner_sku', flat=True))
  53. found_upcs = set(self.products.values_list('upc', flat=True))
  54. found_ids = found_skus.union(found_upcs)
  55. self.missing_skus = new_ids - found_ids
  56. self.duplicate_skus = existing_ids.intersection(ids)
  57. return raw
  58. def get_products(self):
  59. return self.products if hasattr(self, 'products') else []
  60. def get_missing_skus(self):
  61. return self.missing_skus
  62. def get_duplicate_skus(self):
  63. return self.duplicate_skus