diff --git a/horde/apis/v2/base.py b/horde/apis/v2/base.py index 5ab452de..c0980b44 100644 --- a/horde/apis/v2/base.py +++ b/horde/apis/v2/base.py @@ -648,7 +648,7 @@ class TransferKudos(Resource): "username", type=str, required=True, - help="The user ID which will receive the kudos.", + help="The user or shared key ID which will receive the kudos.", location="json", ) parser.add_argument( diff --git a/horde/classes/base/user.py b/horde/classes/base/user.py index 36a06fff..e7f49b8c 100644 --- a/horde/classes/base/user.py +++ b/horde/classes/base/user.py @@ -176,11 +176,17 @@ def consume_kudos(self, kudos): def is_valid(self): if self.kudos == 0: return False, "This shared key has run out of kudos.", "SharedKeyEmpty" - if self.expiry is not None and self.expiry < datetime.utcnow(): + if self.is_expired(): return False, "This shared key has expired", "SharedKeyExpired" else: return True, None, None + def is_expired(self) -> bool: + """Returns true if the key has expired""" + if self.expiry is not None and self.expiry < datetime.utcnow(): + return True + return False + def is_job_within_limits( self, *, diff --git a/horde/database/functions.py b/horde/database/functions.py index 3f5c0a39..604165bb 100644 --- a/horde/database/functions.py +++ b/horde/database/functions.py @@ -473,13 +473,22 @@ def transfer_kudos(source_user, dest_user, amount): def transfer_kudos_to_username(source_user, dest_username, amount): dest_user = find_user_by_username(dest_username) + shared_key = None if not dest_user: - return [0, "Invalid target username.", "InvalidTargetUsername"] + shared_key = find_sharedkey(dest_username) + if not shared_key: + return [0, "Invalid target username.", "InvalidTargetUsername"] + if shared_key.is_expired(): + return [0, "This shared key has expired", "SharedKeyExpired"] + dest_user = shared_key.user if dest_user == get_anon(): return [0, "Tried to burn kudos via sending to Anonymous. Assuming PEBKAC and aborting.", "KudosTransferToAnon"] if dest_user == source_user: return [0, "Cannot send kudos to yourself, ya monkey!", "KudosTransferToSelf"] kudos = transfer_kudos(source_user, dest_user, amount) + if kudos[0] > 0 and shared_key is not None: + shared_key.kudos += kudos[0] + db.session.commit() return kudos diff --git a/horde/templates/transfer_kudos.html b/horde/templates/transfer_kudos.html index 842541e1..24d6d731 100644 --- a/horde/templates/transfer_kudos.html +++ b/horde/templates/transfer_kudos.html @@ -54,7 +54,7 @@

Welcome

{% endif %}
- +