Ejemplo n.º 1
0
def ssplit(readf, writef, fn, *arg):
    out = PdfFileWriter()
    inp = PdfFileReader(readf, False)

    for i in range(inp.getNumPages()):
        page = inp.getPage(i)
        fn(page, out, *arg)

    out.write(writef)
Ejemplo n.º 2
0
def ssplit(readf, writef, fn, *arg):
    out = PdfFileWriter()
    inp = PdfFileReader(readf, False)

    for i in range(inp.getNumPages()):
        page = inp.getPage(i)
        fn(page, out, *arg)

    out.write(writef)
Ejemplo n.º 3
0
def make_labels(event_name, type, present=None, event_id=None):
	import time
	import os
	import StringIO
	from datetime import datetime

	year = datetime.now().year
	filename='labels'

	from reportlab.platypus import SimpleDocTemplate, Paragraph, Table, TableStyle, Image, Spacer
	from reportlab.platypus.flowables import PageBreak
	from reportlab.lib.styles import ParagraphStyle
	from reportlab.lib.enums import TA_CENTER, TA_LEFT
	from reportlab.lib.pagesizes import letter, inch
	from reportlab.lib import colors

	doc_io = StringIO.StringIO()

	doc = SimpleDocTemplate(doc_io, pagesize=letter, 
		rightMargin=0.18*inch, leftMargin=0.18*inch, topMargin=0.4*inch, bottomMargin=0)

	elements = list()

	if type == 'leaders':
		if present is not None and event_id is not None:
			if present == True:
				people = db((db.person.leader==True) & (db.attendance.present==True)).select(
					db.person.ALL, db.crew.room, db.crew.wefsk,
					left=[db.crew.on(db.person.crew==db.crew.id),
						db.attendance.on((db.person.id==db.attendance.person_id) & 
							(db.attendance.event_id==event_id))],
					orderby=[db.person.last_name, db.person.first_name])
			else:
				people = db((db.person.leader==True) & 
						((db.attendance.present==False) | (db.attendance.present==None))).select(
					db.person.ALL, db.crew.room, db.crew.wefsk,
					left=[db.crew.on(db.person.crew==db.crew.id),
						db.attendance.on((db.person.id==db.attendance.person_id) & 
							(db.attendance.event_id==event_id))],
					orderby=[db.person.last_name, db.person.first_name])
		else:
			people = db(db.person.leader==True).select(
				db.person.ALL, db.crew.room, db.crew.wefsk,
				left=db.crew.on(db.person.crew==db.crew.id),
				orderby=[db.person.last_name, db.person.first_name])

	else:

		if present is not None and event_id is not None:
			if present == True:
				people = db((db.person.grade==9) & (db.attendance.present==True)).select(
					db.person.ALL, db.crew.room, db.crew.wefsk,
					left=[db.crew.on(db.person.crew==db.crew.id),
						db.attendance.on((db.person.id==db.attendance.person_id) & 
							(db.attendance.event_id==event_id))],
					orderby=[db.person.last_name, db.person.first_name])
			else:
				people = db((db.person.grade==9) & 
						((db.attendance.present==False) | (db.attendance.present==None))).select(
					db.person.ALL, db.crew.room, db.crew.wefsk,
					left=[db.crew.on(db.person.crew==db.crew.id),
						db.attendance.on((db.person.id==db.attendance.person_id) & 
							(db.attendance.event_id==event_id))],
					orderby=[db.person.last_name, db.person.first_name])
		else:
			people = db(db.person.grade==9).select(
				db.person.ALL, db.crew.room, db.crew.wefsk,
				left=db.crew.on(db.person.crew==db.crew.id),
				orderby=[db.person.last_name, db.person.first_name])


	centerStyle = ParagraphStyle(name='Center', alignment=TA_CENTER)
	leftStyle = ParagraphStyle(name='Left', alignment=TA_LEFT)
	tableStyle = TableStyle([('VALIGN',(0,-1),(-1,-1),'TOP')])
	label_num = 0
	row_num = 0
	labels = list()

	rotations = {
		'A' : {
			'I' : 'E1 -> P21 -> SS1 -> M1 -> P15',
			'II' : 'P21 -> SS1 -> M1 -> P15 -> E1',
			'III' : 'SS1 -> M1 -> P15 -> E1 -> P21',
			'IV' : 'M1 -> P15 -> E1 -> P21 -> SS1',
			'V' : 'P15 -> E1 -> P21 -> SS1 -> M1'
		},
		'B' : {
			'I' : 'E2 -> P23 -> SS3 -> M2 -> P16',
			'II' : 'P23 -> SS3 -> M2 -> P16 -> E2',
			'III' : 'SS3 -> M2 -> P16 -> E2 -> P23',
			'IV' : 'M2 -> P16 -> E2 -> P23 -> SS3',
			'V' : 'P16 -> E2 -> P23 -> SS3 -> M2'
		},
		'C' : {
			'I' : 'E3 -> P24 -> SS4 -> M3 -> P17',
			'II' : 'P24 -> SS4 -> M3 -> P17 -> E3',
			'III' : 'SS4 -> M3 -> P17 -> E3 -> P24',
			'IV' : 'M3 -> P17 -> E3 -> P24 -> SS4',
			'V' : 'P17 -> E3 -> P24 -> SS4 -> M3'
		},
		'D' : {
			'I' : 'E4 -> P25 -> SS5 -> M4 -> P18',
			'II' : 'P25 -> SS5 -> M4 -> P18 -> E4',
			'III' : 'SS5 -> M4 -> P18 -> E4 -> P25',
			'IV' : 'M4 -> P18 -> E4 -> P25 -> SS5',
			'V' : 'P18 -> E4 -> P25 -> SS5 -> M4'
		},
		'E' : {
			'I' : 'E5 -> P26 -> SS6 -> M5 -> P19',
			'II' : 'P26 -> SS6 -> M5 -> P19 -> E5',
			'III' : 'SS6 -> M5 -> P19 -> E5 -> P26',
			'IV' : 'M5 -> P19 -> E5 -> P26 -> SS6',
			'V' : 'P19 -> E5 -> P26 -> SS6 -> M5'
		}
	}

	def rotation(station, letter):
		return rotations[station][letter]

	for row in people:
		
		label = list()

		if label_num == 2:
			table = Table([labels], colWidths=[4*inch,0.14*inch,4*inch], rowHeights=[2*inch]*(len(labels)/2))
			table.setStyle(tableStyle)
			elements.append(table)

			label_num = 0
			labels = list()
			row_num += 1
			if row_num == 5:
				row_num = 0
				elements.append(PageBreak())

		header = Paragraph("<font face='Times-Bold' size=11>{} {}</font>".format(year, event_name), centerStyle)

		label.append(header)
		label.append(Spacer(1,11))

		firstName = Paragraph("<font face='Times-Bold' size=18>{}</font>".format(row.person.first_name), centerStyle)
		label.append(firstName)
		label.append(Spacer(1, 11))

		lastName = Paragraph("<font face='Times-Roman' size=11>{}</font>".format(row.person.last_name), centerStyle)
		label.append(lastName)
		label.append(Spacer(1,20))

		if row.crew.wefsk != '' or row.crew.wefsk != None or row.crew.wefsk != 'N/A':
			try:
				rooms = rotation(row.crew.wefsk.split('-')[0], row.crew.wefsk.split('-')[1])
			except:
				rooms = 'N/A'
		else:
			rooms = 'N/A'

		label.append(Paragraph("<font face='Times-Roman' size=11>ID#: {}</font>".format(row.person.student_id), leftStyle))
		label.append(Paragraph("<font face='Times-Roman' size=11>Crew #: {}</font>".format(row.person.crew), leftStyle))

		label.append(Paragraph("<font face='Times-Roman' size=11>Crew Room: {}</font>".format(row.crew.room), leftStyle))
		label.append(Paragraph("<font face='Times-Roman' size=11>W.E.F.S.K. Rotation: {}</font>".format(rooms), leftStyle))

		labels.append(label)

		if label_num == 0:
			labels.append(Spacer(14, 144))

		label_num += 1

	doc.build(elements)

	from pypdf2 import PdfFileWriter, PdfFileReader
	from reportlab.pdfgen.canvas import Canvas
	from reportlab.lib.pagesizes import letter

	doc_io.seek(0)
	existing = PdfFileReader(doc_io)
	imagefile = "{}/applications/javelin/static/images/vc2emblem.jpg".format(os.getcwd())

	packet = StringIO.StringIO()
	canvas = Canvas(packet, pagesize=letter)

	x_values = [36, 240, 334, 542]
	y = 708
	for i in range(20):
		x = x_values[i%4]
		if i%4 == 0 and i != 0:
			y -= 144
		canvas.drawImage(imagefile, x, y, 32, 32)

	canvas.showPage()
	canvas.save()

	packet.seek(0)
	emblems = PdfFileReader(packet)

	writer = PdfFileWriter()

	for page in existing.pages:
		page.mergePage(emblems.getPage(0))
		writer.addPage(page)

	output = StringIO.StringIO()
	writer.write(output)
	output.seek(0)


	filename = "{}_{}_{}.pdf".format('_'.join(event_name.lower().split()),filename, int(time.time()))

	file_id = db.file.insert(name=filename, file=db.file.file.store(output, filename))

	db_file = db.file(file_id).file

	return dict(filename=db_file)