Skip to content
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

Incidents API update #95

Merged
merged 8 commits into from
Aug 13, 2019
Merged

Incidents API update #95

merged 8 commits into from
Aug 13, 2019

Conversation

milesbxf
Copy link
Contributor

This refreshes the API for managing incidents, allowing list (/incidents), detail (/incidents/) and updating (PUT /incidents/<pk).

@milesbxf milesbxf added the wip Work in Progress label Aug 13, 2019
class Meta:
model = ExternalUser
fields = ('app_id', 'external_id', 'owner', 'display_name')
fields = ("app_id", "external_id", "display_name")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously we were returning the owner user primary key. I took it out, as it's basically meaningless, and we can more easily identify an External User by the other three (we have a UNIQUE constraint on them)

class IncidentSerializer(serializers.HyperlinkedModelSerializer):
reporter = serializers.PrimaryKeyRelatedField(queryset=ExternalUser.objects.all(), required=False)
lead = serializers.PrimaryKeyRelatedField(queryset=ExternalUser.objects.all(), required=False)
class IncidentSerializer(serializers.ModelSerializer):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have context on why we were using a HyperlinkedModelSerializer, but this means we return hyperlinks to other API endpoints instead of primary keys. We don't need either in this serializer, so might as well use the simpler ModelSerializer

"severity",
"start_time",
"summary",
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatted, sorted and added a few more fields to the request/response

if 'actions' in expand:
self.fields['action_set'] = ActionSerializer(many=True, read_only=True)
instance.save()
return instance
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new update method here to control exactly how we're deserializing a PUT request and updating the incident instance.

I removed the previous __init__ function as I don't think we need this functionality, though might well put it back in when I get to the Actions API

@@ -5,7 +5,7 @@

# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'incidents', IncidentViewSet, base_name='Incidents')
router.register(r'incidents', IncidentViewSet, basename='incident')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed for consistency with the auto-generated base names for the other routes. base_name is also deprecated in favour of basename


return Incident.objects.filter(start_time__gte=start, start_time__lte=end)
serializer_class = serializers.IncidentSerializer
pagination_class = pagination.LimitOffsetPagination
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UIs should be controlling pagination, so remove logic around getting the previous months worth of incidents, and simplify the queryset. We'll sort by newest reported incidents to oldest.

@pytest.fixture()
def api_user(transactional_db):
e = ExternalUserFactory()
return e.owner
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used for auth. Any authenticated user will do (though may have to update this in future if we start doing more fine-grained permissions)

tests/api/test_incidents.py Show resolved Hide resolved
tests/api/test_incidents.py Show resolved Hide resolved


@factory.django.mute_signals(post_save)
class IncidentFactory(factory.DjangoModelFactory):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use factory_boy to easily generate random test data 🙌

from tests.factories import UserFactory, ExternalUserFactory

@pytest.fixture
def arf():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐶

assert incident["report_time"]

# incidents should be in order of newest to oldest
if idx != len(incidents) - 1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could store report_time outside the loop and update/compare it on each iteration to simplify here, not a blocker though.

@milesbxf milesbxf removed the wip Work in Progress label Aug 13, 2019
@milesbxf milesbxf merged commit a60580a into release-0.1 Aug 13, 2019
@milesbxf milesbxf deleted the api-incidents branch August 13, 2019 15:05
milesbxf added a commit that referenced this pull request Aug 14, 2019
* Update incidents-list (/api/incidents)

* Update incidents with PUT /incidents/<pk>

* Tidy up tests

* Update incident lead via API

* /incidents list should be sorted by report date

* Return primary key in incident response

* Change deprecated parameter

* Add docs for incidents endpoint
milesbxf added a commit that referenced this pull request Aug 15, 2019
* Update incidents-list (/api/incidents)

* Update incidents with PUT /incidents/<pk>

* Tidy up tests

* Update incident lead via API

* /incidents list should be sorted by report date

* Return primary key in incident response

* Change deprecated parameter

* Add docs for incidents endpoint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants