Esempio n. 1
0
def show(request, object_id):
#	if not request.is_ajax():
#		return HttpResponseNotAllowed('Invalid request (use ajax)')
	object_id = long(object_id)
	if object_id is None:
		return HttpResponseBadRequest(_("Missing argument: %s") % 'object_id')
	messages = Message.objects \
		.filter(conversation_id=object_id) \
		.order_by('timestamp')
	if not messages:
		#return HttpResponseBadRequest(_('Conversation not found'))
		raise Http404('No %s matches the given query.' % 'Message')
	messages[0].clientip = ip_long_to_str(messages[0].clientip)
	context_instance = RequestContext(request)
	template_name = 'conversations/show.html'
	extra_context = {
		'first_message': messages[0],
		'messages': messages,
		'colors': color_dict()
	}
	return render_to_response(template_name, extra_context, context_instance)
Esempio n. 2
0
def report_pdf(request, object_id):
	object_id = long(object_id)
	if object_id is None:
		return HttpResponseBadRequest(_("Missing argument: %s") % 'object_id')
	messages = Message.objects \
		.filter(conversation_id=object_id) \
		.order_by('timestamp')
	if not messages:
		return HttpResponseBadRequest(_('Conversation not found'))
	messages[0].clientip = ip_long_to_str(messages[0].clientip)

	# PDF generation
	styles = getSampleStyleSheet()
	styles.add(ParagraphStyle(name='Header',
		alignment=TA_JUSTIFY,
		parent=styles['Normal'],
		fontName='Times-Roman',
		fontSize=10,
		leading=11,
		firstLineIndent=0,
		leftIndent=0))
	styles.add(ParagraphStyle(name='Message',
		alignment=TA_JUSTIFY,
		parent=styles['Normal'],
		fontName='Times-Roman',
		fontSize=10,
		leading=11,
		firstLineIndent=0,
		leftIndent=0))
	buffer = StringIO()
	PAGESIZE = A4

	def renderHeader(canvas, doc):
		def headerPara(key, value):
			return Paragraph('<strong>%s</strong>: %s' % (key, value), styles['Header'])
		def headerPara2(key1, value1, key2, value2):
			return Paragraph('<strong>%s</strong>: %s <strong>%s</strong>: %s'
				% (key1, value1, key2, value2), styles['Header'])
#		def renderPageNumber(canvas):
#			x, y = coord_tr(PAGESIZE, 1.5*cm, 1,5*cm)
#			canvas.setFont('Helvetica', 10)
#			canvas.drawRightString(x, y,
#				_('Page %(this)i of %(total)i') % canvas.getPageNumber(), 0)
		canvas.saveState()
		flowables = []
		flowables.append(headerPara(_('Conversation'), '#%i' % messages[0].conversation_id))
		flowables.append(headerPara2(_('User'), messages[0].localim, 'IP', messages[0].clientip))
		flowables.append(headerPara(_('Buddy'), messages[0].remoteim))
		flowables.append(headerPara(_('Started in'), messages[0].timestamp.strftime(_("%m/%d/%Y - %I:%M:%S %p"))))
		flowables.append(headerPara(_('Total messages'), '%i' % len(messages)))
		flowables.append(Spacer(0, 0.5 * cm))
		x, y = coord_tl(PAGESIZE, 1.5 * cm, 1.5 * cm)
		#renderPageNumber(canvas)
		for f in flowables:
			f.wrap(PAGESIZE[0], PAGESIZE[1])
			f.drawOn(canvas, x, y)
			y -= f.height
		canvas.restoreState()

	class ReportCanvas(NumberedCanvas):
		def __init__(self, *args, **kwargs):
			NumberedCanvas.__init__(self, *args, **kwargs)
			self.setAuthor('')
			self.setTitle('')
			self.setSubject('')
			self.setKeywords('')
			#self.setEncrypt()
		def drawPageNumber(self, page_count):
			self.setFont("Times-Roman", 10)
			x, y = coord_tr(self._pagesize, 1.5 * cm, 1.5 * cm)
			self.drawRightString(x, y,
				_("Page %(this)i of %(total)i") % {
					'this': self._pageNumber,
					'total': page_count
				}
			)

	def format_sender(msg, colors):
		sender = msg.remoteim if msg.inbound else msg.localim
		return '<font color="' + colors.get(sender) + '">' + sender + '</font>'
	def format_prefix(msg, colors):
		time = msg.timestamp.strftime(_('%m/%d/%Y %I:%M:%S %p'))
		sender = format_sender(msg, colors)
		text = '<strong>' + time + ' - ' + sender + '</strong>: '
		if msg.filtered:
			return text + '<font color="red"><b>[x]</b></font> '
		return text
	def format_msg(msg, colors):
		return format_prefix(msg, colors) + escape(msg.content)
	def format_file(msg, colors):
		parts = msg.content.split(' ')
		message = '<b>%s</b>: %s (%s)' % (
			_('File transfer'),
			escape(' '.join(parts[1:])),
			filesizeformat(parts[0])
		)
		return format_prefix(msg, colors) + message
	def format_webcam(msg, colors):
		return format_prefix(msg, colors) + '<b>%s</b>' % _('Video Call')
	def format_remotedesktop(msg, colors):
		return format_prefix(msg, colors) + '<b>%s</b>' % _('Remote Desktop')
	def format_application(msg, colors):
		return format_prefix(msg, colors) + '<b>%s</b>' % _('MSN Activity')
	def format_emoticon(msg, colors):
		return format_prefix(msg, colors) + '<b>%s</b>' % _('Custom emoticon')
	def format_ink(msg, colors):
		return format_prefix(msg, colors) + '<b>%s</b>' % _('Handwriting')
	def format_nudge(msg, colors):
		return format_prefix(msg, colors) + '<b>%s</b>' % _('Nudge')
	def format_wink(msg, colors):
		return format_prefix(msg, colors) + '<b>%s</b>' % _('Wink')
	def format_voiceclip(msg, colors):
		return format_prefix(msg, colors) + '<b>%s</b>' % _('Voice clip')
	def format_games(msg, colors):
		return format_prefix(msg, colors) + '<b>%s</b>' % _('MSN Game')
	def format_photo(msg, colors):
		return format_prefix(msg, colors) + '<b>%s</b>' % _('Photo sharing')
	def messagePara(msg, colors):
		formatters = {
			#Message.Type.UNKNOWN: format_msg,
			Message.Type.MSG: format_msg,
			Message.Type.FILE: format_file,
			#Message.Type.TYPING: format_msg,
			#Message.Type.CAPS: format_msg,
			Message.Type.WEBCAM: format_webcam,
			Message.Type.REMOTEDESKTOP: format_remotedesktop,
			Message.Type.APPLICATION: format_application,
			Message.Type.EMOTICON: format_emoticon,
			Message.Type.INK: format_ink,
			Message.Type.NUDGE: format_nudge,
			Message.Type.WINK: format_wink,
			Message.Type.VOICECLIP: format_voiceclip,
			Message.Type.GAMES: format_games,
			Message.Type.PHOTO: format_photo,
		}
		text = formatters.get(msg.type, lambda x: None)(msg, colors)
		return Paragraph(text, styles['Message'])
	contents = []
	colors = color_dict()
	# Force localim and remoteim colors
	if len(messages) > 0:
		colors.get(messages[0].localim)
		colors.get(messages[0].remoteim)
	for msg in messages:
		contents.append(messagePara(msg, colors))

	doc = SimpleDocTemplate(buffer, pagesize=PAGESIZE,
		rightMargin=1.5 * cm, leftMargin=1.3 * cm,
		topMargin=3.5 * cm, bottomMargin=1.5 * cm)
	doc.build(
		contents,
		onFirstPage=renderHeader,
		onLaterPages=renderHeader,
		canvasmaker=ReportCanvas
	)

	response = HttpResponse(buffer.getvalue(), mimetype='application/pdf')
	response['Content-Disposition'] = 'attachment; filename=report-%i.pdf' % object_id
	buffer.close()
	return response