<s> import json <EOL> from django . contrib . auth . decorators import permission_required <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . core . urlresolvers import reverse <EOL> from django . forms import ModelForm <EOL> from django . http import HttpResponse <EOL> from django . http import HttpResponseRedirect <EOL> from django . shortcuts import render_to_response <EOL> from django . template import RequestContext <EOL> from django . template . loader import render_to_string <EOL> from django . views . decorators . http import require_POST <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 . core . utils import LazyEncoder <EOL> from lfs . core . utils import set_category_levels <EOL> from lfs . core . widgets . image import LFSImageInput <EOL> from lfs . manage import utils as manage_utils <EOL> from lfs . manage . categories . view import category_view <EOL> from lfs . manage . categories . portlet import manage_categories_portlet <EOL> from lfs . manage . seo . views import SEOView <EOL> from lfs . manage . views . lfs_portlets import portlets_inline <EOL> class CategoryAddForm ( ModelForm ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> model = Category <EOL> fields = ( "<STR_LIT:name>" , "<STR_LIT>" ) <EOL> class CategoryForm ( ModelForm ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( CategoryForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ "<STR_LIT:image>" ] . widget = LFSImageInput ( ) <EOL> class Meta : <EOL> model = Category <EOL> fields = ( "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:description>" , <EOL> "<STR_LIT>" , "<STR_LIT:image>" , "<STR_LIT>" ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def manage_categories ( request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> category = Category . objects . all ( ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> url = reverse ( "<STR_LIT>" ) <EOL> else : <EOL> url = reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT>" : category . id } ) <EOL> return HttpResponseRedirect ( url ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def manage_category ( request , category_id , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> category = Category . objects . get ( pk = category_id ) <EOL> return render_to_response ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : manage_categories_portlet ( request , category_id ) , <EOL> "<STR_LIT>" : category , <EOL> "<STR_LIT:data>" : category_data ( request , category_id ) , <EOL> "<STR_LIT>" : SEOView ( Category ) . render ( request , category ) , <EOL> "<STR_LIT>" : category_view ( request , category_id ) , <EOL> "<STR_LIT>" : portlets_inline ( request , category ) , <EOL> "<STR_LIT>" : _ ( "<STR_LIT>" ) % { "<STR_LIT:name>" : category . name } , <EOL> } ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def category_data ( request , category_id , form = None , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> category = Category . objects . get ( pk = category_id ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = CategoryForm ( instance = category , data = request . POST ) <EOL> else : <EOL> form = CategoryForm ( instance = category ) <EOL> return render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : category , <EOL> "<STR_LIT>" : form , <EOL> } ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def category_by_id ( request , category_id ) : <EOL> """<STR_LIT>""" <EOL> category = Category . objects . get ( pk = category_id ) <EOL> url = reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT>" : category . slug } ) <EOL> return HttpResponseRedirect ( url ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def edit_category_data ( request , category_id , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> category = Category . objects . get ( pk = category_id ) <EOL> form = CategoryForm ( instance = category , data = request . POST , files = request . FILES ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> message = _ ( u"<STR_LIT>" ) <EOL> else : <EOL> message = _ ( u"<STR_LIT>" ) <EOL> if request . POST . get ( "<STR_LIT>" ) : <EOL> category . image . delete ( ) <EOL> html = [ <EOL> [ "<STR_LIT>" , category_data ( request , category . id ) ] , <EOL> [ "<STR_LIT>" , manage_categories_portlet ( request , category . id ) ] , <EOL> ] <EOL> result = json . dumps ( { <EOL> "<STR_LIT:message>" : message , <EOL> "<STR_LIT:html>" : html , <EOL> } , cls = LazyEncoder ) <EOL> return HttpResponse ( result , content_type = '<STR_LIT:application/json>' ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def add_category ( request , category_id = "<STR_LIT>" , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if category_id == "<STR_LIT>" : <EOL> parent = None <EOL> else : <EOL> try : <EOL> parent = Category . objects . get ( pk = category_id ) <EOL> except ObjectDoesNotExist : <EOL> parent = None <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = CategoryAddForm ( data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> new_category = form . save ( commit = False ) <EOL> new_category . parent = parent <EOL> new_category . position = <NUM_LIT> <EOL> if parent : <EOL> new_category . level = parent . level + <NUM_LIT:1> <EOL> new_category . save ( ) <EOL> manage_utils . update_category_positions ( parent ) <EOL> url = reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT>" : new_category . id } ) <EOL> return HttpResponseRedirect ( url ) <EOL> else : <EOL> form = CategoryAddForm ( initial = { "<STR_LIT>" : category_id } ) <EOL> return render_to_response ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : parent , <EOL> "<STR_LIT>" : form , <EOL> "<STR_LIT>" : ( request . POST if request . method == '<STR_LIT:POST>' else request . GET ) . get ( "<STR_LIT>" , reverse ( "<STR_LIT>" ) ) , <EOL> } ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> @ require_POST <EOL> def delete_category ( request , id ) : <EOL> """<STR_LIT>""" <EOL> category = lfs_get_object_or_404 ( Category , pk = id ) <EOL> parent = category . parent <EOL> category . delete ( ) <EOL> manage_utils . update_category_positions ( parent ) <EOL> url = reverse ( "<STR_LIT>" ) <EOL> return HttpResponseRedirect ( url ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def sort_categories ( request ) : <EOL> """<STR_LIT>""" <EOL> category_list = request . POST . get ( "<STR_LIT>" , "<STR_LIT>" ) . split ( '<STR_LIT:&>' ) <EOL> assert ( isinstance ( category_list , list ) ) <EOL> if len ( category_list ) > <NUM_LIT:0> : <EOL> pos = <NUM_LIT:10> <EOL> for cat_str in category_list : <EOL> child , parent_id = cat_str . split ( '<STR_LIT:=>' ) <EOL> child_id = child [ <NUM_LIT:9> : - <NUM_LIT:1> ] <EOL> child_obj = Category . objects . get ( pk = child_id ) <EOL> parent_obj = None <EOL> if parent_id != '<STR_LIT:root>' : <EOL> parent_obj = Category . objects . get ( pk = parent_id ) <EOL> child_obj . parent = parent_obj <EOL> child_obj . position = pos <EOL> child_obj . save ( ) <EOL> pos = pos + <NUM_LIT:10> <EOL> set_category_levels ( ) <EOL> result = json . dumps ( { <EOL> "<STR_LIT:message>" : _ ( u"<STR_LIT>" ) , <EOL> } , cls = LazyEncoder ) <EOL> return HttpResponse ( result , content_type = '<STR_LIT:application/json>' ) <EOL> def _category_choices ( context ) : <EOL> """<STR_LIT>""" <EOL> categories = [ ( "<STR_LIT>" , "<STR_LIT:->" ) ] <EOL> for category in Category . objects . filter ( parent = None ) : <EOL> if context != category : <EOL> categories . append ( ( category . id , category . name ) ) <EOL> _category_choices_children ( categories , category , context ) <EOL> return categories <EOL> def _category_choices_children ( categories , category , context , level = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> for category in category . category_set . all ( ) : <EOL> if context != category : <EOL> categories . append ( ( category . id , "<STR_LIT>" % ( "<STR_LIT:->" * level * <NUM_LIT:5> , category . name ) ) ) <EOL> _category_choices_children ( categories , category , context , level + <NUM_LIT:1> ) </s>
