class UserConditionSet(ModelConditionSet): username = String() email = String() is_anonymous = Boolean(label='Anonymous') is_active = Boolean(label='Active') is_staff = Boolean(label='Staff') is_superuser = Boolean(label='Superuser') date_joined = OnOrAfterDate(label='Joined on or after') def can_execute(self, instance): return isinstance(instance, (User, AnonymousUser)) def is_active(self, instance, conditions): """ value is the current value of the switch instance is the instance of our type """ if isinstance(instance, User): return super(UserConditionSet, self).is_active(instance, conditions) # HACK: allow is_authenticated to work on AnonymousUser condition = conditions.get(self.get_namespace(), {}).get('is_anonymous') if condition is not None: return bool(condition) return None
class UTCTodayConditionSet(ConditionSet): """ Checks conditions against current time in UTC """ today_is_on_or_after = OnOrAfterDate('in UTC on or after') today_is_before = BeforeDate('in UTC before') def get_namespace(self): return 'now_utc' def can_execute(self, instance): return instance is None def get_field_value(self, instance, field_name): return datetime.utcnow() def get_group_label(self): return 'Today'
class ActiveTimezoneTodayConditionSet(ConditionSet): """ Checks conditions against current time of active timezone or against current server time if Django timezone support disabled (USE_TZ=False) """ today_is_on_or_after = OnOrAfterDate('in active timezone on or after') today_is_before = BeforeDate('in active timezone before') def get_namespace(self): return 'now_active_tz' def can_execute(self, instance): return instance is None def get_field_value(self, instance, field_name): now_dt = timezone.now() if timezone.is_aware(now_dt): now_dt = timezone.make_naive(now_dt) return now_dt def get_group_label(self): return 'Today'
def test_is_active_date_greater(self): condition = OnOrAfterDate() assert condition.is_active("2016-08-05", datetime.date(2016, 8, 10))
def test_is_active_date_less(self): condition = OnOrAfterDate() assert not condition.is_active("2016-08-05", datetime.date(2016, 8, 2))