Skip to content

Commit

Permalink
fixes after rebase and some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
elfjes committed Dec 11, 2024
1 parent cc99370 commit cdd8160
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
18 changes: 5 additions & 13 deletions src/argus/auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ class MyPreferences:
if namespace is None:
raise ValueError("namespace may not be None")

FIELDS = getattr(cls, "FIELDS", None)
if FIELDS is None or not all(isinstance(k, str) and isinstance(v, PreferenceField) for k, v in FIELDS.items()):
raise TypeError(f"{cls.__name__}.FIELDS must be set to a Dict[str, PreferenceField]")

class Meta:
pass

Expand Down Expand Up @@ -209,7 +205,7 @@ class Meta:
models.UniqueConstraint(name="unique_preference", fields=["user", "namespace"]),
]

NAMESPACES: dict[str, Type[Preferences]] = {}
NAMESPACES: dict[str, type[Preferences]] = {}

user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="preferences")
namespace = models.CharField(blank=False, max_length=255)
Expand All @@ -219,19 +215,15 @@ class Meta:
unregistered = UnregisteredPreferencesManager()

# must be set by the subclasses
FORMS: dict[str, forms.Form]
_FIELD_DEFAULTS: dict[str, Any]

FIELDS: dict[str, PreferenceField]

# django methods

# called when subclass is constructing itself
def __init_subclass__(cls, **kwargs):
assert isinstance(getattr(cls, "FORMS", None), dict), f"{cls.__name__}.FORMS must be a dictionary"
assert isinstance(
getattr(cls, "_FIELD_DEFAULTS", None), dict
), f"{cls.__name__}._FIELD_DEFAULTS must be a dictionary"
FIELDS = getattr(cls, "FIELDS", None)
if FIELDS is None or not all(isinstance(k, str) and isinstance(v, PreferenceField) for k, v in FIELDS.items()):
raise TypeError(f"{cls.__name__}.FIELDS must be set to a dict[str, PreferenceField]")

super().__init_subclass__(**kwargs)
cls.NAMESPACES[cls._namespace] = cls
Expand Down Expand Up @@ -263,7 +255,7 @@ def get_defaults(cls):
return {key: field.default for key, field in cls.FIELDS.items()}

@classmethod
def ensure_for_user(cls, user) -> List[Preferences]:
def ensure_for_user(cls, user) -> list[Preferences]:
all_preferences = {p.namespace: p for p in user.preferences.all()}
valid_preferences = []

Expand Down
4 changes: 2 additions & 2 deletions src/argus/auth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"get_psa_authentication_backends",
"get_preference_obj",
"get_preference",
"save_preference",
"get_or_update_preference",
]


Expand Down Expand Up @@ -76,7 +76,7 @@ def save_preferences(request, data, namespace_or_prefs: Union[str, Preferences])
return saved, failed


def save_preference(request, data, namespace, preference):
def get_or_update_preference(request, data, namespace, preference):
"""Save the single preference given in data to the given namespace
Returns a tuple (value, success). Value is the value of the preference, and success a boolean
Expand Down

0 comments on commit cdd8160

Please sign in to comment.