Django如何发送表单发布数据以供审批?

时间:2018-07-18 14:15:13

标签: django django-forms django-views

我正在为客户准备一份注册表,希望由我们的员工批准客户。如果他满足要求,那么我希望他的所有详细信息都显示在我的员工页面中,该页面将检查并批准他的表单详细信息。我不知道该怎么办?我是新手,请帮助我。 在我的模型中,我使用客户,客户将预订这辆车后,我要向我的员工站点显示我要预订的那辆车的所有详细信息,从而限制了雇员页面登录,只有公司员工才能登录。因此,请帮我,我希望将所有表格详细信息发送到我的员工页面,在该页面上,他可以查看哪个客户已注册并预定了汽车。如果他单击“批准”按钮,则会确认发送给该客户的电子邮件,表明您已完成预订。

models.py

class Booking(models.Model):
    class Meta():
        db_table = "booking"
        verbose_name = "Booking"
        verbose_name_plural = "Bookings"
        ordering = ['-booking_date']

    booking_car_car = models.ForeignKey(
        Car,
        on_delete=models.CASCADE,
        related_name='booking_car_car_key'
    )
    booking_customer_customer = models.ForeignKey(
        Customer,
        on_delete=models.CASCADE,
        related_name='booking_customer_customer'
    )
    booking_start_date = models.DateField(
        blank=False,
        null=False
    )
    booking_end_date = models.DateField(
        blank=False,
        null=False
    )
    booking_total_price = models.IntegerField(
        blank=False,
        null=False
    )
    booking_approved = models.NullBooleanField(
        blank=True,
        null=True
    )
    booking_date = models.DateTimeField(
        auto_now_add=True,
        blank=False,
        null=False
    )

    def __str__(self):
        return self.booking_customer_customer.customer_firstname
class Customer(models.Model):
    class Meta():
        db_table = "customer"
        verbose_name = "Customer"
        verbose_name_plural = "Customers"
        ordering = ['customer_firstname', 'customer_lastname']
    def get_customer_image_path_primary(self, filename):
        path = ''.join([str('static/img/customer/') + str(self.id) + str('/license/primary/'), slugify(filename) + str('.jpg')])
        return path

    customer_firstname = models.CharField(
        max_length=64,
        blank=False,
        null=False
    )
    customer_lastname = models.CharField(
        max_length=64,
        blank=False,
        null=False
    )
    customer_age = models.IntegerField(
        blank=False,
        null=False
    )
    customer_idno = models.IntegerField(
        blank=False,
        null=False,
    )
    customer_license_expire = models.DateField(
        blank=False,
        null=False)
    customer_phone = models.IntegerField(
        blank=False,
        null=False)
    customer_email = models.EmailField(
        blank=False
    )

def __str__(self):
    return self.customer_firstname


class BookingApproval(models.Model):
    book_customer_name = models.CharField(
        max_length=64,
        blank=False,
        null=False
    )
    book_customer_lastname = models.CharField(
        max_length=64,
        blank=False,
        null=False
    )
    book_customer_age = models.IntegerField(
        blank=False,
        null=False
        )
    book_customer_idno = models.IntegerField(
        blank=False,
        null=False,
    )
    book_customer_license_expire = models.DateField(
        blank=False,
        null=False
        )
    book_customer_phone =models.IntegerField(
        blank=False,
        null=False
        )
    book_customer_email = models.EmailField(
        blank =False
        )
    book_booking_start_date = models.DateTimeField(
        auto_now_add=False,
        blank=False,
        null=False
    )
    book_booking_end_date=models.DateTimeField(
        auto_now_add=False,
        blank=False,
        null=False
    )
    book_booking_car_car = models.ForeignKey(
        Car,
        on_delete=models.CASCADE,
        related_name='book_booking_car_car_key'
    )
    book_booking_total_price = models.IntegerField(
        blank=False,
        null=False
    )
    book_unique_id = models.UUIDField(
        default=uuid.uuid4,
        editable=False,
        unique=True)
    book_approval_confirmed = models.BooleanField(default=False)

views.py

def booking_save(request):
    args = {}
    if request.POST:
        book_customer_name = request.POST.get('book_customer_name')
        book_customer_lastname = request.POST.get('book_customer_lastname')
        book_customer_age = request.POST.get('book_customer_age')
        book_customer_idno = request.POST.get('book_customer_idno')
        book_customer_license_expire = request.POST.get('book_customer_license_expire')
        book_customer_phone = request.POST.get('book_customer_phone')
        book_customer_email = request.POST.get('book_customer_email')
        pre_book_booking_start_date = request.POST.get('book_booking_start_date')
        pre_book_booking_end_date = request.POST.get('book_booking_end_date')
        book_booking_start_date = str(pre_book_booking_start_date[0:10]) + str(' ') + str(pre_book_booking_start_date[11:])
        book_booking_end_date = str(pre_book_booking_end_date[0:10]) + str(' ') + str(pre_book_booking_end_date[11:])
        book_booking_car_car = request.POST.get('book_booking_car_car')
        booking_save_form = BookingApprovalForm(request.POST)
        date_format = "%Y-%m-%d"
        a = datetime.strptime(str(book_booking_start_date[0:10]), date_format)
        b = datetime.strptime(str(book_booking_end_date[0:10]), date_format)
        days_delta = b - a
        car_price = Car.objects.get(id__exact=book_booking_car_car)
        total_price = int(car_price.car_price) * int(days_delta.days)

        if booking_save_form.is_valid():
            booking_approval = booking_save_form.save(commit=False)
            booking_approval.book_customer_name = book_customer_name
            booking_approval.book_customer_lastname = book_customer_lastname
            booking_approval.book_customer_age = book_customer_age
            booking_approval.book_customer_idno = book_customer_idno
            booking_approval.book_customer_license_expire = book_customer_license_expire
            booking_approval.book_customer_phone = book_customer_phone
            booking_approval.book_customer_email = book_customer_email
            booking_approval.book_booking_start_date = book_booking_start_date
            booking_approval.book_booking_end_date = book_booking_end_date
            booking_approval.book_booking_car_car_id = book_booking_car_car
            booking_approval.book_booking_total_price = total_price
            booking_approval.save()
            messages.success(request, 'Booking is placed.')
            template = 'buggy_app/bookinfo.html'
            args['booking'] = booking_approval
            return render(request, template, args)
        else:
            return HttpResponseRedirect('/')
    else:
        return HttpResponseRedirect('/')

def booking_confirm(request):
    args = {}
    if request.POST:
        uuid = request.POST.get('book_unique_id')
        clear_uuid = uuid.replace('-', '')
        try:
            prebook = BookingApproval.objects.get(book_unique_id=request.POST.get('book_unique_id'))
        except:
            prebook = None
        if prebook is not None:
            customer_save_form = CustomerForm(request.POST)
            if customer_save_form.is_valid():
                customer = customer_save_form.save(commit=False)
                customer.customer_firstname = prebook.book_customer_name
                customer.customer_lastname = prebook.book_customer_lastname
                customer.customer_age = prebook.book_customer_age
                customer.customer_idno = prebook.book_customer_idno
                customer.customer_license_expire = prebook.book_customer_license_expire
                customer.customer_phone = prebook.book_customer_phone
                customer.customer_email = prebook.book_customer_email
                customer.save()
                try:
                    saved_customer = Customer.objects.get(id=customer.id)
                except:
                    saved_customer = None
                if saved_customer is not None:
                    booking_save_form = BookingForm(request.POST)
                    if booking_save_form.is_valid():
                        booking = booking_save_form.save(commit=False)
                        booking.booking_car_car = prebook.book_booking_car_car#Complete all fields for Booking model (except approved) and sir for car? ok because its in prebook . and car is foregn key in it . right sir
                        booking.booking_customer_customer = customer #taking customer that we saved all his details, sure it's a foreign key!
                        booking.booking_start_date = prebook.book_booking_start_date
                        booking.booking_end_date = prebook.book_booking_end_date
                        booking.booking_total_price = prebook.book_booking_total_price
                        booking_save_form.save()
                        return render(request, 'buggy_app/success_message.html', args)#Template which says that booking successful
                    else:
                        return HttpResponseRedirect('/')#Thd response page, that says that form is incorrect
                else:
                    return HttpResponseRedirect('/123')#Thd response page, that says that form is incorrect
            else:
                return HttpResponseRedirect('/')#Thd response page, that says that form is incorrect
        else:
            return HttpResponseRedirect('/'+str(prebook))#Thd response page, that says that form is incorrect
    else:
        return HttpResponseRedirect('/')

0 个答案:

没有答案