-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2136: Add addon filter objects r=uhlissuh a=uhlissuh fixes #2104 So, splitting these into two filter objects came from Rehan, which I thought was a good idea. He mentioned seeing about having them share some code, because there is duplication here, and I kinda looked at that for a while and arrived at wondering if we will really gain much here trying to have these two share code? There's only two that are shaped like this right now. There isn't a clear thing I could pull out, maybe someone sees something. Perhaps that's an argument for making them one filter? Co-authored-by: Alissa Sobo <[email protected]>
- Loading branch information
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,28 @@ def to_jexl(self): | |
raise NotImplementedError | ||
|
||
|
||
class BaseAddonFilter(BaseFilter): | ||
addons = serializers.ListField(child=serializers.CharField(), min_length=1) | ||
any_or_all = serializers.CharField() | ||
|
||
def get_formatted_string(self, addon): | ||
raise NotImplementedError("Not correctly implemented.") | ||
|
||
def to_jexl(self): | ||
any_or_all = self.initial_data["any_or_all"] | ||
|
||
symbol = {"all": "&&", "any": "||"}.get(any_or_all) | ||
|
||
if not symbol: | ||
raise serializers.ValidationError( | ||
f"Unrecognized string for any_or_all: {any_or_all!r}" | ||
) | ||
|
||
return symbol.join( | ||
self.get_formatted_string(addon) for addon in self.initial_data["addons"] | ||
) | ||
|
||
|
||
class BaseComparisonFilter(BaseFilter): | ||
value = serializers.IntegerField() | ||
comparison = serializers.CharField() | ||
|
@@ -224,6 +246,68 @@ def capabilities(self): | |
return set() | ||
|
||
|
||
class AddonActiveFilter(BaseAddonFilter): | ||
"""Match a user based on if a particular addon is active. | ||
.. attribute:: type | ||
``addon_active`` | ||
.. attribute:: addons | ||
List of addon ids to filter against. | ||
:example: ``["[email protected]", "[email protected]"]`` | ||
.. attribute:: any_or_all | ||
This will determine whether the addons are connected with an "&&" operator, | ||
meaning all the addons must be active for the filter to evaluate to true, | ||
or an "||" operator, meaning any of the addons can be active to evaluate to | ||
true. | ||
:example: ``any`` or ``all`` | ||
""" | ||
|
||
type = "addon_active" | ||
|
||
def get_formatted_string(self, addon): | ||
return f'normandy.addons["{addon}"].isActive' | ||
|
||
@property | ||
def capabilities(self): | ||
return set() | ||
|
||
|
||
class AddonInstalledFilter(BaseAddonFilter): | ||
"""Match a user based on if a particular addon is installed. | ||
.. attribute:: type | ||
``addon_installed`` | ||
.. attribute:: addons | ||
List of addon ids to filter against. | ||
:example: ``["[email protected]", "[email protected]"]`` | ||
.. attribute:: any_or_all | ||
This will determine whether the addons are connected with an "&&" operator, | ||
meaning all the addons must be installed for the filter to evaluate to true, | ||
or an "||" operator, meaning any of the addons can be installed to | ||
evaluate to true. | ||
:example: ``any`` or ``all`` | ||
""" | ||
|
||
type = "addon_installed" | ||
|
||
def get_formatted_string(self, addon): | ||
return f'normandy.addons["{addon}"]' | ||
|
||
@property | ||
def capabilities(self): | ||
return set() | ||
|
||
|
||
class PrefCompareFilter(BaseFilter): | ||
"""Match based on a user's pref having a particular value. | ||
|
@@ -751,6 +835,8 @@ def capabilities(self): | |
WindowsVersionFilter, | ||
WindowsBuildNumberFilter, | ||
NegateFilter, | ||
AddonActiveFilter, | ||
AddonInstalledFilter, | ||
] | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters