<s> import json <EOL> from django . contrib . auth . decorators import permission_required <EOL> from django . core . paginator import EmptyPage <EOL> from django . core . paginator import Paginator <EOL> from django . db . models import Q <EOL> from django . http import HttpResponse <EOL> from django . template import RequestContext <EOL> from django . template . loader import render_to_string <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from lfs . caching . utils import lfs_get_object_or_404 <EOL> from lfs . catalog . models import Category <EOL> from lfs . catalog . models import Product <EOL> from lfs . catalog . settings import VARIANT <EOL> from lfs . core . utils import LazyEncoder <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def manage_related_products ( <EOL> request , product_id , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT:U+0020>""" <EOL> product = Product . objects . get ( pk = product_id ) <EOL> inline = manage_related_products_inline ( request , product_id , as_string = True ) <EOL> amount_options = [ ] <EOL> for value in ( <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:50> , <NUM_LIT:100> ) : <EOL> amount_options . append ( { <EOL> "<STR_LIT:value>" : value , <EOL> "<STR_LIT>" : value == request . session . get ( "<STR_LIT>" ) <EOL> } ) <EOL> return render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : product , <EOL> "<STR_LIT>" : inline , <EOL> "<STR_LIT>" : amount_options , <EOL> } ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def manage_related_products_inline ( <EOL> request , product_id , as_string = False , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> product = Product . objects . get ( pk = product_id ) <EOL> related_products = product . related_products . all ( ) <EOL> related_products_ids = [ p . id for p in related_products ] <EOL> r = request . POST if request . method == '<STR_LIT:POST>' else request . GET <EOL> s = request . session <EOL> if r . get ( "<STR_LIT>" ) or r . get ( "<STR_LIT>" ) : <EOL> page = r . get ( "<STR_LIT>" , s . get ( "<STR_LIT>" , <NUM_LIT:1> ) ) <EOL> filter_ = r . get ( "<STR_LIT>" , s . get ( "<STR_LIT>" ) ) <EOL> category_filter = r . get ( "<STR_LIT>" , <EOL> s . get ( "<STR_LIT>" ) ) <EOL> else : <EOL> page = r . get ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> filter_ = r . get ( "<STR_LIT>" ) <EOL> category_filter = r . get ( "<STR_LIT>" ) <EOL> s [ "<STR_LIT>" ] = page <EOL> s [ "<STR_LIT>" ] = filter_ <EOL> s [ "<STR_LIT>" ] = category_filter <EOL> try : <EOL> s [ "<STR_LIT>" ] = int ( r . get ( "<STR_LIT>" , <EOL> s . get ( "<STR_LIT>" ) ) ) <EOL> except TypeError : <EOL> s [ "<STR_LIT>" ] = <NUM_LIT> <EOL> filters = Q ( ) <EOL> if filter_ : <EOL> filters &= ( Q ( name__icontains = filter_ ) | Q ( sku__icontains = filter_ ) ) <EOL> filters |= ( Q ( sub_type = VARIANT ) & Q ( active_sku = False ) & Q ( parent__sku__icontains = filter_ ) ) <EOL> filters |= ( Q ( sub_type = VARIANT ) & Q ( active_name = False ) & Q ( parent__name__icontains = filter_ ) ) <EOL> if category_filter : <EOL> if category_filter == "<STR_LIT:None>" : <EOL> filters &= Q ( categories = None ) <EOL> elif category_filter == "<STR_LIT>" : <EOL> pass <EOL> else : <EOL> category = lfs_get_object_or_404 ( Category , pk = category_filter ) <EOL> categories = [ category ] <EOL> categories . extend ( category . get_all_children ( ) ) <EOL> filters &= Q ( categories__in = categories ) <EOL> products = Product . objects . filter ( filters ) . exclude ( pk__in = related_products_ids ) . exclude ( pk = product . pk ) <EOL> paginator = Paginator ( products , s [ "<STR_LIT>" ] ) <EOL> total = products . count ( ) <EOL> try : <EOL> page = paginator . page ( page ) <EOL> except EmptyPage : <EOL> page = <NUM_LIT:0> <EOL> result = render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : product , <EOL> "<STR_LIT>" : related_products , <EOL> "<STR_LIT>" : total , <EOL> "<STR_LIT>" : page , <EOL> "<STR_LIT>" : paginator , <EOL> "<STR_LIT>" : filter_ <EOL> } ) ) <EOL> if as_string : <EOL> return result <EOL> else : <EOL> return HttpResponse ( <EOL> json . dumps ( { <EOL> "<STR_LIT:html>" : [ [ "<STR_LIT>" , result ] ] , <EOL> } ) , content_type = '<STR_LIT:application/json>' ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def load_tab ( request , product_id ) : <EOL> """<STR_LIT:U+0020>""" <EOL> related_products = manage_related_products ( request , product_id ) <EOL> return HttpResponse ( related_products ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def add_related_products ( request , product_id ) : <EOL> """<STR_LIT>""" <EOL> parent_product = Product . objects . get ( pk = product_id ) <EOL> for temp_id in request . POST . keys ( ) : <EOL> if temp_id . startswith ( "<STR_LIT>" ) == False : <EOL> continue <EOL> temp_id = temp_id . split ( "<STR_LIT:->" ) [ <NUM_LIT:1> ] <EOL> parent_product . related_products . add ( temp_id ) <EOL> parent_product . save ( ) <EOL> html = [ [ "<STR_LIT>" , manage_related_products_inline ( request , product_id , as_string = True ) ] ] <EOL> result = json . dumps ( { <EOL> "<STR_LIT:html>" : html , <EOL> "<STR_LIT:message>" : _ ( u"<STR_LIT>" ) <EOL> } , cls = LazyEncoder ) <EOL> return HttpResponse ( result , content_type = '<STR_LIT:application/json>' ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def remove_related_products ( request , product_id ) : <EOL> """<STR_LIT>""" <EOL> parent_product = Product . objects . get ( pk = product_id ) <EOL> for temp_id in request . POST . keys ( ) : <EOL> if temp_id . startswith ( "<STR_LIT>" ) == False : <EOL> continue <EOL> temp_id = temp_id . split ( "<STR_LIT:->" ) [ <NUM_LIT:1> ] <EOL> parent_product . related_products . remove ( temp_id ) <EOL> parent_product . save ( ) <EOL> html = [ [ "<STR_LIT>" , manage_related_products_inline ( request , product_id , as_string = True ) ] ] <EOL> result = json . dumps ( { <EOL> "<STR_LIT:html>" : html , <EOL> "<STR_LIT:message>" : _ ( u"<STR_LIT>" ) <EOL> } , cls = LazyEncoder ) <EOL> return HttpResponse ( result , content_type = '<STR_LIT:application/json>' ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def update_related_products ( request , product_id ) : <EOL> """<STR_LIT>""" <EOL> product = Product . objects . get ( pk = product_id ) <EOL> if request . POST . get ( "<STR_LIT>" ) : <EOL> product . active_related_products = True <EOL> else : <EOL> product . active_related_products = False <EOL> product . save ( ) <EOL> html = [ [ "<STR_LIT>" , manage_related_products_inline ( request , product_id , as_string = True ) ] ] <EOL> result = json . dumps ( { <EOL> "<STR_LIT:html>" : html , <EOL> "<STR_LIT:message>" : _ ( u"<STR_LIT>" ) <EOL> } , cls = LazyEncoder ) <EOL> return HttpResponse ( result , content_type = '<STR_LIT:application/json>' ) </s>
