<s> from django . utils . translation import ugettext_lazy as _ <EOL> from django . apps import apps <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django_select2 . fields import AutoModelSelect2Field <EOL> from cms . plugin_pool import plugin_pool <EOL> from cmsplugin_cascade . link . forms import TextLinkForm <EOL> from cmsplugin_cascade . link . models import SimpleLinkElement <EOL> from cmsplugin_cascade . link . plugin_base import TextLinkPluginBase <EOL> from cmsplugin_cascade . utils import resolve_dependencies <EOL> from shop . models . product import Product <EOL> class ProductSearchField ( AutoModelSelect2Field ) : <EOL> empty_value = [ ] <EOL> search_fields = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> queryset = Product . objects . all ( ) <EOL> def security_check ( self , request , * args , ** kwargs ) : <EOL> user = request . user <EOL> if user and not user . is_anonymous ( ) and user . is_staff : <EOL> return True <EOL> return False <EOL> def prepare_value ( self , value ) : <EOL> if not value : <EOL> return None <EOL> return super ( ProductSearchField , self ) . prepare_value ( value ) <EOL> class LinkForm ( TextLinkForm ) : <EOL> LINK_TYPE_CHOICES = ( ( '<STR_LIT>' , _ ( "<STR_LIT>" ) ) , ( '<STR_LIT>' , _ ( "<STR_LIT>" ) ) , ( '<STR_LIT>' , _ ( "<STR_LIT>" ) ) , ( '<STR_LIT:email>' , _ ( "<STR_LIT>" ) ) , ) <EOL> product = ProductSearchField ( required = False , label = '<STR_LIT>' , <EOL> help_text = _ ( "<STR_LIT>" ) ) <EOL> def clean_product ( self ) : <EOL> if self . cleaned_data . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> self . cleaned_data [ '<STR_LIT>' ] = { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . cleaned_data [ '<STR_LIT>' ] and self . cleaned_data [ '<STR_LIT>' ] . pk or None , <EOL> } <EOL> def set_initial_product ( self , initial ) : <EOL> try : <EOL> Model = apps . get_model ( * initial [ '<STR_LIT>' ] [ '<STR_LIT>' ] . split ( '<STR_LIT:.>' ) ) <EOL> initial [ '<STR_LIT>' ] = Model . objects . get ( pk = initial [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> except ( KeyError , ObjectDoesNotExist ) : <EOL> pass <EOL> class LinkPlugin ( TextLinkPluginBase ) : <EOL> model = SimpleLinkElement <EOL> fields = ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' , ) <EOL> form = LinkForm <EOL> class Media : <EOL> js = resolve_dependencies ( '<STR_LIT>' ) <EOL> plugin_pool . register_plugin ( LinkPlugin ) </s>
