Skip to content

Commit cf2f6ad

Browse files
Merge pull request #117 from azavea/jw/refactor-role
Drop Role table
2 parents 5bc9aa4 + b62410e commit cf2f6ad

File tree

8 files changed

+40
-50
lines changed

8 files changed

+40
-50
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4747
- Update Edit Polygon border to be lighter on dark backgrounds [#58](https://github.com/azavea/iow-boundary-tool/pull/58)
4848
- Split DrawMap commponents into Layers and DrawTools [#57](https://github.com/azavea/iow-boundary-tool/pull/57)
4949
- Make `Roles` enum an `IntEnum` subclass [#74](https://github.com/azavea/iow-boundary-tool/pull/74)
50+
- Drop `Role` table and refactor as choice field on User [#117](https://github.com/azavea/iow-boundary-tool/pull/117)
5051

5152
### Fixed
5253

doc/arch/adr-001-data-models.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@ Below is the Entity Relationship Diagram illustrating the initial data model as
66

77
```mermaid
88
erDiagram
9-
Role {
10-
string description
11-
}
129
User }o--o{ Utility : "has"
1310
User }o--|{ State : "within"
14-
User }o--|| Role : is
1511
User {
16-
Role role FK
1712
email email
1813
boolean is_staff
1914
boolean is_active
15+
string role "max_length=1"
2016
date date_joined
2117
password password
2218
boolean is_locked_out

src/django/api/management/commands/resetdb.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from django.core.management import call_command
44
from django.core.management.base import BaseCommand
55

6-
from api.models import Role, Utility, User
6+
from api.models import Utility, User
77
from api.models.boundary import Boundary
8-
from api.models.role import Roles
8+
from api.models.user import Roles
99
from api.models.submission import Approval, Submission
1010

1111
from ..test_shapes import (
@@ -36,13 +36,13 @@ def handle(self, *args, **options):
3636
3737
password="password",
3838
has_admin_generated_password=False,
39-
role=Role.objects.get(pk=Roles.VALIDATOR),
39+
role=Roles.VALIDATOR,
4040
)
4141
contributor = User.objects.create_user(
4242
4343
password="password",
4444
has_admin_generated_password=False,
45-
role=Role.objects.get(pk=Roles.CONTRIBUTOR),
45+
role=Roles.CONTRIBUTOR,
4646
)
4747
contributor.utilities.add(test_utility)
4848

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Generated by Django 3.2.13 on 2022-10-10 12:40
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('api', '0013_user_has_admin_generated_password'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='user',
15+
name='role',
16+
field=models.CharField(choices=[('C', 'Contributor'), ('V', 'Validator'), ('A', 'Administrator')], default='C', max_length=1),
17+
),
18+
migrations.DeleteModel(
19+
name='Role',
20+
),
21+
]

src/django/api/models/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# flake8: noqa: F401
2-
from .role import Role
32
from .user import User, EmailAsUsernameUserManager
43
from .utility import Utility
54
from .state import State

src/django/api/models/role.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/django/api/models/submission.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
from django.contrib.gis.db import models as gis_models
44

55
from .boundary import Boundary
6-
from .role import Roles
7-
from .user import User
6+
from .user import User, Roles
87

98
__all__ = ["Submission", "Approval", "Review", "Annotation"]
109

1110

1211
def limit_by_validator_or_admin():
13-
return models.Q(role__pk__in=[Roles.VALIDATOR, Roles.ADMINISTRATOR])
12+
return models.Q(role__in=[Roles.VALIDATOR, Roles.ADMINISTRATOR])
1413

1514

1615
class Submission(models.Model):

src/django/api/models/user.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from django.core.validators import EmailValidator
99
from django.utils import timezone
1010

11-
from .role import Role, Roles
1211
from .utility import Utility
1312
from .state import State
1413

@@ -45,8 +44,13 @@ def create_superuser(self, email, password=None, **extra_fields):
4544
if extra_fields.get("is_superuser") is not True:
4645
raise ValueError("Superuser must have is_superuser=True.")
4746

48-
admin_role = Role.objects.get(pk=Roles.ADMINISTRATOR)
49-
return self._create_user(email, admin_role, password, **extra_fields)
47+
return self._create_user(email, Roles.ADMINISTRATOR, password, **extra_fields)
48+
49+
50+
class Roles(models.TextChoices):
51+
CONTRIBUTOR = "C"
52+
VALIDATOR = "V"
53+
ADMINISTRATOR = "A"
5054

5155

5256
class User(AbstractBaseUser, PermissionsMixin):
@@ -61,11 +65,10 @@ class User(AbstractBaseUser, PermissionsMixin):
6165
date_joined = models.DateTimeField(default=timezone.now)
6266
has_admin_generated_password = models.BooleanField(default=True)
6367

64-
role = models.ForeignKey(
65-
Role,
66-
related_name="actors",
67-
on_delete=models.PROTECT,
68+
role = models.CharField(
6869
default=Roles.CONTRIBUTOR,
70+
choices=Roles.choices,
71+
max_length=1,
6972
)
7073

7174
utilities = models.ManyToManyField(
@@ -81,11 +84,7 @@ class User(AbstractBaseUser, PermissionsMixin):
8184
)
8285

8386
def clean(self):
84-
if (
85-
self.id
86-
and self.role.pk == Roles.CONTRIBUTOR
87-
and not self.utilities.exists()
88-
):
87+
if self.id and self.role == Roles.CONTRIBUTOR and not self.utilities.exists():
8988
raise ValidationError("Contributors must be assigned a utility.")
9089

9190
super().clean()

0 commit comments

Comments
 (0)