Skip to content

Commit

Permalink
feat: Add logic to update settings and delete related data based on s…
Browse files Browse the repository at this point in the history
…erver type

The code in `settings_api.py` was modified to handle updates to the "server_type" setting. When the "server_type" is updated, related data in the `Libraries`, `Users`, `Invitations`, and `Requests` tables is deleted. Additionally, if the "server_type" is changed to "plex", specific records in the `Requests` table are deleted.

refactor: Rename functions related to user synchronization

In `users_api.py`, the functions `global_sync_users` and `global_delete_user` were renamed to `global_sync_users_to_media_server` and `global_delete_user_from_media_server`, respectively. The functionality of these functions remains the same.

fix: Handle creation of webhooks in webhooks_api.py

The code in `webhooks_api.py` was modified to properly create a webhook object using the request form data. The created webhook now includes the creation timestamp and is returned as a response.

fix: Update function names in scheduler.py

In `scheduler.py`, the functions `global_delete_user` and `global_sync_users` were renamed to `global_delete_user_from_media_server` and `global_sync_users_to_media_server`, respectively. These function names were updated to match the renamed functions in `users_api.py`.

misc: Remove unnecessary print statements from plex.py

In `plex.py`, unnecessary print statements were removed from the `delete_plex_user` function. These print statements were used for debugging purposes and are not important for regular execution.
  • Loading branch information
realashleybailey committed Oct 2, 2023
1 parent a0c0a5d commit 336854d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
17 changes: 17 additions & 0 deletions backend/api/routes/settings_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
from app.models.database.settings import Settings
from app.security import is_setup_required

from app.models.database.libraries import Libraries
from app.models.database.users import Users
from app.models.database.invitations import Invitations
from app.models.database.requests import Requests


api = Namespace("Settings", description="Settings related operations", path="/settings")

api.add_model("SettingsPostModel", SettingsPostModel)
Expand Down Expand Up @@ -78,6 +84,17 @@ def put(self):
else:
Settings.update(value=value).where(Settings.key == key).execute()

if key == "server_type":
Libraries.delete().execute()
Users.delete().execute()
Invitations.delete().execute()

if value == "plex":
Requests.delete().where(Requests.service == "jellyseerr").execute()
elif value == "jellyfin":
Requests.delete().where(Requests.service == "overseerr").execute()


return response, 200


Expand Down
6 changes: 3 additions & 3 deletions backend/api/routes/users_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from flask_restx import Namespace, Resource
from json import loads, dumps

from helpers.universal import global_sync_users, global_delete_user, global_get_user_profile_picture
from helpers.universal import global_sync_users_to_media_server, global_delete_user_from_media_server, global_get_user_profile_picture
from app.models.database.users import Users

from app.extensions import cache
Expand Down Expand Up @@ -37,7 +37,7 @@ class UsersAPI(Resource):
@api.doc(description="Delete a user from the database and media server")
@api.response(500, "Internal server error")
def delete(self, user_id):
return global_delete_user(user_id), 200
return global_delete_user_from_media_server(user_id), 200


@api.route("/<string:user_id>/profile-picture")
Expand All @@ -61,5 +61,5 @@ class UsersScanAPI(Resource):
@api.doc(description="Scan for new users")
@api.response(500, "Internal server error")
def get(self):
return global_sync_users(), 200
return global_sync_users_to_media_server(), 200

6 changes: 5 additions & 1 deletion backend/api/routes/webhooks_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ def get(self):
@api.response(200, "Successfully created a webhook")
def post(self):
"""Create a webhook"""
webhook = Webhooks.create(name=str(request.form.get("name")), url=str(request.form.get("url")))

# Create the webhook
webhook = Webhooks.create(**request.form)
webhook.created = datetime.utcnow()

# Return the webhook
return loads(dumps(model_to_dict(webhook), indent=4, sort_keys=True, default=str)), 200

@api.route("/<int:webhook_id>")
Expand Down
8 changes: 4 additions & 4 deletions backend/app/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def check_expiring_users():
if not server_verified(): return

# Import the function here to avoid circular imports
from helpers.universal import global_delete_user
from helpers.universal import global_delete_user_from_media_server
from helpers.users import get_users_by_expiring

# Log message to console
Expand All @@ -26,7 +26,7 @@ def check_expiring_users():

# Delete all expired users
for user in expiring:
global_delete_user(user.id)
global_delete_user_from_media_server(user.id)
info(f"Deleting user { user.email if user.email else user.username } due to expired invite.")


Expand Down Expand Up @@ -54,10 +54,10 @@ def scan_users():
if not server_verified(): return

# Import the function here to avoid circular imports
from helpers.universal import global_sync_users
from helpers.universal import global_sync_users_to_media_server

info("Scanning for new users")
global_sync_users()
global_sync_users_to_media_server()

@schedule.task("interval", id="checkForUpdates", hours=1, misfire_grace_time=900)
def check_for_updates():
Expand Down
6 changes: 4 additions & 2 deletions backend/helpers/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,14 @@ def delete_plex_user(user_id: str, server_api_key: Optional[str] = None, server_
try:
plex_account.removeFriend(user_id)
except NotFound as e:
print(e)
print("NOT IMPORTANT: ", e)
print("The above error is not important, it just means that the user is not a Plex Friend.")

try:
plex_account.removeHomeUser(user_id)
except NotFound as e:
print(e)
print("NOT IMPORTANT: ", e)
print("The above error is not important, it just means that the user is not a Plex Home User.")


# ANCHOR - Plex Sync Users
Expand Down

0 comments on commit 336854d

Please sign in to comment.