mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-17 20:45:44 +00:00
Switched to global owner model, need to validate use-cases table and fix tests
This commit is contained in:
@ -16,6 +16,11 @@ class UsersConfig(AppConfig):
|
||||
except (OperationalError, ProgrammingError):
|
||||
pass
|
||||
|
||||
try:
|
||||
self.update_owners()
|
||||
except (OperationalError, ProgrammingError):
|
||||
pass
|
||||
|
||||
def assign_permissions(self):
|
||||
|
||||
from django.contrib.auth.models import Group
|
||||
@ -31,3 +36,9 @@ class UsersConfig(AppConfig):
|
||||
for group in Group.objects.all():
|
||||
|
||||
update_group_roles(group)
|
||||
|
||||
def update_owners(self):
|
||||
|
||||
from users.models import create_owners
|
||||
|
||||
create_owners(full_update=True)
|
||||
|
27
InvenTree/users/migrations/0004_owner_model.py
Normal file
27
InvenTree/users/migrations/0004_owner_model.py
Normal file
@ -0,0 +1,27 @@
|
||||
# Generated by Django 3.0.7 on 2021-01-11 18:54
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('contenttypes', '0002_remove_content_type_name'),
|
||||
('users', '0003_auto_20201005_2227'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Owner',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('owner_id', models.PositiveIntegerField(blank=True, null=True)),
|
||||
('owner_type', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
|
||||
],
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='owner',
|
||||
constraint=models.UniqueConstraint(fields=('owner_type', 'owner_id'), name='unique_owner'),
|
||||
),
|
||||
]
|
@ -1,7 +1,10 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from django.contrib.auth.models import Group, Permission
|
||||
from django.contrib.auth.models import User, Group, Permission
|
||||
from django.contrib.contenttypes.fields import GenericForeignKey
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db.models import UniqueConstraint
|
||||
from django.db.utils import IntegrityError
|
||||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
@ -385,3 +388,79 @@ def check_user_role(user, role, permission):
|
||||
|
||||
# No matching permissions found
|
||||
return False
|
||||
|
||||
|
||||
class Owner(models.Model):
|
||||
"""
|
||||
An owner is either a group or user.
|
||||
Owner can be associated to any InvenTree model (part, stock, etc.)
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
constraints = [
|
||||
UniqueConstraint(fields=['owner_type', 'owner_id'],
|
||||
name='unique_owner')
|
||||
]
|
||||
|
||||
owner_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True, blank=True)
|
||||
owner_id = models.PositiveIntegerField(null=True, blank=True)
|
||||
owner = GenericForeignKey('owner_type', 'owner_id')
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.owner} ({self.owner_type.name})'
|
||||
|
||||
def get_users(self):
|
||||
|
||||
owner_users = None
|
||||
|
||||
if type(self.owner) is Group:
|
||||
users = User.objects.filter(groups__name=self.owner.name)
|
||||
owner_users = Owner.objects.filter(owner_id__in=users,
|
||||
owner_type=ContentType.objects.get_for_model(User).id)
|
||||
|
||||
return owner_users
|
||||
|
||||
|
||||
def create_owners(full_update=False, group=None, user=None):
|
||||
""" Create all owners """
|
||||
|
||||
if full_update:
|
||||
# Create group owners
|
||||
for group in Group.objects.all():
|
||||
try:
|
||||
Owner.objects.create(owner=group)
|
||||
except IntegrityError:
|
||||
pass
|
||||
|
||||
# Create user owners
|
||||
for user in User.objects.all():
|
||||
try:
|
||||
Owner.objects.create(owner=user)
|
||||
except IntegrityError:
|
||||
pass
|
||||
else:
|
||||
if group:
|
||||
try:
|
||||
Owner.objects.create(owner=group)
|
||||
except IntegrityError:
|
||||
pass
|
||||
|
||||
if user:
|
||||
try:
|
||||
Owner.objects.create(owner=user)
|
||||
except IntegrityError:
|
||||
pass
|
||||
|
||||
|
||||
@receiver(post_save, sender=Group)
|
||||
def create_new_owner_group(sender, instance, **kwargs):
|
||||
""" Called *after* a Group object is saved. """
|
||||
|
||||
create_owners(group=instance)
|
||||
|
||||
|
||||
@receiver(post_save, sender=User)
|
||||
def create_new_owner_user(sender, instance, **kwargs):
|
||||
""" Called *after* a User object is saved. """
|
||||
|
||||
create_owners(user=instance)
|
||||
|
Reference in New Issue
Block a user