-
Notifications
You must be signed in to change notification settings - Fork 10
Expose data on api #28
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import json | ||
|
||
from dataclasses import dataclass | ||
from datetime import date | ||
from typing import List | ||
|
@@ -27,6 +29,20 @@ def is_valid(self): | |
|
||
return True | ||
|
||
def to_JSON(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Can we rename it to |
||
""" | ||
Serialize object to json | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This short docstring could be in one:
But is this docstring really necessary? I feel like the name |
||
d = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does |
||
"url": self.url, | ||
"source": self.source, | ||
"title": self.title, | ||
"image_or_video": self.image_or_video, | ||
"summary": self.summary, | ||
"case_id": self.case_id, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any special reason to not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used variable here cause I had to somehow serialize def to_json(self):
return json.dumps(asdict(self), cls=DateEncoder, sort_keys=True) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good way. Alternatively simpler though: def to_json(self):
data = asdict(self)
data['when'] = date['when'].strftime("%Y-%m-%d")
return json.dumps(data, sort_keys=True) |
||
return json.dumps(d, sort_keys=True) | ||
|
||
|
||
@dataclass | ||
class Case: | ||
|
@@ -48,3 +64,18 @@ def tags_and_colors(self): | |
|
||
def is_valid(self): | ||
return bool(self.when) | ||
|
||
def to_JSON(self): | ||
""" | ||
Serialize object to json | ||
""" | ||
d = { | ||
"id": self.id, | ||
"aggressor_side": self.aggressor_side, | ||
"when": str(self.when), | ||
"state": self.state, | ||
"city": self.city, | ||
"tags": self.tags, | ||
"stories": [story.to_JSON() for story in self.stories], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line should not encode There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point, thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @baltazarix do you mind adding a simple test for this endpoint? It's pretty simple and can avoid assure we're serializing data correctly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @turicas, sure! I was thinking about it but I've had no idea how test scenario should look like. Only ideas I have are:
Feel free to tell me if I'm missing something :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Take a look at existing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yep, I wanted to make something similar, sorry if I wasn't clear enough. |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any special reason to not use |
||
return json.dumps(d, sort_keys=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just the same as my comments in the other |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no
case.to_JSON()
anymore, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right, my bad