-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add backward linkage of story to the topic it belongs. #6855
Conversation
core/domain/story_domain.py
Outdated
'Expected belongs_to_topic should be a string, received: %s' % ( | ||
self.belongs_to_topic)) | ||
|
||
if len(self.belongs_to_topic) != 12: |
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.
@seanlip, Can you give your suggestion on these questions:
(belongs_to_topic = the topic id to which the story belongs)
- Do we need to expose belongs_to_topic value to domain layer as we don't expect users to change this value?
- If yes to the above question, then do we have to validate the topic_id? (note we do not validate the story_id)
@aks681, is there any specific reason for using created_on and last_updated in the domain layer?
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.
"belongs_to_topic" sounds like a boolean. Maybe corresponding_topic_id or something like that.
Yes, expose to domain layer. Domain layer is where the logic is.
No need to validate topic ID.
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.
I remember using collections as a reference when creating the new structure models and it had those 2 fields. So, I added that here as well.
I don't think you need to validate the existence of the topic, though you can make sure it's a 12 character string. I think the former is usually done in the controller layer. What do you think?
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.
Yeah, I've done the validation check for the existence of topic in the service layer. Removed the simple validation check for the topic id from the domain layer.
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.
Thanks @DubeySandeep! I took a quick glance at the overall structure, but woudl defer the more thorough review to @aks681.
One thing I'm confused about is -- shouldn't we have some code that handles stuff like "story added to topic", "story deleted from topic" (and adjusts the topic linkages accordingly)? I can't see where that code lives, but perhaps I'm missing it.
utils.py
Outdated
@@ -487,7 +487,10 @@ def generate_random_string(length): | |||
Returns: | |||
str. Random string of specified length. | |||
""" | |||
return base64.urlsafe_b64encode(os.urandom(length)) | |||
random_string = '' |
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.
Oh, hm, interesting. Was the old function creating strings that were too short? Based on reading the python docs, I'm not sure I understand why that would happen... what's an example?
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 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.
Oh, I see, urandom() creates a string of length 12 but urlsafe_b64encode might extend that. Thanks for clarifying!
We could alternatively just take the first "length" characters...
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.
Done!
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.
Just some minor comments, but looks good!
One question though, would this be required in the frontend?
core/domain/story_domain.py
Outdated
'Expected belongs_to_topic should be a string, received: %s' % ( | ||
self.belongs_to_topic)) | ||
|
||
if len(self.belongs_to_topic) != 12: |
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.
I remember using collections as a reference when creating the new structure models and it had those 2 fields. So, I added that here as well.
I don't think you need to validate the existence of the topic, though you can make sure it's a 12 character string. I think the former is usually done in the controller layer. What do you think?
core/domain/story_services.py
Outdated
|
||
if story.id not in topic.canonical_story_ids + topic.additional_story_ids: | ||
raise Exception( | ||
'Expected story to belongs to the topic %s, but it is ' |
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.
..belong to the topic.
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.
Done!
core/domain/story_services.py
Outdated
if story.id not in topic.canonical_story_ids + topic.additional_story_ids: | ||
raise Exception( | ||
'Expected story to belongs to the topic %s, but it is ' | ||
'neither a part of canonical story or the addition story of ' |
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.
'the canonical stories or the additional stories'
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.
Done!
core/storage/story/gae_models.py
Outdated
@@ -58,6 +58,8 @@ class StoryModel(base_models.VersionedModel): | |||
# The schema version for the story_contents. | |||
story_contents_schema_version = ( | |||
ndb.IntegerProperty(required=True, indexed=True)) | |||
# The topic id to which the story belongs. | |||
belongs_to_topic = ndb.StringProperty(indexed=False, required=False) |
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.
@seanlip. Will this cause issues with existing stories in the datastore?
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.
No, I think it's fine. They'll just be set to None.
But I think indexed should be True. And once we've populated the story models on the test server correctly, we should make this required=True (is that right, @DubeySandeep? If so, we should add a TODO to do this.)
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.
For now, I've changed both of the value to True once we will deploy the new branch to testserver we can add topic_id manually before start testing. I'll add an email (with the story id and corresponding topic id) to Sean for the same (for tracking).
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.
LGTM!
Though, I'd suggest adding at least the simple basestring validation check to story_domain, as all other fields have their checks there also.
Also note, failing test.
@aks681, I've fixed the tests and added @nithusha21, Can you please review this PR as a codeowner? |
Codecov Report
@@ Coverage Diff @@
## develop #6855 +/- ##
===========================================
+ Coverage 95.68% 95.68% +<.01%
===========================================
Files 371 371
Lines 51360 51429 +69
===========================================
+ Hits 49143 49212 +69
Misses 2217 2217
Continue to review full report at Codecov.
|
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.
Just a couple of nits in the docstring. LGTM as a codeowner.
core/tests/test_utils.py
Outdated
@@ -1281,13 +1281,16 @@ def save_new_story( | |||
description: str. The high level description of the story. | |||
notes: str. A set of notes, that describe the characters, | |||
main storyline, and setting. | |||
corresponding_topic_id: str. The topic id to which the story belongs |
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.
corresponding_topic_id: str. The topic id to which the story belongs | |
corresponding_topic_id: str. The id of the topic to which the story belongs. |
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.
Done!
core/tests/test_utils.py
Outdated
@@ -1281,13 +1281,16 @@ def save_new_story( | |||
description: str. The high level description of the story. | |||
notes: str. A set of notes, that describe the characters, | |||
main storyline, and setting. | |||
corresponding_topic_id: str. The topic id to which the story belongs | |||
to. |
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.
Remove the second "to"
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.
Done!
core/tests/test_utils.py
Outdated
@@ -1317,6 +1321,8 @@ def save_new_story_with_story_contents_schema_v1( | |||
description: str. The high level description of the story. | |||
notes: str. A set of notes, that describe the characters, | |||
main storyline, and setting. | |||
corresponding_topic_id: str. The topic id to which the story belongs |
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.
Ditto.
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.
Done!
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.
No comments, this looks excellent IMO. Thanks @DubeySandeep (and @aks681 @nithusha21 for reviewing)!
Explanation
Add backward linkage of story to the topic it belongs.
Checklist
python scripts/pre_commit_linter.py
andbash scripts/run_frontend_tests.sh
.