Skip to content

Commit

Permalink
refactoring (#423)
Browse files Browse the repository at this point in the history
* refactoring
  • Loading branch information
jacobjove authored May 25, 2021
1 parent 16898ac commit ef11337
Show file tree
Hide file tree
Showing 79 changed files with 885 additions and 432 deletions.
5 changes: 2 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ backups/
.init/

# Testing / linting / coverage reports
.cache
.cache/
.coverage_html/
.hypothesis/
.mypy_cache/
.nox/
.pytest_cache/
.pyre/
.selenium/
.tox/
htmlcov/
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ runs:
shell: bash
# Install and configure Poetry.
- name: Install and configure Poetry
uses: snok/[email protected].4
uses: snok/[email protected].6
with:
virtualenvs-create: true
virtualenvs-in-project: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/delivery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ jobs:
shell: bash
# Install and configure Poetry.
- name: Install and configure Poetry
uses: snok/[email protected].4
uses: snok/[email protected].6
with:
virtualenvs-create: true
virtualenvs-in-project: true
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ jobs:
shell: bash
# Install and configure Poetry.
- name: Install and configure Poetry
uses: snok/[email protected].4
uses: snok/[email protected].6
with:
virtualenvs-create: true
virtualenvs-in-project: true
Expand Down Expand Up @@ -202,7 +202,7 @@ jobs:
shell: bash
# Install and configure Poetry.
- name: Install and configure Poetry
uses: snok/[email protected].4
uses: snok/[email protected].6
with:
virtualenvs-create: true
virtualenvs-in-project: true
Expand Down Expand Up @@ -282,7 +282,7 @@ jobs:
shell: bash
# Install and configure Poetry.
- name: Install and configure Poetry
uses: snok/[email protected].4
uses: snok/[email protected].6
with:
virtualenvs-create: true
virtualenvs-in-project: true
Expand Down Expand Up @@ -357,7 +357,7 @@ jobs:
shell: bash
# Install and configure Poetry.
- name: Install and configure Poetry
uses: snok/[email protected].4
uses: snok/[email protected].6
with:
virtualenvs-create: true
virtualenvs-in-project: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/seed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
shell: bash
# Install and configure Poetry.
- name: Install and configure Poetry
uses: snok/[email protected].4
uses: snok/[email protected].6
with:
virtualenvs-create: true
virtualenvs-in-project: true
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ backups/
.cache
.coverage_html/
.hypothesis/
.mypy_cache/
.nox/
.pytest_cache/
.pytype/
.pyre/
.selenium/
.tox/
htmlcov/
Expand Down
11 changes: 11 additions & 0 deletions .pyre_configuration
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"source_directories": [
"."
],
"exclude": [
".*\/node_modules\/.*",
".*\/.venv\/.*",
".*\/.cache\/.*"
],
"taint_models_path": ".venv/lib"
}
3 changes: 3 additions & 0 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"Bennion",
"braintree",
"buildx",
"crontabschedule",
"CSRF",
"ctype",
"dbbackup",
Expand All @@ -26,6 +27,7 @@
"ghcr",
"graphiql",
"graphviz",
"grappelli",
"iframe",
"iglob",
"Imgur",
Expand Down Expand Up @@ -68,6 +70,7 @@
"stylelint",
"testcafe",
"tinymce",
"uncachable",
"unstaged",
"venv",
"virtualenvs",
Expand Down
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"python.linting.banditEnabled": false,
"python.linting.banditPath": "${workspaceFolder}/.venv/bin/bandit",
"python.linting.lintOnSave": false,
"python.linting.mypyEnabled": true,
"python.linting.mypyPath": "${workspaceFolder}/.venv/bin/mypy",
"python.linting.pycodestylePath": "${workspaceFolder}/.venv/bin/pycodestyle",
"python.linting.pydocstylePath": "${workspaceFolder}/.venv/bin/pydocstyle",
Expand Down
1 change: 1 addition & 0 deletions .watchmanconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
7 changes: 1 addition & 6 deletions apps/admin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
"""Admin app for ModularHistory."""

from .inlines import (
GenericStackedInline,
GenericTabularInline,
StackedInline,
TabularInline,
)
from .inlines import StackedInline, TabularInline
from .model_admin import ModelAdmin, admin_site
7 changes: 6 additions & 1 deletion apps/admin/admin_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from admin_tools.menu import Menu, items
from django.apps import apps
from django.conf import settings
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _

Expand Down Expand Up @@ -56,6 +57,7 @@ def __init__(self, **kwargs):
title='Applications',
exclude=[
'allauth.*',
'admin_honeypot.*',
'rest_framework.*',
'defender.*',
'django_celery_*',
Expand All @@ -73,7 +75,10 @@ def _menu_items(self):
for model_cls in models:
model_name = model_cls.__name__
children.append(
items.MenuItem(model_name, f'/admin/{app}/{model_name.lower()}/')
items.MenuItem(
model_name,
f'/{settings.ADMIN_URL_PREFIX}/{app}/{model_name.lower()}/',
)
)
menu_items.append(items.MenuItem(app, children=children))
return menu_items
45 changes: 5 additions & 40 deletions apps/admin/inlines.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,28 @@
from typing import TYPE_CHECKING, List, Optional
from typing import TYPE_CHECKING

from nested_admin.nested import (
NestedGenericStackedInline,
NestedGenericTabularInline,
NestedStackedInline,
NestedTabularInline,
)
# from django.contrib.admin import StackedInline as BaseStackedInline
# from django.contrib.admin import TabularInline as BaseTabularInline
from nested_admin.nested import NestedStackedInline, NestedTabularInline

from apps.admin.model_admin import FORM_FIELD_OVERRIDES

if TYPE_CHECKING:
from core.models.model import Model


class GenericTabularInline(NestedGenericTabularInline):
"""Tabular inline admin for generically related objects."""

formfield_overrides = FORM_FIELD_OVERRIDES


class GenericStackedInline(NestedGenericStackedInline):
"""Stacked inline admin for generically related objects."""

formfield_overrides = FORM_FIELD_OVERRIDES


class StackedInline(NestedStackedInline):
"""Inline admin with fields stacked vertically."""

formfield_overrides = FORM_FIELD_OVERRIDES

def get_fields(self, request, model_instance=None) -> List[str]:
"""Return reordered fields to be displayed in the admin."""
fields = super().get_fields(request, model_instance)
return reorder_fields(fields)


class TabularInline(NestedTabularInline):
"""Inline admin with fields laid out horizontally."""

formfield_overrides = FORM_FIELD_OVERRIDES

def get_extra(self, request, model_instance: Optional['Model'] = None, **kwargs):
def get_extra(self, request, *args, **kwargs):
"""Return the number of extra/blank rows to display."""
if len(self.get_queryset(request)):
return 0
return 1

def get_fields(self, request, model_instance=None) -> List[str]:
"""Return reordered fields to be displayed in the admin."""
fields = super().get_fields(request, model_instance)
return reorder_fields(fields)


def reorder_fields(fields) -> List[str]:
"""Return a reordered list of fields to display in the admin."""
ordered_fields = ('page_number', 'end_page_number', 'notes', 'position')
for field_name in ordered_fields:
if field_name in fields:
fields.remove(field_name)
fields.append(field_name)
return fields
38 changes: 29 additions & 9 deletions apps/admin/model_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from aenum import Constant
from django.conf import settings
from django.contrib.admin import ListFilter
from django.contrib.admin import ModelAdmin as BaseModelAdmin
from django.contrib.contenttypes.models import ContentType
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
from django.contrib.sites.models import Site
Expand All @@ -16,8 +17,11 @@
PeriodicTask,
SolarSchedule,
)
from django_celery_results.admin import TaskResult, TaskResultAdmin
from nested_admin import NestedModelAdmin, NestedPolymorphicInlineSupportMixin
from django_celery_results.admin import TaskResultAdmin
from django_celery_results.models import TaskResult
from nested_admin.nested import NestedModelAdmin
from nested_admin.polymorphic import NestedPolymorphicInlineSupportMixin
from polymorphic.admin import PolymorphicInlineSupportMixin
from sass_processor.processor import sass_processor

from apps.admin.admin_site import admin_site
Expand Down Expand Up @@ -46,13 +50,8 @@
ADMIN_CSS = 'styles/admin.css'


class ModelAdmin(NestedPolymorphicInlineSupportMixin, NestedModelAdmin):
"""
Base admin class for ModularHistory's models.
Uses the NestedPolymorphicInlineSupportMixin as instructed in
https://django-nested-admin.readthedocs.io/en/latest/integrations.html.
"""
class ModelAdmin(PolymorphicInlineSupportMixin, BaseModelAdmin):
"""Base admin class for ModularHistory's models."""

model: Type[Model]

Expand Down Expand Up @@ -138,6 +137,27 @@ def get_search_results(
queryset = queryset.exclude(pk=pk)
return queryset, use_distinct

def save_form(self, request, form, change):
"""
Given a ModelForm return an unsaved instance. ``change`` is True if
the object is being changed, and False if it's being added.
"""
print(f'>>>>>>save_form>>>>>')
return super().save_form(request, form, change)

def save_model(self, request, obj, form, change):
"""
Given a model instance save it to the database.
"""
print(f'>>>>>>after save_model>>>>> {getattr(obj, "summary", None)}')
obj.save()
print(f'>>>>>>after save_model>>>>> {getattr(obj, "summary", None)}')
obj.refresh_from_db()
print(f'>>>>>>after refresh_from_db>>>>> {getattr(obj, "summary", None)}')
from time import sleep

sleep(5)


class ContentTypeFields(Constant):
"""Field names of the ContentType model."""
Expand Down
2 changes: 1 addition & 1 deletion apps/entities/admin/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class EntityAdmin(SearchableModelAdmin):
EntityTypeFilter,
]
ordering = ['name', 'birth_date']
readonly_fields = ['pretty_cache']
readonly_fields = SearchableModelAdmin.readonly_fields + ['title', 'slug']
search_fields = ['name', 'aliases']


Expand Down
29 changes: 29 additions & 0 deletions apps/entities/migrations/0004_auto_20210524_0113.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.1.11 on 2021-05-24 01:13

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('entities', '0003_auto_20210521_1334'),
]

operations = [
migrations.AddField(
model_name='entityimage',
name='position',
field=models.PositiveSmallIntegerField(blank=True, default=0, null=True),
),
migrations.AlterField(
model_name='entity',
name='title',
field=models.CharField(
blank=True,
help_text='The title can be used for the detail page header and title tag, SERP result card header, etc. It should be a noun phrase!',
max_length=120,
null=True,
verbose_name='title',
),
),
]
4 changes: 2 additions & 2 deletions apps/entities/models/entity_image.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from django.db import models

from core.models.model import Model
from core.models.positioned_relation import PositionedRelation

NAME_MAX_LENGTH: int = 100

TRUNCATED_DESCRIPTION_LENGTH: int = 1200


class EntityImage(Model):
class EntityImage(PositionedRelation):
"""An association of an image with an entity."""

entity = models.ForeignKey(
Expand Down
24 changes: 24 additions & 0 deletions apps/images/migrations/0003_auto_20210524_0113.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 3.1.11 on 2021-05-24 01:13

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('images', '0002_auto_20210522_1558'),
]

operations = [
migrations.AlterField(
model_name='image',
name='title',
field=models.CharField(
blank=True,
help_text='The title can be used for the detail page header and title tag, SERP result card header, etc. It should be a noun phrase!',
max_length=120,
null=True,
verbose_name='title',
),
),
]
1 change: 0 additions & 1 deletion apps/occurrences/admin/__init__.py

This file was deleted.

Loading

0 comments on commit ef11337

Please sign in to comment.