Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Commit

Permalink
Receiver of raw_hook_event now uses the same logic as receivers of …
Browse files Browse the repository at this point in the history
…other

Should fix #58

Some backward incompatible changes were introduced (see README.md)
  • Loading branch information
imposeren authored and avelis committed May 14, 2019
1 parent 2a2d6a4 commit d21a070
Show file tree
Hide file tree
Showing 14 changed files with 326 additions and 115 deletions.
7 changes: 0 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ python:
- "3.6"

env:
- DJANGO_VERSION="1.4"
- DJANGO_VERSION="1.5"
- DJANGO_VERSION="1.6"
- DJANGO_VERSION="1.7"
Expand All @@ -24,10 +23,6 @@ script: python runtests.py

matrix:
exclude:
- python: "3.3"
env: DJANGO_VERSION="1.4"
- python: "3.4"
env: DJANGO_VERSION="1.4"
- python: "3.3"
env: DJANGO_VERSION="1.9"
- python: "3.3"
Expand All @@ -36,8 +31,6 @@ matrix:
env: DJANGO_VERSION="1.11"
- python: "3.3"
env: DJANGO_VERSION="2.0"
- python: "3.6"
env: DJANGO_VERSION="1.4"
- python: "3.6"
env: DJANGO_VERSION="1.5"
- python: "3.6"
Expand Down
8 changes: 7 additions & 1 deletion AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ various contributors:

## Patches and Suggestions

- You?
- [Bryan Helmig](https://github.com/bryanhelmig)
- [Arnaud Limbourg](https://github.com/arnaudlimbourg)
- [tdruez](https://github.com/tdruez)
- [Maina Nick](https://github.com/mainanick)
- Jonathan Moss
- [Erik Wickstrom](https://github.com/erikcw)
- [Yaroslav Klyuyev](https://github.com/imposeren)
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Travis CI Build](https://img.shields.io/travis/zapier/django-rest-hooks/master.svg)](https://travis-ci.org/zapier/django-rest-hooks)
[![PyPI Download](https://img.shields.io/pypi/v/django-rest-hooks.svg)](https://pypi.python.org/pypi/django-rest-hooks)
[![PyPI Status](https://img.shields.io/pypi/status/django-rest-hooks.svg)](https://pypi.python.org/pypi/django-rest-hooks)

## What are Django REST Hooks?


Expand Down Expand Up @@ -32,6 +33,53 @@ If you want to make a Django form or API resource, you'll need to do that yourse
(though we've provided some example bits of code below).


### Changelog

#### Version 1.6.0:

Improvements:

* Default handler of `raw_hook_event` uses the same logic as other handlers
(see "Backwards incompatible changes" for details).

* Lookup of event_name by model+action_name now has a complexity of `O(1)`
instead of `O(len(settings.HOOK_EVENTS))`

* `HOOK_CUSTOM_MODEL` is now similar to `AUTH_USER_MODEL`: must be of the form
`app_label.model_name` (for django 1.7+). If old value is of the form
`app_label.models.model_name` then it's automatically adapted.

* `rest_hooks.models.Hook` is now really "swappable", so table creation is
skipped if you have different `settings.HOOK_CUSTOM_MODEL`

* `rest_hooks.models.AbstractHook.deliver_hook` now accepts a callable as
`payload_override` argument (must accept 2 arguments: hook, instance). This
was added to support old behavior of `raw_custom_event`.

Fixes:

* HookAdmin.form now honors `settings.HOOK_CUSTOM_MODEL`

* event_name determined from action+model is now consistent between runs (see
"Backwards incompatible changes")

Backwards incompatible changes:

* Dropped support for django 1.4
* Custom `HOOK_FINDER`-s should accept and handle new argument `payload_override`.
Built-in finder `rest_hooks.utls.find_and_fire_hook` already does this.
* If several event names in `settings.HOOK_EVENTS` share the same
`'app_label.model.action'` (including `'app_label.model.action+'`) then
`django.core.exceptions.ImproperlyConfigured` is raised
* Receiver of `raw_hook_event` now uses the same logic as receivers of other
signals: checks event_name against settings.HOOK_EVENTS, verifies model (if
instance is passed), uses `HOOK_FINDER`. Old behaviour can be achieved by
using `trust_event_name=True`, or `instance=None` to fire a signal.
* If you have `settings.HOOK_CUSTOM_MODEL` of the form different than
`app_label.models.model_name` or `app_label.model_name`, then it must
be changed to `app_label.model_name`.


### Development

Running the tests for Django REST Hooks is very easy, just:
Expand All @@ -57,7 +105,7 @@ python runtests.py
### Requirements

* Python 2 or 3 (tested on 2.7, 3.3, 3.4, 3.6)
* Django 1.4+ (tested on 1.5, 1.6, 1.7, 1.8, 1.9, 1.10, 1.11, 2.0)
* Django 1.5+ (tested on 1.5, 1.6, 1.7, 1.8, 1.9, 1.10, 1.11, 2.0)

### Installing & Configuring

Expand Down
2 changes: 0 additions & 2 deletions devrequirements_1.4.txt

This file was deleted.

2 changes: 1 addition & 1 deletion devrequirements_1.8.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-r devrequirements.txt
django-contrib-comments>=1.6.1
django-contrib-comments>=1.6.1,<1.7.0
Django>=1.8,<1.9
2 changes: 1 addition & 1 deletion devrequirements_1.9.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-r devrequirements.txt
django-contrib-comments>=1.6.2
django-contrib-comments>=1.6.2,<1.7.0
Django>=1.9,<1.10
2 changes: 1 addition & 1 deletion rest_hooks/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = (1, 5, 0)
VERSION = (1, 6, 0)
23 changes: 14 additions & 9 deletions rest_hooks/admin.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
from django.contrib import admin
from django.conf import settings
from django import forms
from rest_hooks.models import Hook
from rest_hooks.utils import get_hook_model


HOOK_EVENTS = getattr(settings, 'HOOK_EVENTS', None)
if HOOK_EVENTS is None:
if getattr(settings, 'HOOK_EVENTS', None) is None:
raise Exception("You need to define settings.HOOK_EVENTS!")


HookModel = get_hook_model()


class HookForm(forms.ModelForm):
"""
Model form to handle registered events, asuring
only events declared on HOOK_EVENTS settings
can be registered.
"""
ADMIN_EVENTS = [(x, x) for x in HOOK_EVENTS.keys()]

class Meta:
model = Hook
model = HookModel
fields = ['user', 'target', 'event']

def __init__(self, *args, **kwargs):
super(HookForm, self).__init__(*args, **kwargs)
self.fields['event'] = forms.ChoiceField(choices=self.ADMIN_EVENTS)
self.fields['event'] = forms.ChoiceField(choices=self.get_admin_events())

@classmethod
def get_admin_events(cls):
return [(x, x) for x in getattr(settings, 'HOOK_EVENTS', None).keys()]


class HookAdmin(admin.ModelAdmin):
list_display = [f.name for f in Hook._meta.fields]
list_display = [f.name for f in HookModel._meta.fields]
raw_id_fields = ['user', ]
form = HookForm

admin.site.register(Hook, HookAdmin)

admin.site.register(HookModel, HookAdmin)
1 change: 1 addition & 0 deletions rest_hooks/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Migration(migrations.Migration):
('user', models.ForeignKey(related_name='hooks', to=settings.AUTH_USER_MODEL, on_delete=django.db.models.deletion.CASCADE)),
],
options={
'swappable': 'HOOK_CUSTOM_MODEL',
},
bases=(models.Model,),
),
Expand Down
18 changes: 18 additions & 0 deletions rest_hooks/migrations/0002_swappable_hook_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('rest_hooks', '0001_initial'),
]

operations = [
migrations.AlterModelOptions(
name='Hook',
options={
'swappable': 'HOOK_CUSTOM_MODEL',
},
),
]
Loading

0 comments on commit d21a070

Please sign in to comment.