Beispiel #1
0
	def post(self, request, *args, **kwargs):
		try:
			thread_id = int(kwargs['thread_id'])
			name = request.POST['name']
			mail = request.POST['mail']
			body = request.POST['body']
			try:
				attach = request.POST['attach']
				attach_sfx = request.POST['attach_sfx']
			except KeyError:
				attach = None
				attach_sfx = None
		except KeyError:
			return HttpResponseBadRequest()

		timestamp = time.time()
		try:
			recStr = makeRecordStr(timestamp, name, mail, body, attach, attach_sfx)
		except BadRecordException:
			return HttpResponseBadRequest()
		_, bin_id_hex, body = tuple(str2recordInfo(recStr))[0]
		bin_id = a2b_hex(bin_id_hex)
		with Session() as s:
			try:
				Record.add(s, thread_id, timestamp, bin_id, body)
				s.commit()
				threadTitle = Thread.get(s, id=thread_id).value(Thread.title)
				Recent.add(s, timestamp, bin_id, Thread.getFileName(threadTitle))
				s.commit()
				msgqueue.updateRecord(thread_id, bin_id_hex, timestamp)
			except IntegrityError:
				s.rollback()
		return HttpResponse()
Beispiel #2
0
def getRecord(msg):
	with Session() as s:
		if len(msg.msg.split()) == 4:
			mode = 'single'
			addr, thread_id, hex_id, atime = msg.msg.split()
			thread_id = int(thread_id)
			bin_id = a2b_hex(hex_id)
			atime = int(atime)
		else:
			mode = 'multi'
			addr, thread_id, timeRange = msg.msg.split()

		if mode == 'single' and Record.get(s, thread_id, bin_id, atime).value(Record.record_id):
			log.isEnabledFor(logging.INFO) and log.info('getRecord: NOP {}/{}/{} {}'.format(thread_id, atime, hex_id, addr))
			return True

		title = Thread.get(s, id=thread_id).value(Thread.title)
		filename = Thread.getFileName(title)
		if mode == 'single':
			http_addr = 'http://{}/get/{}/{}'.format(addr, filename, atime)
		else:
			http_addr = 'http://{}/get/{}/{}'.format(addr, filename, timeRange)
		try:
			for record in str2recordInfo(httpGet(http_addr)):
				try:
					timestamp, hex_id, body = record
					bin_id = a2b_hex(hex_id)
				except binascii.Error as e:
					log.isEnabledFor(logging.INFO) and log.info('getRecord: Fail {} {} {}'.format(thread_id, http_addr, str(e)))
					continue
				try:
					timestamp = int(timestamp)
					if Record.get(s, thread_id, bin_id, timestamp).first():
						continue
					Record.add(s, thread_id, timestamp, bin_id, body)
					log.isEnabledFor(logging.INFO) and log.info('getRecord: Add {}/{}/{} {}'.format(thread_id, timestamp, b2a_hex(bin_id), addr))
					s.commit()
				except (binascii.Error, sqlalchemy.exc.StatementError) as e:
					log.isEnabledFor(logging.INFO) and log.info('getRecord: Fail {} {} {}'.format(thread_id, http_addr, str(e)))
					s.rollback()
					Record.delete(s, thread_id, timestamp, bin_id)
					s.commit()
		except URLError as e:
			log.isEnabledFor(logging.INFO) and log.info('getRecord: Fail {} {} {}'.format(thread_id, http_addr, str(e)))
			return False
		return True