from ckeditor.widgets import CKEditorWidget from crispy_forms.layout import Layout, Field, Button, Div, HTML from django import forms from crispy_forms.helper import FormHelper from django.forms import ALL_FIELDS from django.urls import reverse_lazy from django.utils.translation import ugettext_lazy as _ from core.forms import QueryFormBase from core.utils import parse_path from products.templatetags.products_filters import apply_query_params from .models import Product, Manufacturer class ProductSearchForm(QueryFormBase, forms.ModelForm): field_template = 'bootstrap/forms/product_search.html' submit_css_class = None def __init__(self, *args, **kwargs): self.submit_css_class = kwargs.pop('submit_css_class') self.helper = FormHelper() self.helper.form_method = 'get' self.helper.layout = Layout( Field('name', template=self.field_template, placeholder="Поиск программы..."), Button(_('search'), value="search", css_class=self.submit_css_class, template=self.field_template) ) super().__init__(*args, **kwargs) self.helper.form_action = reverse_lazy(**self.form_action) + apply_query_params( self.query_params) if self.query_params else "" class Meta: model = Product fields = ('name',) # --------------------------- Product Filter Types -------------------------# class ProductManufacturerFilterForm(QueryFormBase): manufacturer = forms.ChoiceField() submit_css_button = None field_template = 'bootstrap/forms/product_filter.html' title = _('Производитель') def __init__(self, *args, **kwargs): self.helper = FormHelper() self.helper.form_method = 'get' self.helper.layout = Layout( Div(HTML(self.title), css_class='category__title'), Field('manufacturer', template=self.field_template) ) super().__init__(*args, **kwargs) self.helper.form_action = self.get_form_action_url() def get_initial_for_field(self, field, field_name): if field_name == 'manufacturer': man_qs = Manufacturer.active category_instance = '' if self.form_action.get('kwargs', None): category_instance = parse_path(self.form_action.get('kwargs').get('path', '')) prod_qs = Product.active.filter( parent__name=category_instance, name__icontains=self.query_params.get('name', '') ).only('pk') if prod_qs.count(): man_qs = man_qs.filter(product__pk__in=prod_qs.all()) return man_qs.distinct('name').only('name', 'slug') return super().get_initial_for_field(field, field_name) class ProductSortForm(QueryFormBase): submit_css_button = None field_template = 'bootstrap/forms/product_sorting.html' title = _('Сортировать по') sort_fields = { 'price': _('Цене'), 'popular': _('Популярности'), 'rate': _('Рейтингу') } sort = forms.ChoiceField(label=title) def __init__(self, *args, **kwargs): self.helper = FormHelper() self.helper.form_method = 'get' self.helper.layout = Layout( HTML(self.title), Field('sort', template=self.field_template) ) super().__init__(*args, **kwargs) self.helper.form_action = self.get_form_action_url() def get_sort_field_initial_data(self): query_keys = self.query_params.keys() initial_data = [] for key in self.sort_fields: order = "DESC" if key in query_keys else "ASC" initial_data.append({ 'value': "{url}?{query_params}".format(**{ 'url': reverse_lazy(**self.form_action), 'query_params': apply_query_params({key: order, **self.query_params}, True) }), 'name': self.sort_fields[key] }) return initial_data def get_initial_for_field(self, field, field_name): if field_name == 'sort': return self.get_sort_field_initial_data() return super().get_initial_for_field(field, field_name) # ----------------------------------------- Admin forms ------------------------------------# class ProductAdminForm(forms.ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['description'].widget = CKEditorWidget(config_name='awesome_ckeditor') class Meta: model = Product fields = ALL_FIELDS