<s> from django . contrib import messages <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import JsonResponse <EOL> from django . shortcuts import get_object_or_404 , redirect <EOL> from django . template . response import TemplateResponse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from ... product . models import Category <EOL> from . . views import staff_member_required <EOL> from . forms import CategoryForm <EOL> @ staff_member_required <EOL> def category_list ( request , root_pk = None ) : <EOL> root = None <EOL> path = None <EOL> categories = Category . tree . root_nodes ( ) <EOL> if root_pk : <EOL> root = get_object_or_404 ( Category , pk = root_pk ) <EOL> path = root . get_ancestors ( include_self = True ) if root else [ ] <EOL> categories = root . get_children ( ) <EOL> ctx = { '<STR_LIT>' : categories , '<STR_LIT:path>' : path , '<STR_LIT:root>' : root } <EOL> return TemplateResponse ( request , '<STR_LIT>' , ctx ) <EOL> @ staff_member_required <EOL> def category_create ( request , root_pk = None ) : <EOL> category = Category ( ) <EOL> form = CategoryForm ( request . POST or None , parent_pk = root_pk ) <EOL> if form . is_valid ( ) : <EOL> category = form . save ( ) <EOL> messages . success ( request , _ ( '<STR_LIT>' ) % category ) <EOL> if root_pk : <EOL> return redirect ( '<STR_LIT>' , root_pk = root_pk ) <EOL> else : <EOL> return redirect ( '<STR_LIT>' ) <EOL> ctx = { '<STR_LIT>' : category , '<STR_LIT>' : form } <EOL> return TemplateResponse ( request , '<STR_LIT>' , ctx ) <EOL> @ staff_member_required <EOL> def category_edit ( request , root_pk = None ) : <EOL> category = get_object_or_404 ( Category , pk = root_pk ) <EOL> form = CategoryForm ( request . POST or None , instance = category , <EOL> parent_pk = category . parent_id ) <EOL> status = <NUM_LIT:200> <EOL> if form . is_valid ( ) : <EOL> category = form . save ( ) <EOL> messages . success ( request , _ ( '<STR_LIT>' ) % category ) <EOL> if root_pk : <EOL> return redirect ( '<STR_LIT>' , root_pk = root_pk ) <EOL> else : <EOL> return redirect ( '<STR_LIT>' ) <EOL> elif form . errors : <EOL> status = <NUM_LIT> <EOL> ctx = { '<STR_LIT>' : category , '<STR_LIT>' : form , '<STR_LIT:status>' : status } <EOL> template = '<STR_LIT>' <EOL> return TemplateResponse ( request , template , ctx , status = status ) <EOL> @ staff_member_required <EOL> def category_delete ( request , pk ) : <EOL> category = get_object_or_404 ( Category , pk = pk ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> category . delete ( ) <EOL> messages . success ( request , _ ( '<STR_LIT>' ) % category ) <EOL> root_pk = None <EOL> if category . parent : <EOL> root_pk = category . parent . pk <EOL> if root_pk : <EOL> if request . is_ajax ( ) : <EOL> response = { '<STR_LIT>' : reverse ( <EOL> '<STR_LIT>' , kwargs = { '<STR_LIT>' : root_pk } ) } <EOL> return JsonResponse ( response ) <EOL> return redirect ( '<STR_LIT>' , root_pk = root_pk ) <EOL> else : <EOL> if request . is_ajax ( ) : <EOL> response = { '<STR_LIT>' : reverse ( '<STR_LIT>' ) } <EOL> return JsonResponse ( response ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> ctx = { '<STR_LIT>' : category , <EOL> '<STR_LIT>' : list ( category . get_descendants ( ) ) , <EOL> '<STR_LIT>' : len ( category . products . all ( ) ) } <EOL> return TemplateResponse ( request , <EOL> '<STR_LIT>' , <EOL> ctx ) </s>
