-
Notifications
You must be signed in to change notification settings - Fork 81
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
Fix #935: Conditional attributes not exported during download process #1083
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,43 @@ | ||
from jsonattrs.models import Schema | ||
from collections import OrderedDict | ||
from core.mixins import SchemaSelectorMixin | ||
from django.contrib.contenttypes.models import ContentType | ||
|
||
|
||
class Exporter(): | ||
class Exporter(SchemaSelectorMixin): | ||
def __init__(self, project): | ||
self.project = project | ||
self._schema_attrs = {} | ||
|
||
def get_schema_attrs(self, content_type): | ||
content_type_key = '{}.{}'.format(content_type.app_label, | ||
content_type.model) | ||
|
||
if content_type_key not in self._schema_attrs.keys(): | ||
selectors = [ | ||
self.project.organization.id, | ||
self.project.id, | ||
self.project.current_questionnaire | ||
] | ||
schemas = Schema.objects.lookup( | ||
content_type=content_type, selectors=selectors | ||
) | ||
|
||
attrs = [] | ||
if schemas: | ||
attrs = [ | ||
a for s in schemas | ||
for a in s.attributes.all() if not a.omit | ||
] | ||
self._schema_attrs[content_type_key] = attrs | ||
|
||
return self._schema_attrs[content_type_key] | ||
label = '{0}.{1}'.format(content_type.app_label, content_type.model) | ||
if self._schema_attrs == {}: | ||
self._schema_attrs = self.get_attributes(self.project) | ||
return self._schema_attrs[label] | ||
|
||
def get_values(self, item, model_attrs, schema_attrs): | ||
values = [] | ||
values = OrderedDict() | ||
for attr in model_attrs: | ||
if '.' in attr: | ||
attr_items = attr.split('.') | ||
value = None | ||
for a in attr_items: | ||
value = (getattr(item, a) | ||
if not value else getattr(value, a)) | ||
values.append(value) | ||
values[attr] = value | ||
else: | ||
values.append(getattr(item, attr)) | ||
|
||
for attr in schema_attrs: | ||
attr_value = item.attributes.get(attr.name, '') | ||
values[attr] = getattr(item, attr) | ||
|
||
content_type = ContentType.objects.get_for_model(item) | ||
conditional_selector = self.get_conditional_selector(content_type) | ||
if conditional_selector: | ||
entity_type = getattr(item, conditional_selector) | ||
attributes = schema_attrs.get(entity_type, {}) | ||
else: | ||
attributes = schema_attrs.get('DEFAULT', {}) | ||
for key, attr in attributes.items(): | ||
attr_value = item.attributes.get(key, '') | ||
if type(attr_value) == list: | ||
attr_value = (', ').join(attr_value) | ||
values.append(attr_value) | ||
values[key] = attr_value | ||
|
||
return values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This method is an example of how I feel the code seems to currently cater to the party entity being the only one right now to have a conditional selector. Maybe it is a good database design to have the conditional selector be a field of the model instead of a foreign field, but I kinda feel iffy with basing the decision on whether we have a conditional selector hinge on the presence of a period on the last schema selector.
Note that this is just an observation and should not be a blocker for the PR's approval.