Пример #1
0
def registerTool(request):
    currentUser = request.user
    sharezone = currentUser.share_zone[:5]
    sheds = Shed.objects.filter(address__zip__startswith=sharezone)
    invalidImage = False

    # check if uploaded file is an image or not
    for key, value in request.FILES.items():
        if not 'image' in value.content_type:
            invalidImage = True

    if request.method == 'POST':
        shedChoice = request.POST.get('shedChoice')

        if invalidImage:
            tool_form = AddToolForm(request.POST)

        else:
            tool_form = AddToolForm(request.POST, request.FILES)

        if tool_form.is_valid():
            with transaction.atomic():
                new_tool = tool_form.save(commit=False)
                new_tool.owner = currentUser
                new_tool.status = 'A'
                if new_tool.location == 'S':
                    new_tool.shed = Shed.objects.get(pk=shedChoice)
                new_tool.save()
                tool_form.save_m2m()

            #NOTE: the 'safe' extra_tag allows the string to be autoescaped so that links can be processed by the template.
            #It SHOULD NOT be used unless you need to add a hyperlink to your message!
            messages.success(
                request, 'You have successfully added a new tool! <br> <br> '
                '<a href="/tool/register_tool">Click here if you wish to add another tool. </a>',
                extra_tags='safe')

            success_url = reverse_lazy("toolManagement:toolbox")
            return redirect(success_url)
        else:
            return render(
                request, 'registertool.html',
                RequestContext(
                    request, {
                        'form': tool_form,
                        'sheds': sheds,
                        'invalidImage': invalidImage
                    }))
    else:
        tool_form = AddToolForm(
            initial={'pickupArrangement': currentUser.pickup_arrangements})
        return render(
            request, 'registertool.html',
            RequestContext(request, {
                'form': tool_form,
                'sheds': sheds
            }))
Пример #2
0
def registerTool(request):
    currentUser = request.user
    sharezone = currentUser.share_zone[:5]
    sheds = Shed.objects.filter(address__zip__startswith=sharezone)
    invalidImage = False

    # check if uploaded file is an image or not
    for key, value in request.FILES.items():
        if not 'image' in value.content_type:
            invalidImage = True

    if request.method == 'POST':
        shedChoice = request.POST.get('shedChoice')

        if invalidImage:
            tool_form = AddToolForm(request.POST)

        else:
            tool_form = AddToolForm(request.POST, request.FILES)


        if tool_form.is_valid():
            with transaction.atomic():
                new_tool = tool_form.save(commit=False)
                new_tool.owner = currentUser
                new_tool.status = 'A'
                if new_tool.location == 'S':
                    new_tool.shed = Shed.objects.get(pk=shedChoice)
                new_tool.save()
                tool_form.save_m2m()

            #NOTE: the 'safe' extra_tag allows the string to be autoescaped so that links can be processed by the template.
            #It SHOULD NOT be used unless you need to add a hyperlink to your message!
            messages.success(request,'You have successfully added a new tool! <br> <br> '
                                     '<a href="/tool/register_tool">Click here if you wish to add another tool. </a>', extra_tags='safe')

            success_url = reverse_lazy("toolManagement:toolbox")
            return redirect(success_url)
        else:
            return render(request, 'registertool.html',
                          RequestContext(request, {'form': tool_form, 'sheds': sheds, 'invalidImage': invalidImage}))
    else:
        tool_form = AddToolForm(initial={'pickupArrangement': currentUser.pickup_arrangements})
        return render(request, 'registertool.html', RequestContext(request, {'form': tool_form, 'sheds': sheds}))
Пример #3
0
def updateTool(request, tool_id):
    today = datetime.date.today()
    currentUser = request.user
    sharezone = currentUser.share_zone[:5]
    sheds = Shed.objects.filter(address__zip__startswith=sharezone)
    tooldata = get_object_or_404(Tool, pk=tool_id)
    denyAccess = True
    invalidImage = False

    # check if uploaded file is an image or not
    for key, value in request.FILES.items():
        if not 'image' in value.content_type:
            invalidImage = True

    if tooldata.owner == request.user:
        denyAccess = False

    if tooldata.location == 'S':
        if tooldata.shed.owner == request.user:
            denyAccess = False

    if denyAccess:
        error_url = reverse_lazy("toolManagement:toolbox")
        messages.error(request,'Error! You do not have permission to edit this tool.', extra_tags='safe')
        return redirect(error_url)

    # TODO : use tool is ready to move instead of futureres
    futureRes = tooldata.reservation_set.filter(Q(status='P') | Q(status='A'))
    unorderedDates = tooldata.blackoutdate_set.exclude(blackoutEnd__lt=today)
    blackoutdates = unorderedDates.order_by('blackoutStart')

    if request.method == 'POST':
        if 'updatetool' in request.POST:
            if invalidImage:
                updateform = AddToolForm(request.POST or None, instance=tooldata)
                messages.warning(request, 'Tool picture was not updated because file was not an image.')
            else:
                updateform = AddToolForm(request.POST or None, request.FILES or None, instance=tooldata)

            blackoutform = forms.BlackoutDateForm(tooldata)
            shedChoice = request.POST.get('shedChoice')
            if updateform.is_valid():
                tool = updateform.save(commit=False)
                if tool.location == 'S':
                    tool.shed = Shed.objects.get(pk=shedChoice)
                tool.save()
                updateform.save_m2m()

                #NOTE: the 'safe' extra_tag allows the string to be autoescaped so that links can be processed by the template.
                #It SHOULD NOT be used unless you need to add a hyperlink to your message!
                messages.success(request,'Your tool was successfully updated! <br> <br> '
                                         '<a href="/tool/toolbox">Click here if you wish to return to your toolbox </a>', extra_tags='safe')
                return redirect('.')
            else:
                return render(request, 'updatetool.html', RequestContext(request, {'updateform': updateform,
                                                                                   'blackoutform': blackoutform,
                                                                                   'tool': tooldata,
                                                                                   'futureRes': futureRes,
                                                                                   'sheds': sheds,
                                                                                   'blackoutdates': blackoutdates}))
        elif 'addblackout' in request.POST:
            updateform = AddToolForm(instance=tooldata)
            blackoutform = forms.BlackoutDateForm(tooldata, request.POST)
            if blackoutform.is_valid():
                blackoutform.save()
                messages.success(request,'Blackout dates have been added to this tool. <br> <br> '
                                         '<a href="/tool/toolbox">Click here if you wish to return to your toolbox </a>', extra_tags='safe')
                return redirect('.')
            else:
                return render(request, 'updatetool.html', RequestContext(request, {'updateform': updateform,
                                                                                   'blackoutform': blackoutform,
                                                                                   'tool': tooldata,
                                                                                   'futureRes': futureRes,
                                                                                   'sheds': sheds,
                                                                                   'blackoutdates': blackoutdates}))
        elif 'deactivate' in request.POST:
            updateform = AddToolForm(request.POST or None, request.FILES or None, instance=tooldata)
            blackoutform = forms.BlackoutDateForm(tooldata)
            if updateform.is_valid():
                tool = updateform.save(commit=False)
                tool.status = 'D'
                tool.save()
                updateform.save_m2m()
                messages.success(request,'Your tool has been deactivated. <br> <br> '
                                         '<a href="/tool/toolbox">Click here if you wish to return to your toolbox </a>', extra_tags='safe')
                return redirect('.')
            else:
                updateform = AddToolForm(instance=tooldata)
                return render(request, 'updatetool.html', RequestContext(request, {'updateform': updateform,
                                                                                   'blackoutform': blackoutform,
                                                                                   'tool': tooldata,
                                                                                   'futureRes': futureRes,
                                                                                   'sheds': sheds,
                                                                                   'blackoutdates': blackoutdates}))
        elif 'activate' in request.POST:
            updateform = AddToolForm(request.POST or None, request.FILES or None, instance=tooldata)
            blackoutform = forms.BlackoutDateForm(tooldata)
            if updateform.is_valid():
                tool = updateform.save(commit=False)
                tool.status = 'A'
                tool.save()
                updateform.save_m2m()
                messages.success(request,'Your tool was successfully activated! <br> <br> '
                                         '<a href="/tool/toolbox">Click here if you wish to return to your toolbox </a>', extra_tags='safe')
                return redirect('.')
            else:
                return render(request, 'updatetool.html', RequestContext(request, {'updateform': updateform,
                                                                                   'blackoutform': blackoutform,
                                                                                   'tool': tooldata,
                                                                                   'futureRes': futureRes,
                                                                                   'sheds': sheds,
                                                                                   'blackoutdates': blackoutdates}))
        elif 'delete' in request.POST:
            dateID = request.POST['delete']
            dateToDelete = blackoutdates.get(pk=dateID)
            dateToDelete.delete()
            messages.success(request,'Blackout date was successfully deleted! <br> <br> '
                                         '<a href="/tool/toolbox">Click here if you wish to return to your toolbox </a>', extra_tags='safe')
            return redirect('.')
    else:
        updateform = AddToolForm(instance=tooldata)
        blackoutform = forms.BlackoutDateForm(tooldata)
        return render(request, 'updatetool.html', RequestContext(request, {'updateform': updateform,
                                                                           'blackoutform': blackoutform,
                                                                           'tool': tooldata, 'futureRes': futureRes,
                                                                           'sheds': sheds,
                                                                           'blackoutdates': blackoutdates}))
Пример #4
0
def updateTool(request, tool_id):
    today = datetime.date.today()
    currentUser = request.user
    sharezone = currentUser.share_zone[:5]
    sheds = Shed.objects.filter(address__zip__startswith=sharezone)
    tooldata = get_object_or_404(Tool, pk=tool_id)
    denyAccess = True
    invalidImage = False

    # check if uploaded file is an image or not
    for key, value in request.FILES.items():
        if not 'image' in value.content_type:
            invalidImage = True

    if tooldata.owner == request.user:
        denyAccess = False

    if tooldata.location == 'S':
        if tooldata.shed.owner == request.user:
            denyAccess = False

    if denyAccess:
        error_url = reverse_lazy("toolManagement:toolbox")
        messages.error(request,
                       'Error! You do not have permission to edit this tool.',
                       extra_tags='safe')
        return redirect(error_url)

    # TODO : use tool is ready to move instead of futureres
    futureRes = tooldata.reservation_set.filter(Q(status='P') | Q(status='A'))
    unorderedDates = tooldata.blackoutdate_set.exclude(blackoutEnd__lt=today)
    blackoutdates = unorderedDates.order_by('blackoutStart')

    if request.method == 'POST':
        if 'updatetool' in request.POST:
            if invalidImage:
                updateform = AddToolForm(request.POST or None,
                                         instance=tooldata)
                messages.warning(
                    request,
                    'Tool picture was not updated because file was not an image.'
                )
            else:
                updateform = AddToolForm(request.POST or None,
                                         request.FILES or None,
                                         instance=tooldata)

            blackoutform = forms.BlackoutDateForm(tooldata)
            shedChoice = request.POST.get('shedChoice')
            if updateform.is_valid():
                tool = updateform.save(commit=False)
                if tool.location == 'S':
                    tool.shed = Shed.objects.get(pk=shedChoice)
                tool.save()
                updateform.save_m2m()

                #NOTE: the 'safe' extra_tag allows the string to be autoescaped so that links can be processed by the template.
                #It SHOULD NOT be used unless you need to add a hyperlink to your message!
                messages.success(
                    request, 'Your tool was successfully updated! <br> <br> '
                    '<a href="/tool/toolbox">Click here if you wish to return to your toolbox </a>',
                    extra_tags='safe')
                return redirect('.')
            else:
                return render(
                    request, 'updatetool.html',
                    RequestContext(
                        request, {
                            'updateform': updateform,
                            'blackoutform': blackoutform,
                            'tool': tooldata,
                            'futureRes': futureRes,
                            'sheds': sheds,
                            'blackoutdates': blackoutdates
                        }))
        elif 'addblackout' in request.POST:
            updateform = AddToolForm(instance=tooldata)
            blackoutform = forms.BlackoutDateForm(tooldata, request.POST)
            if blackoutform.is_valid():
                blackoutform.save()
                messages.success(
                    request,
                    'Blackout dates have been added to this tool. <br> <br> '
                    '<a href="/tool/toolbox">Click here if you wish to return to your toolbox </a>',
                    extra_tags='safe')
                return redirect('.')
            else:
                return render(
                    request, 'updatetool.html',
                    RequestContext(
                        request, {
                            'updateform': updateform,
                            'blackoutform': blackoutform,
                            'tool': tooldata,
                            'futureRes': futureRes,
                            'sheds': sheds,
                            'blackoutdates': blackoutdates
                        }))
        elif 'deactivate' in request.POST:
            updateform = AddToolForm(request.POST or None,
                                     request.FILES or None,
                                     instance=tooldata)
            blackoutform = forms.BlackoutDateForm(tooldata)
            if updateform.is_valid():
                tool = updateform.save(commit=False)
                tool.status = 'D'
                tool.save()
                updateform.save_m2m()
                messages.success(
                    request, 'Your tool has been deactivated. <br> <br> '
                    '<a href="/tool/toolbox">Click here if you wish to return to your toolbox </a>',
                    extra_tags='safe')
                return redirect('.')
            else:
                updateform = AddToolForm(instance=tooldata)
                return render(
                    request, 'updatetool.html',
                    RequestContext(
                        request, {
                            'updateform': updateform,
                            'blackoutform': blackoutform,
                            'tool': tooldata,
                            'futureRes': futureRes,
                            'sheds': sheds,
                            'blackoutdates': blackoutdates
                        }))
        elif 'activate' in request.POST:
            updateform = AddToolForm(request.POST or None,
                                     request.FILES or None,
                                     instance=tooldata)
            blackoutform = forms.BlackoutDateForm(tooldata)
            if updateform.is_valid():
                tool = updateform.save(commit=False)
                tool.status = 'A'
                tool.save()
                updateform.save_m2m()
                messages.success(
                    request, 'Your tool was successfully activated! <br> <br> '
                    '<a href="/tool/toolbox">Click here if you wish to return to your toolbox </a>',
                    extra_tags='safe')
                return redirect('.')
            else:
                return render(
                    request, 'updatetool.html',
                    RequestContext(
                        request, {
                            'updateform': updateform,
                            'blackoutform': blackoutform,
                            'tool': tooldata,
                            'futureRes': futureRes,
                            'sheds': sheds,
                            'blackoutdates': blackoutdates
                        }))
        elif 'delete' in request.POST:
            dateID = request.POST['delete']
            dateToDelete = blackoutdates.get(pk=dateID)
            dateToDelete.delete()
            messages.success(
                request, 'Blackout date was successfully deleted! <br> <br> '
                '<a href="/tool/toolbox">Click here if you wish to return to your toolbox </a>',
                extra_tags='safe')
            return redirect('.')
    else:
        updateform = AddToolForm(instance=tooldata)
        blackoutform = forms.BlackoutDateForm(tooldata)
        return render(
            request, 'updatetool.html',
            RequestContext(
                request, {
                    'updateform': updateform,
                    'blackoutform': blackoutform,
                    'tool': tooldata,
                    'futureRes': futureRes,
                    'sheds': sheds,
                    'blackoutdates': blackoutdates
                }))