ProviderForm   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 13
Duplicated Lines 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 13
loc 13
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A __init__() 6 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
from django import forms
2
from django_th.models import UserService
3
from django.utils.translation import ugettext_lazy as _
4
5
6
class ServiceChoiceForm(forms.Form):
7
8
    def activated_services(self, user, provider=None):
9
        """
10
            get the activated services added from the administrator
11
            :param user: user
12
            :param provider: the selected provider
13
            :type user: current user
14
            :type provider: string
15
            :return: list of activated services
16
            :rtype: list
17
        """
18
        services = UserService.objects.filter(name__status=1, user=user)
19
20
        choices = []
21
        data = ()
22
23
        if provider is not None:
24
            services = services.exclude(name__exact=provider)
25
26
        for class_name in services:
27
            data = (class_name.name,
28
                    class_name.name.name.rsplit('Service', 1)[1])
29
            choices.append(data)
30
31
        return choices
32
33
34 View Code Duplication
class ProviderForm(ServiceChoiceForm):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
35
36
    """
37
        Set some HTML class to the Provider form
38
    """
39
    provider = forms.ChoiceField()
40
41
    def __init__(self, *args, **kwargs):
42
        super(ProviderForm, self).__init__(*args, **kwargs)
43
        self.fields['provider'].choices = self.activated_services(
44
            user=self.initial['user']
45
        )
46
        self.fields['provider'].widget.attrs['class'] = 'form-control'
47
48
49 View Code Duplication
class ConsumerForm(ServiceChoiceForm):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
50
51
    """
52
        Set some HTML class to the Consumer form
53
    """
54
    consumer = forms.ChoiceField()
55
56
    def __init__(self, *args, **kwargs):
57
        super(ConsumerForm, self).__init__(*args, **kwargs)
58
        # get the list of service without the one selected in
59
        # the provider form
60
        self.fields['consumer'].choices = self.activated_services(
61
            user=self.initial['user'],
62
            provider=self.initial['provider']
63
        )
64
        self.fields['consumer'].widget.attrs['class'] = 'form-control'
65
66
67
class ServicesDescriptionForm(forms.Form):
68
69
    """
70
        Set some HTML class to the Service form
71
    """
72
    description = forms.CharField(
73
        widget=forms.TextInput(attrs={'placeholder':
74
                                      _('A description for your new service')})
75
    )
76
77
78
class DummyForm(forms.Form):
79
    pass
80