Menu

[r5519]: / trunk / gui / services / views.py  Maximize  Restore  History

Download this file

207 lines (196 with data), 7.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#+
# Copyright 2010 iXsystems
# All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted providing that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# $FreeBSD$
#####################################################################
from freenasUI.services.forms import *
from freenasUI.services.models import services as Services
from django.forms.models import modelformset_factory
from django.contrib.auth.decorators import permission_required
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login, logout
from django.template import RequestContext
from django.http import Http404
from django.views.generic.list_detail import object_detail, object_list
from freenasUI.middleware.notifier import notifier
import os, commands
def helperView(request, theForm, model, url):
if request.method == 'POST':
form = theForm(request.POST)
if form.is_valid():
form.save()
if model.objects.count() > 3:
stale_id = model.objects.order_by("-id")[3].id
model.objects.filter(id__lte=stale_id).delete()
else:
# This is a debugging aid to raise exception when validation
# is not passed.
form.save()
else:
try:
_entity = model.objects.order_by("-id").values()[0]
except:
# TODO: We throw an exception (which makes this try/except
# meaningless) for now. A future version will have the
# ability to set up default values.
raise
form = theForm(data = _entity)
variables = RequestContext(request, {
'form': form
})
return render_to_response(url, variables)
def helperViewEm(request, theForm, model):
data_saved = 0
if request.method == 'POST':
form = theForm(request.POST)
if form.is_valid():
# TODO: test if the data is the same as what is in the database?
form.save()
data_saved = 1
if model.objects.count() > 3:
stale_id = model.objects.order_by("-id")[3].id
model.objects.filter(id__lte=stale_id).delete()
else:
pass
else:
try:
_entity = model.objects.order_by("-id").values()[0]
except:
_entity = None
form = theForm(data = _entity)
return (data_saved, form)
@login_required
def cronjobView(request):
if request.method == 'POST':
form = cronjobForm(request.POST)
if form.is_valid():
form.save()
else:
form = cronjobForm()
variables = RequestContext(request, {
'form': form
})
return render_to_response('freenas/system/cronjob.html', variables)
## Services section
@login_required
def services(request):
# Counter for forms we have validated.
forms_saved = 0
saved, cifs = helperViewEm(request, CIFSForm, CIFS)
forms_saved = forms_saved + saved
saved, afp = helperViewEm(request, AFPForm, AFP)
forms_saved = forms_saved + saved
saved, nfs = helperViewEm(request, NFSForm, NFS)
forms_saved = forms_saved + saved
saved, rsync = helperViewEm(request, rsyncjobForm, rsyncjob)
forms_saved = forms_saved + saved
saved, unison = helperViewEm(request, UnisonForm, Unison)
forms_saved = forms_saved + saved
saved, iscsi = helperViewEm(request, iSCSITargetForm, iSCSITarget)
forms_saved = forms_saved + saved
saved, dynamicdns = helperViewEm(request, DynamicDNSForm, DynamicDNS)
forms_saved = forms_saved + saved
saved, snmp = helperViewEm(request, SNMPForm, SNMP)
forms_saved = forms_saved + saved
saved, ups = helperViewEm(request, UPSForm, UPS)
forms_saved = forms_saved + saved
saved, webserver = helperViewEm(request, WebserverForm, Webserver)
forms_saved = forms_saved + saved
saved, bittorrent = helperViewEm(request, BitTorrentForm, BitTorrent)
forms_saved = forms_saved + saved
saved, ftp = helperViewEm(request, FTPForm, FTP)
forms_saved = forms_saved + saved
saved, tftp = helperViewEm(request, TFTPForm, TFTP)
forms_saved = forms_saved + saved
saved, ssh = helperViewEm(request, SSHForm, SSH)
forms_saved = forms_saved + saved
saved, activedirectory = helperViewEm(request, ActiveDirectoryForm, ActiveDirectory)
forms_saved = forms_saved + saved
saved, ldap = helperViewEm(request, LDAPForm, LDAP)
forms_saved = forms_saved + saved
if request.method == 'POST':
srv = Services.objects.all()
if forms_saved > 0:
return HttpResponseRedirect('/services/')
else:
pass # Need to raise a validation exception
else:
srv = Services.objects.all()
variables = RequestContext(request, {
'srv': srv,
'cifs': cifs,
'afp': afp,
'nfs': nfs,
'rsync': rsync,
'unison': unison,
'iscsi': iscsi,
'dynamicdns': dynamicdns,
'snmp': snmp,
'ups': ups,
'webserver': webserver,
'bittorrent': bittorrent,
'ftp': ftp,
'tftp': tftp,
'ssh': ssh,
'activedirectory': activedirectory,
'ldap': ldap,
})
return render_to_response('freenas/services/index.html', variables)
"""TODO: This should be rewritten in a better way."""
@login_required
def servicesToggleView(request, formname):
form2namemap = {
'cifs_toggle' : 'cifs',
'afp_toggle' : 'afp',
'nfs_toggle' : 'nfs',
'unison_toggle' : 'unison',
'iscsi_toggle' : 'iscsi',
'dyndns_toggle' : 'dyndns',
'snmp_toggle' : 'snmp',
'ups_toggle' : 'ups',
'bt_toggle' : 'bittorrent',
'httpd_toggle' : 'httpd',
'ftp_toggle' : 'ftp',
'tftp_toggle' : 'tftp',
'ssh_toggle' : 'ssh',
'ad_toggle' : 'activedirectory',
'ldap_toggle' : 'ldap',
}
changing_service = form2namemap[formname]
if changing_service == "":
raise "Unknown service - Invalid request?"
svc_entry = Services.objects.get(srv_service=changing_service)
if svc_entry.srv_enable:
svc_entry.srv_enable = 0
else:
svc_entry.srv_enable = 1
svc_entry.save()
# forcestop then start to make sure the service is of the same
# status.
notifier().restart(changing_service)
return HttpResponseRedirect('/services/')
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.