Passed
Push — master ( 52cdbc...8b6a19 )
by Alexander
02:57
created

tcms.utils.user.delete_user()   B

Complexity

Conditions 6

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 21
rs 8.6666
c 0
b 0
f 0
cc 6
nop 1
1
# -*- coding: utf-8 -*-
2
from django.db import transaction
3
4
5
def delete_user(user):
6
    """
7
        Delete user across DB schemas.
8
    """
9
    if hasattr(user, 'tenant_set'):
10
        from django_tenants.utils import schema_context  # pylint: disable=import-error
11
12
        # using transactions b/c multiple schemas can refer to the same
13
        # user ID as FK references!
14
        with transaction.atomic():
15
            # delete user and all of its data across tenants
16
            for tenant in user.tenant_set.all():
17
                with schema_context(tenant.schema_name):
18
                    user.delete()
19
20
            # then delete everything from the public schema
21
            with schema_context('public'):
22
                user.delete()
23
24
    else:
25
        user.delete()
26