Skip to content

Commit bbfc955

Browse files
committed
feat(django): add label idp_user to the django app
1 parent e969784 commit bbfc955

15 files changed

+374
-140
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "cardo-python-utils"
7-
version = "0.5.dev45"
7+
version = "0.5.dev46"
88
description = "Python library enhanced with a wide range of functions for different scenarios."
99
readme = "README.rst"
1010
requires-python = ">=3.8"

python_utils/django/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ MIDDLEWARE = [
3333
...
3434
]
3535

36+
AUTH_USER_MODEL = "idp_user.User"
37+
3638
# Include the database configuration for each tenant in the DATABASES setting.
3739
# You can use the get_database_configs() function from python_utils.django.db.utils as a helper.
3840
from python_utils.django.db.utils import get_database_configs

python_utils/django/apps.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33

44
class DjangoAppConfig(AppConfig):
55
name = "python_utils.django"
6+
label = "idp_user"

python_utils/django/db/alias.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from ..tenant_context import TenantContext
2+
3+
4+
class DynamicDatabaseAlias(str):
5+
def __new__(cls):
6+
# We initialize with a dummy value; the logic happens in the methods below
7+
return super().__new__(cls, "default")
8+
9+
def __getattribute__(self, name):
10+
return getattr(TenantContext.get(), name)
11+
12+
def __str__(self):
13+
return TenantContext.get()
14+
15+
def __repr__(self):
16+
return repr(self.__str__())
17+
18+
def __hash__(self):
19+
return hash(self.__str__())
20+
21+
def __eq__(self, other):
22+
return self.__str__() == str(other)
Lines changed: 55 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,73 @@
1+
# Generated by Django 3.1.13 on 2021-10-26 09:12
2+
13
import django.contrib.auth.models
24
import django.contrib.auth.validators
35
import django.utils.timezone
6+
from django.conf import settings
47
from django.db import migrations, models
58

69

710
class Migration(migrations.Migration):
811
initial = True
912

1013
dependencies = [
11-
("auth", "0012_alter_user_first_name_max_length"),
14+
('auth', '0012_alter_user_first_name_max_length'),
1215
]
1316

1417
operations = [
15-
migrations.SeparateDatabaseAndState(
16-
state_operations=[
17-
migrations.CreateModel(
18-
name="User",
19-
fields=[
20-
(
21-
"id",
22-
models.AutoField(
23-
auto_created=True,
24-
primary_key=True,
25-
serialize=False,
26-
verbose_name="ID",
27-
),
28-
),
29-
(
30-
"password",
31-
models.CharField(max_length=128, verbose_name="password"),
32-
),
33-
(
34-
"last_login",
35-
models.DateTimeField(blank=True, null=True, verbose_name="last login"),
36-
),
37-
(
38-
"is_superuser",
39-
models.BooleanField(
40-
default=False,
41-
help_text=(
42-
"Designates that this user has all permissions without explicitly assigning them."
43-
),
44-
verbose_name="superuser status",
45-
),
46-
),
47-
(
48-
"username",
49-
models.CharField(
50-
error_messages={"unique": "A user with that username already exists."},
51-
help_text=("Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only."),
52-
max_length=150,
53-
unique=True,
54-
validators=[django.contrib.auth.validators.UnicodeUsernameValidator()],
55-
verbose_name="username",
56-
),
57-
),
58-
(
59-
"first_name",
60-
models.CharField(blank=True, max_length=150, verbose_name="first name"),
61-
),
62-
(
63-
"last_name",
64-
models.CharField(blank=True, max_length=150, verbose_name="last name"),
65-
),
66-
(
67-
"email",
68-
models.EmailField(
69-
blank=True,
70-
max_length=254,
71-
verbose_name="email address",
72-
),
73-
),
74-
(
75-
"is_staff",
76-
models.BooleanField(
77-
default=False,
78-
help_text=("Designates whether the user can log into this admin site."),
79-
verbose_name="staff status",
80-
),
81-
),
82-
(
83-
"is_active",
84-
models.BooleanField(
85-
default=True,
86-
help_text=(
87-
"Designates whether this user should be treated "
88-
"as active. Unselect this instead of deleting "
89-
"accounts."
90-
),
91-
verbose_name="active",
92-
),
93-
),
94-
(
95-
"date_joined",
96-
models.DateTimeField(
97-
default=django.utils.timezone.now,
98-
verbose_name="date joined",
99-
),
100-
),
101-
(
102-
"is_demo",
103-
models.BooleanField(
104-
default=False,
105-
help_text="Whether this user is a demo user.",
106-
),
107-
),
108-
(
109-
"groups",
110-
models.ManyToManyField(
111-
blank=True,
112-
help_text=(
113-
"The groups this user belongs to. A user will "
114-
"get all permissions granted to each of their "
115-
"groups."
116-
),
117-
related_name="idp_user_set",
118-
related_query_name="user",
119-
to="auth.group",
120-
verbose_name="groups",
121-
),
122-
),
123-
(
124-
"user_permissions",
125-
models.ManyToManyField(
126-
blank=True,
127-
help_text="Specific permissions for this user.",
128-
related_name="idp_user_set",
129-
related_query_name="user",
130-
to="auth.permission",
131-
verbose_name="user permissions",
132-
),
133-
),
134-
],
135-
options={
136-
"db_table": "idp_user_user",
137-
},
138-
managers=[
139-
("objects", django.contrib.auth.models.UserManager()),
140-
],
141-
),
18+
migrations.CreateModel(
19+
name='User',
20+
fields=[
21+
('password', models.CharField(max_length=128, verbose_name='password')),
22+
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
23+
('is_superuser', models.BooleanField(default=False,
24+
help_text='Designates that this user has all permissions without explicitly assigning them.',
25+
verbose_name='superuser status')),
26+
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'},
27+
help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.',
28+
max_length=150, unique=True,
29+
validators=[django.contrib.auth.validators.UnicodeUsernameValidator()],
30+
verbose_name='username')),
31+
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
32+
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
33+
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
34+
('is_staff', models.BooleanField(default=False,
35+
help_text='Designates whether the user can log into this admin site.',
36+
verbose_name='staff status')),
37+
('is_active', models.BooleanField(default=True,
38+
help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.',
39+
verbose_name='active')),
40+
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
41+
('idp_user_id', models.BigIntegerField(primary_key=True, serialize=False)),
42+
('groups', models.ManyToManyField(blank=True,
43+
help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.',
44+
related_name='user_set', related_query_name='user', to='auth.Group',
45+
verbose_name='groups')),
46+
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.',
47+
related_name='user_set', related_query_name='user',
48+
to='auth.Permission', verbose_name='user permissions')),
14249
],
143-
database_operations=[
144-
migrations.RunSQL(
145-
sql="ALTER TABLE IF EXISTS auth_user RENAME TO idp_user_user;",
146-
reverse_sql="ALTER TABLE IF EXISTS idp_user_user RENAME TO auth_user;",
147-
),
50+
options={
51+
'verbose_name': 'user',
52+
'verbose_name_plural': 'users',
53+
'abstract': False,
54+
},
55+
managers=[
56+
('objects', django.contrib.auth.models.UserManager()),
14857
],
14958
),
150-
migrations.RunSQL(
151-
sql="DROP TABLE IF EXISTS idp_user_userrole CASCADE;",
152-
reverse_sql=migrations.RunSQL.noop,
59+
migrations.CreateModel(
60+
name='UserRole',
61+
fields=[
62+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
63+
('role', models.CharField(max_length=125)),
64+
('app_config', models.JSONField(null=True)),
65+
('permission_restrictions', models.JSONField(default=dict)),
66+
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='user_roles',
67+
to=settings.AUTH_USER_MODEL)),
68+
],
69+
options={
70+
'unique_together': {('user', 'role')},
71+
},
15372
),
15473
]

0 commit comments

Comments
 (0)