Completed
Push — master ( da0207...5a5796 )
by Mathias
01:37
created

DidAdmin   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %
Metric Value
wmc 2
dl 0
loc 34
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A has_change_permission() 0 5 2
1
# Copyright 2013 Mathias WOLFF
2
# This file is part of pyfreebilling.
3
#
4
# pyfreebilling is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# pyfreebilling is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with pyfreebilling.  If not, see <http://www.gnu.org/licenses/>
16
17
from django.contrib import admin
18
from django.contrib import messages
19
from django.template import Context, loader
20
from django.utils.safestring import mark_safe
21
from django.utils.translation import ugettext_lazy as _
22
23
from yawdadmin import admin_site
24
25
from switch import esl
26
27
from did.models import Did, RoutesDid
28
29
30
def didupdate(modeladmin, request, queryset):
31
    """ generate new did xml config file """
32
    try:
33
        t = loader.get_template('xml/00_did.xml')
34
    except IOError:
35
        messages.error(request, """did config xml file update failed.
36
            Can not load template file !""")
37
    dids = Did.objects.all()
38
    c = Context({"dids": dids, })
39
    try:
40
        f = open('/usr/local/freeswitch/conf/dialplan/public/00_did.xml', 'w')
41
        try:
42
            f.write(t.render(c))
43
            f.close()
44
            try:
45
                esl.getReloadDialplan()
46
                messages.success(request, "FS successfully reload")
47
            except IOError:
48
                messages.error(request, """DID config xml file update failed.
49
                    FS update failed ! Try manually""")
50
        finally:
51
            #f.close()
52
            messages.success(request, "DID config xml file update success")
53
    except IOError:
54
        messages.error(request, """DID config xml file update failed. Can not
55
            create file !""")
56
didupdate.short_description = _(u"update DID config xml file")
57
58
admin.site.add_action(didupdate, _(u"generate DID configuration file"))
59
60
61
class RoutesDidInline(admin.StackedInline):
62
    #form = RoutesDidForm
63
    description = 'Did routes'
64
    model = RoutesDid
65
    modal = True
66
    extra = 0
67
    collapse = False
68
69
70
class DidAdmin(admin.ModelAdmin):
71
    list_display = ('number',
72
                    'city',
73
                    'provider',
74
                    'prov_max_channels',
75
                    'customer',
76
                    'cust_max_channels',
77
                    'date_modified')
78
    readonly_fields = ('date_added',
79
                       'date_modified')
80
    list_filter = ('provider',
81
                   'customer')
82
    list_display_links = ('number',)
83
    ordering = ('number',)
84
    search_fields = ('number',)
85
    inlines = [RoutesDidInline, ]
86
    actions = [didupdate]
87
88
    # def get_reserved(self, obj):
89
    #     if ContractDid.objects.get(did=obj):
90
    #         return mark_safe("""<span class="label label-success">
91
    #                             <i class="icon-thumbs-up">
92
    #                             </i> Reserved</span>""")
93
    #     return mark_safe("""<span class="label label-danger">
94
    #                         <i class="icon-thumbs-down">
95
    #                         </i> NO</span>""")
96
    # get_reserved.short_description = 'Reserved'
97
    # get_reserved.admin_order_field = 'reserved'
98
99
    def has_change_permission(self, request, obj=None):
100
        if request.user.is_superuser:
101
            return True
102
        else:
103
            return False
104
105
#----------------------------------------
106
# register
107
#----------------------------------------
108
admin_site.register(Did, DidAdmin)
109