|
| 1 | +from django.core.management.base import BaseCommand |
| 2 | +from django.contrib.sites.models import Site |
| 3 | +from allauth.socialaccount.models import SocialApp |
| 4 | +import getpass |
| 5 | + |
| 6 | + |
| 7 | +class Command(BaseCommand): |
| 8 | + help = 'Updates default Site and Social Application configurations' |
| 9 | + |
| 10 | + def handle(self, *args, **options): |
| 11 | + self.stdout.write(self.style.HTTP_INFO("\n=== Site Configuration Update ===")) |
| 12 | + self.update_site() |
| 13 | + self.stdout.write(self.style.HTTP_INFO("\n=== Social Applications Update ===")) |
| 14 | + self.update_social_apps() |
| 15 | + |
| 16 | + def update_site(self): |
| 17 | + """Update the default Site domain and display name""" |
| 18 | + try: |
| 19 | + site = Site.objects.get_current() |
| 20 | + self.stdout.write(self.style.HTTP_INFO("\nCurrent Site Configuration:")) |
| 21 | + self.stdout.write(f"Domain: {site.domain}") |
| 22 | + self.stdout.write(f"Display Name: {site.name}\n") |
| 23 | + |
| 24 | + new_domain = input("Enter new domain (e.g., example.com): ").strip() |
| 25 | + new_name = input("Enter new display name: ").strip() |
| 26 | + |
| 27 | + if new_domain: |
| 28 | + site.domain = new_domain |
| 29 | + if new_name: |
| 30 | + site.name = new_name |
| 31 | + |
| 32 | + site.save() |
| 33 | + self.stdout.write(self.style.SUCCESS("Site updated successfully!")) |
| 34 | + except Exception as e: |
| 35 | + self.stdout.write(self.style.ERROR(f"Error updating site: {str(e)}")) |
| 36 | + |
| 37 | + def update_social_apps(self): |
| 38 | + """Update all Social Applications""" |
| 39 | + social_apps = SocialApp.objects.all() |
| 40 | + |
| 41 | + if not social_apps.exists(): |
| 42 | + self.stdout.write(self.style.WARNING("No social applications found.")) |
| 43 | + return |
| 44 | + |
| 45 | + for app in social_apps: |
| 46 | + self.stdout.write(self.style.HTTP_INFO(f"\nUpdating {app.provider} application:")) |
| 47 | + |
| 48 | + # Get new values with current values as placeholders |
| 49 | + new_name = input(f"Application name [{app.name}]: ").strip() or app.name |
| 50 | + new_client_id = input(f"Client ID [{app.client_id}]: ").strip() or app.client_id |
| 51 | + |
| 52 | + # For secret key, don't show current value for security |
| 53 | + self.stdout.write("Current secret key: [hidden]") |
| 54 | + new_secret = getpass.getpass("New Secret Key (leave blank to keep current): ").strip() |
| 55 | + |
| 56 | + # Update fields |
| 57 | + app.name = new_name |
| 58 | + app.client_id = new_client_id |
| 59 | + if new_secret: |
| 60 | + app.secret = new_secret |
| 61 | + |
| 62 | + try: |
| 63 | + app.save() |
| 64 | + self.stdout.write(self.style.SUCCESS(f"{app.provider} application updated!")) |
| 65 | + except Exception as e: |
| 66 | + self.stdout.write(self.style.ERROR(f"Error updating {app.provider}: {str(e)}")) |
0 commit comments