Skip to content

Commit

Permalink
Merge pull request #32 from vanallenlab/update_feature_display_via_api
Browse files Browse the repository at this point in the history
Update feature display via api
  • Loading branch information
brendanreardon authored Apr 8, 2021
2 parents d930a9a + 704e590 commit 73ac0f8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 8 deletions.
1 change: 1 addition & 0 deletions almanac_browser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def create_app(name=__name__):
app.register_blueprint(api, url_prefix='/api')
# app.register_blueprint(editor, url_prefix='/editor')
app.register_blueprint(portal)
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True

db.init_app(app)
ma.init_app(app) # must initialize after SQLAlchemy
Expand Down
87 changes: 79 additions & 8 deletions almanac_browser/modules/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,84 @@
api = Blueprint('api', __name__)


def pop_assertions(list_of_assertions):
for assertion in list_of_assertions:
assertion.pop("_sa_instance_state", None)


def pop_sources(list_of_assertions):
sources = []
for assertion in list_of_assertions:
for source in assertion['sources']:
if source not in sources:
sources.append(source)
[item.pop("_sa_instance_state", None) for item in sources]


def reformat_assertion(assertion, pop=True):
dictionary = assertion.__dict__
new_dictionary = {}
for key, value in dictionary.items():
new_dictionary[key] = value
if pop:
new_dictionary.pop("_sa_instance_state", None)

sources = [item.__dict__ for item in assertion.sources]
if pop:
[item.pop("_sa_instance_state", None) for item in sources]
new_dictionary['sources'] = sources

features = [reformat_feature(item) for item in assertion.features]
new_dictionary['features'] = features
return new_dictionary


def reformat_assertions(list_assertions):
reformatted_assertions = [reformat_assertion(assertion, pop=False) for assertion in list_assertions]
pop_sources(reformatted_assertions)
pop_assertions(reformatted_assertions)
return reformatted_assertions


def reformat_attributes(feature_definition, attributes):
new_dictionary = {}
for i in range(0, len(attributes)):
key = feature_definition.attribute_definitions[i].name
value = attributes[i].value
new_dictionary[key] = value
new_dictionary['feature_type'] = feature_definition.name
return new_dictionary


def reformat_feature(feature):
return {
'feature_type': feature.feature_definition.name,
'attributes': [reformat_attributes(feature.feature_definition, feature.attributes)]
}


def reformat_features(list_features):
attributes = []
unique_features = []
for item in list_features:
if item['attributes'] not in attributes:
unique_features.append(item)
attributes.append(item['attributes'])
return unique_features


@api.route('/assertions/<int:assertion_id>', methods=['GET'])
def get_assertion(assertion_id):
assertion = Assertion.query.get_or_404(assertion_id)
return AssertionSchema().jsonify(assertion)
raw_data = Assertion.query.get_or_404(assertion_id)
data = reformat_assertion(raw_data)
return jsonify(data)


@api.route('/assertions', methods=['GET'])
def get_assertions():
data = Assertion.query.all()
return AssertionSchema(many=True).jsonify(data)
raw_data = Assertion.query.all()
data = reformat_assertions(raw_data)
return jsonify(data)


@api.route('/feature_definitions/<int:definition_id>', methods=['GET'])
Expand All @@ -45,14 +113,17 @@ def get_feature_definitions():

@api.route('/features/<int:feature_id>', methods=['GET'])
def get_feature(feature_id):
data = Feature.query.get_or_404(feature_id)
return FeatureSchema().jsonify(data)
raw_data = Feature.query.get_or_404(feature_id)
data = reformat_feature(raw_data)
return jsonify(data)


@api.route('/features', methods=['GET'])
def get_features():
data = Feature.query.all()
return FeatureSchema(many=True).jsonify(data)
raw_data = Feature.query.all()
duplicate_data = [reformat_feature(item) for item in raw_data]
data = reformat_features(duplicate_data)
return jsonify(data)


@api.route('/attribute_definitions/<int:attribute_def_id>', methods=['GET'])
Expand Down

0 comments on commit 73ac0f8

Please sign in to comment.