<s> """<STR_LIT>""" <EOL> import logging <EOL> import math <EOL> import urllib <EOL> import urllib2 <EOL> try : <EOL> from cStringIO import cStringIO as StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> from reportlab . pdfgen import canvas <EOL> from reportlab . lib import pagesizes <EOL> from reportlab . lib . units import inch <EOL> from reportlab . platypus import ( <EOL> SimpleDocTemplate , BaseDocTemplate , Paragraph , Preformatted , Spacer , <EOL> PageBreak , Frame , FrameBreak , PageTemplate , Image , Table ) <EOL> from reportlab . platypus . doctemplate import LayoutError <EOL> from reportlab . lib . styles import getSampleStyleSheet , ParagraphStyle <EOL> from reportlab . rl_config import defaultPageSize <EOL> from reportlab . lib . units import inch <EOL> from reportlab . lib . enums import TA_LEFT , TA_CENTER <EOL> from reportlab . lib import colors <EOL> from reportlab . lib import textsplit <EOL> from reportlab . lib . utils import ImageReader <EOL> from django . conf import settings <EOL> from django . http import ( HttpResponseRedirect , HttpResponse , <EOL> HttpResponseForbidden , HttpResponseNotFound ) <EOL> from django . utils . html import conditional_escape <EOL> def render_claims_to_pdf ( request , slug , claim_group , deferred_awards ) : <EOL> """<STR_LIT>""" <EOL> metrics = dict ( <EOL> page_width = ( <NUM_LIT> * inch ) , <EOL> page_height = ( <NUM_LIT> * inch ) , <EOL> top_margin = ( <NUM_LIT:0.5> * inch ) , <EOL> left_margin = ( ( <NUM_LIT> / <NUM_LIT> ) * inch ) , <EOL> qr_overlap = ( ( <NUM_LIT:1.0> / <NUM_LIT> ) * inch ) , <EOL> padding = ( ( <NUM_LIT:1.0> / <NUM_LIT> ) * inch ) , <EOL> horizontal_spacing = ( ( <NUM_LIT> / <NUM_LIT> ) * inch ) , <EOL> vertical_spacing = ( ( <NUM_LIT> / <NUM_LIT> ) * inch ) , <EOL> width = ( <NUM_LIT> * inch ) , <EOL> height = ( <NUM_LIT> * inch ) , <EOL> ) <EOL> debug = ( request . GET . get ( '<STR_LIT>' , False ) is not False ) <EOL> pagesize = ( metrics [ '<STR_LIT>' ] , metrics [ '<STR_LIT>' ] ) <EOL> cols = int ( ( metrics [ '<STR_LIT>' ] - metrics [ '<STR_LIT>' ] ) / <EOL> ( metrics [ '<STR_LIT:width>' ] + metrics [ '<STR_LIT>' ] ) ) <EOL> rows = int ( ( metrics [ '<STR_LIT>' ] - metrics [ '<STR_LIT>' ] ) / <EOL> ( metrics [ '<STR_LIT>' ] + metrics [ '<STR_LIT>' ] ) ) <EOL> per_page = ( cols * rows ) <EOL> label_ct = len ( deferred_awards ) <EOL> page_ct = math . ceil ( label_ct / per_page ) <EOL> pages = [ deferred_awards [ x : x + ( per_page ) ] <EOL> for x in range ( <NUM_LIT:0> , label_ct , per_page ) ] <EOL> response = HttpResponse ( content_type = '<STR_LIT>' ) <EOL> if not debug : <EOL> response [ '<STR_LIT>' ] = ( '<STR_LIT>' % <EOL> ( slug . encode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) , claim_group ) ) <EOL> badge_img = None <EOL> fout = StringIO ( ) <EOL> c = canvas . Canvas ( fout , pagesize = pagesize ) <EOL> for page in pages : <EOL> c . translate ( metrics [ '<STR_LIT>' ] , <EOL> metrics [ '<STR_LIT>' ] - metrics [ '<STR_LIT>' ] ) <EOL> for row in range ( <NUM_LIT:0> , rows , <NUM_LIT:1> ) : <EOL> c . translate ( <NUM_LIT:0.0> , <NUM_LIT:0> - metrics [ '<STR_LIT>' ] ) <EOL> c . saveState ( ) <EOL> for col in range ( <NUM_LIT:0> , cols , <NUM_LIT:1> ) : <EOL> try : <EOL> da = page . pop ( <NUM_LIT:0> ) <EOL> except IndexError : <EOL> continue <EOL> if not badge_img : <EOL> image_fin = da . badge . image . file <EOL> image_fin . open ( ) <EOL> badge_img = ImageReader ( StringIO ( image_fin . read ( ) ) ) <EOL> c . saveState ( ) <EOL> render_label ( request , c , metrics , da , badge_img , debug ) <EOL> c . restoreState ( ) <EOL> dx = ( metrics [ '<STR_LIT:width>' ] + metrics [ '<STR_LIT>' ] ) <EOL> c . translate ( dx , <NUM_LIT:0.0> ) <EOL> c . restoreState ( ) <EOL> c . translate ( <NUM_LIT:0.0> , <NUM_LIT:0> - metrics [ '<STR_LIT>' ] ) <EOL> c . showPage ( ) <EOL> c . save ( ) <EOL> response . write ( fout . getvalue ( ) ) <EOL> return response <EOL> def render_label ( request , c , metrics , da , badge_img , debug ) : <EOL> """<STR_LIT>""" <EOL> badge = da . badge <EOL> badge_image_width = ( <NUM_LIT:1.0> + ( <NUM_LIT:1.0> / <NUM_LIT> ) ) * inch <EOL> badge_image_height = ( <NUM_LIT:1.0> + ( <NUM_LIT:1.0> / <NUM_LIT> ) ) * inch <EOL> qr_left = badge_image_width - metrics [ '<STR_LIT>' ] <EOL> qr_bottom = badge_image_height - metrics [ '<STR_LIT>' ] <EOL> qr_width = metrics [ '<STR_LIT:width>' ] - qr_left <EOL> qr_height = metrics [ '<STR_LIT>' ] - qr_bottom <EOL> if False and debug : <EOL> c . setLineWidth ( <NUM_LIT> ) <EOL> c . rect ( <NUM_LIT:0> , <NUM_LIT:0> , metrics [ '<STR_LIT:width>' ] , metrics [ '<STR_LIT>' ] ) <EOL> c . rect ( qr_left , qr_bottom , qr_width , qr_height ) <EOL> c . rect ( <NUM_LIT:0> , <NUM_LIT:0> , badge_image_width , badge_image_height ) <EOL> fit_text ( c , da . badge . title , <EOL> <NUM_LIT:0.0> , badge_image_height , <EOL> badge_image_width , qr_height ) <EOL> c . saveState ( ) <EOL> c . rotate ( - <NUM_LIT> ) <EOL> code_height = qr_height * ( <NUM_LIT> ) <EOL> claim_height = qr_height - code_height <EOL> c . setFont ( "<STR_LIT>" , code_height ) <EOL> c . drawCentredString ( <NUM_LIT:0> - ( badge_image_width / <NUM_LIT> ) , <EOL> metrics [ '<STR_LIT>' ] - code_height , <EOL> da . claim_code ) <EOL> text = """<STR_LIT>""" % ( settings . SITE_TITLE ) <EOL> fit_text ( c , text , <EOL> <NUM_LIT:0> - badge_image_height , badge_image_width , <EOL> badge_image_width , claim_height ) <EOL> c . restoreState ( ) <EOL> claim_url = request . build_absolute_uri ( da . get_claim_url ( ) ) <EOL> qr_img = None <EOL> try : <EOL> from PyQRNative import QRCode , QRErrorCorrectLevel <EOL> if len ( claim_url ) < <NUM_LIT:20> : <EOL> qr = QRCode ( <NUM_LIT:3> , QRErrorCorrectLevel . L ) <EOL> elif len ( claim_url ) < <NUM_LIT:50> : <EOL> qr = QRCode ( <NUM_LIT:4> , QRErrorCorrectLevel . L ) <EOL> else : <EOL> qr = QRCode ( <NUM_LIT:10> , QRErrorCorrectLevel . L ) <EOL> qr . addData ( claim_url ) <EOL> qr . make ( ) <EOL> qr_img = ImageReader ( qr . makeImage ( ) ) <EOL> except ImportError : <EOL> try : <EOL> qr_url = ( "<STR_LIT>" % <EOL> urllib . urlencode ( { '<STR_LIT:size>' : '<STR_LIT>' % ( <NUM_LIT> , <NUM_LIT> ) , <EOL> '<STR_LIT:data>' : claim_url } ) ) <EOL> qr_img = ImageReader ( StringIO ( urllib2 . urlopen ( qr_url ) . read ( ) ) ) <EOL> except Exception : <EOL> pass <EOL> if qr_img : <EOL> c . drawImage ( qr_img , qr_left , qr_bottom , qr_width , qr_height ) <EOL> c . drawImage ( badge_img , <EOL> <NUM_LIT:0.0> * inch , <NUM_LIT:0.0> * inch , <EOL> badge_image_width , badge_image_height ) <EOL> def fit_text ( c , text , x , y , max_w , max_h , font_name = '<STR_LIT>' , <EOL> padding_w = <NUM_LIT> , padding_h = <NUM_LIT> , font_decrement = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> max_w -= ( padding_w * <NUM_LIT> ) <EOL> max_h -= ( padding_h * <NUM_LIT> ) <EOL> x += padding_w <EOL> y += padding_h <EOL> font_size = max_h <EOL> while font_size > <NUM_LIT:1.0> : <EOL> ps = ParagraphStyle ( name = '<STR_LIT:text>' , alignment = TA_CENTER , <EOL> fontName = font_name , fontSize = font_size , <EOL> leading = font_size ) <EOL> p = Paragraph ( text , ps ) <EOL> actual_w , actual_h = p . wrapOn ( c , max_w , max_h ) <EOL> if actual_h > max_h or actual_w > max_w : <EOL> font_size -= font_decrement <EOL> else : <EOL> y_pad = ( max_h - actual_h ) / <NUM_LIT:2> <EOL> p . drawOn ( c , x , y + y_pad ) <EOL> return </s>
