Giter Site home page Giter Site logo

westerveltco / django-twc-toolbox Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 169 KB

Various tools for Django projects at The Westervelt Company

Home Page: https://django-twc-toolbox.westervelt.dev

License: MIT License

Just 2.45% Python 93.90% HTML 3.65%

django-twc-toolbox's Issues

Bump `django-twc-package` to v2024.21

v2024.21

What's Changed

Full Changelog: westerveltco/django-twc-package@v2024.20...v2024.21

v2024.20

What's Changed

Full Changelog: westerveltco/django-twc-package@v2024.19...v2024.20

django-twc-package v2024.19

What's Changed

Full Changelog: westerveltco/django-twc-package@v2024.18...v2024.19

Add `messages` app

In one of our private projects, we have a messages app that handles common messages that need to be sent externally, e.g. email, webhooks, or Teams. That should be ported over to this public package so that our other projects can use it.

Use `westerveltco/setup-ci-action`

coverage
Node.js 16 actions are deprecated. Please update the following actions to use Node.js 20: actions/setup-python@v4. For more information see: https://github.blog/changelog/2023-09-22-github-actions-transitioning-from-node-16-to-node-20/.

types
Node.js 16 actions are deprecated. Please update the following actions to use Node.js 20: actions/setup-python@v4. For more information see: https://github.blog/changelog/2023-09-22-github-actions-transitioning-from-node-16-to-node-20/.

Fallback to form class fields within `CRUDView.get_fields`

If I go to the trouble of defining a custom Form for a CRUDView, shouldn't the view just use the fields from that form? As it stands right now, you have to duplicate the fields across the form and view, as CRUDView.get_fields expects either the fields attribute to be defined or for get_fields to be overridden.

Example

Let's say I have a model with a status field that I want rendered as radio buttons in the form.

# models.py
from django.db import models


class Book(models.Model):
    author = models.CharField(max_length=255)

    class Status(models.TextChoices):
        DRAFT = "D", "Draft"
        PUBLISHED = "P", "Published"

    status = models.CharField(max_length=1, choices=Status.choices, default=Status.DRAFT)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
# forms.py
from django import forms

from .models import Book


class BookModelForm(forms.ModelForm):
    class Meta:
        model = Book
        fields = ["author", "status"]
        widgets = {
            "status": forms.RadioSelect(),
        }

This is what neapolitan expects at the moment:

# views.py
from neapolitan.views import CRUDView

from .forms import BookModelForm
from .models import Book


class BookCRUDView(CRUDView):
    model = Book
    form_class = BookModelForm
    fields = ["author", "status"]

To me, if I've gone to the trouble to define a custom form_class on the view but no fields, it should fallback to the form class.

Add documentation

  • Create initial scaffolding
  • Create docs site on Read the Docs
  • Document TimeStamped
  • Document DatePaginator and DatePage

Adjust `DatePage.min_date` and `DatePage.max_date`

This snuck in with all the furious coding to get it shipped. In addition to the start_date and end_date of the DatePage, which correspond to the first and last object's date, respectively, I wanted to also include the oldest and newest date regardless of the sorting.

Add `min_date` and `max_date` to `DatePaginator`

Similar to #7, it'd be nice if regardless of the order of the object_list, that there was two properties for the min and max date of a paginator.

There's already a bunch of duplicated logic in the paginator methods dealing with this anyway, should be consolidated.

if isinstance(self.object_list, QuerySet): # type: ignore[misc]
first_obj = self.object_list.first()
last_obj = self.object_list.last()
else:
first_obj = self.object_list[0]
last_obj = self.object_list[-1]
first_date = getattr(first_obj, self.date_field)
last_date = getattr(last_obj, self.date_field)

if isinstance(self.object_list, QuerySet): # type: ignore[misc]
first_obj = self.object_list.first()
last_obj = self.object_list.last()
else:
first_obj = self.object_list[0]
last_obj = self.object_list[-1]
first_date = getattr(first_obj, self.date_field)
last_date = getattr(last_obj, self.date_field)

"Cannot reorder a query once a slice has been taken."

Trying to use CRUDView with a tables.Table -- setting an order_by in the table's Meta causes a TypeError exception to be raised.

class ClubTable(tables.Table):
    class Meta:
        fields = [
            "name",
            "primary_tract_number",
            "lease_manager_name",
            "officer",
        ]
        model = Club
        order_by = ("primary_tract_number", "name")
        template_name = "twc_tables/table.html"

class ClubCRUDView(LoginRequiredMixin, CRUDView):
    model = Club
    queryset = Club.objects.with_primary_tract_info()
    table_class = ClubTable

Traceback:

Environment:


Request Method: GET
Request URL: http://localhost:8000/club/

Django Version: 5.0.7
Python Version: 3.12.4
Installed Applications:
['debug_toolbar',
 'whitenoise.runserver_nostatic',
 'lease.adjustments',
 'lease.charges',
 'lease.clubs',
 'lease.clubtracts',
 'lease.contrib.ordered_model',
 'lease.core',
 'lease.customers',
 'lease.finance',
 'lease.invoices',
 'lease.leases',
 'lease.managers',
 'lease.members',
 'lease.mgmtunits',
 'lease.payments',
 'lease.search',
 'lease.surveys',
 'lease.system',
 'lease.tracts',
 'lease.users',
 'django_twc_ui',
 'django_twc_ui.favicons',
 'django_twc_ui.forms',
 'django_twc_ui.layouts',
 'django_twc_ui.tables',
 'django_q_registry',
 'django_simple_nav',
 'django_twc_toolbox',
 'django_twc_toolbox.crud',
 'email_relay',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'allauth.socialaccount.providers.okta',
 'corsheaders',
 'django_browser_reload',
 'django_extensions',
 'django_filters',
 'django_htmx',
 'django_q',
 'django_tables2',
 'django_tailwind_cli',
 'django_vite',
 'health_check',
 'health_check.cache',
 'health_check.contrib.migrations',
 'health_check.db',
 'health_check.storage',
 'heroicons',
 'import_export',
 'ordered_model',
 'rangefilter',
 'rest_framework',
 'rest_framework.authtoken',
 'sequences',
 'simple_history',
 'template_partials',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.gis',
 'django.contrib.humanize',
 'django.contrib.messages',
 'django.contrib.postgres',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.staticfiles',
 'django.forms']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'allauth.account.middleware.AccountMiddleware',
 'simple_history.middleware.HistoryRequestMiddleware',
 'django_htmx.middleware.HtmxMiddleware',
 'django_flyio.middleware.FlyResponseMiddleware',
 'django_browser_reload.middleware.BrowserReloadMiddleware']



Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/neapolitan/views.py", line 472, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/contrib/auth/mixins.py", line 73, in dispatch
    return super().dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/views/generic/base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_twc_toolbox/crud/views.py", line 135, in list
    context = self.get_context_data(
              
  File "/usr/local/lib/python3.12/site-packages/django_tables2/views.py", line 161, in get_context_data
    table = self.get_table(**self.get_table_kwargs())
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_tables2/views.py", line 122, in get_table
    table = table_class(data=self.get_table_data(), **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_tables2/tables.py", line 364, in __init__
    self.order_by = order_by
    ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_tables2/tables.py", line 542, in order_by
    self.data.order_by(self._order_by)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django_tables2/data.py", line 224, in order_by
    self.data = self.data.order_by(*order_by_accessors)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/models/query.py", line 1698, in order_by
    raise TypeError("Cannot reorder a query once a slice has been taken.")
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Exception Type: TypeError at /club/
Exception Value: Cannot reorder a query once a slice has been taken.

Looks like it's line 135 in django_twc_toolbox.crud.views:

                paginator=None,
                filterset=filterset,
            )
        else:
            # Paginated response
            page = self.paginate_queryset(queryset, paginate_by)
            self.object_list = page.object_list
            context = self.get_context_data(
                            โ€ฆ
                page_obj=page,
                is_paginated=page.has_other_pages(),
                paginator=page.paginator,
                filterset=filterset,
            )

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.