diff --git a/web/models.py b/web/models.py index 6b8ea8ef4..4548c62fd 100644 --- a/web/models.py +++ b/web/models.py @@ -3,10 +3,10 @@ import string import time import uuid +import logging from datetime import datetime, timedelta from io import BytesIO from urllib.parse import parse_qs, urlparse - from allauth.account.signals import user_signed_up from django.conf import settings from django.contrib.auth.models import User @@ -27,6 +27,8 @@ from web.utils import calculate_and_update_user_streak +logger = logging.getLogger(__name__) + class Notification(models.Model): NOTIFICATION_TYPES = [ @@ -487,17 +489,14 @@ def fetch_coordinates(self): coordinates = geocode_address(self.location) if coordinates: self.latitude, self.longitude = coordinates - print("location store:", self.latitude, self.longitude) + logger.debug("location store: %s %s", self.latitude, self.longitude) else: - print( + logger.debug( f"Skipping session {self.id} due to invalid coordinates:", f"lat={self.latitude}, \n lng={self.longitude}", ) except Exception as e: - import logging - - logger = logging.getLogger(__name__) - logger.error("Error geocoding session %s location '%s': %s", self.id, self.location, str(e)) + logger.exception("Error geocoding session %s location '%s'", self.id, self.location) def is_live(self): """Returns True if the session is live right now.""" diff --git a/web/quiz_views.py b/web/quiz_views.py index e86890a69..cc5734c4d 100644 --- a/web/quiz_views.py +++ b/web/quiz_views.py @@ -1,3 +1,5 @@ +import logging + import json import random @@ -18,6 +20,7 @@ TakeQuizForm, ) from .models import Quiz, QuizQuestion, UserQuiz +logger = logging.getLogger(__name__) @login_required @@ -182,7 +185,7 @@ def add_question(request, quiz_id): else: return redirect("quiz_detail", quiz_id=quiz.id) except Exception as e: - print(e) + logger.error("An error occurred: %s", e) # Re-raise the exception raise else: diff --git a/web/views.py b/web/views.py index b4d485749..e4e87195d 100644 --- a/web/views.py +++ b/web/views.py @@ -1134,7 +1134,7 @@ def run_cmd(cmd): def send_slack_message(message): webhook_url = os.getenv("SLACK_WEBHOOK_URL") if not webhook_url: - print("Warning: SLACK_WEBHOOK_URL not configured") + logger.warning("SLACK_WEBHOOK_URL not configured") return payload = {"text": f"```{message}```"} @@ -3254,7 +3254,7 @@ def create_forum_category(request): messages.success(request, f"Forum category '{category.name}' created successfully!") return redirect("forum_category", slug=category.slug) else: - print(form.errors) + logger.debug("Form errors: %s", form.errors) else: form = ForumCategoryForm() @@ -3480,7 +3480,7 @@ def system_status(request): if sendgrid_api_key: status["sendgrid"]["api_key_configured"] = True try: - print("Checking SendGrid API...") + logger.debug("Checking SendGrid API...") response = requests.get( "https://api.sendgrid.com/v3/user/account", headers={"Authorization": f"Bearer {sendgrid_api_key}"}, @@ -8113,7 +8113,7 @@ def post_to_twitter(request, post_id): post.posted_at = timezone.now() post.save() except Exception as e: - print(f"Error posting tweet: {e}") + logger.exception("Error posting tweet") return redirect("social_media_dashboard") return redirect("social_media_dashboard") @@ -8485,7 +8485,7 @@ def add_contributor(username, avatar_url, profile_url): except Exception as e: # Log the error - print(f"Error fetching contributors: {e}") + logger.exception("Error fetching contributors") # Return an empty list in case of error return render(request, "web/contributors_list.html", {"contributors": []})