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()
def updateRecord(msg): with Session() as s: thread_id, hex_id, atime, nodeName = msg.msg.split() thread_id = int(thread_id) atime = int(atime) filename = Thread.getFileName(Thread.get(s, id=thread_id).value(Thread.title)) queue = Queue() for host in Node.getLinkedNode(s).values(Node.host): queue.put(( host.host, filename, atime, hex_id, thread_id, nodeName )) log.isEnabledFor(logging.INFO) and log.info('updateRecord: Run {}/{}/{} {}'.format(thread_id, atime, hex_id, nodeName)) multiThread(_updateRecord_httpGetWrapper, queue, maxWorkers=settings.MAX_CONNECTIONS) return True
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
def post(self, request, *args, **kwargs): title = request.POST['title'] with Session() as s: query = Thread.get(s, title=title).all() if len(query) == 0: thread = Thread.add(s, title) MessageQueue.enqueue(s, msgtype='get_thread', msg=Thread.getFileName(title)) s.commit() notify() else: thread = query[0] return JsonResponse({ 'thread': { "id": thread.id, "title": thread.title, "timestamp": thread.timestamp, "records": thread.records, } })