Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add datetime_rule_variable/DateTimeType. #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions business_rules/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
FIELD_NO_INPUT = 'none'
FIELD_SELECT = 'select'
FIELD_SELECT_MULTIPLE = 'select_multiple'
FIELD_DATETIME = 'datetime'
41 changes: 40 additions & 1 deletion business_rules/operators.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import inspect
import re
from datetime import datetime
from functools import wraps
from six import string_types, integer_types

from .fields import (FIELD_TEXT, FIELD_NUMERIC, FIELD_NO_INPUT,
FIELD_SELECT, FIELD_SELECT_MULTIPLE)
FIELD_SELECT, FIELD_SELECT_MULTIPLE,
FIELD_DATETIME)
from .utils import fn_name_to_pretty_label, float_to_decimal
from decimal import Decimal, Inexact, Context

Expand Down Expand Up @@ -235,3 +237,40 @@ def shares_exactly_one_element_with(self, other_value):
@type_operator(FIELD_SELECT_MULTIPLE)
def shares_no_elements_with(self, other_value):
return not self.shares_at_least_one_element_with(other_value)


@export_type
class DateTimeType(BaseType):
# This implementation handles only naive datetimes, as provided by
# HTML5 datetime-local inputs. For another approach, see:
# business_rules in https://github.com/TencentBlueKing/bk-itsm/

name = "datetime"
# HTML5 datetime-local text format:
DATETIME_FORMAT = "%Y-%m-%dT%H:%M"

def _assert_valid_value_and_cast(self, value):
"""
Parse string into datetime.datetime instance.
"""
if isinstance(value, datetime):
return value

try:
return datetime.strptime(value, self.DATETIME_FORMAT)
except (ValueError, TypeError):
raise AssertionError("{0} is not a valid date/time.".format(value))

@type_operator(FIELD_DATETIME, label="Equal To")
def equal_to(self, other_datetime):
return self.value == other_datetime

@type_operator(FIELD_DATETIME, label="After")
def after_than_or_equal_to(self, other_datetime):
print(f'after_than_or_equal_to {self.value} {other_datetime}')
return self.value >= other_datetime

@type_operator(FIELD_DATETIME, label="Before")
def before_than_or_equal_to(self, other_datetime):
print(f'before_than_or_equal_to {self.value} {other_datetime}')
return self.value <= other_datetime
6 changes: 5 additions & 1 deletion business_rules/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
StringType,
BooleanType,
SelectType,
SelectMultipleType)
SelectMultipleType,
DateTimeType)

class BaseVariables(object):
""" Classes that hold a collection of variables to use with the rules
Expand Down Expand Up @@ -59,3 +60,6 @@ def select_rule_variable(label=None, options=None):

def select_multiple_rule_variable(label=None, options=None):
return rule_variable(SelectMultipleType, label=label, options=options)

def datetime_rule_variable(label=None):
return _rule_variable_wrapper(DateTimeType, label)