Skip to content

Commit

Permalink
Add Account model.
Browse files Browse the repository at this point in the history
Fixes #28
  • Loading branch information
mblayman committed Jun 29, 2023
1 parent 07d8834 commit aae9f39
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
49 changes: 49 additions & 0 deletions journal/accounts/migrations/0002_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Generated by Django 4.2.1 on 2023-06-29 02:06

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


class Migration(migrations.Migration):
dependencies = [
("accounts", "0001_initial"),
]

operations = [
migrations.CreateModel(
name="Account",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"status",
models.IntegerField(
choices=[
(1, "Trialing"),
(2, "Active"),
(3, "Exempt"),
(4, "Canceled"),
(5, "Trial Expired"),
],
db_index=True,
default=1,
),
),
(
"user",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
31 changes: 31 additions & 0 deletions journal/accounts/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver


class Account(models.Model):
"""Account holds the user's state"""

class Status(models.IntegerChoices):
TRIALING = 1
ACTIVE = 2
EXEMPT = 3
CANCELED = 4
TRIAL_EXPIRED = 5

user = models.OneToOneField(
"accounts.User",
on_delete=models.CASCADE,
)
status = models.IntegerField(
choices=Status.choices,
default=Status.TRIALING,
db_index=True,
)


class User(AbstractUser):
pass


@receiver(post_save, sender=User)
def create_account(sender, instance, created, **kwargs):
"""A new user gets an associated account."""
if created:
Account.objects.create(user=instance)
9 changes: 9 additions & 0 deletions journal/accounts/tests/factories.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import factory
from django.db.models.signals import post_save


@factory.django.mute_signals(post_save)
class AccountFactory(factory.django.DjangoModelFactory):
class Meta:
model = "accounts.Account"

user = factory.SubFactory("journal.accounts.tests.factories.UserFactory")


class UserFactory(factory.django.DjangoModelFactory):
Expand Down
12 changes: 11 additions & 1 deletion journal/accounts/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
from journal.accounts.tests.factories import UserFactory
from journal.accounts.tests.factories import AccountFactory, UserFactory


class TestAccount:
def test_factory(self):
account = AccountFactory()

assert account is not None
assert account.user is not None
assert account.status == account.Status.TRIALING


class TestUser:
Expand All @@ -7,3 +16,4 @@ def test_factory(self):
user = UserFactory()

assert user is not None
assert user.account is not None

0 comments on commit aae9f39

Please sign in to comment.