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

Feature: add datetime to rules context #484

Merged
merged 5 commits into from
Apr 19, 2024
Merged
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
5 changes: 5 additions & 0 deletions examples/profiles/only_linux/profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
"path": "$.environment.operating_system_code",
"value": "linux",
"operator": "equal"
},
{
"path": "$.date.current_year",
"value": 2023,
"operator": "greater_than_inclusive"
}
]
}
Expand Down
7 changes: 5 additions & 2 deletions qgis_deployment_toolbelt/jobs/job_profiles_synchronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
# package
from qgis_deployment_toolbelt.jobs.generic_job import GenericJob
from qgis_deployment_toolbelt.profiles.qdt_profile import QdtProfile
from qgis_deployment_toolbelt.utils.computer_environment import environment_dict
from qgis_deployment_toolbelt.utils.computer_environment import (
date_dict,
environment_dict,
)

# #############################################################################
# ########## Globals ###############
Expand Down Expand Up @@ -129,7 +132,7 @@ def filter_profiles_on_rules(
li_profiles_matched = []
li_profiles_unmatched = []

context_object = {"environment": environment_dict()}
context_object = {"date": date_dict(), "environment": environment_dict()}
for profile in li_downloaded_profiles:
if profile.rules is None:
logger.debug(f"No rules to apply to {profile.name}")
Expand Down
20 changes: 19 additions & 1 deletion qgis_deployment_toolbelt/utils/computer_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
# ########## Libraries #############
# ##################################

# Standard library
import logging
import platform

# Standard library
from datetime import date
from sys import platform as opersys

# #############################################################################
Expand All @@ -29,6 +31,22 @@
# ##################################


def date_dict() -> dict:
"""Returns a context dictionary with date informations that can be used in QDT
various places: rules...

Returns:
dict: dict with current date informations
"""
today = date.today()
return {
"current_day": today.day,
"current_weekday": today.weekday(), # monday = 0, sunday = 6
"current_month": today.month,
"current_year": today.year,
}


def environment_dict() -> dict:
"""Returns a dictionary containing some environment information (computer, network,
platform) that can be used in QDT various places: rules...
Expand Down