<s> import json <EOL> from django . contrib . auth . decorators import permission_required <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 . shortcuts import get_object_or_404 <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 django . views . decorators . http import require_POST <EOL> import lfs . core . utils <EOL> from lfs . catalog . settings import STANDARD_PRODUCT <EOL> from lfs . catalog . settings import PRODUCT_WITH_VARIANTS <EOL> from lfs . catalog . models import Category <EOL> from lfs . catalog . models import Product <EOL> from lfs . core . utils import LazyEncoder <EOL> from lfs . export . models import Export <EOL> from lfs . export . models import CategoryOption <EOL> from lfs . export . settings import CATEGORY_VARIANTS_CHOICES <EOL> from lfs . export . settings import CATEGORY_VARIANTS_NONE <EOL> class ExportDataForm ( ModelForm ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> model = Export <EOL> exclude = ( "<STR_LIT>" , ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def manage_export ( request , export_id , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> export = Export . objects . get ( pk = export_id ) <EOL> categories = [ ] <EOL> for category in Category . objects . filter ( parent = None ) : <EOL> options = [ ] <EOL> try : <EOL> category_option = CategoryOption . objects . get ( export = export , category = category ) <EOL> except CategoryOption . DoesNotExist : <EOL> variants_option = None <EOL> else : <EOL> variants_option = category_option . variants_option <EOL> for option in CATEGORY_VARIANTS_CHOICES : <EOL> options . append ( { <EOL> "<STR_LIT:name>" : option [ <NUM_LIT:1> ] , <EOL> "<STR_LIT:value>" : option [ <NUM_LIT:0> ] , <EOL> "<STR_LIT>" : option [ <NUM_LIT:0> ] == variants_option , <EOL> } ) <EOL> checked , klass = _get_category_state ( export , category ) <EOL> categories . append ( { <EOL> "<STR_LIT:id>" : category . id , <EOL> "<STR_LIT:name>" : category . name , <EOL> "<STR_LIT>" : checked , <EOL> "<STR_LIT>" : klass , <EOL> "<STR_LIT>" : options , <EOL> } ) <EOL> data_form = ExportDataForm ( instance = export ) <EOL> return render_to_response ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : categories , <EOL> "<STR_LIT>" : export_id , <EOL> "<STR_LIT>" : export . slug , <EOL> "<STR_LIT>" : selectable_exports_inline ( request , export_id ) , <EOL> "<STR_LIT>" : export_data_inline ( request , export_id , data_form ) , <EOL> } ) ) <EOL> def export_data_inline ( request , export_id , form , <EOL> template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> return render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : export_id , <EOL> "<STR_LIT>" : form , <EOL> } ) ) <EOL> def selectable_exports_inline ( request , export_id , <EOL> template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> return render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : Export . objects . all ( ) , <EOL> "<STR_LIT>" : int ( export_id ) , <EOL> } ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def export_inline ( request , export_id , category_id , <EOL> template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> export = Export . objects . get ( pk = export_id ) <EOL> selected_products = export . products . all ( ) <EOL> products = [ ] <EOL> for product in Product . objects . filter ( sub_type__in = [ STANDARD_PRODUCT , PRODUCT_WITH_VARIANTS ] , categories__in = [ category_id ] , active = True ) : <EOL> if product . is_standard ( ) : <EOL> type = "<STR_LIT:P>" <EOL> else : <EOL> type = "<STR_LIT>" <EOL> products . append ( { <EOL> "<STR_LIT:id>" : product . id , <EOL> "<STR_LIT:name>" : product . get_name ( ) , <EOL> "<STR_LIT>" : product in selected_products , <EOL> "<STR_LIT:type>" : type , <EOL> } ) <EOL> categories = [ ] <EOL> for category in Category . objects . filter ( parent = category_id ) : <EOL> options = [ ] <EOL> try : <EOL> category_option = CategoryOption . objects . get ( export = export , category = category ) <EOL> except CategoryOption . DoesNotExist : <EOL> variants_option = None <EOL> else : <EOL> variants_option = category_option . variants_option <EOL> for option in CATEGORY_VARIANTS_CHOICES : <EOL> options . append ( { <EOL> "<STR_LIT:name>" : option [ <NUM_LIT:1> ] , <EOL> "<STR_LIT:value>" : option [ <NUM_LIT:0> ] , <EOL> "<STR_LIT>" : option [ <NUM_LIT:0> ] == variants_option , <EOL> } ) <EOL> checked , klass = _get_category_state ( export , category ) <EOL> categories . append ( { <EOL> "<STR_LIT:id>" : category . id , <EOL> "<STR_LIT:name>" : category . name , <EOL> "<STR_LIT>" : checked , <EOL> "<STR_LIT>" : klass , <EOL> "<STR_LIT>" : options , <EOL> } ) <EOL> result = render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : categories , <EOL> "<STR_LIT>" : products , <EOL> "<STR_LIT>" : export_id , <EOL> } ) ) <EOL> html = ( ( "<STR_LIT>" % category_id , result ) , ) <EOL> return HttpResponse ( <EOL> json . dumps ( { "<STR_LIT:html>" : html } ) , content_type = '<STR_LIT:application/json>' ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def add_export ( request , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = ExportDataForm ( data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> new_export = form . save ( ) <EOL> return HttpResponseRedirect ( <EOL> reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT>" : new_export . id } ) ) <EOL> else : <EOL> form = ExportDataForm ( ) <EOL> return render_to_response ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : form , <EOL> "<STR_LIT>" : selectable_exports_inline ( request , <NUM_LIT:0> ) , <EOL> } ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def export_dispatcher ( request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> export = Export . objects . all ( ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> else : <EOL> return HttpResponseRedirect ( <EOL> reverse ( "<STR_LIT>" , kwargs = { "<STR_LIT>" : export . id } ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> @ require_POST <EOL> def delete_export ( request , export_id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> export = Export . objects . get ( pk = export_id ) <EOL> except Export . DoesNotExist : <EOL> pass <EOL> else : <EOL> export . delete ( ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def edit_category ( request , export_id , category_id ) : <EOL> """<STR_LIT>""" <EOL> export = Export . objects . get ( pk = export_id ) <EOL> category = Category . objects . get ( pk = category_id ) <EOL> if request . POST . get ( "<STR_LIT:action>" ) == "<STR_LIT>" : <EOL> for product in category . get_all_products ( ) : <EOL> export . products . add ( product ) <EOL> else : <EOL> for product in category . get_all_products ( ) : <EOL> export . products . remove ( product ) <EOL> return HttpResponse ( "<STR_LIT>" ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def edit_product ( request , export_id , product_id ) : <EOL> """<STR_LIT>""" <EOL> export = Export . objects . get ( pk = export_id ) <EOL> product = Product . objects . get ( pk = product_id ) <EOL> if request . POST . get ( "<STR_LIT:action>" ) == "<STR_LIT>" : <EOL> export . products . add ( product ) <EOL> else : <EOL> export . products . remove ( product ) <EOL> return HttpResponse ( "<STR_LIT>" ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def export ( request , slug ) : <EOL> """<STR_LIT>""" <EOL> export = get_object_or_404 ( Export , slug = slug ) <EOL> module = lfs . core . utils . import_module ( export . script . module ) <EOL> return getattr ( module , export . script . method ) ( request , export ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def category_state ( request , export_id , category_id ) : <EOL> """<STR_LIT>""" <EOL> export = Export . objects . get ( pk = export_id ) <EOL> category = Category . objects . get ( pk = category_id ) <EOL> checked , klass = _get_category_state ( export , category ) <EOL> if klass == "<STR_LIT>" : <EOL> result = "<STR_LIT>" <EOL> else : <EOL> result = "<STR_LIT>" <EOL> html = ( "<STR_LIT>" % category_id , result ) <EOL> checkbox = ( "<STR_LIT>" % category_id , checked ) <EOL> return HttpResponse ( <EOL> json . dumps ( { <EOL> "<STR_LIT:html>" : html , <EOL> "<STR_LIT>" : checkbox <EOL> } ) , content_type = '<STR_LIT:application/json>' <EOL> ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def update_category_variants_option ( request , export_id , category_id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> variants_option = int ( request . POST . get ( "<STR_LIT>" ) ) <EOL> except ValueError : <EOL> variants_option = <NUM_LIT:0> <EOL> try : <EOL> category = Category . objects . get ( pk = category_id ) <EOL> except Category . DoesNotExist : <EOL> return HttpResponse ( "<STR_LIT>" ) <EOL> try : <EOL> export = Export . objects . get ( pk = export_id ) <EOL> except Export . DoesNotExist : <EOL> return HttpResponse ( "<STR_LIT>" ) <EOL> try : <EOL> category_option = CategoryOption . objects . get ( export = export , category = category ) <EOL> except CategoryOption . DoesNotExist : <EOL> category_option = None <EOL> if variants_option == CATEGORY_VARIANTS_NONE : <EOL> if category_option : <EOL> category_option . delete ( ) <EOL> else : <EOL> if category_option is None : <EOL> CategoryOption . objects . create ( <EOL> export = export , category = category , variants_option = variants_option ) <EOL> else : <EOL> category_option . variants_option = variants_option <EOL> category_option . save ( ) <EOL> return HttpResponse ( "<STR_LIT>" ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def update_data ( request , export_id ) : <EOL> """<STR_LIT>""" <EOL> export = Export . objects . get ( pk = export_id ) <EOL> form = ExportDataForm ( instance = export , data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> msg = _ ( u"<STR_LIT>" ) <EOL> html = ( <EOL> ( "<STR_LIT>" , export_data_inline ( request , export_id , form ) ) , <EOL> ( "<STR_LIT>" , selectable_exports_inline ( request , export_id ) ) , <EOL> ) <EOL> result = json . dumps ( { <EOL> "<STR_LIT:html>" : html , <EOL> "<STR_LIT:message>" : msg <EOL> } , cls = LazyEncoder ) <EOL> return HttpResponse ( result , content_type = '<STR_LIT:application/json>' ) <EOL> def _get_category_state ( export , category ) : <EOL> """<STR_LIT>""" <EOL> selected_products = export . products . all ( ) <EOL> found = False <EOL> not_found = False <EOL> for product in category . get_all_products ( ) : <EOL> if product in selected_products : <EOL> found = True <EOL> else : <EOL> not_found = True <EOL> if found and not_found : <EOL> checked = True <EOL> klass = "<STR_LIT>" <EOL> elif found : <EOL> checked = True <EOL> klass = "<STR_LIT>" <EOL> else : <EOL> checked = False <EOL> klass = "<STR_LIT>" <EOL> return ( checked , klass ) </s>
