<s> import re <EOL> import urlparse <EOL> import json <EOL> from django . contrib . auth . decorators import permission_required <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponse <EOL> from django . http import HttpResponseRedirect <EOL> from django . template import RequestContext <EOL> from django . template . loader import render_to_string <EOL> from django . views . decorators . http import require_POST <EOL> from django . utils . translation import ugettext as _ , ungettext <EOL> from django . core . paginator import Paginator , EmptyPage , InvalidPage <EOL> from lfs . catalog . models import Image <EOL> from lfs . catalog . settings import THUMBNAIL_SIZES <EOL> from lfs . core . utils import LazyEncoder , lfs_pagination <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def images ( request , as_string = False , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> req = request . POST if request . method == '<STR_LIT:POST>' else request . GET <EOL> start = req . get ( '<STR_LIT:start>' ) <EOL> try : <EOL> start = int ( start ) <EOL> except ( ValueError , TypeError ) : <EOL> start = <NUM_LIT:1> <EOL> query = req . get ( '<STR_LIT:q>' , '<STR_LIT>' ) <EOL> if query : <EOL> images_qs = Image . objects . filter ( content_id = None , title__istartswith = query ) <EOL> else : <EOL> images_qs = Image . objects . filter ( content_id = None ) <EOL> paginator = Paginator ( images_qs , <NUM_LIT:50> ) <EOL> try : <EOL> current_page = paginator . page ( start ) <EOL> except ( EmptyPage , InvalidPage ) : <EOL> current_page = paginator . page ( paginator . num_pages ) <EOL> amount_of_images = images_qs . count ( ) <EOL> pagination_data = lfs_pagination ( request , current_page , url = request . path ) <EOL> pagination_data [ '<STR_LIT>' ] = ungettext ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> amount_of_images ) % { '<STR_LIT:count>' : amount_of_images } <EOL> result = render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : current_page . object_list , <EOL> "<STR_LIT>" : pagination_data , <EOL> "<STR_LIT>" : query <EOL> } ) ) <EOL> if as_string : <EOL> return result <EOL> return HttpResponse ( result ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def images_list ( request , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> result = images ( request , as_string = True , template_name = template_name ) <EOL> result = json . dumps ( { <EOL> "<STR_LIT:html>" : result , <EOL> "<STR_LIT:message>" : _ ( u"<STR_LIT>" ) , <EOL> } , cls = LazyEncoder ) <EOL> return HttpResponse ( result , content_type = '<STR_LIT:application/json>' ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> @ require_POST <EOL> def delete_images ( request ) : <EOL> """<STR_LIT>""" <EOL> Image . objects . filter ( pk__in = request . POST . getlist ( "<STR_LIT>" ) ) . delete ( ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def add_images ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . method == "<STR_LIT:POST>" : <EOL> for file_content in request . FILES . getlist ( "<STR_LIT>" ) : <EOL> image = Image ( title = file_content . name ) <EOL> try : <EOL> image . image . save ( file_content . name , file_content , save = True ) <EOL> except Exception , e : <EOL> image . delete ( ) <EOL> logger . info ( "<STR_LIT>" % ( file_content . name , e ) ) <EOL> continue <EOL> result = json . dumps ( { "<STR_LIT:name>" : file_content . name , "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT:size>" : "<STR_LIT>" } ) <EOL> return HttpResponse ( result , content_type = '<STR_LIT:application/json>' ) <EOL> @ permission_required ( "<STR_LIT>" ) <EOL> def imagebrowser ( request , template_name = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> selected_size = None <EOL> selected_image = None <EOL> selected_class = request . GET . get ( "<STR_LIT:class>" ) <EOL> url = request . GET . get ( "<STR_LIT:url>" ) <EOL> start = request . GET . get ( '<STR_LIT:start>' , <NUM_LIT:1> ) <EOL> if url : <EOL> parsed_url = urlparse . urlparse ( url ) <EOL> try : <EOL> temp_url = "<STR_LIT:/>" . join ( parsed_url . path . split ( "<STR_LIT:/>" ) [ <NUM_LIT:2> : ] ) <EOL> result = re . search ( "<STR_LIT>" , temp_url ) <EOL> if result : <EOL> temp_url = result . groups ( ) [ <NUM_LIT:0> ] + result . groups ( ) [ <NUM_LIT:3> ] <EOL> selected_image = Image . objects . get ( image = temp_url ) <EOL> selected_size = result . groups ( ) [ <NUM_LIT:2> ] <EOL> else : <EOL> selected_size = None <EOL> except ( IndexError , Image . DoesNotExist ) : <EOL> pass <EOL> sizes = [ ] <EOL> for size in THUMBNAIL_SIZES : <EOL> size = "<STR_LIT>" % ( size [ <NUM_LIT:0> ] , size [ <NUM_LIT:1> ] ) <EOL> sizes . append ( { <EOL> "<STR_LIT:value>" : size , <EOL> "<STR_LIT:title>" : size , <EOL> "<STR_LIT>" : size == selected_size , <EOL> } ) <EOL> classes = [ { "<STR_LIT:value>" : '<STR_LIT>' , <EOL> "<STR_LIT:title>" : _ ( u'<STR_LIT>' ) , <EOL> "<STR_LIT>" : '<STR_LIT>' == selected_class } , <EOL> { "<STR_LIT:value>" : '<STR_LIT:left>' , <EOL> "<STR_LIT:title>" : _ ( u'<STR_LIT:left>' ) , <EOL> "<STR_LIT>" : '<STR_LIT:left>' == selected_class } , <EOL> { "<STR_LIT:value>" : '<STR_LIT:right>' , <EOL> "<STR_LIT:title>" : _ ( u'<STR_LIT:right>' ) , <EOL> "<STR_LIT>" : '<STR_LIT:right>' == selected_class } ] <EOL> try : <EOL> start = int ( start ) <EOL> except ( ValueError , TypeError ) : <EOL> start = <NUM_LIT:1> <EOL> query = ( request . POST if request . method == '<STR_LIT:POST>' else request . GET ) . get ( '<STR_LIT:q>' , '<STR_LIT>' ) <EOL> if query : <EOL> images_qs = Image . objects . filter ( content_id = None , title__istartswith = query ) <EOL> else : <EOL> images_qs = Image . objects . filter ( content_id = None ) <EOL> paginator = Paginator ( images_qs , <NUM_LIT> ) <EOL> try : <EOL> current_page = paginator . page ( start ) <EOL> except ( EmptyPage , InvalidPage ) : <EOL> current_page = paginator . page ( paginator . num_pages ) <EOL> amount_of_images = images_qs . count ( ) <EOL> pagination_data = lfs_pagination ( request , current_page , url = request . path ) <EOL> pagination_data [ '<STR_LIT>' ] = ungettext ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> amount_of_images ) % { '<STR_LIT:count>' : amount_of_images } <EOL> images = [ ] <EOL> for i , image in enumerate ( current_page . object_list ) : <EOL> images . append ( { <EOL> "<STR_LIT:id>" : image . id , <EOL> "<STR_LIT:title>" : image . title , <EOL> "<STR_LIT>" : image == selected_image , <EOL> "<STR_LIT:url>" : image . image . url_100x100 , <EOL> } ) <EOL> html = render_to_string ( template_name , RequestContext ( request , { <EOL> "<STR_LIT>" : sizes , <EOL> "<STR_LIT>" : classes , <EOL> "<STR_LIT>" : images , <EOL> "<STR_LIT>" : query , <EOL> "<STR_LIT>" : pagination_data <EOL> } ) ) <EOL> result = json . dumps ( { <EOL> "<STR_LIT:html>" : html , <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> } , cls = LazyEncoder ) <EOL> return HttpResponse ( result , content_type = '<STR_LIT:application/json>' ) </s>
