def dispatch(self, request, *args, **kwargs): with Session() as s: try: prefix, basename = splitFileName(kwargs['file']) stime, etime = getTimeRange( kwargs.get('time'), kwargs.get('stime'), kwargs.get('etime'), ) except (BadFileNameException, BadTimeRange): return HttpResponseBadRequest() response = HttpResponse() if prefix=='thread': try: title = a2b_hex(basename) except binascii.Error: return HttpResponseBadRequest() thread_id = Thread.get(s, title=title).value(Thread.id) allRecords = Record.gets(s, thread_id, stime, etime) for line in record2str(allRecords, 1): response.write(line) return response
def get(self, request, *args, **kwargs): records = [] thread_id = int(kwargs['thread_id']) with Session() as s: if 'timestamp' in kwargs: """ 単一のレコードを返す方のAPI """ timestamp = int(kwargs['timestamp']) bin_id = a2b_hex(kwargs['record_id']) r = Record.get(s, thread_id, bin_id, timestamp).with_entities( Record.bin_id, Record.timestamp, Record.name, Record.mail, Record.body, sql_func.length(Record.attach).label('attach_len'), Record.suffix).first() if r: records.append({ 'id': b2a_hex(r.bin_id).decode('ascii'), 'timestamp': int(datetime2timestamp(r.timestamp)), 'name': r.name, 'mail': r.mail, 'body': r.body, 'attach': bool(r.attach_len), 'suffix': r.suffix, }) else: """ 複数のレコードを返す方のAPI """ bin_id = request.GET.get('record_id') if bin_id: bin_id = a2b_hex(bin_id) matchRecords = Record.gets(s, thread_id=thread_id, stime=intOrNone(request.GET.get('start_time')), etime=intOrNone(request.GET.get('end_time')), bin_id=bin_id, limit=intOrNone(request.GET.get('limit')), ).with_entities( Record.bin_id, Record.timestamp, Record.name, Record.mail, Record.body, sql_func.length(Record.attach).label('attach_len'), Record.suffix) for r in matchRecords: records.append({ 'id': b2a_hex(r.bin_id).decode('ascii'), 'timestamp': int(datetime2timestamp(r.timestamp)), 'name': r.name, 'mail': r.mail, 'body': r.body, 'attach': bool(r.attach_len), 'suffix': r.suffix, }) obj = { 'records': records, } return JsonResponse(obj)