-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add start of work to add webhook modeling for each integration
- Loading branch information
Showing
20 changed files
with
892 additions
and
172 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
"""Integration admin models""" | ||
|
||
from datetime import datetime, timedelta | ||
|
||
from django.contrib import admin | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.core import urlresolvers | ||
from django.utils.safestring import mark_safe | ||
from django.utils.translation import ugettext_lazy as _ | ||
from pygments.formatters import HtmlFormatter | ||
|
||
from .models import Integration, HttpExchange | ||
|
||
|
||
def pretty_json_field(field, description, include_styles=False): | ||
# There is some styling here because this is easier than reworking how the | ||
# admin is getting stylesheets. We only need minimal styles here, and there | ||
# isn't much user impact to these styles as well. | ||
def inner(self, obj): | ||
styles = '' | ||
if include_styles: | ||
formatter = HtmlFormatter(style='colorful') | ||
styles = '<style>' + formatter.get_style_defs() + '</style>' | ||
return mark_safe('<div style="{0}">{1}</div>{2}'.format( | ||
'float: left;', | ||
obj.formatted_json(field), | ||
styles, | ||
)) | ||
|
||
inner.short_description = description | ||
return inner | ||
|
||
|
||
class HttpExchangeAdmin(admin.ModelAdmin): | ||
|
||
readonly_fields = [ | ||
'date', | ||
'status_code', | ||
'pretty_request_headers', | ||
'pretty_request_body', | ||
'pretty_response_headers', | ||
'pretty_response_body', | ||
] | ||
fields = readonly_fields | ||
list_display = [ | ||
'related_object', | ||
'date', | ||
'status_code', | ||
'failed_icon', | ||
] | ||
|
||
pretty_request_headers = pretty_json_field( | ||
'request_headers', | ||
'Request headers', | ||
include_styles=True, | ||
) | ||
pretty_request_body = pretty_json_field( | ||
'request_body', | ||
'Request body', | ||
) | ||
pretty_response_headers = pretty_json_field( | ||
'response_headers', | ||
'Response headers', | ||
) | ||
pretty_response_body = pretty_json_field( | ||
'response_body', | ||
'Response body', | ||
) | ||
|
||
def failed_icon(self, obj): | ||
return not obj.failed | ||
|
||
failed_icon.boolean = True | ||
failed_icon.short_description = 'Passed' | ||
|
||
|
||
class IntegrationAdmin(admin.ModelAdmin): | ||
|
||
search_fields = ('project__slug', 'project__name') | ||
readonly_fields = ['exchanges'] | ||
|
||
def exchanges(self, obj): | ||
"""Manually make an inline-ish block | ||
JSONField doesn't do well with fieldsets for whatever reason. This is | ||
just to link to the exchanges. | ||
""" | ||
url = urlresolvers.reverse('admin:{0}_{1}_changelist'.format( | ||
HttpExchange._meta.app_label, | ||
HttpExchange._meta.model_name, | ||
)) | ||
return mark_safe('<a href="{0}?{1}={2}">{3} HTTP transactions</a>'.format( | ||
url, | ||
'integrations', | ||
obj.pk, | ||
obj.exchanges.count(), | ||
)) | ||
|
||
exchanges.short_description = 'HTTP exchanges' | ||
|
||
|
||
admin.site.register(HttpExchange, HttpExchangeAdmin) | ||
admin.site.register(Integration, IntegrationAdmin) |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.9.12 on 2017-03-29 21:29 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import jsonfield.fields | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('integrations', '0001_add_http_exchange'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Integration', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('integration_type', models.CharField(choices=[(b'github_webhook', 'GitHub incoming webhook'), (b'bitbucket_webhook', 'Bitbucket incoming webhook'), (b'gitlab_webhook', 'GitLab incoming webhook'), (b'api_webhook', 'Generic API incoming webhook')], max_length=32, verbose_name='Type')), | ||
('provider_data', jsonfield.fields.JSONField(verbose_name='Provider data')), | ||
('project', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='integrations', to='projects.Project')), | ||
], | ||
), | ||
] |
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.