setup project

This commit is contained in:
Thomas Leichtfuß
2020-09-05 00:40:58 +02:00
parent 94e57d4e8c
commit ae75692609
21 changed files with 549 additions and 64 deletions
+21
View File
@@ -0,0 +1,21 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'testapp.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
View File
+37
View File
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
from django.contrib import admin
from more_filters import (
MultiSelectFilter, MultiSelectRelatedFilter, MultiSelectDropdownFilter,
MultiSelectRelatedDropdownFilter, DropdownFilter, ChoicesDropdownFilter,
RelatedDropdownFilter, PlusMinusFilter, AnnotationListFilter,
BooleanAnnotationListFilter
)
from .models import ModelA
from .models import ModelB
@admin.register(ModelA)
class ModelAAdmin(admin.ModelAdmin):
search_fields = ('dropdown_less_than_four',)
list_display = (
'dropdown_less_than_four',
'dropdown_more_than_three',
'multiselect',
'multiselect_dropdown',
'choices_dropdown',
'related_dropdown',
'multiselect_related',
'multiselect_related_dropdown',
)
list_filter = (
('dropdown_less_than_four', DropdownFilter),
('dropdown_more_than_three', DropdownFilter),
('multiselect', MultiSelectFilter),
('multiselect_dropdown', MultiSelectDropdownFilter),
('choices_dropdown', ChoicesDropdownFilter),
('related_dropdown', RelatedDropdownFilter),
('multiselect_related', MultiSelectRelatedFilter),
('multiselect_related_dropdown', MultiSelectRelatedDropdownFilter),
)
+7
View File
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
from django.apps import AppConfig
class TestappConfig(AppConfig):
name = 'testapp'
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
from datetime import timedelta
from django.core.management.base import BaseCommand
from django.core.management.base import CommandError
from django.contrib.auth.models import User
from django.db.utils import IntegrityError
from ...models import ModelA, ModelB
def create_test_data():
try:
User.objects.create_superuser(
'admin',
'admin@testapp.org',
'adminpassword')
except IntegrityError:
pass
# clear existing data
ModelA.objects.all().delete()
ModelB.objects.all().delete()
for i in range(1, 30):
model_a = ModelA()
model_b = ModelB()
model_b.id = i
model_b.save()
model_a.dropdown_less_than_four = i % 3
model_a.dropdown_more_than_three = i % 4
model_a.choices_dropdown = i % 9 +1
model_a.multiselect = i % 4
model_a.multiselect_dropdown = i % 4
model_a.related_dropdown = model_b
model_a.multiselect_related = model_b
model_a.multiselect_related_dropdown = model_b
model_a.save()
class Command(BaseCommand):
help = 'Create test data.'
def handle(self, *args, **options):
create_test_data()
# if options['create_test_data']:
+35
View File
@@ -0,0 +1,35 @@
# Generated by Django 3.0.10 on 2020-09-04 22:23
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='ModelB',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
],
),
migrations.CreateModel(
name='ModelA',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('dropdown_less_than_four', models.IntegerField()),
('dropdown_more_than_three', models.IntegerField()),
('multiselect', models.IntegerField()),
('multiselect_dropdown', models.IntegerField()),
('choices_dropdown', models.CharField(blank=True, choices=[('1', 'one'), ('2', 'two'), ('3', 'three'), ('4', 'four'), ('5', 'five'), ('6', 'six'), ('7', 'seven'), ('8', 'eight'), ('9', 'nine')], max_length=255)),
('multiselect_related', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='multiselect_related_reverse', to='testapp.ModelB')),
('multiselect_related_dropdown', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='multiselect_related_dropdown_reverse', to='testapp.ModelB')),
('related_dropdown', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='related_dropdown_reverse', to='testapp.ModelB')),
],
),
]
+28
View File
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
from django.db import models
class ModelA(models.Model):
CHOICES = (
('1', 'one'),
('2', 'two'),
('3', 'three'),
('4', 'four'),
('5', 'five'),
('6', 'six'),
('7', 'seven'),
('8', 'eight'),
('9', 'nine'),
)
dropdown_less_than_four = models.IntegerField()
dropdown_more_than_three = models.IntegerField()
multiselect = models.IntegerField()
multiselect_dropdown = models.IntegerField()
choices_dropdown = models.CharField(max_length=255, blank=True, choices=CHOICES)
related_dropdown = models.ForeignKey('ModelB', on_delete=models.CASCADE, related_name='related_dropdown_reverse')
multiselect_related = models.ForeignKey('ModelB', on_delete=models.CASCADE, related_name='multiselect_related_reverse')
multiselect_related_dropdown = models.ForeignKey('ModelB', on_delete=models.CASCADE, related_name='multiselect_related_dropdown_reverse')
class ModelB(models.Model):
id = models.AutoField(primary_key=True)
+124
View File
@@ -0,0 +1,124 @@
"""
Django settings for testapp project.
Generated by 'django-admin startproject' using Django 2.2.10.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import os
import sys
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*xje--vy(__r5_*7t&z^im09#v4#auk*1!t@7!^duf=e$vuzy4'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'testapp',
'more_filters',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'testapp.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'testapp.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': ':memory:',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
View File
+22
View File
@@ -0,0 +1,22 @@
from django.test import TestCase
from django.contrib.auth.models import User
from django.urls import reverse
from ..management.commands.createtestdata import create_test_data
class ExportTest(TestCase):
@classmethod
def setUpTestData(cls):
create_test_data()
def setUp(self):
self.admin = User.objects.get(username='admin')
self.client.force_login(self.admin)
self.url = reverse('admin:testapp_modela_changelist')
def test_01_load_changelist(self):
resp = self.client.get(self.url)
self.assertEqual(resp.status_code, 200)
+21
View File
@@ -0,0 +1,21 @@
"""testapp URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.conf.urls import url
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
+16
View File
@@ -0,0 +1,16 @@
"""
WSGI config for testapp project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'testapp.settings')
application = get_wsgi_application()