<s> from django . conf import settings <EOL> from django . core . urlresolvers import reverse <EOL> from django . shortcuts import render <EOL> from django . core . cache import cache <EOL> from django . core . paginator import Paginator , EmptyPage , InvalidPage <EOL> from django . template . loader import render_to_string <EOL> from django . template import RequestContext <EOL> from django . utils . translation import ungettext <EOL> from lfs . caching . utils import lfs_get_object_or_404 <EOL> from lfs . manufacturer . models import Manufacturer <EOL> from lfs . core . utils import lfs_pagination <EOL> def manufacturers ( request , <EOL> template_name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> start = int ( ( request . POST if request . method == '<STR_LIT:POST>' else request . GET ) . get ( "<STR_LIT:start>" , <NUM_LIT:1> ) ) <EOL> except ( ValueError , TypeError ) : <EOL> start = <NUM_LIT:1> <EOL> manufacturers = Manufacturer . objects . all ( ) <EOL> paginator = Paginator ( manufacturers , <NUM_LIT> ) <EOL> try : <EOL> current_page = paginator . page ( start ) <EOL> except ( EmptyPage , InvalidPage ) : <EOL> current_page = paginator . page ( paginator . num_pages ) <EOL> pagination_data = lfs_pagination ( request , current_page , url = reverse ( '<STR_LIT>' ) ) <EOL> count = manufacturers . count ( ) <EOL> pagination_data [ '<STR_LIT>' ] = ungettext ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> count ) % { '<STR_LIT:count>' : count } <EOL> return render ( request , template_name , { <EOL> "<STR_LIT>" : pagination_data , <EOL> "<STR_LIT>" : current_page . object_list , <EOL> "<STR_LIT>" : manufacturers <EOL> } ) <EOL> def manufacturer_view ( request , slug , <EOL> template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> start = ( request . POST if request . method == '<STR_LIT:POST>' else request . GET ) . get ( "<STR_LIT:start>" , <NUM_LIT:1> ) <EOL> manufacturer = lfs_get_object_or_404 ( Manufacturer , slug = slug ) <EOL> inline = manufacturer_products ( request , slug , start ) <EOL> request . session [ "<STR_LIT>" ] = manufacturer <EOL> return render ( request , template_name , { <EOL> "<STR_LIT>" : manufacturer , <EOL> "<STR_LIT>" : inline <EOL> } ) <EOL> def manufacturer_products ( request , slug , start = <NUM_LIT:1> , <EOL> template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> last_manufacturer = request . session . get ( "<STR_LIT>" ) <EOL> if ( last_manufacturer is None ) or ( last_manufacturer . slug != slug ) : <EOL> if "<STR_LIT>" in request . session : <EOL> del request . session [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in request . session : <EOL> del request . session [ "<STR_LIT>" ] <EOL> try : <EOL> default_sorting = settings . LFS_PRODUCTS_SORTING <EOL> except AttributeError : <EOL> default_sorting = "<STR_LIT>" <EOL> sorting = request . session . get ( "<STR_LIT>" , default_sorting ) <EOL> product_filter = request . session . get ( "<STR_LIT>" , { } ) <EOL> product_filter = product_filter . items ( ) <EOL> cache_key = "<STR_LIT>" % ( <EOL> settings . CACHE_MIDDLEWARE_KEY_PREFIX , <EOL> slug ) <EOL> sub_cache_key = "<STR_LIT>" % ( <EOL> settings . CACHE_MIDDLEWARE_KEY_PREFIX , <EOL> start , sorting ) <EOL> filter_key = [ "<STR_LIT>" % ( i [ <NUM_LIT:0> ] , i [ <NUM_LIT:1> ] ) for i in product_filter ] <EOL> if filter_key : <EOL> sub_cache_key += "<STR_LIT>" % "<STR_LIT:->" . join ( filter_key ) <EOL> price_filter = request . session . get ( "<STR_LIT>" ) <EOL> if price_filter : <EOL> sub_cache_key += "<STR_LIT>" % ( price_filter [ "<STR_LIT>" ] , price_filter [ "<STR_LIT>" ] ) <EOL> temp = cache . get ( cache_key ) <EOL> if temp is not None : <EOL> try : <EOL> return temp [ sub_cache_key ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> temp = dict ( ) <EOL> manufacturer = lfs_get_object_or_404 ( Manufacturer , slug = slug ) <EOL> try : <EOL> start = int ( start ) <EOL> except ( ValueError , TypeError ) : <EOL> start = <NUM_LIT:1> <EOL> format_info = manufacturer . get_format_info ( ) <EOL> amount_of_rows = format_info [ "<STR_LIT>" ] <EOL> amount_of_cols = format_info [ "<STR_LIT>" ] <EOL> amount = amount_of_rows * amount_of_cols <EOL> all_products = manufacturer . get_filtered_products ( product_filter , <EOL> price_filter , <EOL> sorting ) <EOL> paginator = Paginator ( all_products , amount ) <EOL> try : <EOL> current_page = paginator . page ( start ) <EOL> except ( EmptyPage , InvalidPage ) : <EOL> current_page = paginator . page ( paginator . num_pages ) <EOL> row = [ ] <EOL> products = [ ] <EOL> for i , product in enumerate ( current_page . object_list ) : <EOL> if product . is_product_with_variants ( ) : <EOL> default_variant = product . get_default_variant ( ) <EOL> if default_variant : <EOL> product = default_variant <EOL> image = None <EOL> product_image = product . get_image ( ) <EOL> if product_image : <EOL> image = product_image . image <EOL> row . append ( { <EOL> "<STR_LIT>" : product , <EOL> "<STR_LIT>" : product . slug , <EOL> "<STR_LIT:name>" : product . get_name ( ) , <EOL> "<STR_LIT:image>" : image , <EOL> "<STR_LIT>" : product . price_unit , <EOL> "<STR_LIT>" : product . price_includes_tax ( request ) , <EOL> } ) <EOL> if ( i + <NUM_LIT:1> ) % amount_of_cols == <NUM_LIT:0> : <EOL> products . append ( row ) <EOL> row = [ ] <EOL> if len ( row ) > <NUM_LIT:0> : <EOL> products . append ( row ) <EOL> amount_of_products = all_products . count ( ) <EOL> pagination_data = lfs_pagination ( request , <EOL> current_page , <EOL> url = manufacturer . get_absolute_url ( ) ) <EOL> pagination_data [ '<STR_LIT>' ] = ungettext ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> amount_of_products ) % { '<STR_LIT:count>' : amount_of_products } <EOL> result = render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : manufacturer , <EOL> "<STR_LIT>" : products , <EOL> "<STR_LIT>" : amount_of_products , <EOL> "<STR_LIT>" : pagination_data , <EOL> "<STR_LIT>" : all_products , <EOL> } ) ) <EOL> temp [ sub_cache_key ] = result <EOL> cache . set ( cache_key , temp ) <EOL> return result </s>
