Add file and admin panel
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -58,3 +58,4 @@ db.sqlite3
|
||||
app/migrations/*
|
||||
!app/migrations/__init__.py
|
||||
venv/
|
||||
media/*
|
||||
|
||||
23
app/admin.py
23
app/admin.py
@@ -1,8 +1,25 @@
|
||||
from django.contrib import admin
|
||||
from django.contrib import admin
|
||||
from app.models import Setting
|
||||
from app.models import Setting, File
|
||||
from django.conf import settings
|
||||
|
||||
# Register your models here.
|
||||
|
||||
|
||||
admin.site.register(Setting)
|
||||
class SettingAdmin(admin.ModelAdmin):
|
||||
list_display = ('name', 'language', 'string_short')
|
||||
search_fields = ('name', )
|
||||
|
||||
def string_short(self, obj):
|
||||
if len(obj.string) > 70:
|
||||
return obj.string[:70] + '...'
|
||||
else:
|
||||
return obj.string
|
||||
|
||||
|
||||
class FileAdmin(admin.ModelAdmin):
|
||||
list_display = ('name', )
|
||||
|
||||
|
||||
admin.site.register(Setting, SettingAdmin)
|
||||
admin.site.register(File, FileAdmin)
|
||||
admin.site.site_header = settings.APP_NAME + ' administration'
|
||||
|
||||
@@ -4,6 +4,15 @@ from django.db import models
|
||||
|
||||
|
||||
class Setting(models.Model):
|
||||
def __str__(self):
|
||||
return 'Setting {0} - {1} ({2})'.format(self.name, self.language, self.id)
|
||||
name = models.CharField(max_length=100)
|
||||
language = models.CharField(max_length=5)
|
||||
string = models.TextField()
|
||||
|
||||
|
||||
class File(models.Model):
|
||||
def __str__(self):
|
||||
return 'File {0} ({1})'.format(self.name, self.id)
|
||||
name = models.CharField(max_length=100)
|
||||
file = models.FileField()
|
||||
|
||||
23
app/tests.py
23
app/tests.py
@@ -1,6 +1,7 @@
|
||||
from django.test import TestCase
|
||||
from app.models import Setting
|
||||
from app.models import Setting, File
|
||||
from django.conf import settings
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
|
||||
# Create your tests here.
|
||||
|
||||
@@ -23,8 +24,28 @@ class HomeTestCase(TestCase):
|
||||
response = self.client.get('/')
|
||||
self.assertContains(response, 'Строка русского текста', 1, 200)
|
||||
self.assertContains(response, '<html lang="ru">', 1, 200)
|
||||
self.assertContains(response, 'A<br>correct<br><h1>HTML<h1> string', 1, 200)
|
||||
|
||||
def test_ru_header_load(self):
|
||||
response = self.client.get('/', HTTP_ACCEPT_LANGUAGE='ru')
|
||||
self.assertContains(response, 'Строка русского текста', 1, 200)
|
||||
self.assertContains(response, '<html lang="ru">', 1, 200)
|
||||
self.assertContains(response, 'A<br>correct<br><h1>HTML<h1> string', 1, 200)
|
||||
|
||||
|
||||
class FileTestCase(TestCase):
|
||||
"""Uploaded files are correctly served"""
|
||||
def setUp(self):
|
||||
File.objects.create(name='sample_file', file=SimpleUploadedFile('sample.txt', b'This is sample file.'))
|
||||
|
||||
def doCleanups(self):
|
||||
clean = File.objects.get(name='sample_file')
|
||||
clean.file.delete(save=False)
|
||||
clean.delete()
|
||||
|
||||
def test_file_read(self):
|
||||
self.assertEqual(File.objects.get(name='sample_file').file.readline(), b'This is sample file.')
|
||||
|
||||
def test_file_get_url(self):
|
||||
response = self.client.get('/get_file', {'name': 'sample_file'})
|
||||
self.assertEqual(response.url, '/media/sample.txt')
|
||||
|
||||
13
app/views.py
13
app/views.py
@@ -1,8 +1,10 @@
|
||||
from django.shortcuts import render
|
||||
from django.shortcuts import render, get_object_or_404, redirect
|
||||
from django.utils.translation import gettext as _
|
||||
from django.conf import settings
|
||||
from app.settings_db import get_setting
|
||||
from django.utils import translation
|
||||
from app.models import File
|
||||
from django.http import HttpResponseBadRequest
|
||||
|
||||
|
||||
# Create your views here.
|
||||
@@ -15,3 +17,12 @@ def home(request):
|
||||
'app_home_head_html': get_setting('app_home_head_html', language=translation.get_language()),
|
||||
'app_home_body_html': get_setting('app_home_body_html', language=translation.get_language()),
|
||||
})
|
||||
|
||||
|
||||
def get_file(request):
|
||||
if not request.GET:
|
||||
return HttpResponseBadRequest()
|
||||
try:
|
||||
return redirect(get_object_or_404(File, name=request.GET['name']).file.url)
|
||||
except KeyError:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
@@ -126,8 +126,17 @@ STATICFILES_DIRS = [
|
||||
os.path.join(BASE_DIR, "static"),
|
||||
]
|
||||
|
||||
|
||||
# Media files dir (For testing purposes! This is not suitable for production)
|
||||
MEDIA_URL = '/media/'
|
||||
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
|
||||
|
||||
|
||||
# App name (Used in templates)
|
||||
APP_NAME = 'NoNiko'
|
||||
|
||||
|
||||
# Try to load local setting file
|
||||
try:
|
||||
from local_settings import *
|
||||
except ImportError:
|
||||
|
||||
@@ -16,8 +16,11 @@ Including another URLconf
|
||||
from django.contrib import admin
|
||||
from django.urls import path
|
||||
import app.views
|
||||
from django.conf import settings
|
||||
from django.conf.urls.static import static
|
||||
|
||||
urlpatterns = [
|
||||
path('', app.views.home, name='home'),
|
||||
path('get_file', app.views.get_file, name='get_file'),
|
||||
path('admin/', admin.site.urls),
|
||||
]
|
||||
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # Warning! This is not suitable for production.
|
||||
|
||||
Reference in New Issue
Block a user