Exemple #1
0
def edit(request, rack_id):

    autosuggest_url = reverse('api-js-user')
    show_fb = True
    rack = Rack.objects.get(id=rack_id)
    
    has_perm = request.user.has_perm('change', rack)
    if request.method == 'POST':
        form = AddRackForm(autosuggest_url, request.POST)
        if form.is_valid():
            try:
                if form.cleaned_data['owner'] and request.user.is_superuser:
                    owner = form.cleaned_data['owner']
                else:
                    owner = None

                if not has_perm:
                    messages.add_message(request, messages.ERROR, 'Permission denied.')
                else:
                    rack.row = form.cleaned_data['row']
                    rack.number = form.cleaned_data['number']
                    rack.location = form.cleaned_data['location']
                    if owner:
                        rack.owner = owner
                    rack.save()
                    
                    messages.add_message(request, messages.SUCCESS, 'Rack updated.')
                    show_fb = False
            except Exception, e:
                messages.add_message(request, messages.ERROR, 'Error: ' + str(e),
                                     )
Exemple #2
0
def add(request):
    
    autosuggest_url = reverse('api-js-user')
    show_fb = True
    if request.method == 'POST':
        form = AddRackForm(autosuggest_url, request.POST)
        if form.is_valid():
            
            if form.cleaned_data['owner'] and request.user.is_superuser:
                owner = form.cleaned_data['owner']
            else:
                owner = request.user

            try:
                Rack.objects.create(row=form.cleaned_data['row'],
                                    number=form.cleaned_data['number'],
                                    location=form.cleaned_data['location'],
                                    created_by=request.user,
                                    owner=owner
                                    )
                messages.add_message(request, messages.SUCCESS, 'A new rack added.',
                                     )
                show_fb = False
            except Exception, e:
                messages.add_message(request, messages.ERROR, 'Error: ' + str(e),
                                     )