Skip to content
Browse files

Conditions are now all stored as strings and can be removed

  • Loading branch information...
1 parent 7c492f4 commit d54159ea78d3fa4cca01664f678f23a762c34bdd @dcramer dcramer committed Dec 19, 2010
View
13 gargoyle/conditions.py
@@ -31,6 +31,7 @@ def validate(self, data):
value = data.get(self.name)
if value:
value = self.clean(value)
+ assert isinstance(value, basestring), 'clean methods must return strings'
return value
def clean(self, value):
@@ -77,18 +78,19 @@ def validate(self, data):
def clean(self, value):
if value:
try:
- map(int, value)
+ numeric = map(int, value)
except (TypeError, ValueError):
raise ValidationError('You must enter valid integer values.')
return '-'.join(value)
def render(self, value):
if not value:
value = ['', '']
- return mark_safe('<input type="text" value="%s" placeholder="from" name="%s[min]"/> - <input type="text" placeholder="to" value="%s" name="%s[max]"/>' % \
+ return mark_safe('<input type="text" value="%s" placeholder="from" name="%s[min]"/> - <input type="text" placeholder="to" value="%s" name="%s[max]"/>' % \
(escape(value[0]), escape(self.name), escape(value[1]), escape(self.name)))
def display(self, value):
+ value = value.split('-')
return '%s: %s-%s' % (self.label, value[0], value[1])
class Percent(Range):
@@ -100,15 +102,16 @@ def is_active(self, condition, value):
return mod >= condition[0] and mod <= condition[1]
def display(self, value):
+ value = value.split('-')
return '%s: %s%% (%s-%s)' % (self.label, int(value[1]) - int(value[0]), value[0], value[1])
def clean(self, value):
value = super(Percent, self).clean(value)
if value:
- value = value.split('-')
- if int(value[0]) < 0 or int(value[1]) > 100:
+ numeric = value.split('-')
+ if int(numeric[0]) < 0 or int(numeric[1]) > 100:
raise ValidationError('You must enter values between 0 and 100.')
- if int(value[0]) > int(value[1]):
+ if int(numeric[0]) > int(numeric[1]):
raise ValidationError('Start value must be less than end value.')
return value
View
15 gargoyle/media/js/gargoyle.js
@@ -134,11 +134,20 @@ $(document).ready(function () {
$("div.conditions span.value a.delete-condition").live("click", function (ev) {
ev.preventDefault();
+ var el = $(this).parents("span:first");
+
var data = {
- key: $(this).parents("tr:first").attr("data-switch-key"),
- id: $(this).attr("data-switch"),
- field: $(this).attr("data-field")
+ key: el.parents("tr:first").attr("data-switch-key"),
+ id: el.attr("data-switch"),
+ field: el.attr("data-field"),
+ value: el.attr("data-value")
};
+
+ api(GARGOYLE.delCondition, data, function (swtch) {
+ var result = $("#switchData").tmpl(swtch);
+ $("table.switches tr[data-switch-key="+ data.key + "]").replaceWith(result);
+ });
+
})
$("#facebox .closeFacebox").live("click", function (ev) {
View
27 gargoyle/models.py
@@ -64,20 +64,32 @@ def to_dict(self):
'status': self.status,
'label': self.label,
'description': self.description,
- 'conditions': {}
+ 'conditions': [],
}
- for group, field, value, is_exclude in self.get_active_conditions():
- if group not in data['conditions']:
- data['conditions'][group] = [(field.name, value, field.display(value), is_exclude)]
- else:
- data['conditions'][group].append((field.name, value, field.display(value), is_exclude))
+ last = None
+ for condition_set_id, group, field, value, exclude in self.get_active_conditions():
+ if not last or last['id'] != condition_set_id:
+ if last:
+ data['conditions'].append(last)
+
+ last = {
+ 'id': condition_set_id,
+ 'label': group,
+ 'conditions': []
+ }
+
+ last['conditions'].append((field.name, value, field.display(value), exclude))
+ if last:
+ data['conditions'].append(last)
return data
def add_condition(self, condition_set, field_name, condition, exclude=False, commit=True):
condition_set = gargoyle.get_condition_set_by_id(condition_set)
+ assert isinstance(condition, basestring), 'conditions must be strings'
+
namespace = condition_set.get_namespace()
if namespace not in self.value:
@@ -128,12 +140,13 @@ def get_active_conditions(self):
"Returns groups of lists of active conditions"
for condition_set in sorted(gargoyle.get_condition_sets(), key=lambda x: x.get_group_label()):
ns = condition_set.get_namespace()
+ condition_set_id = condition_set.get_id()
if ns in self.value:
group = condition_set.get_group_label()
for name, field in condition_set.fields.iteritems():
for value in self.value[ns].get(name, []):
try:
- yield group, field, value[1], value[0] == EXCLUDE
+ yield condition_set_id, group, field, value[1], value[0] == EXCLUDE
except TypeError:
continue
View
7 gargoyle/nexus_modules.py
@@ -176,14 +176,11 @@ def remove_condition(self, request):
key = request.POST.get("key")
condition_set_id = request.POST.get("id")
field_name = request.POST.get("field")
+ value = request.POST.get("value")
- if not all([key, condition_set_id, field_name]):
+ if not all([key, condition_set_id, field_name, value]):
raise GargoyleException("Fields cannot be empty")
- field = gargoyle.get_condition_set_by_id(condition_set_id).fields[field_name]
-
- value = field.validate(request.POST)
-
switch = Switch.objects.get(key=key)
switch.remove_condition(condition_set_id, field_name, value)
View
16 gargoyle/templates/gargoyle/index.html
@@ -49,11 +49,11 @@
<p>{{ switch.description }}</p>
<div class="conditions">
- {% for group, conds in switch.conditions.iteritems %}
+ {% for group in switch.conditions %}
<div class="group">
- <label>{{ group }}</label>
- {% for field, value, display, is_exclude in conds %}
- <span data-field="{{ field }}" data-value="{{ value }}" class="value">
+ <label>{{ group.label }}</label>
+ {% for field, value, display, is_exclude in group.conditions %}
+ <span data-switch="{{ group.id }}" data-field="{{ field }}" data-value="{{ value }}" class="value">
<nobr>{% if is_exclude %}<strong>not</strong> {% endif %}{{ display }}
<a href="#" class="delete-condition" title="Delete this condition">
<img src="{% url nexus:gargoyle:media 'img/delete.png' %}">
@@ -156,11 +156,11 @@
<p>${description}</p>
<div class="conditions">
- {{ each(group, conds) conditions }}
+ {{ each(i, group) conditions }}
<div class="group">
- <label>${group}</label>
- {{ each(i, item) conds }}
- <span data-field="${item[0]}" data-value="${item[1]}" class="value">
+ <label>${group.label}</label>
+ {{ each(i, item) group.conditions }}
+ <span data-switch="${group.id}" data-field="${item[0]}" data-value="${item[1]}" class="value">
<nobr>{{if item[3]}}<strong>not</strong> {{/if}}${item[2]}
<a href="#" class="delete-condition" title="Delete this condition">
<img src="${GARGOYLE.deleteImage}">

0 comments on commit d54159e

Please sign in to comment.
Something went wrong with that request. Please try again.