2
0

views.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from django.shortcuts import render, redirect
  2. from django.contrib.auth import logout
  3. from django.views.decorators.http import require_POST
  4. from django.contrib.auth.decorators import login_required
  5. from allauth.socialaccount.models import SocialToken
  6. from django.http import HttpResponse
  7. from django.contrib.auth.models import User
  8. from django.contrib import messages
  9. from apps.main.models import Playlist
  10. from django.template import loader
  11. # Create your views here.
  12. def index(request):
  13. if request.user.is_anonymous:
  14. return render(request, 'index.html')
  15. else:
  16. return redirect('home')
  17. @require_POST
  18. def update_settings(request):
  19. print(request.POST)
  20. user = request.user
  21. username_input = request.POST['username'].strip()
  22. message_content = "Saved! Refresh to see changes!"
  23. message_type = "success"
  24. if username_input != user.username:
  25. if User.objects.filter(username__exact=username_input).count() != 0:
  26. message_type = "danger"
  27. message_content = f"Username {request.POST['username'].strip()} already taken"
  28. else:
  29. user.username = request.POST['username'].strip()
  30. user.save()
  31. message_content = f"Username updated to {username_input}!"
  32. return HttpResponse(loader.get_template("intercooler/messages.html").render(
  33. {"message_type": message_type, "message_content": message_content}))
  34. @login_required
  35. def delete_account(request):
  36. request.user.profile.delete()
  37. request.user.delete()
  38. request.session.flush()
  39. messages.success(request, "Account data deleted successfully.")
  40. return redirect('index')
  41. def log_out(request):
  42. request.session.flush() # delete all stored session keys
  43. logout(request) # log out authenticated user
  44. return redirect('/')
  45. def test(request):
  46. return render(request, 'test.html')
  47. def start_import(request):
  48. '''
  49. Initializes only the user's playlist data in the database. Returns the progress bar, which will
  50. keep calling continue_import
  51. :param request:
  52. :return:
  53. '''
  54. user_profile = request.user.profile
  55. if user_profile.access_token == "" or user_profile.refresh_token == "":
  56. user_social_token = SocialToken.objects.get(account__user=request.user)
  57. user_profile.access_token = user_social_token.token
  58. user_profile.refresh_token = user_social_token.token_secret
  59. user_profile.expires_at = user_social_token.expires_at
  60. request.user.save()
  61. result = Playlist.objects.getAllPlaylistsFromYT(request.user)
  62. channel_found = True
  63. if result["status"] == -1:
  64. print("User has no YT channel")
  65. channel_found = False
  66. return HttpResponse(loader.get_template('intercooler/progress_bar.html').render(
  67. {"channel_found": channel_found}
  68. ))
  69. elif result["status"] == -2:
  70. print("User has no playlists on YT")
  71. return HttpResponse(loader.get_template('intercooler/progress_bar.html').render(
  72. {"total_playlists": 0,
  73. "playlists_imported": 0,
  74. "done": True,
  75. "progress": 100,
  76. "channel_found": channel_found}))
  77. else:
  78. return HttpResponse(loader.get_template('intercooler/progress_bar.html').render(
  79. {"total_playlists": result["num_of_playlists"],
  80. "playlist_name": result["first_playlist_name"],
  81. "playlists_imported": 0,
  82. "progress": 0,
  83. "channel_found": channel_found}
  84. ))
  85. def continue_import(request):
  86. if request.user.profile.import_in_progress is False:
  87. return redirect('home')
  88. num_of_playlists = request.user.profile.playlists.count()
  89. try:
  90. remaining_playlists = request.user.profile.playlists.filter(is_in_db=False)
  91. playlists_imported = num_of_playlists - remaining_playlists.count() + 1
  92. playlist = remaining_playlists.order_by("created_at")[0]
  93. playlist_name = playlist.name
  94. playlist_id = playlist.playlist_id
  95. Playlist.objects.getAllVideosForPlaylist(request.user, playlist.playlist_id)
  96. except:
  97. playlist_id = -1
  98. if playlist_id != -1:
  99. return HttpResponse(loader.get_template('intercooler/progress_bar.html').render(
  100. {"total_playlists": num_of_playlists,
  101. "playlists_imported": playlists_imported,
  102. "playlist_name": playlist_name,
  103. "progress": round((playlists_imported / num_of_playlists) * 100, 1),
  104. "channel_found": True}))
  105. else:
  106. # request.user.profile.just_joined = False
  107. request.user.profile.import_in_progress = False
  108. request.user.save()
  109. return HttpResponse(loader.get_template('intercooler/progress_bar.html').render(
  110. {"total_playlists": num_of_playlists,
  111. "playlists_imported": num_of_playlists,
  112. "done": True,
  113. "progress": 100,
  114. "channel_found": True}))