Skip to content

Commit

Permalink
Fix community API bug
Browse files Browse the repository at this point in the history
A community not having a community schema caused the community REST API to
throw an error. A community should have a community schema, but this
caused test_communities_rest.test_valid_get() to fail since no schema
is created in that test.
  • Loading branch information
Janne Karjalainen authored and hevp committed Mar 6, 2020
1 parent e26f0ff commit d692240
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
26 changes: 14 additions & 12 deletions b2share/modules/communities/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ def community_self_link(community, **kwargs):


def community_to_dict(community):
community_schema = CommunitySchema.get_community_schema(community.id)
community_schema_dict = json.loads(community_schema.community_schema)
props = community_schema_dict['properties']
return dict(
ret = dict(
id=community.id,
name=community.name,
description=community.description,
Expand All @@ -62,12 +59,6 @@ def community_to_dict(community):
restricted_submission=community.restricted_submission,
links=dict(
self=community_self_link(community, _external=True),
schema=community_schema_json_schema_link(community_schema, _external=True),
block_schema=next(iter(props.values()))['$ref'] if props else ""
),
schema=dict(
version=community_schema.version,
block_schema_id=next(iter(props)) if props else ""
),
roles=dict(
admin=dict(id=community.admin_role.id,
Expand All @@ -76,9 +67,20 @@ def community_to_dict(community):
member=dict(id=community.member_role.id,
name=community.member_role.name,
description=community.member_role.description),
),
)
)

try:
community_schema = CommunitySchema.get_community_schema(community.id)
community_schema_dict = json.loads(community_schema.community_schema)
props = community_schema_dict['properties']
ret['links']['schema'] = community_schema_json_schema_link(community_schema, _external=True)
ret['links']['block_schema'] = next(iter(props.values()))['$ref']
ret['schema'] = dict(
version=community_schema.version,
block_schema_id=next(iter(props))
)
finally:
return ret

def community_to_json_serializer(community, code=200, headers=None):
"""Build a json flask response using the given community data.
Expand Down
4 changes: 2 additions & 2 deletions b2share/modules/schemas/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,13 +558,13 @@ def get_community_schema(cls, community_id, version=None):
).one()
else:
model = CommunitySchemaVersion.query.filter(
CommunitySchemaVersion.community == community_id
CommunitySchemaVersion.community == str(community_id)
).order_by(
CommunitySchemaVersion.version.desc()
).limit(1).one()
return cls(model)
except NoResultFound as e:
raise CommunitySchemaDoesNotExistError(id) from e
raise CommunitySchemaDoesNotExistError(str(community_id)) from e

@classmethod
def create_version(cls, community_id, community_schema,
Expand Down

0 comments on commit d692240

Please sign in to comment.