def treemenu(request, plugin_id): """ This is how we inject nodes to the Tree Menu The FreeNAS GUI will access this view, expecting for a JSON that describes a node and possible some children. """ (btsync_key, btsync_secret) = utils.get_btsync_oauth_creds() url = utils.get_rpc_url(request) trans = OAuthTransport(url, key=btsync_key, secret=btsync_secret) server = jsonrpclib.Server(url, transport=trans) jail = json.loads(server.plugins.jail.info(plugin_id))[0] jail_name = jail["fields"]["jail_host"] number = jail_name.rsplit("_", 1) name = "BtSync" if len(number) == 2: try: number = int(number) if number > 1: name = "BtSync (%d)" % number except: pass plugin = { "name": name, "append_to": "plugins", "icon": reverse("treemenu_icon", kwargs={"plugin_id": plugin_id}), "type": "pluginsfcgi", "url": reverse("btsync_edit", kwargs={"plugin_id": plugin_id}), "kwargs": {"plugin_name": "btsync", "plugin_id": plugin_id}, } return HttpResponse(json.dumps([plugin]), content_type="application/json")
def treemenu(request, plugin_id): """ This is how we inject nodes to the Tree Menu The FreeNAS GUI will access this view, expecting for a JSON that describes a node and possible some children. """ (btsync_key, btsync_secret) = utils.get_btsync_oauth_creds() url = utils.get_rpc_url(request) trans = OAuthTransport(url, key=btsync_key, secret=btsync_secret) server = jsonrpclib.Server(url, transport=trans) jail = json.loads(server.plugins.jail.info(plugin_id))[0] jail_name = jail['fields']['jail_host'] number = jail_name.rsplit('_', 1) name = "BtSync" if len(number) == 2: try: number = int(number) if number > 1: name = "BtSync (%d)" % number except: pass plugin = { 'name': name, 'append_to': 'plugins', 'icon': reverse('treemenu_icon', kwargs={'plugin_id': plugin_id}), 'type': 'pluginsfcgi', 'url': reverse('btsync_edit', kwargs={'plugin_id': plugin_id}), 'kwargs': {'plugin_name': 'btsync', 'plugin_id': plugin_id }, } return HttpResponse(json.dumps([plugin]), content_type='application/json')
def stop(request, plugin_id): (btsync_key, btsync_secret) = utils.get_btsync_oauth_creds() url = utils.get_rpc_url(request) trans = OAuthTransport(url, key=btsync_key, secret=btsync_secret) server = jsonrpclib.Server(url, transport=trans) auth = server.plugins.is_authenticated(request.COOKIES.get("sessionid", "")) jail_path = server.plugins.jail.path(plugin_id) assert auth try: btsync = models.BtSync.objects.order_by("-id")[0] btsync.enable = False btsync.save() except IndexError: btsync = models.BtSync.objects.create(enable=False) try: form = forms.BtSyncForm(btsync.__dict__, instance=btsync, jail_path=jail_path) form.is_valid() form.save() except ValueError: pass cmd = "%s onestop" % utils.btsync_control pipe = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True, close_fds=True) out = pipe.communicate()[0] return HttpResponse(simplejson.dumps({"error": False, "message": out}), content_type="application/json")
def edit(request, plugin_id): (btsync_key, btsync_secret) = utils.get_btsync_oauth_creds() url = utils.get_rpc_url(request) trans = OAuthTransport(url, key=btsync_key, secret=btsync_secret) """ Get the BtSync object If it does not exist create a new entry """ try: btsync = models.BtSync.objects.order_by('-id')[0] except IndexError: btsync = models.BtSync.objects.create() try: server = jsonrpclib.Server(url, transport=trans) jail_path = server.plugins.jail.path(plugin_id) jail = json.loads(server.plugins.jail.info(plugin_id))[0]['fields'] jail_ipv4 = jail['jail_ipv4'].split('/')[0] if btsync.force_https: scheme = "https" else: scheme = "http" auth = server.plugins.is_authenticated( request.COOKIES.get("sessionid", "") ) assert auth except Exception as e: raise if request.method == "GET": form = forms.BtSyncForm(instance=btsync, jail_path=jail_path) return render(request, "edit.html", { 'form': form, 'ipv4': jail_ipv4, 'scheme' : scheme, 'port': btsync.webui_port }) if not request.POST: return JsonResponse(request, error=True, message="A problem occurred.") form = forms.BtSyncForm(request.POST, instance=btsync, jail_path=jail_path) if form.is_valid(): form.save() cmd = "%s restart" % utils.btsync_control pipe = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True, close_fds=True) return JsonResponse(request, error=True, message="BtSync settings successfully saved.") return JsonResponse(request, form=form)
def start(request, plugin_id): (btsync_key, btsync_secret) = utils.get_btsync_oauth_creds() url = utils.get_rpc_url(request) trans = OAuthTransport(url, key=btsync_key, secret=btsync_secret) server = jsonrpclib.Server(url, transport=trans) auth = server.plugins.is_authenticated( request.COOKIES.get("sessionid", "") ) jail_path = server.plugins.jail.path(plugin_id) assert auth try: btsync = models.BtSync.objects.order_by('-id')[0] btsync.enable = True btsync.save() except IndexError: btsync = models.BtSync.objects.create(enable=True) try: form = forms.BtSyncForm(btsync.__dict__, instance=btsync, jail_path=jail_path) form.is_valid() form.save() except ValueError: return HttpResponse(simplejson.dumps({ 'error': True, 'message': ('BtSync data did not validate, configure ' 'it first.'), }), content_type='application/json') cmd = "%s onestart" % utils.btsync_control pipe = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True, close_fds=True) out = pipe.communicate()[0] return HttpResponse(simplejson.dumps({ 'error': False, 'message': out, }), content_type='application/json')