#NEWLINE# def serialize_options( product , selected_options = ( ) ) : #NEWLINE# #INDENT# "\n    Return a list of optiongroups and options for display to the customer.\n    Only returns options that are actually used by members of this product.\n\n    Return Value:\n    [\n    {\n    name: 'group name',\n    id: 'group id',\n    items: [{\n        name: 'opt name',\n        value: 'opt value',\n        price_change: 'opt price',\n        selected: False,\n        },{..}]\n    },\n    {..}\n    ]\n\n    Note: This doesn't handle the case where you have multiple options and\n    some combinations aren't available. For example, you have option_groups\n    color and size, and you have a yellow/large, a yellow/small, and a\n    white/small, but you have no white/large - the customer will still see\n    the options white and large.\n    " #NEWLINE# all_options = product . get_valid_options ( ) #NEWLINE# group_sortmap = OptionGroup . objects . get_sortmap ( ) #NEWLINE# values = [ ] #NEWLINE# if ( all_options != [ [ ] ] ) : #NEWLINE# #INDENT# vals = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# groups = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# opts = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# serialized = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for options in all_options : #NEWLINE# #INDENT# for option in options : #NEWLINE# #INDENT# if ( not opts . has_key ( option ) ) : #NEWLINE# #INDENT# ( k , v ) = split_option_unique_id ( option ) #NEWLINE# vals [ v ] = False #NEWLINE# groups [ k ] = False #NEWLINE# opts [ option ] = None #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# for option in Option . objects . filter ( option_group__id__in = groups . keys ( ) , value__in = vals . keys ( ) ) : #NEWLINE# #INDENT# uid = option . unique_id #NEWLINE# if opts . has_key ( uid ) : #NEWLINE# #INDENT# opts [ uid ] = option #NEWLINE# #UNINDENT# #UNINDENT# for option in opts . values ( ) : #NEWLINE# #INDENT# if ( not serialized . has_key ( option . option_group_id ) ) : #NEWLINE# #INDENT# serialized [ option . option_group . id ] = { #NEWLINE# #INDENT# 'name' : option . option_group . translated_name ( ) , #NEWLINE# 'description' : option . option_group . translated_description ( ) , #NEWLINE# 'id' : option . option_group . id , #NEWLINE# 'items' : [ ] , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# if ( not ( option in serialized [ option . option_group_id ] [ 'items' ] ) ) : #NEWLINE# #INDENT# serialized [ option . option_group_id ] [ 'items' ] += [ option ] #NEWLINE# option . selected = ( option . unique_id in selected_options ) #NEWLINE# #UNINDENT# #UNINDENT# for ( k , v ) in serialized . items ( ) : #NEWLINE# #INDENT# values . append ( ( group_sortmap [ k ] , v ) ) #NEWLINE# #UNINDENT# if values : #NEWLINE# #INDENT# values . sort ( ) #NEWLINE# values = zip ( * values ) [ 1 ] #NEWLINE# #UNINDENT# for v in values : #NEWLINE# #INDENT# v [ 'items' ] = _sort_options ( v [ 'items' ] ) #NEWLINE# #UNINDENT# #UNINDENT# log . debug ( 'serialized: %s' , values ) #NEWLINE# log . debug ( 'Serialized Options %s: %s' , product . product . slug , values ) #NEWLINE# return values