Exemplo n.º 1
0
def validate_hostname(request):
    """
        This function:
            Validates that a Jenkins hostname is valid

        Requested by:
            add_job.html, edit_job.html

        Request method:
            POST
    """
    print request.POST.get('hostname')
    job = RetrieveJob(append_http(request.POST.get('hostname',None)),None)
    print request.POST.get('username') == 'Username'
    test = job.lookup_hostname(request.POST.get('username') != 'Username', request.POST.get('username'), request.POST.get('password1'))
    
    if test == urllib2.URLError:
        result = dict(status = 500)
    elif test == ValueError:
        result = dict(status = 404)
    elif test == 403: #autherization required
        result = dict(status = 403)
    elif test == 401: #invalid cerendtials
        result = dict(status = 401)
    else:
        result = dict(status = 200)
    
    return HttpResponse(simplejson.dumps([result]))
Exemplo n.º 2
0
def add_job(request):
    """
        This Function is POSTed to by the Add Job modal to create a new widget. It:
            - Checks if the user is authenticated
            - Initializes widget data not provided by the add job modal
            - If a displayname isn't provided, uses the jobname
            - Creates a new Ci Server object if provided
            - Checks if the user has a widget for that job on that server
                Yes: Updates that widget with the form data
                 No: Creates a new widget with the form data
            - Saves the widget
            - Redirects to '/'
    """
    if request.user.is_authenticated():
        widget_data = request.POST.copy()  # request.POST is immutable

        widget_data['user'] = request.user.id
        widget_data['icon'] = 'checkmark.png'
        widget_data['readonly'] = False
        widget_data['entity_active'] = True
        widget_data['appletv'] = False
        widget_data['appletv_active'] = True

        if not 'displayname' in widget_data or widget_data['displayname'] == '':
            widget_data['displayname'] = widget_data['jobname']

        if 'new_ci_server' in widget_data and not widget_data[
                'new_ci_server'] == '':
            job = RetrieveJob(append_http(widget_data['new_ci_server']), None)
            if job.lookup_hostname(False):
                newserver = models.CiServer(
                    hostname=widget_data['new_ci_server'])
                newserver.save()
                widget_data['ci_server'] = newserver.hostname

        matches = models.UserCiJob.objects.filter(
            user__username=request.user.username,
            ci_server__hostname=widget_data['ci_server'],
            jobname=widget_data['jobname'])

        if len(matches) > 0:
            # Don't change the Public TV Display properties of the existing widget.
            widget_data['appletv'] = matches[0].appletv
            widget_data['appletv_active'] = matches[0].appletv_active

            form = forms.UserCiJobForm(widget_data, instance=matches[0])
        else:
            form = forms.UserCiJobForm(widget_data)

        if form.is_valid():
            form.save()
        else:
            print form.errors

    return HttpResponseRedirect('/')
Exemplo n.º 3
0
def add_job(request):
    """
        This Function is POSTed to by the Add Job modal to create a new widget. It:
            - Checks if the user is authenticated
            - Initializes widget data not provided by the add job modal
            - If a displayname isn't provided, uses the jobname
            - Creates a new Ci Server object if provided
            - Checks if the user has a widget for that job on that server
                Yes: Updates that widget with the form data
                 No: Creates a new widget with the form data
            - Saves the widget
            - Redirects to '/'
    """
    if request.user.is_authenticated():
        widget_data = request.POST.copy() # request.POST is immutable

        widget_data['user'] = request.user.id
        widget_data['icon'] = 'checkmark.png'
        widget_data['readonly'] = False
        widget_data['entity_active'] = True
        widget_data['appletv'] = False
        widget_data['appletv_active'] = True

        if not 'displayname' in widget_data or widget_data['displayname'] == '':
            widget_data['displayname'] = widget_data['jobname']

        if 'new_ci_server' in widget_data and not widget_data['new_ci_server'] == '':
            job = RetrieveJob(append_http(widget_data['new_ci_server']), None)
            if job.lookup_hostname(False):
                newserver = models.CiServer(hostname = widget_data['new_ci_server'])
                newserver.save()
                widget_data['ci_server'] = newserver.hostname

        matches = models.UserCiJob.objects.filter(user__username = request.user.username,
                    ci_server__hostname = widget_data['ci_server'],
                    jobname = widget_data['jobname'])

        if len(matches) > 0:
            # Don't change the Public TV Display properties of the existing widget.
            widget_data['appletv'] = matches[0].appletv
            widget_data['appletv_active'] = matches[0].appletv_active

            form = forms.UserCiJobForm(widget_data, instance=matches[0])
        else:
            form = forms.UserCiJobForm(widget_data)

        if form.is_valid():
            form.save()
        else:
            print form.errors

    return HttpResponseRedirect('/')
Exemplo n.º 4
0
def save_widget(request):
    """
        This Function:
            Is POST-ed to by the edit_widget modal in order to save changes
            to the widget.
    """
    if not request.user.is_authenticated():
        return HttpResponse(status=401)

    if 'widget_id' in request.POST:
        new_data = request.POST.copy()
        widget = models.UserCiJob.objects.get(pk=request.POST['widget_id'])
        old_data = widget_to_dictionary(widget)

        # The form has to be populated by old data because the edit_widget
        # modal form doesn't provide all attributes of the UserCiJob model.
        # The widget's attributes are copied to a dictionary and then
        # the values from the form data are used to update the dictionary.

        for key in ['displayname', 'jobname', 'ci_server']:
            if key in new_data:
                old_data[key] = new_data[key]

        # If these checkboxes are unchecked, the field is not returned
        # from the form, therefore in order to update them from the
        # edit widget modal we check if they exist in the POST data at all.

        for key in ['entity_active', 'appletv', 'appletv_active']:
            if key in new_data:
                if new_data[key] != "current":
                    old_data[key] = True
            else:
                old_data[key] = False

        # If the form provides a new CiServer hostname a new model object is
        # created and then the ci_server value is changed in the form data to match.

        if 'new_ci_server' in new_data and not new_data['new_ci_server'] == '':
            job = RetrieveJob(append_http(new_data['new_ci_server']),None)
            if job.lookup_hostname(False):
                newserver = models.CiServer(hostname = new_data['new_ci_server'])
                newserver.save()
                old_data['ci_server'] = newserver.hostname

        form = forms.UserCiJobForm(old_data, instance=widget)
        if form.is_valid():
            form.save()
        else:
            print form.errors

    return HttpResponseRedirect('/')
Exemplo n.º 5
0
def save_widget(request):
    """
        This Function:
            Is POST-ed to by the edit_widget modal in order to save changes
            to the widget.
    """
    if not request.user.is_authenticated():
        return HttpResponse(status=401)

    if 'widget_id' in request.POST:
        new_data = request.POST.copy()
        widget = models.UserCiJob.objects.get(pk=request.POST['widget_id'])
        old_data = widget_to_dictionary(widget)

        # The form has to be populated by old data because the edit_widget
        # modal form doesn't provide all attributes of the UserCiJob model.
        # The widget's attributes are copied to a dictionary and then
        # the values from the form data are used to update the dictionary.

        for key in ['displayname', 'jobname', 'ci_server']:
            if key in new_data:
                old_data[key] = new_data[key]

        # If these checkboxes are unchecked, the field is not returned
        # from the form, therefore in order to update them from the
        # edit widget modal we check if they exist in the POST data at all.

        for key in ['entity_active', 'appletv', 'appletv_active']:
            if key in new_data:
                if new_data[key] != "current":
                    old_data[key] = True
            else:
                old_data[key] = False

        # If the form provides a new CiServer hostname a new model object is
        # created and then the ci_server value is changed in the form data to match.

        if 'new_ci_server' in new_data and not new_data['new_ci_server'] == '':
            job = RetrieveJob(append_http(new_data['new_ci_server']), None)
            if job.lookup_hostname(False):
                newserver = models.CiServer(hostname=new_data['new_ci_server'])
                newserver.save()
                old_data['ci_server'] = newserver.hostname

        form = forms.UserCiJobForm(old_data, instance=widget)
        if form.is_valid():
            form.save()
        else:
            print form.errors

    return HttpResponseRedirect('/')
Exemplo n.º 6
0
Arquivo: views.py Projeto: aboarya/bvd
def validate_hostname(request):
    job = RetrieveJob(append_http(request.POST.get('hostname',None)),None)
    print(request.POST.get('username') == 'Username')
    test = job.lookup_hostname(request.POST.get('username') != 'Username', request.POST.get('username'), request.POST.get('password1'))
    
    if test == urllib2.URLError:
        result = dict(status = 500)
    elif test == ValueError:
        result = dict(status = 404)
    elif test == 403: #autherization required
        result = dict(status = 403)
    elif test == 401: #invalid cerendtials
        result = dict(status = 401)
    else:
        result = dict(status = 200)
    
    return HttpResponse(simplejson.dumps([result]), content_type = 'application/javascript')
Exemplo n.º 7
0
Arquivo: views.py Projeto: webiken/bvd
def validate_hostname(request):
    job = RetrieveJob(append_http(request.POST.get('hostname',None)),None)
    print request.POST.get('username') == 'Username'
    test = job.lookup_hostname(request.POST.get('username') != 'Username', request.POST.get('username'), request.POST.get('password1'))
    
    if test == urllib2.URLError:
        result = dict(status = 500)
    elif test == ValueError:
        result = dict(status = 404)
    elif test == 403: #autherization required
        result = dict(status = 403)
    elif test == 401: #invalid cerendtials
        result = dict(status = 401)
    else:
        result = dict(status = 200)
    
    return HttpResponse(simplejson.dumps([result]), content_type = 'application/javascript')