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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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', 'excluded_products', 'classes',
  11. 'proxy_class')
  12. class RangeProductForm(forms.Form):
  13. query = forms.CharField(max_length=1024,
  14. label=_("Product SKUs or UPCs"),
  15. widget=forms.Textarea,
  16. required=False,
  17. help_text=_("You can paste in a selection of SKUs or UPCs"))
  18. file_upload = forms.FileField(label=_("File of SKUs or UPCs"), required=False,
  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(_("You must submit either a list of SKU/UPCs or a file"))
  27. return clean_data
  28. def clean_query(self):
  29. raw = self.cleaned_data['query']
  30. if not raw:
  31. return raw
  32. # Check that the search matches some products
  33. ids = set(re.compile(r'[\w-]+').findall(raw))
  34. products = self.range.included_products.all()
  35. existing_skus = set(products.values_list('stockrecord__partner_sku', flat=True))
  36. existing_upcs = set(products.values_list('upc', flat=True))
  37. existing_ids = existing_skus.union(existing_upcs)
  38. new_ids = ids - existing_ids
  39. if len(new_ids) == 0:
  40. raise forms.ValidationError(
  41. _("The products with SKUs or UPCs matching %s are already in this range") % (
  42. ', '.join(ids)))
  43. self.products = Product._default_manager.filter(
  44. Q(stockrecord__partner_sku__in=new_ids) |
  45. Q(upc__in=new_ids))
  46. if len(self.products) == 0:
  47. raise forms.ValidationError(_("No products exist with a SKU or UPC matching %s") % ", ".join(ids))
  48. found_skus = set(self.products.values_list('stockrecord__partner_sku', flat=True))
  49. found_upcs = set(self.products.values_list('upc', flat=True))
  50. found_ids = found_skus.union(found_upcs)
  51. self.missing_skus = new_ids - found_ids
  52. self.duplicate_skus = existing_ids.intersection(ids)
  53. return raw
  54. def get_products(self):
  55. return self.products if hasattr(self, 'products') else []
  56. def get_missing_skus(self):
  57. return self.missing_skus
  58. def get_duplicate_skus(self):
  59. return self.duplicate_skus