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

tcms.utils.user   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 12
dl 0
loc 26
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B delete_user() 0 21 6
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