Skip to content

Commit

Permalink
Add Account model.
Browse files Browse the repository at this point in the history
Fixes #187
  • Loading branch information
mblayman committed Nov 3, 2020
1 parent 67a87db commit 1137b26
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ deploy:

graph:
./manage.py graph_models \
accounts \
core \
courses \
schools \
Expand Down
Empty file added homeschool/accounts/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions homeschool/accounts/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.contrib import admin

from .models import Account


@admin.register(Account)
class AccountAdmin(admin.ModelAdmin):
list_display = ("id", "user", "status")
list_filter = ("status",)
raw_id_fields = ("user",)
5 changes: 5 additions & 0 deletions homeschool/accounts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class AccountsConfig(AppConfig):
name = "accounts"
52 changes: 52 additions & 0 deletions homeschool/accounts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Generated by Django 3.1.2 on 2020-11-03 04:46

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


class Migration(migrations.Migration):

initial = True

dependencies = [migrations.swappable_dependency(settings.AUTH_USER_MODEL)]

operations = [
migrations.CreateModel(
name="Account",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"status",
models.IntegerField(
choices=[
(1, "Exempt"),
(2, "Beta"),
(3, "Trialing"),
(4, "Active"),
(5, "Past Due"),
(6, "Canceled"),
],
db_index=True,
default=2,
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="+",
to=settings.AUTH_USER_MODEL,
),
),
],
)
]
Empty file.
32 changes: 32 additions & 0 deletions homeschool/accounts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from django.conf import settings
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver

from homeschool.users.models import User


class Account(models.Model):
"""A record to track the status of the account."""

class AccountStatus(models.IntegerChoices):
EXEMPT = 1 # For special accounts that require no subscription
BETA = 2 # For beta users
TRIALING = 3
ACTIVE = 4
PAST_DUE = 5
CANCELED = 6

user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="+"
)
status = models.IntegerField(
choices=AccountStatus.choices, default=AccountStatus.BETA, db_index=True
)


@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)
Empty file.
8 changes: 8 additions & 0 deletions homeschool/accounts/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import factory


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

user = factory.SubFactory("homeschool.users.tests.factories.UserFactory")
11 changes: 11 additions & 0 deletions homeschool/accounts/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from homeschool.accounts.models import Account
from homeschool.accounts.tests.factories import AccountFactory
from homeschool.test import TestCase


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

assert account.user is not None
assert account.status == Account.AccountStatus.BETA
Empty file added homeschool/accounts/views.py
Empty file.
24 changes: 24 additions & 0 deletions homeschool/core/migrations/0002_auto_20201103_0418.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 3.1.2 on 2020-11-03 04:18

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [("core", "0001_initial")]

operations = [
migrations.AlterField(
model_name="flag",
name="everyone",
field=models.BooleanField(
blank=True,
help_text=(
"Flip this flag on (Yes) or off (No) for everyone, overriding all"
" other settings. Leave as Unknown to use normally."
),
null=True,
verbose_name="Everyone",
),
)
]
7 changes: 7 additions & 0 deletions homeschool/users/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from homeschool.accounts.models import Account
from homeschool.schools.models import School
from homeschool.schools.tests.factories import SchoolFactory
from homeschool.test import TestCase
Expand All @@ -16,3 +17,9 @@ def test_create_school(self):
user = self.make_user()

assert user.school == School.objects.get(admin=user)

def test_create_account(self):
"""A new user automatically has an account created."""
user = self.make_user()

assert Account.objects.filter(user=user).exists()
1 change: 1 addition & 0 deletions project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"ordered_model",
"tz_detect",
"waffle",
"homeschool.accounts",
"homeschool.core",
"homeschool.courses",
"homeschool.schools",
Expand Down

0 comments on commit 1137b26

Please sign in to comment.