Beispiel #1
0
def generate_datetime_field_comparison(field, comparator, value, org):
    lookup = DATETIME_LOOKUP_ALIASES.get(comparator, None)
    if not lookup:
        raise SearchException("Unsupported comparator %s for datetime field" % comparator)

    # parse as localized date and then convert to UTC
    tz = pytz.timezone(org.timezone)
    local_date = str_to_datetime(value, tz, org.get_dayfirst(), fill_time=False)

    # passed date wasn't parseable so don't match any contact
    if not local_date:
        return Q(pk=-1)

    value = local_date.astimezone(pytz.utc)

    if lookup == '<equal>':  # check if datetime is between date and date + 1d, i.e. anytime in that 24 hour period
        return Q(**{
            'values__contact_field__id': field.id,
            'values__datetime_value__gte': value,
            'values__datetime_value__lt': value + timedelta(days=1)})
    elif lookup == 'lte':  # check if datetime is less then date + 1d, i.e. that day and all previous
        return Q(**{
            'values__contact_field__id': field.id,
            'values__datetime_value__lt': value + timedelta(days=1)})
    elif lookup == 'gt':  # check if datetime is greater than or equal to date + 1d, i.e. day after and subsequent
        return Q(**{
            'values__contact_field__id': field.id,
            'values__datetime_value__gte': value + timedelta(days=1)})
    else:
        return Q(**{'values__contact_field__id': field.id, 'values__datetime_value__%s' % lookup: value})
Beispiel #2
0
def generate_datetime_field_comparison(field, comparator, value, org):
    lookup = DATETIME_LOOKUP_ALIASES.get(comparator, None)
    if not lookup:
        raise SearchException("Unsupported comparator %s for datetime field" % comparator)

    # parse as localized date and then convert to UTC
    tz = pytz.timezone(org.timezone)
    local_date = str_to_datetime(value, tz, org.get_dayfirst(), fill_time=False)

    # passed date wasn't parseable so don't match any contact
    if not local_date:
        return Q(pk=-1)

    value = local_date.astimezone(pytz.utc)

    if lookup == '<equal>':  # check if datetime is between date and date + 1d, i.e. anytime in that 24 hour period
        return Q(**{
            'values__contact_field__key': field.key,
            'values__datetime_value__gte': value,
            'values__datetime_value__lt': value + timedelta(days=1)})
    elif lookup == 'lte':  # check if datetime is less then date + 1d, i.e. that day and all previous
        return Q(**{
            'values__contact_field__key': field.key,
            'values__datetime_value__lt': value + timedelta(days=1)})
    elif lookup == 'gt':  # check if datetime is greater than or equal to date + 1d, i.e. day after and subsequent
        return Q(**{
            'values__contact_field__key': field.key,
            'values__datetime_value__gte': value + timedelta(days=1)})
    else:
        return Q(**{'values__contact_field__key': field.key, 'values__datetime_value__%s' % lookup: value})
Beispiel #3
0
    def _build_datetime_field_params(self, field):
        lookup = self.DATETIME_LOOKUPS.get(self.comparator)
        if not lookup:
            raise SearchException(
                _("Can't query date fields with %s") % self.comparator)

        # parse as localized date
        local_date = str_to_datetime(self.value,
                                     field.org.timezone,
                                     field.org.get_dayfirst(),
                                     fill_time=False)
        if not local_date:
            raise SearchException(
                _("Unable to parse the date %s") % self.value)

        # get the range of UTC datetimes for this local date
        utc_range = date_to_utc_range(local_date.date(), field.org)

        if lookup == '<equal>':
            return {
                'contact_field': field,
                'datetime_value__gte': utc_range[0],
                'datetime_value__lt': utc_range[1]
            }
        elif lookup == 'lt':
            return {'contact_field': field, 'datetime_value__lt': utc_range[0]}
        elif lookup == 'lte':
            return {'contact_field': field, 'datetime_value__lt': utc_range[1]}
        elif lookup == 'gt':
            return {
                'contact_field': field,
                'datetime_value__gte': utc_range[1]
            }
        elif lookup == 'gte':
            return {
                'contact_field': field,
                'datetime_value__gte': utc_range[0]
            }