<s> from decimal import Decimal <EOL> from django import http <EOL> from django . contrib . messages import constants , get_messages <EOL> from django . core . xheaders import populate_xheaders <EOL> from django . shortcuts import get_object_or_404 , render_to_response <EOL> from django . template import RequestContext <EOL> from django . template . loader import select_template <EOL> from django . utils . translation import ugettext as _ <EOL> from l10n . utils import moneyfmt <EOL> from livesettings import config_value <EOL> from product . models import Category , Product <EOL> from product . modules . configurable . models import ConfigurableProduct , sorted_tuple <EOL> from product . signals import index_prerender <EOL> from product . utils import find_best_auto_discount <EOL> from satchmo_utils . json import json_encode <EOL> from satchmo_utils . numbers import RoundedDecimalError , round_decimal <EOL> from satchmo_utils . views import bad_or_missing <EOL> import logging <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> def find_product_template ( product , producttypes = None ) : <EOL> """<STR_LIT>""" <EOL> if producttypes is None : <EOL> producttypes = product . get_subtypes ( ) <EOL> templates = [ "<STR_LIT>" % x . lower ( ) for x in producttypes ] <EOL> templates . append ( '<STR_LIT>' ) <EOL> log . debug ( "<STR_LIT>" , templates ) <EOL> return select_template ( templates ) <EOL> def optionids_from_post ( configurableproduct , POST ) : <EOL> """<STR_LIT>""" <EOL> chosen_options = [ ] <EOL> for opt_grp in configurableproduct . option_group . all ( ) : <EOL> if POST . has_key ( str ( opt_grp . id ) ) : <EOL> chosen_options . append ( '<STR_LIT>' % ( opt_grp . id , POST [ str ( opt_grp . id ) ] ) ) <EOL> return sorted_tuple ( chosen_options ) <EOL> def category_index ( request , template = "<STR_LIT>" , root_only = True ) : <EOL> """<STR_LIT>""" <EOL> cats = Category . objects . root_categories ( ) <EOL> ctx = { <EOL> '<STR_LIT>' : cats , <EOL> } <EOL> return render_to_response ( template , context_instance = RequestContext ( request , ctx ) ) <EOL> def category_view ( request , slug , parent_slugs = '<STR_LIT>' , template = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> category = Category . objects . get_by_site ( slug = slug ) <EOL> products = list ( category . active_products ( ) ) <EOL> sale = find_best_auto_discount ( products ) <EOL> except Category . DoesNotExist : <EOL> return bad_or_missing ( request , _ ( '<STR_LIT>' ) ) <EOL> child_categories = category . get_all_children ( ) <EOL> ctx = { <EOL> '<STR_LIT>' : category , <EOL> '<STR_LIT>' : child_categories , <EOL> '<STR_LIT>' : sale , <EOL> '<STR_LIT>' : products , <EOL> } <EOL> index_prerender . send ( Product , request = request , context = ctx , category = category , object_list = products ) <EOL> return render_to_response ( template , context_instance = RequestContext ( request , ctx ) ) <EOL> def display_featured ( num_to_display = None , random_display = None ) : <EOL> """<STR_LIT>""" <EOL> if num_to_display is None : <EOL> num_to_display = config_value ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if random_display is None : <EOL> random_display = config_value ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> q = Product . objects . featured_by_site ( ) <EOL> if not random_display : <EOL> return q [ : num_to_display ] <EOL> else : <EOL> return q . order_by ( '<STR_LIT:?>' ) [ : num_to_display ] <EOL> def get_configurable_product_options ( request , id ) : <EOL> """<STR_LIT>""" <EOL> cp = get_object_or_404 ( ConfigurableProduct , product__id = id ) <EOL> options = '<STR_LIT>' <EOL> for og in cp . option_group . all ( ) : <EOL> for opt in og . option_set . all ( ) : <EOL> options += '<STR_LIT>' % ( opt . id , str ( opt ) ) <EOL> if not options : <EOL> return '<STR_LIT>' % cp . product . slug <EOL> return http . HttpResponse ( options , mimetype = "<STR_LIT>" ) <EOL> def get_product ( request , product_slug = None , selected_options = ( ) , <EOL> default_view_tax = None ) : <EOL> """<STR_LIT>""" <EOL> errors = [ m for m in get_messages ( request ) if m . level == constants . ERROR ] <EOL> try : <EOL> product = Product . objects . get_by_site ( active = True , slug = product_slug ) <EOL> except Product . DoesNotExist : <EOL> return bad_or_missing ( request , _ ( '<STR_LIT>' ) ) <EOL> if default_view_tax is None : <EOL> default_view_tax = config_value ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> subtype_names = product . get_subtypes ( ) <EOL> product_id = product . id <EOL> current_product = product <EOL> if '<STR_LIT>' in subtype_names : <EOL> selected_options = product . productvariation . unique_option_ids <EOL> product = product . productvariation . parent . product <EOL> subtype_names = product . get_subtypes ( ) <EOL> best_discount = find_best_auto_discount ( product ) <EOL> if errors : <EOL> error_message = errors [ <NUM_LIT:0> ] <EOL> else : <EOL> error_message = None <EOL> extra_context = { <EOL> '<STR_LIT>' : product , <EOL> '<STR_LIT>' : current_product , <EOL> '<STR_LIT>' : default_view_tax , <EOL> '<STR_LIT>' : best_discount , <EOL> '<STR_LIT>' : error_message , <EOL> } <EOL> extra_context = product . add_template_context ( context = extra_context , <EOL> request = request , selected_options = selected_options , <EOL> default_view_tax = default_view_tax ) <EOL> template = find_product_template ( product , producttypes = subtype_names ) <EOL> context = RequestContext ( request , extra_context ) <EOL> response = http . HttpResponse ( template . render ( context ) ) <EOL> populate_xheaders ( request , response , Product , product_id ) <EOL> return response <EOL> def get_price ( request , product_slug ) : <EOL> """<STR_LIT>""" <EOL> quantity = Decimal ( '<STR_LIT:1>' ) <EOL> try : <EOL> product = Product . objects . get_by_site ( active = True , slug = product_slug ) <EOL> except Product . DoesNotExist : <EOL> return http . HttpResponseNotFound ( json_encode ( ( '<STR_LIT>' , _ ( "<STR_LIT>" ) ) ) , mimetype = "<STR_LIT>" ) <EOL> prod_slug = product . slug <EOL> if request . method == "<STR_LIT:POST>" and request . POST . has_key ( '<STR_LIT>' ) : <EOL> try : <EOL> quantity = round_decimal ( request . POST [ '<STR_LIT>' ] , places = <NUM_LIT:2> , roundfactor = <NUM_LIT> ) <EOL> except RoundedDecimalError : <EOL> quantity = Decimal ( '<STR_LIT:1.0>' ) <EOL> log . warn ( "<STR_LIT>" , request . POST [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in product . get_subtypes ( ) : <EOL> cp = product . configurableproduct <EOL> chosen_options = optionids_from_post ( cp , request . POST ) <EOL> pvp = cp . get_product_from_options ( chosen_options ) <EOL> if not pvp : <EOL> return http . HttpResponse ( json_encode ( ( '<STR_LIT>' , _ ( "<STR_LIT>" ) ) ) , mimetype = "<STR_LIT>" ) <EOL> prod_slug = pvp . slug <EOL> price = moneyfmt ( pvp . get_qty_price ( quantity ) ) <EOL> else : <EOL> price = moneyfmt ( product . get_qty_price ( quantity ) ) <EOL> if not price : <EOL> return http . HttpResponse ( json_encode ( ( '<STR_LIT>' , _ ( "<STR_LIT>" ) ) ) , mimetype = "<STR_LIT>" ) <EOL> return http . HttpResponse ( json_encode ( ( prod_slug , price ) ) , mimetype = "<STR_LIT>" ) <EOL> def get_price_detail ( request , product_slug ) : <EOL> """<STR_LIT>""" <EOL> results = { <EOL> "<STR_LIT:success>" : False , <EOL> "<STR_LIT:message>" : _ ( "<STR_LIT>" ) <EOL> } <EOL> price = None <EOL> if request . method == "<STR_LIT:POST>" : <EOL> reqdata = request . POST <EOL> else : <EOL> reqdata = request . GET <EOL> try : <EOL> product = Product . objects . get_by_site ( active = True , slug = product_slug ) <EOL> found = True <EOL> prod_slug = product . slug <EOL> if reqdata . has_key ( '<STR_LIT>' ) : <EOL> try : <EOL> quantity = round_decimal ( reqdata [ '<STR_LIT>' ] , places = <NUM_LIT:2> , roundfactor = <NUM_LIT> ) <EOL> except RoundedDecimalError : <EOL> quantity = Decimal ( '<STR_LIT:1.0>' ) <EOL> log . warn ( "<STR_LIT>" , reqdata [ '<STR_LIT>' ] ) <EOL> else : <EOL> quantity = Decimal ( '<STR_LIT:1.0>' ) <EOL> if '<STR_LIT>' in product . get_subtypes ( ) : <EOL> cp = product . configurableproduct <EOL> chosen_options = optionids_from_post ( cp , reqdata ) <EOL> product = cp . get_product_from_options ( chosen_options ) <EOL> if product : <EOL> price = product . get_qty_price ( quantity ) <EOL> results [ '<STR_LIT>' ] = product . slug <EOL> results [ '<STR_LIT>' ] = float ( price ) <EOL> results [ '<STR_LIT:success>' ] = True <EOL> results [ '<STR_LIT:message>' ] = "<STR_LIT>" <EOL> except Product . DoesNotExist : <EOL> found = False <EOL> data = json_encode ( results ) <EOL> if found : <EOL> return http . HttpResponse ( data , mimetype = "<STR_LIT>" ) <EOL> else : <EOL> return http . HttpResponseNotFound ( data , mimetype = "<STR_LIT>" ) </s>
