<gh_stars>1-10
from django.shortcuts import render, redirect
from django.views import View
from django.utils.crypto import get_random_string
from django.http import JsonResponse
from django.contrib.staticfiles.utils import get_files

from models.models import *

from API.serializers import PlaylistSerializer

import json

"""
  Since we are using django default template with Vue 3.0
  we will render all templates with python.
  written by @lyonkvalid - https://github.com/lyonkvalid/
"""

class HomeView(View):
  def __init__(self):
    self.template_name = "activities/home.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)

class HomeIntentView(View):
  def __init__(self):
    self.template_name = "intents/home.intent.html"
  def get(self, request, *args, **kwargs):
    _id = kwargs.get("id")
    collection = Collection.objects.get(id=_id)
    return render(request, self.template_name, {"id":_id, "collection":collection})
  def post(self, request, *args, **kwargs):
    type = request.GET.get("type")
    if type == "add-library":
      id = json.loads(request.body.decode("utf-8")).get("id")
      my_library = MyLibrary.objects.get(user=request.user)
      if my_library.playlists.filter(collection__id=id).exists():
        playlist = my_library.playlists.get(collection__id=id)
        my_library.playlists.remove(playlist)
        playlist.delete()
        return JsonResponse({"status":True, "type":"remove"})
      else:
        collection = Collection.objects.get(id=id)
        playlist = Playlist.objects.create(user=request.user, collection=collection)
        my_library.playlists.add(playlist)
      return JsonResponse({"status":True, "type":"add"})
    elif type == "add-to-playlist":
      id = request.GET.get("id")
      song_id = request.GET.get("song_id")
      song = Song.objects.get(id=song_id)
      collection = Collection.objects.get(id=id)
      collection.songs.add(song)
      return JsonResponse({"status":True, "type":"add"})
    elif type == "delete-playlist":
      id = request.GET.get("id")
      playlist = Playlist.objects.get(collection__id=id)
      playlist.collection.delete()
      playlist.delete()
      return JsonResponse({"status":True, "type":"remove"})
    else:
      return JsonResponse({"status":False, "msg":"provide a type in get query"})

class LibraryView(View):
  def __init__(self):
    self.template_name = "activities/library.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)
  def post(self, request, *args, **kwargs):
    type = request.GET.get("type")
    if type == "create-playlist":
      title = request.POST.get("title")
      if title == None or title == "":
        title = get_random_string()
      meta_data = MetaData(title=title, artist=request.user.username)
      meta_data.save()
      collection = Collection(user=request.user, meta_data=meta_data, type="playlist")
      collection.save()
      playlist = Playlist.objects.create(user=request.user, collection=collection)
      my_library = MyLibrary.objects.get(user=request.user, type="online")
      my_library.playlists.add(playlist)
      my_library.save()
      return JsonResponse({"status":True, "playlist":PlaylistSerializer(playlist).data})
    else:
      return JsonResponse({"status": False, "type":type})

class SearchView(View):
  def __init__(self):
    self.template_name = "activities/search.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)

class SearchIntentView(View):
  def __init__(self):
    self.template_name = "intents/search.intent.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)

class ExploreView(View):
  def __init__(self):
    self.template_name = "activities/explore.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)

class ProfileView(View):
  def __init__(self):
    self.template_name = "fragments/profile.html"
  def get(self, request, *args, **kwargs):
    id = kwargs.get("id")
    if id is not None:
      user = User.objects.get(id=id)
      my_library = MyLibrary.objects.get(user=user)
      return render(request, self.template_name, {"my_library":my_library, "user":user})
    #error 404 callback
  def post(self, request, *args, **kwargs):
    query = json.loads(request.body.decode("utf-8"))
    if True:
      following = User.objects.get(id=query.get("id"))
      if following.following.filter(id=request.user.id).exists() and request.user.followers.filter(id=following.id).exists():
        following.followers.remove(request.user)
        request.user.following.remove(following)
        return JsonResponse({"status":True, "type":"remove", "followers_count":following.followers.count()})
      else:
        following.followers.add(request.user)
        request.user.following.add(following)
        return JsonResponse({"status":True, "type":"add", "followers_count":following.followers.count()})
    #except:
    return JsonResponse({"status":False, "msg":"Unknown error"})

class ProfileIntentView(View):
  def __init__(self):
    self.template_name = "forms/profile/edit_profile.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)
  def post(self, request, *args, **kwargs):
    user = User.objects.get(id = request.user.id)
    type = request.GET.get("type")
    if type == "edit-profile":
      avatar = request.FILES.get("avatar")
      name = request.POST.get("name")
      if avatar is not None:
        user.kyc.avatar.save(request.user.username+".png", avatar)
      if name is not None:
        name = name.strip()
        name = name.split(" ")
        user.first_name = name[0].capitalize()
        if len(name) > 1:
          user.last_name = name[1].capitalize()
      user.save()
      return JsonResponse({"status":True, "msg":"Profile updated"})
    else:
      return JsonResponse({"status":False, "msg":"provide a type in get query"})

class SettingsView(View):
  def __init__(self):
    self.template_name = "fragments/settings.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)
  def post(self, request, *args, **kwargs):
    user = User.objects.get(id=request.user.id)
    profile = user.profile
    type = request.GET.get("type")
    if type == "data-saver":
      if profile.settings.data_saver:
        profile.settings.data_saver = False
      else:
        profile.settings.data_saver = True
      profile.settings.save()
      return JsonResponse({"status":True, "checked":user.settings.data_saver})
    if type == "auto-download":
      if profile.settings.auto_download:
        profile.settings.auto_download = False
      else:
        profile.settings.auto_download = True
      profile.settings.save()
      return JsonResponse({"status":True, "checked":user.settings.auto_download})
    if type == "plan":
      plan = request.GET.get("plan")
      kyc = request.user.kyc
      kyc.plans = plan
      kyc.save()
      return JsonResponse({"status":True, "plan":plan})
    else:
      return JsonResponse({"status":False, "msg":"provide a type in get query"})

class Dialog(View):
  def get(self, request, *args, **kwargs):
    template_name = request.GET.get("template_name")
    return render(request, template_name)

from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
from whitenoise.storage import CompressedManifestStaticFilesStorage

class StaticListView(View):
  def __init__(self):
    self.template_name = "service-worker/sw.js"
  def get(self, request, *args, **kwargs):
    storage = ManifestStaticFilesStorage()
    assets = []
    final_assets = []
    for folder in ("css", "images", "js"):
      assets += list(get_files(storage, location=folder))
    for file in assets:
      if not file.endswith(".gz"):
        final_assets.append("/static/{}".format(file))
    return JsonResponse({"assets":final_assets})

class LandingPageView(View):
  def __init__(self):
    self.template_name = "landing.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)

class OfflineView(View):
  def __init__(self):
    self.template_name = "native-ui/no_item.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)
from django.shortcuts import render, redirect
from django.views import View
from django.utils.crypto import get_random_string
from django.http import JsonResponse
from django.contrib.staticfiles.utils import get_files

from models.models import *

from API.serializers import PlaylistSerializer

import json

"""
  Since we are using django default template with Vue 3.0
  we will render all templates with python.
  written by @lyonkvalid - https://github.com/lyonkvalid/
"""

class HomeView(View):
  def __init__(self):
    self.template_name = "activities/home.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)

class HomeIntentView(View):
  def __init__(self):
    self.template_name = "intents/home.intent.html"
  def get(self, request, *args, **kwargs):
    _id = kwargs.get("id")
    collection = Collection.objects.get(id=_id)
    return render(request, self.template_name, {"id":_id, "collection":collection})
  def post(self, request, *args, **kwargs):
    type = request.GET.get("type")
    if type == "add-library":
      id = json.loads(request.body.decode("utf-8")).get("id")
      my_library = MyLibrary.objects.get(user=request.user)
      if my_library.playlists.filter(collection__id=id).exists():
        playlist = my_library.playlists.get(collection__id=id)
        my_library.playlists.remove(playlist)
        playlist.delete()
        return JsonResponse({"status":True, "type":"remove"})
      else:
        collection = Collection.objects.get(id=id)
        playlist = Playlist.objects.create(user=request.user, collection=collection)
        my_library.playlists.add(playlist)
      return JsonResponse({"status":True, "type":"add"})
    elif type == "add-to-playlist":
      id = request.GET.get("id")
      song_id = request.GET.get("song_id")
      song = Song.objects.get(id=song_id)
      collection = Collection.objects.get(id=id)
      collection.songs.add(song)
      return JsonResponse({"status":True, "type":"add"})
    elif type == "delete-playlist":
      id = request.GET.get("id")
      playlist = Playlist.objects.get(collection__id=id)
      playlist.collection.delete()
      playlist.delete()
      return JsonResponse({"status":True, "type":"remove"})
    else:
      return JsonResponse({"status":False, "msg":"provide a type in get query"})

class LibraryView(View):
  def __init__(self):
    self.template_name = "activities/library.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)
  def post(self, request, *args, **kwargs):
    type = request.GET.get("type")
    if type == "create-playlist":
      title = request.POST.get("title")
      if title == None or title == "":
        title = get_random_string()
      meta_data = MetaData(title=title, artist=request.user.username)
      meta_data.save()
      collection = Collection(user=request.user, meta_data=meta_data, type="playlist")
      collection.save()
      playlist = Playlist.objects.create(user=request.user, collection=collection)
      my_library = MyLibrary.objects.get(user=request.user, type="online")
      my_library.playlists.add(playlist)
      my_library.save()
      return JsonResponse({"status":True, "playlist":PlaylistSerializer(playlist).data})
    else:
      return JsonResponse({"status": False, "type":type})

class SearchView(View):
  def __init__(self):
    self.template_name = "activities/search.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)

class SearchIntentView(View):
  def __init__(self):
    self.template_name = "intents/search.intent.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)

class ExploreView(View):
  def __init__(self):
    self.template_name = "activities/explore.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)

class ProfileView(View):
  def __init__(self):
    self.template_name = "fragments/profile.html"
  def get(self, request, *args, **kwargs):
    id = kwargs.get("id")
    if id is not None:
      user = User.objects.get(id=id)
      my_library = MyLibrary.objects.get(user=user)
      return render(request, self.template_name, {"my_library":my_library, "user":user})
    #error 404 callback
  def post(self, request, *args, **kwargs):
    query = json.loads(request.body.decode("utf-8"))
    if True:
      following = User.objects.get(id=query.get("id"))
      if following.following.filter(id=request.user.id).exists() and request.user.followers.filter(id=following.id).exists():
        following.followers.remove(request.user)
        request.user.following.remove(following)
        return JsonResponse({"status":True, "type":"remove", "followers_count":following.followers.count()})
      else:
        following.followers.add(request.user)
        request.user.following.add(following)
        return JsonResponse({"status":True, "type":"add", "followers_count":following.followers.count()})
    #except:
    return JsonResponse({"status":False, "msg":"Unknown error"})

class ProfileIntentView(View):
  def __init__(self):
    self.template_name = "forms/profile/edit_profile.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)
  def post(self, request, *args, **kwargs):
    user = User.objects.get(id = request.user.id)
    type = request.GET.get("type")
    if type == "edit-profile":
      avatar = request.FILES.get("avatar")
      name = request.POST.get("name")
      if avatar is not None:
        user.kyc.avatar.save(request.user.username+".png", avatar)
      if name is not None:
        name = name.strip()
        name = name.split(" ")
        user.first_name = name[0].capitalize()
        if len(name) > 1:
          user.last_name = name[1].capitalize()
      user.save()
      return JsonResponse({"status":True, "msg":"Profile updated"})
    else:
      return JsonResponse({"status":False, "msg":"provide a type in get query"})

class SettingsView(View):
  def __init__(self):
    self.template_name = "fragments/settings.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)
  def post(self, request, *args, **kwargs):
    user = User.objects.get(id=request.user.id)
    profile = user.profile
    type = request.GET.get("type")
    if type == "data-saver":
      if profile.settings.data_saver:
        profile.settings.data_saver = False
      else:
        profile.settings.data_saver = True
      profile.settings.save()
      return JsonResponse({"status":True, "checked":user.settings.data_saver})
    if type == "auto-download":
      if profile.settings.auto_download:
        profile.settings.auto_download = False
      else:
        profile.settings.auto_download = True
      profile.settings.save()
      return JsonResponse({"status":True, "checked":user.settings.auto_download})
    if type == "plan":
      plan = request.GET.get("plan")
      kyc = request.user.kyc
      kyc.plans = plan
      kyc.save()
      return JsonResponse({"status":True, "plan":plan})
    else:
      return JsonResponse({"status":False, "msg":"provide a type in get query"})

class Dialog(View):
  def get(self, request, *args, **kwargs):
    template_name = request.GET.get("template_name")
    return render(request, template_name)

from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
from whitenoise.storage import CompressedManifestStaticFilesStorage

class StaticListView(View):
  def __init__(self):
    self.template_name = "service-worker/sw.js"
  def get(self, request, *args, **kwargs):
    storage = ManifestStaticFilesStorage()
    assets = []
    final_assets = []
    for folder in ("css", "images", "js"):
      assets += list(get_files(storage, location=folder))
    for file in assets:
      if not file.endswith(".gz"):
        final_assets.append("/static/{}".format(file))
    return JsonResponse({"assets":final_assets})

class LandingPageView(View):
  def __init__(self):
    self.template_name = "landing.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)

class OfflineView(View):
  def __init__(self):
    self.template_name = "native-ui/no_item.html"
  def get(self, request, *args, **kwargs):
    return render(request, self.template_name)
Since we are using django default template with Vue 3.0   we will render all templates with python.   written by @lyonkvalid - https://github.com/lyonkvalid/ #error 404 callback #except: