archive
/
static-share
Archived
1
Fork 0
This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
static-share/project/files/views.py

26 lines
923 B
Python

from django.views.generic import TemplateView
from django.shortcuts import get_object_or_404
from django.http import HttpResponse
from .models import SharedFile, FileToken
import datetime
from django.utils import timezone
class SharedFileDetails(TemplateView):
template_name = "file.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['file'] = get_object_or_404(SharedFile, short_id=kwargs['id'])
context['token'] = FileToken.objects.create(file=context['file'])
return context
def file_download(request, token, id):
time_threshold = timezone.now() - FileToken.valid_time
FileToken.objects.filter(created__lt=time_threshold)
file = get_object_or_404(SharedFile, short_id=id)
token = get_object_or_404(FileToken, token=token, file=file)
token.delete() # delete after used
return HttpResponse(file.file.name)