Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
gargoyle switch based on django settings
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarbugli authored and wolph committed Nov 30, 2011
1 parent c16dbcf commit 85b9cc1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
24 changes: 23 additions & 1 deletion gargoyle/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from gargoyle import gargoyle
from gargoyle.conditions import ModelConditionSet, RequestConditionSet, Percent, String, Boolean, \
ConditionSet, OnOrAfterDate
ConditionSet, OnOrAfterDate, Setting

from django.contrib.auth.models import AnonymousUser, User
from django.core.validators import validate_ipv4_address
Expand Down Expand Up @@ -83,3 +83,25 @@ def get_group_label(self):
return 'Host'

gargoyle.register(HostConditionSet())

class SettingConditionSet(ConditionSet):
setting = Setting()

def get_namespace(self):
return "setting"

def can_execute(self, instance):
return instance is None

def get_field_value(self, instance, field_name):
if field_name == "setting":
from django.conf import settings
return settings
else:
return ConditionSet.get_field_value(self, instance, field_name)

def get_group_label(self):
return 'Setting'

gargoyle.register(SettingConditionSet())

26 changes: 25 additions & 1 deletion gargoyle/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@ def display(self, value):
value = value.split('-')
return '%s: %s-%s' % (self.label, value[0], value[1])

class Setting(Field):

def display(self, value):
return '%s: %s' % (self.label, value)

def validate(self, data):
value = data.get(self.name + '[key]'), data.get(self.name + '[value]')
return self.clean(value)

def clean(self, value):
return '='.join(value)

def is_active(self, condition, settings):
key, value = condition.split('=', 1)
return getattr(settings, key, None) == value

def render(self, value):
if not value:
value = ['', '']
return mark_safe('<input type="text" value="%s" placeholder="key" name="%s[key]"/> - <input type="text" placeholder="value" value="%s" name="%s[value]"/>' % \
(escape(value[0]), escape(self.name), escape(value[1]), escape(self.name)))

class Percent(Range):
default_help_text = 'Enter two ranges. e.g. 0-50 is lower 50%'

Expand Down Expand Up @@ -263,10 +285,12 @@ def is_active(self, instance, conditions):
return_value = None
for name, field in self.fields.iteritems():
field_conditions = conditions.get(self.get_namespace(), {}).get(name)
print 'field condition: ', field_conditions
if field_conditions:
value = self.get_field_value(instance, name)
for status, condition in field_conditions:
exclude = status == EXCLUDE
print condition, value
if field.is_active(condition, value):
if exclude:
return False
Expand Down Expand Up @@ -304,4 +328,4 @@ def get_namespace(self):
return 'request'

def can_execute(self, instance):
return isinstance(instance, HttpRequest)
return isinstance(instance, HttpRequest)

0 comments on commit 85b9cc1

Please sign in to comment.