├── admincharts ├── __init__.py ├── migrations │ └── __init__.py ├── models.py ├── static │ └── admincharts │ │ ├── admincharts.css │ │ ├── admincharts.js │ │ └── chart.min.js ├── apps.py ├── templates │ └── admin │ │ └── admincharts │ │ └── generic_change_list.html ├── utils.py └── admin.py ├── tests ├── accounts │ ├── __init__.py │ ├── migrations │ │ ├── __init__.py │ │ └── 0001_initial.py │ ├── apps.py │ ├── models.py │ └── admin.py ├── manage.py ├── tests.py └── settings.py ├── scripts ├── install ├── test ├── format └── pre-commit ├── .gitignore ├── package.json ├── yarn.lock ├── .github └── workflows │ ├── test.yml │ └── nextrelease.yml ├── LICENSE ├── pyproject.toml ├── README.md └── poetry.lock /admincharts/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/accounts/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /admincharts/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/accounts/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /scripts/install: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | POETRY_VIRTUALENVS_IN_PROJECT=true poetry install 3 | -------------------------------------------------------------------------------- /scripts/test: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | cd tests 3 | ../.venv/bin/python manage.py test "$@" 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | /dist 3 | /.venv 4 | *.pyc 5 | __pycache__ 6 | db.sqlite3 7 | -------------------------------------------------------------------------------- /admincharts/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | -------------------------------------------------------------------------------- /scripts/format: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | ./.venv/bin/black admincharts 3 | ./.venv/bin/black tests 4 | -------------------------------------------------------------------------------- /scripts/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | ./.venv/bin/black admincharts --check 3 | ./.venv/bin/black tests --check 4 | -------------------------------------------------------------------------------- /admincharts/static/admincharts/admincharts.css: -------------------------------------------------------------------------------- 1 | #admincharts { 2 | padding-top: 15px; 3 | padding-left: 15px; 4 | padding-right: 15px; 5 | padding-bottom: 30px; 6 | } 7 | -------------------------------------------------------------------------------- /admincharts/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class AdminchartsConfig(AppConfig): 5 | default_auto_field = "django.db.models.BigAutoField" 6 | name = "admincharts" 7 | -------------------------------------------------------------------------------- /tests/accounts/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class AccountsConfig(AppConfig): 5 | default_auto_field = "django.db.models.BigAutoField" 6 | name = "accounts" 7 | -------------------------------------------------------------------------------- /tests/accounts/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class Account(models.Model): 5 | ctime = models.DateTimeField(auto_now_add=True) 6 | name = models.CharField(max_length=100) 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "scripts": { 3 | "copy-static": "cp node_modules/chart.js/dist/chart.min.js admincharts/static/admincharts/chart.min.js" 4 | }, 5 | "dependencies": { 6 | "chart.js": "3.9.1" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /admincharts/templates/admin/admincharts/generic_change_list.html: -------------------------------------------------------------------------------- 1 | {% extends "admin/change_list.html" %} 2 | 3 | {% block extrahead %} 4 | {{ block.super }} 5 | {{ adminchart_chartjs_config|json_script:"adminchart-chartjs-config" }} 6 | {% endblock %} 7 | 8 | {% block pretitle %} 9 | {{ block.super }} 10 |
11 | {% endblock %} 12 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | chart.js@3.9.1: 6 | version "3.9.1" 7 | resolved "https://registry.yarnpkg.com/chart.js/-/chart.js-3.9.1.tgz#3abf2c775169c4c71217a107163ac708515924b8" 8 | integrity sha512-Ro2JbLmvg83gXF5F4sniaQ+lTbSv18E+TIf2cOeiH1Iqd2PGFOtem+DUufMZsCJwFE7ywPOpfXFBwRTGq7dh6w== 9 | -------------------------------------------------------------------------------- /admincharts/static/admincharts/admincharts.js: -------------------------------------------------------------------------------- 1 | window.addEventListener("DOMContentLoaded", function(event) { 2 | const configScript = document.getElementById("adminchart-chartjs-config"); 3 | if (!configScript) return; 4 | const chartConfig = JSON.parse(configScript.textContent); 5 | if (!chartConfig) return; 6 | var container = document.getElementById('admincharts') 7 | var canvas = document.createElement("canvas") 8 | container.appendChild(canvas) 9 | var ctx = canvas.getContext('2d'); 10 | var chart = new Chart(ctx, chartConfig); 11 | }); 12 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: [push] 4 | 5 | jobs: 6 | test: 7 | runs-on: ubuntu-latest 8 | strategy: 9 | matrix: 10 | python-version: ["3.8", "3.9", "3.10"] 11 | steps: 12 | - uses: actions/checkout@v2 13 | - uses: actions/setup-python@v2 14 | with: 15 | python-version: ${{ matrix.python-version }} 16 | - name: Install dependencies 17 | run: | 18 | pip install poetry 19 | ./scripts/install 20 | - name: Format check 21 | run: | 22 | ./scripts/pre-commit 23 | - name: Test 24 | run: | 25 | ./scripts/test 26 | -------------------------------------------------------------------------------- /tests/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == "__main__": 22 | main() 23 | -------------------------------------------------------------------------------- /.github/workflows/nextrelease.yml: -------------------------------------------------------------------------------- 1 | name: nextrelease 2 | on: 3 | push: 4 | branches: [master] 5 | pull_request: 6 | branches: [master] 7 | types: [labeled, unlabeled, edited, synchronize] 8 | 9 | jobs: 10 | sync: 11 | if: ${{ github.event_name == 'push' || github.event_name == 'pull_request' && github.head_ref == 'nextrelease' }} 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: dropseed/nextrelease@v2 15 | env: 16 | POETRY_PYPI_TOKEN_PYPI: ${{ secrets.DROPSEED_PYPI_TOKEN }} 17 | with: 18 | prepare_cmd: | 19 | sed -i -e "s/version = \"[^\"]*\"$/version = \"$VERSION\"/g" pyproject.toml 20 | publish_cmd: | 21 | pip3 install -U pip poetry && poetry publish --build --no-interaction 22 | github_token: ${{ secrets.GITHUB_TOKEN }} 23 | -------------------------------------------------------------------------------- /admincharts/utils.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | 3 | 4 | def months_between_dates(start_date, end_date): 5 | """ 6 | Given two instances of ``datetime.date``, generate a list of dates on 7 | the 1st of every month between the two dates (inclusive). 8 | 9 | e.g. "5 Jan 2020" to "17 May 2020" would generate: 10 | 11 | 1 Jan 2020, 1 Feb 2020, 1 Mar 2020, 1 Apr 2020, 1 May 2020 12 | 13 | """ 14 | if start_date > end_date: 15 | raise ValueError(f"Start date {start_date} is not before end date {end_date}") 16 | 17 | year = start_date.year 18 | month = start_date.month 19 | 20 | while (year, month) <= (end_date.year, end_date.month): 21 | yield datetime.date(year, month, 1) 22 | 23 | if month == 12: 24 | month = 1 25 | year += 1 26 | else: 27 | month += 1 28 | -------------------------------------------------------------------------------- /tests/accounts/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.2.5 on 2021-07-05 15:46 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [] 11 | 12 | operations = [ 13 | migrations.CreateModel( 14 | name="Account", 15 | fields=[ 16 | ( 17 | "id", 18 | models.BigAutoField( 19 | auto_created=True, 20 | primary_key=True, 21 | serialize=False, 22 | verbose_name="ID", 23 | ), 24 | ), 25 | ("ctime", models.DateTimeField(auto_now_add=True)), 26 | ("name", models.CharField(max_length=100)), 27 | ], 28 | ), 29 | ] 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Dropseed 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /tests/accounts/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from django.utils import timezone 3 | 4 | from admincharts.admin import AdminChartMixin 5 | from admincharts.utils import months_between_dates 6 | 7 | from .models import Account 8 | 9 | 10 | @admin.register(Account) 11 | class AccountAdmin(AdminChartMixin, admin.ModelAdmin): 12 | list_display = ("name", "ctime") 13 | ordering = ("-ctime",) 14 | 15 | def get_list_chart_data(self, queryset): 16 | if not queryset: 17 | return {} 18 | 19 | # Cannot reorder the queryset at this point 20 | earliest = min([x.ctime for x in queryset]) 21 | 22 | labels = [] 23 | totals = [] 24 | for b in months_between_dates(earliest, timezone.now()): 25 | labels.append(b.strftime("%b %Y")) 26 | totals.append( 27 | len( 28 | [ 29 | x 30 | for x in queryset 31 | if x.ctime.year == b.year and x.ctime.month == b.month 32 | ] 33 | ) 34 | ) 35 | 36 | return { 37 | "labels": labels, 38 | "datasets": [ 39 | {"label": "New accounts", "data": totals, "backgroundColor": "#79aec8"}, 40 | ], 41 | } 42 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.poetry] 2 | name = "django-admincharts" 3 | version = "0.4.1" 4 | description = "Chart.js integration for Django admin models" 5 | authors = ["Dave Gaeddert