Skip to content

Commit a765154

Browse files
author
GustavoRPS
committed
error new query return None
1 parent 0ac1e58 commit a765154

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

hash_data_api/resources/article.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,40 @@ def on_get(self, req, resp):
5858
limit=per_page,
5959
order=['-dateCreated'])
6060

61-
if not fields:
62-
req.context['data'] = [{
63-
'id': str(x.get('_id')),
64-
'headline': x.get('name'),
65-
'url': x.get('url'),
66-
'datePublished': x['datePublished'].isoformat() if 'datePublished' in x else None,
67-
'dateCreated': x['dateCreated'].isoformat() if 'dateCreated' in x else None,
68-
'image': x.get('image')[0],
69-
'articleBody': x.get('articleBody'),
70-
'description': x.get('description'),
71-
'keywords': x.get('keywords'),
72-
} for x in query]
73-
else:
74-
data = []; append2data = data.append
75-
for x in query:
76-
o = {'id': str(x['_id'])}
77-
78-
for f in fields:
79-
if f == 'datePublished' and 'datePublished' in x:
80-
o[f] = x[f].isoformat()
81-
elif f == 'dateCreated' and 'dateCreated' in x:
82-
o[f] = x[f].isoformat()
83-
elif f == 'dateModified' and 'dateModified' in x:
84-
o[f] = x[f].isoformat()
85-
else:
86-
if f in x:
87-
o[f] = x[f]
61+
if query:
62+
if not fields:
63+
req.context['data'] = [{
64+
'id': str(x.get('_id')),
65+
'headline': x.get('name'),
66+
'url': x.get('url'),
67+
'datePublished': x['datePublished'].isoformat() if 'datePublished' in x else None,
68+
'dateCreated': x['dateCreated'].isoformat() if 'dateCreated' in x else None,
69+
'image': x.get('image')[0],
70+
'articleBody': x.get('articleBody'),
71+
'description': x.get('description'),
72+
'keywords': x.get('keywords'),
73+
} for x in query]
74+
else:
75+
data = []; append2data = data.append
76+
for x in query:
77+
o = {'id': str(x['_id'])}
78+
79+
for f in fields:
80+
if f == 'datePublished' and 'datePublished' in x:
81+
o[f] = x[f].isoformat()
82+
elif f == 'dateCreated' and 'dateCreated' in x:
83+
o[f] = x[f].isoformat()
84+
elif f == 'dateModified' and 'dateModified' in x:
85+
o[f] = x[f].isoformat()
86+
else:
87+
if f in x:
88+
o[f] = x[f]
8889

89-
append2data(o)
90+
append2data(o)
9091

91-
req.context['data'] = data
92+
req.context['data'] = data
93+
else:
94+
req.context['data'] = []
9295

9396
except Exception as e:
9497
logger.error(e)

0 commit comments

Comments
 (0)