Example #1
0
def update_order(request, pk):
    user = request.user
    order = get_object_or_404(Order, id=pk)

    if request.method == 'POST':
        print("Updated Order form submitted")
        form = NewOrderForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            print("Updated Order form Validated")

            prv_order = order
            try:
                order.design = request.FILES['design']
                file_type = order.design.url.split('.')[-1]
                file_type = file_type.lower()
                if file_type not in DESIGN_FILE_TYPES:
                    messages.error(request, 'Image file must be .zip, .rar or .gz')
                    return render(request, 'service/update_order.html', {'form': form, 'order':order})
                # order.design.url = str(datetime.now())+file_type
            except MultiValueDictKeyError:
                None

            try:
                client_id = request.POST['client_username']
                client_id = int(client_id)
            except Exception:
                client_id = -1

            shipping_address = form.cleaned_data.get('shipping_address')
            client_address = form.cleaned_data.get('client_address')
            if client_id > 0:
                if not User.objects.filter(id=client_id):
                    messages.error(request, 'Client with given username doesn\'t exist.')
                    return render(request, 'service/update_order.html', {'form': form, 'order':order})

                client = User.objects.get(id=client_id)
                if client.profile.account_type != 0:
                    messages.error(request, 'The given client username is not of a client.')
                    if not shipping_address and client_address is True:
                        messages.error(request, 'Ship is client address is invalid here.')
                    return render(request, 'service/update_order.html', {'form': form, 'order':order})
                order.client = client
                if not shipping_address or client_address is True:
                    order.shipping_address = str(client.profile.address) + ", " + str(client.profile.city) + ", " \
                                             + str(client.profile.state) + ", " + str(client.profile.country) + ", " \
                                             + str(client.profile.zip_code)
                else:
                    order.shipping_address = shipping_address
            else:
                if client_address is True:
                    messages.error(request, 'Ship in client address is invalid here.')
                    return render(request, 'service/update_order.html', {'form': form, 'order':order})
                else:
                    order.shipping_address = shipping_address

            # design = form.cleaned_data.get('design')
            # order.design = "Hello"

            order.submitted_by = user
            order.approved_by = user
            order.approved = 1
            order.review_note = request.POST['review_note']
            order.client_name = form.cleaned_data.get('client_name')
            order.order_type = form.cleaned_data.get('order_type')
            order.deadline = form.cleaned_data.get('deadline')
            order.quantity = form.cleaned_data.get('quantity')
            order.budget = form.cleaned_data.get('budget')
            order.specification = form.cleaned_data.get('specification')
            order.order_status = form.cleaned_data.get('order_status')
            order.save()

            if order.progress==100 and OrderHistory.objects.filter(Q(order=order)
                                                                   & Q(progress=100)).count() ==0:
                train_order(order)

            # order_history = OrderHistory(order)
            # order_history.save()
            order_history = OrderHistory()
            order_history.copy(order)
            order_history.save()

            if prv_order.approved == 0:
                # generate notification for production manager
                msg = "Your changes in Order {0} has been discarded by {1}".format(order.id,
                                                     request.user.profile.get_screen_name())
                _recipient = prv_order.submitted_by
                print ("notify to ")
                print(_recipient)
                notify.send(request.user, recipient=_recipient, verb=msg, action_object=order)

            messages.success(request, 'The order is updated successfully.')
        else:
            messages.error(request, 'Order save failed.')
            return render(request, 'service/update_order.html', {'form': form, 'order':order})

    form = NewOrderForm(instance=Order, initial={
        'client_name': order.client_name,
        'order_type' : order.order_type,
        'deadline' : order.deadline,
        'quantity' : order.quantity,
        'budget' : order.budget,
        'shipping_address' : order.shipping_address,
        'specification' : order.specification,
        'order_status' : order.order_status
    })
    # print(form)
    return render(request, 'service/update_order.html', {'form': form, 'order':order})
Example #2
0
def update_order(request, pk):
    user = request.user
    order = get_object_or_404(Order, id=pk)

    if user.employee.manager_id is None:
        messages.error(request,
                       'Please be assigned to a Service Manager to continue.')
        form = NewOrderForm()
        return render(request, 'dashboard/new_order.html', {'form': form})

    if request.method == 'POST':
        print("Updated Order form submitted")
        form = NewOrderForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            print("Updated Order form Validated")

            prv_order = order
            try:
                order.design = request.FILES['design']
                file_type = order.design.url.split('.')[-1]
                file_type = file_type.lower()
                if file_type not in DESIGN_FILE_TYPES:
                    messages.error(request,
                                   'Image file must be .zip, .rar or .gz')
                    return render(request, 'dashboard/update_order.html', {
                        'form': form,
                        'order': order
                    })
                # order.design.url = str(datetime.now())+file_type
            except MultiValueDictKeyError:
                None

            try:
                client_id = request.POST['client_username']
                client_id = int(client_id)
            except Exception:
                client_id = -1

            shipping_address = form.cleaned_data.get('shipping_address')
            client_address = form.cleaned_data.get('client_address')
            if client_id > 0:
                if not User.objects.filter(id=client_id):
                    messages.error(
                        request, 'Client with given username doesn\'t exist.')
                    return render(request, 'dashboard/update_order.html', {
                        'form': form,
                        'order': order
                    })

                client = User.objects.get(id=client_id)
                if client.profile.account_type != 0:
                    messages.error(
                        request,
                        'The given client username is not of a client.')
                    if not shipping_address and client_address is True:
                        messages.error(
                            request, 'Ship is client address is invalid here.')
                    return render(request, 'dashboard/update_order.html', {
                        'form': form,
                        'order': order
                    })
                order.client = client
                if not shipping_address or client_address is True:
                    order.shipping_address = str(client.profile.address) + ", " + str(client.profile.city) + ", " \
                                             + str(client.profile.state) + ", " + str(client.profile.country) + ", " \
                                             + str(client.profile.zip_code)
                else:
                    order.shipping_address = shipping_address
            else:
                if client_address is True:
                    messages.error(request,
                                   'Ship in client address is invalid here.')
                    return render(request, 'dashboard/update_order.html', {
                        'form': form,
                        'order': order
                    })
                else:
                    order.shipping_address = shipping_address

            # design = form.cleaned_data.get('design')
            # order.design = "Hello"

            order.submitted_by = user
            order.client_name = form.cleaned_data.get('client_name')
            order.order_type = form.cleaned_data.get('order_type')
            order.deadline = form.cleaned_data.get('deadline')
            order.quantity = form.cleaned_data.get('quantity')
            order.budget = form.cleaned_data.get('budget')
            order.specification = form.cleaned_data.get('specification')
            order.order_status = form.cleaned_data.get('order_status')
            order.save()

            order_history = OrderHistory()
            order_history.copy(order)
            order_history.save()

            if order.client is not None:
                msg = "A production manager updated your order with id:{0}".format(
                    order.id)
                _recipient = order.client
                notify.send(user,
                            recipient=_recipient,
                            verb=msg,
                            action_object=order)

            msg = "Officer :{0} added updated  the order with id:{1}".format(
                user.profile.get_screen_name(), order.id)
            _recipient = user.employee.manager.user
            notify.send(user,
                        recipient=_recipient,
                        verb=msg,
                        action_object=order)
            messages.success(request, 'The order is updated successfully.')
        else:
            messages.error(request, 'Order save failed.')
            return render(request, 'dashboard/update_order.html', {
                'form': form,
                'order': order
            })

    form = NewOrderForm(instance=Order,
                        initial={
                            'client_name': order.client_name,
                            'order_type': order.order_type,
                            'deadline': order.deadline,
                            'quantity': order.quantity,
                            'budget': order.budget,
                            'shipping_address': order.shipping_address,
                            'specification': order.specification,
                            'order_status': order.order_status
                        })
    # print(form)
    return render(request, 'dashboard/update_order.html', {
        'form': form,
        'order': order
    })
Example #3
0
def new_order(request):
    user = request.user

    if request.method == 'POST':
        print("NewOrderForm form submitted")
        form = NewOrderForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            print("NewOrderForm form Valid")
            # order = Order()
            order = form.save(commit=False)
            try:
                order.design = request.FILES['design']
                file_type = order.design.url.split('.')[-1]
                file_type = file_type.lower()
                if file_type not in DESIGN_FILE_TYPES:
                    messages.error(request, 'Image file must be .zip, .rar or .gz')
                    return render(request, 'service/new_order.html', {'form': form})
            except MultiValueDictKeyError:
                None

            try:
                client_id = request.POST['client_username']
                client_id = int(client_id)
            except Exception:
                client_id = -1

            shipping_address = form.cleaned_data.get('shipping_address')
            client_address = form.cleaned_data.get('client_address')
            if client_id > 0:
                if not User.objects.filter(id=client_id):
                    messages.error(request, 'Client with given username doesn\'t exist.')
                    return render(request, 'service/new_order.html', {'form': form})

                client = User.objects.get(id=client_id)
                if client.profile.account_type != 0:
                    messages.error(request, 'The given client username is not of a client.')
                    if not shipping_address and client_address is True:
                        messages.error(request, 'Ship is client address is invalid here.')
                    return render(request, 'service/new_order.html', {'form': form})
                order.client = client
                if not shipping_address or client_address is True:
                    order.shipping_address = str(client.profile.address) + ", " + str(client.profile.city) + ", " \
                                             + str(client.profile.state) + ", " + str(client.profile.country) + ", " \
                                             + str(client.profile.zip_code)
                else:
                    order.shipping_address = shipping_address
            else:
                if client_address is True:
                    messages.error(request, 'Ship in client address is invalid here.')
                    return render(request, 'service/new_order.html', {'form': form})
                else:
                    order.shipping_address = shipping_address

            order.submitted_by = user
            order.approved_by = user
            order.approved = 1
            order.review_note = request.POST['review_note']
            order.client_name = form.cleaned_data.get('client_name')
            order.order_type = form.cleaned_data.get('order_type')
            order.deadline = form.cleaned_data.get('deadline')
            order.quantity = form.cleaned_data.get('quantity')
            order.budget = form.cleaned_data.get('budget')
            order.specification = form.cleaned_data.get('specification')
            order.order_status = form.cleaned_data.get('order_status')
            order.save()

            if order.progress==100 and OrderHistory.objects.filter(Q(order=order)
                                                                   & Q(progress=100)).count() ==0:
                train_order(order)

            # order_history = OrderHistory(order)
            # order_history.save()

            order_history = OrderHistory()
            order_history.copy(order)
            order_history.save()

            if order.client is not None:
                msg = "An officer created a new order with id:{0} for you".format(order.id)
                _recipient = order.client
                notify.send(user, recipient=_recipient, verb=msg, action_object=order)

            messages.success(request, 'The order is saved successfully.')
        else:
            messages.error(request, 'Order save failed.')
            return render(request, 'service/new_order.html', {'form': form})
    form = NewOrderForm()
    return render(request, 'service/new_order.html', {'form': form})
Example #4
0
def update_order(request, pk):
    user = request.user
    order = get_object_or_404(Order, id=pk)

    if request.method == 'POST':
        print("Updated Order form submitted")
        form = NewOrderForm(request.POST or None, request.FILES or None)
        if form.is_valid():
            print("Updated Order form Validated")

            prv_order = order
            try:
                order.design = request.FILES['design']
                file_type = order.design.url.split('.')[-1]
                file_type = file_type.lower()
                if file_type not in DESIGN_FILE_TYPES:
                    messages.error(request, 'Image file must be .zip, .rar or .gz')
                    return render(request, 'client/update_order.html', {'form': form, 'order':order})
                # order.design.url = str(datetime.now())+file_type
            except MultiValueDictKeyError:
                None

            shipping_address = form.cleaned_data.get('shipping_address')
            client_address = form.cleaned_data.get('client_address')

            order.client = user
            if not shipping_address or client_address is True:
                order.shipping_address = str(user.profile.address) + ", " + str(user.profile.city) + ", " \
                                         + str(user.profile.state) + ", " + str(user.profile.country) + ", " \
                                         + str(user.profile.zip_code)
            else:
                order.shipping_address = shipping_address

            order.submitted_by = user
            order.client_name = user.profile.get_screen_name()
            order.order_type = form.cleaned_data.get('order_type')
            order.deadline = form.cleaned_data.get('deadline')
            order.quantity = form.cleaned_data.get('quantity')
            order.budget = form.cleaned_data.get('budget')
            order.specification = form.cleaned_data.get('specification')
            order.order_status = prv_order.order_status
            order.save()

            # order_history = OrderHistory(order)
            # order_history.save()

            order_history = OrderHistory()
            order_history.copy(order)
            order_history.save()

            msg = "Client :{0} updated the order with id:{1}".format(
                request.user.profile.get_screen_name(), order.id)
            _recipient = User.objects.filter(Q(profile__account_type=1) | Q(profile__account_type=2))
            print(_recipient)
            notify.send(request.user, recipient=_recipient, verb=msg, action_object=order)
            messages.success(request, 'The order is updated successfully.')
        else:
            messages.error(request, 'Order save failed.')
            return render(request, 'client/update_order.html', {'form': form, 'order':order})

    form = NewOrderForm(instance=Order, initial={
        'order_type' : order.order_type,
        'deadline' : order.deadline,
        'quantity' : order.quantity,
        'budget' : order.budget,
        'shipping_address' : order.shipping_address,
        'specification' : order.specification,
    })
    # print(form)
    return render(request, 'client/update_order.html', {'form': form, 'order':order})