def get(self, params=None): """ Check if the user is currently in superstar-prison. If user is currently servin' his sentence in the big house, return the name stored in the forced_nick column of prison_table. If user cannot be found in prison, or if his sentence has expired, return nothing. Data must be provided as params. API key must be provided as header. """ user_id = params.get("user_id") log.debug( f"Checking if user ({user_id}) is permitted to change their nickname." ) data = self.db.get(self.prison_table, user_id) or {} if data and data.get("end_timestamp"): log.trace("User exists in the prison_table.") end_time = data.get("end_timestamp") if is_expired(end_time): log.trace("...But their sentence has already expired.") data = {} # Return nothing if the sentence has expired. return jsonify(data)
def delete(self, json_data): """ Releases a user from superstar-prison. Data must be provided as JSON. API key must be provided as header. """ user_id = json_data.get("user_id") log.debug( f"Attempting to release user ({user_id}) from superstar-prison.") prisoner_data = self.db.get(self.prison_table, user_id) sentence_expired = None log.trace( f"Checking if the user ({user_id}) is currently in superstar-prison." ) if prisoner_data and prisoner_data.get("end_timestamp"): sentence_expired = is_expired(prisoner_data['end_timestamp']) if prisoner_data and not sentence_expired: log.debug( "User is currently in superstar-prison. Deleting the record and releasing the prisoner." ) self.db.delete(self.prison_table, user_id) return jsonify({"success": True}) elif not prisoner_data: log.warning( f"User ({user_id}) is not currently in superstar-prison.") return jsonify({ "success": False, "error_message": "User is not currently in superstar-prison!" }) elif sentence_expired: log.warning( f"User ({user_id}) was in superstar-prison, but has already been released." ) return jsonify({ "success": False, "error_message": "User has already been released from superstar-prison!" })
def test_datetimes_in_the_future_are_not_expired(self): for delta in self.EXPIRY_DELTAS: date = datetime.now(timezone.utc) + delta self.assertFalse(is_expired(date))
def test_datetimes_in_the_past_are_expired(self): for delta in self.EXPIRY_DELTAS: date = datetime.now(timezone.utc) - delta self.assertTrue(is_expired(date))