Ejemplo n.º 1
0
def action_form(request, form_name, object_type):
    if not request.user.customer:
        return HttpResponseRedirect(reverse('admin_index'))

    if request.POST:
        logger.info('%s is using a form action (%s)' % (request.user, object_type))
        form = form_name(data=request.POST,
                         customer=request.user.customer)
        if form.is_valid():
            obj_dict = form.save()
            response = {'status': 'success',
                        'response': make_message(obj_dict)}
            logger.info('Action resulted in %s' % response['response'])
        else:
            logger.info('Action not succesful')
            context = {'form': form,
                       'object_type': object_type}
            response = {'status': 'failure',
                        'response': render_to_string('form.html', RequestContext(request, context))}
		
        return HttpResponse(simplejson.dumps(response), 
                            mimetype="application/json")

    form = form_name(customer=request.user.customer)
    context = {'form': form, 'object_type': object_type}
    return render(request, 'form.html', context)
Ejemplo n.º 2
0
def action(request, class_name):
    if not request.user.customer:
        return HttpResponseRedirect(reverse('admin_index'))

    action = request.POST.get('action')
    object_ids = request.POST.get('object_ids')

    logger.info('%s is attempting an action (%s on %s)' % (request.user, action, object_ids))

    if object_ids:
        object_ids = object_ids.split(',')
    else:
        response = {'response': 'Ingen objekter valgt'}
        return HttpResponse(simplejson.dumps(response),
                            content_type='application/json')

    obj_dict = {}

    for object_id in object_ids:
        obj = get_object_or_404(class_name, pk = object_id)
        obj_name = obj.name

        if action == 'loan_single':
            response = obj.loan(request.user)
            try:
                obj_dict[response].append(obj_name)
            except KeyError:
                obj_dict[response] = [obj_name]
        elif action == 'delete':
            if request.user.is_admin:
                obj.delete()
                try:
                    obj_dict[MESSAGES.OBJECT_DELETE_SUCCESS].append(obj_name)
                except KeyError:
                    obj_dict[MESSAGES.OBJECT_DELETE_SUCCESS] = [obj_name]
            else:
                try:
                    obj_dict[MESSAGES.OBJECT_DELETE_RIGHTS].append(obj_name)
                except KeyError:
                    obj_dict[MESSAGES.OBJECT_DELETE_RIGHTS] = [obj_name]
        else:
            action_function = getattr(obj, action)
            response = action_function(request.user)
            try:
                obj_dict[response].append(obj_name)
            except KeyError:
                obj_dict[response] = [obj_name]

    response = {'response': make_message(obj_dict)}

    logger.info('Action resulted in %s' % response['response'])

    return HttpResponse(simplejson.dumps(response), 
                        content_type="application/json")
Ejemplo n.º 3
0
def qr_action(request, pk):
    if not request.user.customer:
        return HttpResponseRedirect(reverse('admin_index'))

    if request.user.is_authenticated():
        if request.user.is_admin:
            tool = get_object_or_404(Tool, id=pk)
            if tool.location == 'Lager':
                if request.POST:
                    logger.info('%s is attempting a QR action' % request.user)
                    form = QRLoanForm(tool=tool, data=request.POST)
                    if form.is_valid():
                        obj_dict = form.save()

                        context = {'message': make_message(obj_dict)}

                        logger.info('Action resulted in %s' % context['message'])

                        if MESSAGES.TOOL_LOAN_SUCCESS in obj_dict:
                            context['status'] = 'success'
                        else:
                            context['status'] = 'failure'

                        return render(request, 'qr/success.html', context)
                else:
                    form = QRLoanForm(tool=tool)
            
                context = {'form': form}
                return render(request, 'qr/form.html', context)

            elif tool.location == 'Udlånt' or tool.location == 'Reparation':
                logger.info('%s is returning %s via QR' % (request.user, tool.name))
                tool.end_loan(request.user)
                context = {'message': 'Værktøjet blev afleveret',
                           'status': 'success'}
                return render(request, 'qr/success.html', context)
            else:
                logger.info('%s is returning %s via QR, but the tool is scrapped or lost' % (request.user, tool.name))
                context = {'message': 'Værktøjet kan ikke udlånes, da det er kasseret eller bortkommet',
                           'status': 'failure'}
                return render(request, 'qr/success.html', context)

        else:
            tool = get_object_or_404(Tool, id=pk)
            if tool.location == 'Lager':
                tool.loan(request.user) 
                logger.info('%s is loaning %s via QR' % (request.user, tool.name))
                context = {'message': 'Værktøjet blev udlånt til %s' % request.user,
                           'status': 'success'}
                return render(request, 'qr/success.html', context)
            elif tool.location == 'Udlånt' or tool.location == 'Reparation':
                if tool.employee == request.user:
                    tool.end_loan(request.user)
                    logger.info('%s is returning %s via QR' % (request.user, tool.name))
                    context = {'message': 'Værktøjet blev afleveret',
                               'status': 'success'}
                else:
                    logger.info('%s is returning %s via QR, but it fails as he can\' return tools for other employees' % (request.user, tool.name))
                    context = {'message': 'Du har ikke rettigheder til at aflevere værktøj udlånt til andre',
                               'status': 'failure'}
                return render(request, 'qr/success.html', context)
            else:
                logger.info('%s is loaning %s via QR, but it fails as it is lost or scrapped' % (request.user, tool.name))
                context = {'message': 'Værktøjet kan ikke udlånes, da det er kasseret eller bortkommet',
                           'status': 'failure'}
                return render(request, 'qr/success.html', context)

    else:
        if request.POST:
            authentication_form = AuthenticationForm(data=request.POST)
            if authentication_form.is_valid():
                user = authentication_form.get_user()
                login(request, user)
                return HttpResponseRedirect('/tools/%s/qr_action' % pk)
        else:
            authentication_form = AuthenticationForm()

        context_dictionary = {'authentication_form': authentication_form}
        return render(request, 'qr/login.html', context_dictionary)