Skip to content

Commit

Permalink
Add history to Account model.
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
mblayman committed Aug 3, 2023
1 parent e25a05b commit 5e3f4c5
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
8 changes: 7 additions & 1 deletion journal/accounts/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from simple_history.admin import SimpleHistoryAdmin

from .models import User
from .models import Account, User

admin.site.register(User, UserAdmin)


@admin.register(Account)
class AccountAdmin(SimpleHistoryAdmin):
pass
77 changes: 77 additions & 0 deletions journal/accounts/migrations/0003_historicalaccount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Generated by Django 4.2.1 on 2023-08-03 01:36

import django.db.models.deletion
import simple_history.models
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("accounts", "0002_account"),
]

operations = [
migrations.CreateModel(
name="HistoricalAccount",
fields=[
(
"id",
models.BigIntegerField(
auto_created=True, blank=True, db_index=True, verbose_name="ID"
),
),
(
"status",
models.IntegerField(
choices=[
(1, "Trialing"),
(2, "Active"),
(3, "Exempt"),
(4, "Canceled"),
(5, "Trial Expired"),
],
db_index=True,
default=1,
),
),
("history_id", models.AutoField(primary_key=True, serialize=False)),
("history_date", models.DateTimeField(db_index=True)),
("history_change_reason", models.CharField(max_length=100, null=True)),
(
"history_type",
models.CharField(
choices=[("+", "Created"), ("~", "Changed"), ("-", "Deleted")],
max_length=1,
),
),
(
"history_user",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to=settings.AUTH_USER_MODEL,
),
),
(
"user",
models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"verbose_name": "historical account",
"verbose_name_plural": "historical accounts",
"ordering": ("-history_date", "-history_id"),
"get_latest_by": ("history_date", "history_id"),
},
bases=(simple_history.models.HistoricalChanges, models.Model),
),
]
2 changes: 2 additions & 0 deletions journal/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from simple_history.models import HistoricalRecords


class AccountManager(models.Manager):
Expand Down Expand Up @@ -34,6 +35,7 @@ class Status(models.IntegerChoices):
)

objects = AccountManager()
history = HistoricalRecords()


class User(AbstractUser):
Expand Down
1 change: 1 addition & 0 deletions project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
# Needed by default templates even though we're not using a social provider.
"allauth.socialaccount",
"django_extensions",
"simple_history",
"journal.accounts",
"journal.core",
"journal.entries",
Expand Down
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ Django==4.2.1
django-allauth==0.54.0
django-environ==0.10.0
django-extensions==3.2.3
django-simple-history==3.3.0
gunicorn==20.1.0
sentry-sdk==1.28.1
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ django-environ==0.10.0
# via -r requirements.in
django-extensions==3.2.3
# via -r requirements.in
django-simple-history==3.3.0
# via -r requirements.in
gunicorn==20.1.0
# via -r requirements.in
idna==3.4
Expand Down

0 comments on commit 5e3f4c5

Please sign in to comment.