Files
Django_kube/django_code/apps/random_simpsons/views.py
T
pierre geeraert decbfc1bb5
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 2s
stop this async crap for now
2026-07-04 20:56:57 +01:00

67 lines
2.6 KiB
Python

from django.http import HttpResponse
from django.db.models import Count
from django.shortcuts import redirect
from .models import SimpsonsData,simpsons_data_object
from .utils import mark_episode_watched
import random
def hello(request):
return HttpResponse('<h1>Hello Simpsons view!</h1>')
def episode_generator(request):
#simpsons_data_object = SimpsonsData.objects.using('simpsons_db')
boolean_restart = True;
while (boolean_restart):
maximum_number_of_seasons = simpsons_data_object.aggregate(unique_season=Count('season', distinct=True))['unique_season']
random_seasons = random.randint(1, maximum_number_of_seasons)
print("random season: " + str(random_seasons))
maximum_number_of_episodes = simpsons_data_object.filter(season=random_seasons).count()
random_episode = random.randint(1, maximum_number_of_episodes)
print("random episode: " + str(random_episode))
episode_seen = simpsons_data_object.filter(season=random_seasons, episode=random_episode).values('seen')[0]['seen']
if not(episode_seen):
boolean_restart = False
else:
print("episode "+str(random_episode)+" from season "+str(random_seasons)+" already seen")
random_url = simpsons_data_object.filter(season=random_seasons, episode=random_episode).values('url')[0]['url']
#Set episode as seen
# It will still update an already seem episode but it works and I'm lazy
#Mark episode as seen on betaseries
mark_episode_watched(random_seasons,random_episode,"watched")
return redirect(str(random_url))
def download_file(request):
# fill these variables with real values
fl_path = '/data/download/sonarr'
filename = 'The.Boys.2019.S04E05.2160p.HDR10Plus.WEBRip.6CH.x265.HEVC-PSA.mkv'
# fl = open(fl_path, 'r')
# mime_type, _ = mimetypes.guess_type(fl_path)
# response = HttpResponse(fl, content_type=mime_type)
# response['Content-Disposition'] = "attachment; filename=%s" % filename
some_file = open('/data/kubernetes/Backup/nextcloud/notes/2024-07-28/Course.md', "r")
django_file = File(some_file)
return HttpResponse("<h1>Hello download!</h1><h1><a href='{{ MEDIA_URL }}{{ django_file.relative_path }}'>{{ django_file.name }}</a></h1><h1>bye download!</h1>")
# return response
def download_mo(request,file):
path_to_file = os.path.realpath(file)
response = FileResponse(open(path_to_file, 'rb'))
file_name = file[5:]
response['Content-Disposition'] = 'inline; filename=' + file_name
return response