Skip to content

Commit d23cde4

Browse files
committed
Tidy up settings loading
1 parent 6ca32ab commit d23cde4

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

webhooks/default_settings.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

webhooks/models.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,15 @@
11
import re
2-
from django.conf import settings
32
from django.db import models
43
from django.contrib.contenttypes import generic
54
from django.contrib.contenttypes.models import ContentType
65
from django.core.urlresolvers import reverse_lazy
76

87
from uuidfield import UUIDField
98

10-
from .default_settings import WEB_HOOK_OWNER_MODEL, WEB_HOOK_ACTIONS, WEB_HOOK_OWNER_LOCAL
9+
from .settings import WEB_HOOK_ACTIONS, WEB_HOOK_OWNER_LOCAL, OWNER_MODEL
1110

1211
from .signals import webhook_triggered_signal
1312

14-
# Load defaults
15-
if hasattr(settings, 'WEB_HOOK_OWNER_MODEL'):
16-
WEB_HOOK_OWNER_MODEL = settings.WEB_HOOK_OWNER_MODEL
17-
18-
if hasattr(settings, 'WEB_HOOK_ACTIONS'):
19-
WEB_HOOK_ACTIONS = settings.WEB_HOOK_ACTIONS
20-
21-
if hasattr(settings, 'WEB_HOOK_OWNER_LOCAL'):
22-
WEB_HOOK_OWNER_LOCAL = settings.WEB_HOOK_OWNER_LOCAL
23-
24-
# Work out our dynamic relation
25-
app_name = WEB_HOOK_OWNER_MODEL.rsplit('.', 1)[0]
26-
model_name = WEB_HOOK_OWNER_MODEL.rsplit('.', 1)[1]
27-
module = __import__(app_name, fromlist=[model_name])
28-
owner_model = getattr(module, model_name)
29-
3013

3114
class WebHook(models.Model):
3215

@@ -39,12 +22,13 @@ class WebHook(models.Model):
3922
)
4023

4124
id = UUIDField(auto=True, primary_key=True)
42-
owner = models.ForeignKey(owner_model, editable=False) # Editable?
25+
owner = models.ForeignKey(OWNER_MODEL, editable=False) # Editable?
4326
action = models.CharField(max_length=1, choices=ACTIONS, default='R')
4427
triggered = models.DateTimeField("Time Triggered", auto_now=True)
4528
method = models.CharField(max_length=1, choices=TRIGGER_METHOD, default='P')
4629
auth = models.CharField("API Key", max_length=64, blank=True) # Not used for G / H requests
47-
filter = models.CharField("Regex Filter Payload", max_length=64, blank=True, help_text='Filter which events apply') # Not used for HEAD
30+
filter = models.CharField("Regex Filter Payload", max_length=64, blank=True,
31+
help_text='Filter which events apply') # Not used for HEAD
4832

4933
# Following fields are required for using GenericForeignKey
5034
content_type = models.ForeignKey(ContentType)
@@ -96,4 +80,4 @@ def trigger(self):
9680
action=self.action,
9781
content_object=self.content_object,
9882
content_type=self.content_type)
99-
self.save() # Update triggered field
83+
self.save() # Update 'triggered' timestamp field

webhooks/settings.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from django.conf import settings
2+
3+
# App Defaults
4+
WEB_HOOK_OWNER_MODEL = 'django.contrib.auth.models.User'
5+
WEB_HOOK_ACTIONS = ()
6+
WEB_HOOK_OWNER_LOCAL = True # Do we get owner from account() or from content_object() ?
7+
8+
# Override defaults, if defined in django project settings
9+
WEB_HOOK_OWNER_MODEL = getattr(settings, 'WEB_HOOK_OWNER_MODEL', WEB_HOOK_OWNER_MODEL)
10+
WEB_HOOK_ACTIONS = getattr(settings, 'WEB_HOOK_ACTIONS', WEB_HOOK_ACTIONS)
11+
WEB_HOOK_OWNER_LOCAL = getattr(settings, 'WEB_HOOK_OWNER_LOCAL', WEB_HOOK_OWNER_LOCAL)
12+
13+
# Work out our dynamic relation. Nb. Django 1.7 has a function to do this.
14+
app_name = WEB_HOOK_OWNER_MODEL.rsplit('.', 1)[0]
15+
model_name = WEB_HOOK_OWNER_MODEL.rsplit('.', 1)[1]
16+
module = __import__(app_name, fromlist=[model_name])
17+
OWNER_MODEL = getattr(module, model_name)

0 commit comments

Comments
 (0)