<s> import json <EOL> from django . contrib . auth . decorators import permission_required <EOL> from django . http import HttpResponse <EOL> from django . template . loader import render_to_string <EOL> from django . template import RequestContext <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from lfs . caching . utils import lfs_get_object_or_404 <EOL> from lfs . core . utils import LazyEncoder <EOL> from lfs . core . signals import category_changed <EOL> from lfs . catalog . models import Product <EOL> from lfs . catalog . models import Category <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def manage_categories ( request , product_id , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> product = lfs_get_object_or_404 ( Product , pk = product_id ) <EOL> product_category_ids = [ p . id for p in product . get_categories ( ) ] <EOL> categories = [ ] <EOL> for category in Category . objects . filter ( parent = None ) : <EOL> children = children_categories ( request , category , product_category_ids ) <EOL> categories . append ( { <EOL> "<STR_LIT:id>" : category . id , <EOL> "<STR_LIT>" : category . slug , <EOL> "<STR_LIT:name>" : category . name , <EOL> "<STR_LIT:url>" : category . get_absolute_url ( ) , <EOL> "<STR_LIT>" : category . id in product_category_ids , <EOL> "<STR_LIT>" : children , <EOL> } ) <EOL> result = render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : product , <EOL> "<STR_LIT>" : categories <EOL> } ) ) <EOL> return HttpResponse ( result ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def children_categories ( request , category , product_category_ids , <EOL> template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> categories = [ ] <EOL> for category in category . category_set . all ( ) : <EOL> children = children_categories ( request , category , product_category_ids ) <EOL> categories . append ( { <EOL> "<STR_LIT:id>" : category . id , <EOL> "<STR_LIT>" : category . slug , <EOL> "<STR_LIT:name>" : category . name , <EOL> "<STR_LIT:url>" : category . get_absolute_url ( ) , <EOL> "<STR_LIT>" : category . id in product_category_ids , <EOL> "<STR_LIT>" : children , <EOL> } ) <EOL> result = render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : categories <EOL> } ) ) <EOL> return result <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def change_categories ( request , product_id ) : <EOL> """<STR_LIT>""" <EOL> product = lfs_get_object_or_404 ( Product , pk = product_id ) <EOL> for category in product . categories . all ( ) : <EOL> category_changed . send ( category ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> product . categories = request . POST . getlist ( "<STR_LIT>" ) <EOL> product . save ( ) <EOL> for category in product . categories . all ( ) : <EOL> category_changed . send ( category ) <EOL> return HttpResponse ( json . dumps ( { <EOL> "<STR_LIT:message>" : _ ( u"<STR_LIT>" ) , <EOL> } , cls = LazyEncoder ) , content_type = '<STR_LIT:application/json>' ) </s>
