| author | ymh <ymh.work@gmail.com> |
| Mon, 19 Nov 2012 22:57:12 +0100 | |
| changeset 74 | 1b68d4acab0d |
| parent 65 | 6289931858a7 |
| child 83 | 07c9aa7de765 |
| permissions | -rw-r--r-- |
| 45 | 1 |
# -*- coding: utf-8 -*- |
2 |
''' |
|
3 |
Created on Nov 14, 2012 |
|
4 |
||
5 |
@author: ymh |
|
6 |
''' |
|
7 |
import urlparse |
|
8 |
import requests |
|
9 |
from . import settings |
|
10 |
import logging |
|
11 |
||
12 |
logger = logging.getLogger(__name__) |
|
13 |
||
14 |
def get_abs_url(url, default_domain): |
|
15 |
||
16 |
if not url: |
|
17 |
return url |
|
18 |
url_part = urlparse.urlparse(url) |
|
19 |
if url_part.netloc: |
|
20 |
return url |
|
21 |
else: |
|
22 |
pr = urlparse.ParseResult('http', default_domain, url_part.path, url_part.params, url_part.query, url_part.fragment) |
|
23 |
return pr.geturl() |
|
24 |
||
25 |
||
26 |
def get_all_objects(res_url, field_filter): |
|
|
65
6289931858a7
add a project property to override the front project.
ymh <ymh.work@gmail.com>
parents:
45
diff
changeset
|
27 |
objects = [] |
| 45 | 28 |
url = res_url |
29 |
while url: |
|
30 |
r = requests.get(url) |
|
31 |
if r.status_code != requests.codes.ok: #@UndefinedVariable |
|
|
65
6289931858a7
add a project property to override the front project.
ymh <ymh.work@gmail.com>
parents:
45
diff
changeset
|
32 |
logger.error("Error when requesting objects " + repr(r.status_code) + " : " + repr(r.text)) |
| 45 | 33 |
break |
|
65
6289931858a7
add a project property to override the front project.
ymh <ymh.work@gmail.com>
parents:
45
diff
changeset
|
34 |
objects.extend([ c for c in r.json['objects'] if field_filter is None or c.get(field_filter, None) ]) |
| 45 | 35 |
url = get_abs_url(r.json.get('meta',{}).get('next',None), settings.LDT_NETLOC) |
|
65
6289931858a7
add a project property to override the front project.
ymh <ymh.work@gmail.com>
parents:
45
diff
changeset
|
36 |
return objects |
|
6289931858a7
add a project property to override the front project.
ymh <ymh.work@gmail.com>
parents:
45
diff
changeset
|
37 |
|
|
6289931858a7
add a project property to override the front project.
ymh <ymh.work@gmail.com>
parents:
45
diff
changeset
|
38 |