create basic POC views
This commit is contained in:
parent
81783ad025
commit
1dd4ac8088
4 changed files with 42 additions and 4 deletions
7
project/files/urls.py
Normal file
7
project/files/urls.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
from django.conf.urls import url
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^(?P<token>.*)/(?P<id>.*)/$', views.file_download, name="file_download"),
|
||||
url(r'^(?P<id>.*)/$', views.SharedFileDetails.as_view(), name="file"),
|
||||
]
|
|
@ -1,3 +1,25 @@
|
|||
from django.shortcuts import render
|
||||
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
|
||||
|
||||
# Create your views here.
|
||||
|
||||
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)
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
from django.conf.urls import include, url
|
||||
from django.contrib import admin
|
||||
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
|
||||
|
||||
from project.files import views
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^admin/', include(admin.site.urls)),
|
||||
url(r'^admin_resumable/', include('admin_resumable.urls')),
|
||||
url(r'', include("project.pages.urls", namespace="pages"))
|
||||
url(r'^files/', include('project.files.urls', namespace="files"))
|
||||
url(r'', include("project.pages.urls", namespace="pages")),
|
||||
]
|
||||
|
||||
|
||||
|
|
8
templates/file.html
Normal file
8
templates/file.html
Normal file
|
@ -0,0 +1,8 @@
|
|||
{% extends 'base.html' %}
|
||||
|
||||
{% block title %}file {{ file.get_original_filename }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h1>{{ file }}</h1>
|
||||
<h1>{{ token.token }}</h1>
|
||||
{% endblock %}
|
Reference in a new issue