| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import json | 5 import json |
| 6 import calendar | |
| 7 | 6 |
| 8 from google.appengine.datastore.datastore_query import Cursor | 7 from google.appengine.datastore.datastore_query import Cursor |
| 9 import webapp2 | 8 import webapp2 |
| 10 | 9 |
| 11 from shared.config import MAXIMUM_QUERY_SIZE | 10 from shared.config import MAXIMUM_QUERY_SIZE |
| 12 from shared.parsing import ( | 11 from shared.parsing import ( |
| 13 parse_cursor, | 12 parse_cursor, |
| 14 parse_url_tags, | 13 parse_url_tags, |
| 15 parse_fields, | 14 parse_fields, |
| 16 parse_key, | 15 parse_key, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 for tag in tags: | 47 for tag in tags: |
| 49 filters.append(Record.tags == tag) | 48 filters.append(Record.tags == tag) |
| 50 query = Record.query().filter(*filters).order(-Record.timestamp) | 49 query = Record.query().filter(*filters).order(-Record.timestamp) |
| 51 page_records, next_cursor, more = query.fetch_page(count - len(records), | 50 page_records, next_cursor, more = query.fetch_page(count - len(records), |
| 52 start_cursor=Cursor(urlsafe=next_cursor or cursor)) | 51 start_cursor=Cursor(urlsafe=next_cursor or cursor)) |
| 53 next_cursor = next_cursor.urlsafe() if next_cursor else '' | 52 next_cursor = next_cursor.urlsafe() if next_cursor else '' |
| 54 for record in page_records: | 53 for record in page_records: |
| 55 if matches_fields(fields, record): | 54 if matches_fields(fields, record): |
| 56 records.append(record) | 55 records.append(record) |
| 57 | 56 |
| 58 results = [] | |
| 59 for record in records: | |
| 60 result = record.to_dict(exclude=['timestamp']) | |
| 61 result['timestamp'] = calendar.timegm(record.timestamp.timetuple()) | |
| 62 record_key = record.key.id() | |
| 63 result['key'] = record_key if type(record_key) != long else None | |
| 64 results.append(result) | |
| 65 | |
| 66 return { | 57 return { |
| 67 'results': results, | 58 'results': [record.to_dict() for record in records], |
| 68 'cursor': next_cursor, | 59 'cursor': next_cursor, |
| 69 'more': more, | 60 'more': more, |
| 70 } | 61 } |
| 71 | 62 |
| 72 def matches_fields(fields, record): # pragma: no cover | 63 def matches_fields(fields, record): # pragma: no cover |
| 73 for field, value in fields.items(): | 64 for field, value in fields.items(): |
| 74 if not field in record.fields or record.fields[field] != value: | 65 if not field in record.fields or record.fields[field] != value: |
| 75 return False | 66 return False |
| 76 return True | 67 return True |
| 77 | 68 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 89 }) | 80 }) |
| 90 data['tags'].extend(parse_url_tags(url_tags)) | 81 data['tags'].extend(parse_url_tags(url_tags)) |
| 91 except ValueError, e: | 82 except ValueError, e: |
| 92 self.response.write(e) | 83 self.response.write(e) |
| 93 return | 84 return |
| 94 | 85 |
| 95 results = execute_query(**data) | 86 results = execute_query(**data) |
| 96 self.response.headers.add_header("Access-Control-Allow-Origin", "*") | 87 self.response.headers.add_header("Access-Control-Allow-Origin", "*") |
| 97 self.response.headers.add_header('Content-Type', 'application/json') | 88 self.response.headers.add_header('Content-Type', 'application/json') |
| 98 json.dump(results, self.response) | 89 json.dump(results, self.response) |
| OLD | NEW |