1
0

Public release

This commit is contained in:
2021-07-30 21:29:14 +02:00
commit 8eddb50cf1
30 changed files with 1233 additions and 0 deletions

2
testing/__init__.py Normal file
View File

@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
default_app_config = 'testing.apps.TestingConfig'

88
testing/admin.py Normal file
View File

@@ -0,0 +1,88 @@
# -*- coding: utf-8 -*-
from urllib.parse import urlencode
from django.contrib import admin
from django.urls import reverse
from django.utils.safestring import mark_safe
from django_changelist_inline import (
ChangelistInline,
ChangelistInlineAdmin,
ChangelistInlineModelAdmin,
)
from testing.models import RelatedThing, Thing
@admin.register(RelatedThing)
class RelatedThingModelAdmin(admin.ModelAdmin):
list_display = ('pk', 'name')
class RelatedThingChangelistInline(ChangelistInline):
model = RelatedThing
class ChangelistModelAdmin(ChangelistInlineModelAdmin):
list_display = ('name', 'format_actions')
list_display_links = None
list_per_page = 5
def get_queryset(self, request):
return RelatedThing.objects.filter(thing=self.parent_instance)
@mark_safe
def format_actions(self, obj=None):
if obj is None:
return self.empty_value_display
change_link_url = reverse(
'admin:{app_label}_{model_name}_change'.format(
app_label=RelatedThing._meta.app_label,
model_name=RelatedThing._meta.model_name,
),
args=[obj.pk],
)
return (
f'<a class="button" href="{change_link_url}">'
'Edit' # noqa: E131
'</a>'
)
format_actions.short_description = 'Actions'
@property
def title(self):
return 'Linked Related Things'
@property
def no_results_message(self):
return 'No Related Things?'
def get_add_url(self, request):
result = super().get_add_url(request)
if result is not None:
return result + '?' + urlencode({
'thing': self.parent_instance.pk,
})
return result
def get_show_all_url(self, request):
result = super().get_show_all_url(request)
if result is not None:
return result + '?' + urlencode({
'thing': self.parent_instance.pk,
})
return result
def get_toolbar_links(self, request):
return (
'<a href="https://www.bthlabs.pl/">'
'BTHLabs' # noqa: E131
'</a>'
)
@admin.register(Thing)
class ThingModelAdmin(ChangelistInlineAdmin):
inlines = (RelatedThingChangelistInline,)

7
testing/apps.py Normal file
View File

@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
from django.apps import AppConfig
class TestingConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'testing'

31
testing/factories.py Normal file
View File

@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
from django.contrib.auth.models import User
import factory
from testing.models import RelatedThing, Thing
class UserFactory(factory.Factory):
class Meta:
model = User
email = factory.Faker('ascii_email')
username = factory.Faker('ascii_email')
password = factory.Faker('password')
is_staff = False
class ThingFactory(factory.Factory):
class Meta:
model = Thing
name = factory.Faker('words', nb=1)
data = factory.LazyAttribute(lambda thing: {'thing': True})
class RelatedThingFactory(factory.Factory):
class Meta:
model = RelatedThing
name = factory.Faker('words', nb=1)
data = factory.LazyAttribute(lambda thing: {'related_thing': True})

View File

@@ -0,0 +1,22 @@
# Generated by Django 3.2.5 on 2021-07-29 06:06
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Thing',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
('data', models.JSONField(blank=True, default=None, null=True)),
],
),
]

View File

@@ -0,0 +1,23 @@
# Generated by Django 3.2.5 on 2021-07-29 06:07
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('testing', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='RelatedThing',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
('data', models.JSONField(blank=True, default=None, null=True)),
('thing', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='testing.thing')),
],
),
]

View File

13
testing/models.py Normal file
View File

@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
from django.db import models
class Thing(models.Model):
name = models.CharField(max_length=255, null=False, blank=False)
data = models.JSONField(null=True, default=None, blank=True)
class RelatedThing(models.Model):
thing = models.ForeignKey(Thing, null=False, on_delete=models.CASCADE)
name = models.CharField(max_length=255, null=False, blank=False)
data = models.JSONField(null=True, default=None, blank=True)

7
testing/urls.py Normal file
View File

@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]