Exemple #1
0
def edit(request, plugin_id):
    (transmission_key,
     transmission_secret) = utils.get_transmission_oauth_creds()
    url = utils.get_rpc_url(request)
    trans = OAuthTransport(url,
                           key=transmission_key,
                           secret=transmission_secret)
    """
    Get the Transmission object
    If it does not exist create a new entry
    """
    try:
        transmission = models.Transmission.objects.order_by('-id')[0]
    except IndexError:
        transmission = models.Transmission.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]
        auth = server.plugins.is_authenticated(
            request.COOKIES.get("sessionid", ""))
        assert auth
    except Exception:
        raise

    if request.method == "GET":
        form = forms.TransmissionForm(instance=transmission,
                                      jail_path=jail_path)
        return render(request, "edit.html", {
            'form': form,
            'ipv4': jail_ipv4,
            'port': transmission.rpc_port
        })

    if not request.POST:
        return JsonResponse(request, error=True, message="A problem occurred.")

    form = forms.TransmissionForm(request.POST,
                                  instance=transmission,
                                  jail_path=jail_path)
    if form.is_valid():
        form.save()

        cmd = "%s onereload" % utils.transmission_control
        pipe = Popen(cmd,
                     stdin=PIPE,
                     stdout=PIPE,
                     stderr=PIPE,
                     shell=True,
                     close_fds=True)

        return JsonResponse(
            request,
            error=True,
            message="Transmission settings successfully saved.")

    return JsonResponse(request, form=form)
Exemple #2
0
def start(request, plugin_id):
    (transmission_key,
     transmission_secret) = utils.get_transmission_oauth_creds()

    url = utils.get_rpc_url(request)
    trans = OAuthTransport(url,
                           key=transmission_key,
                           secret=transmission_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:
        transmission = models.Transmission.objects.order_by('-id')[0]
        transmission.enable = True
        transmission.save()
    except IndexError:
        transmission = models.Transmission.objects.create(enable=True)

    try:
        form = forms.TransmissionForm(transmission.__dict__,
                                      instance=transmission,
                                      jail_path=jail_path)
        form.is_valid()
        form.save()
    except ValueError:
        return HttpResponse(simplejson.dumps({
            'error':
            True,
            'message': ('Transmission data did not validate, configure '
                        'it first.'),
        }),
                            content_type='application/json')

    cmd = "%s onestart" % utils.transmission_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')