示例#1
0
def user_email_verify(request, code):
    email_verify = EmailVerify.objects.get(user=request.user, token=code)
    if email_verify and email_verify.verify_date == None:
        email_verify.verify_date = timezone.now()
        email_verify.save()
        send_notification_by_system(request.user, 'Xác nhận email thành công.')
    return redirect('home:index')
示例#2
0
def change_status(request):
    id_order = request.POST.get("id_order")
    if request.POST.get("order_note") != "":
        note = request.POST.get("order_note")
    else:
        note = None

    try:
        with transaction.atomic():
            id_status = int(request.POST.get("change_status_" + id_order))
            HistoryOrderStatus.objects.create(order_id=id_order,
                                              order_status_id=id_status,
                                              created_date=timezone.now(),
                                              created_by=request.user,
                                              note=note)
            # lấy id_user của người order
            order_user_id = Order.objects.get(id=id_order).user_id
            order_user = User.objects.get(id=order_user_id)

            if id_status == 4:
                update_quantity_after_cancel_order(id_order)
                # gửi thông báo hủy kèm lý do
                send_notification_by_system(
                    order_user,
                    "Đơn hàng " + id_order + " đã bị hủy với lý do " + note)

            elif id_status == 2:
                # gửi thông báo dời lại kèm lý do
                send_notification_by_system(
                    order_user, "Đơn hàng " + id_order +
                    " đã bị dời lại với lý do " + note)
            elif id_status == 3:
                # giảm số lượng trong merchandise và kiểm tra còn hàng 0 sau khi giao thành công
                detail_order = DetailOrder.objects.filter(order_id=id_order)
                for item in detail_order:
                    merchandise = Merchandise.objects.get(
                        pk=item.merchandise_id)
                    merchandise.quantity -= item.quantity
                    if merchandise.quantity == 0:
                        merchandise.stopped_date = timezone.now()
                    merchandise.save()

                    # increase review times
                    try:
                        allow_rv = AllowedReviewTimes.objects.get(
                            user=order_user, merchandise=merchandise)
                        allow_rv.times += 1
                        allow_rv.save()
                    except:
                        AllowedReviewTimes.objects.create(
                            user=order_user, merchandise=merchandise, times=1)

    except DatabaseError as error:
        print(error)
示例#3
0
def user_register(request):
    form = RegistrationForm()
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = form.save()
            email_token = get_random_string(32)
            email_verify = EmailVerify(user = user, token = email_token)
            email_verify.save()
            send_mail_verification_code(form.cleaned_data['email'], email_token)
            send_notification_by_system(user, 'Chào mừng đến với Chợ Sách.')
            login(request, user)
            return redirect('home:index')
    return render(request, 'user/register.html', {'form': form})
示例#4
0
def user_info(request):
    info_form = UpdateProfile(instance=request.user)
    password_change_form = PasswordChangeForm(current_user = request.user)

    if request.method == 'POST':
        if 'submit_info_form' in request.POST:
            info_form = UpdateProfile(request.POST, instance=request.user)
            if info_form.is_valid():
                info_form.save()
                return redirect('user:info')
        if 'submit_password_change_form' in request.POST:
            password_change_form = PasswordChangeForm(request.POST, current_user=request.user)
            if password_change_form.is_valid():
                password_change_form.save()
                send_notification_by_system(request.user, 'Mật khẩu của bạn đã được cập nhật thành công.')
                return redirect('user:info')

    return render(request, 'user/info.html',
        {'info_form':info_form, 'password_change_form':password_change_form})
示例#5
0
 def response_change(self, request, obj):
     status_code = obj.get_merchandise_status()['code']
     if 'block' in request.POST and status_code in ['stopping', 'selling']:
         obj.blocked_date = timezone.now()
         obj.save()
         self.message_user(request, "Đã khóa sản phẩm.")
         send_notification_by_system(obj.user, "Sản phẩm có mã "+ str(obj.id) +" đã bị quản trị viên khóa")
         return HttpResponseRedirect(".")
     if 'pending_reject' in request.POST and status_code == 'pending':
         obj.blocked_date = timezone.now()
         obj.save()
         self.message_user(request, "Đã từ chối sản phẩm.")
         send_notification_by_system(obj.user, "Sản phẩm có mã "+ str(obj.id) +" đã bị quản trị viên từ chối")
         return HttpResponseRedirect(".")
     if 'pending_accept' in request.POST and status_code == 'pending':
         obj.activated_date = timezone.now()
         obj.activated_by = request.user
         obj.save()
         self.message_user(request, "Đã cho phép bán sản phẩm.")
         send_notification_by_system(obj.user, "Sản phẩm có mã "+ str(obj.id) +" đã được phê duyệt")
         return HttpResponseRedirect(".")
     
     return super().response_change(request, obj)
示例#6
0
def check_out(request):
    #get cart item
    cart_items = list(
        Cart.objects.raw(
            '''
        select `cart`.`id`, `book`.`name`, `cart`.`quantity`, `m`.`id` `merchandise_id`, `m`.`price`
        from `cart` join `merchandise` `m` join `book`
        where `cart`.`id_merchandise` = `m`.`id` AND `m`.`id_product` = `book`.`id`
            AND `cart`.`id_user` = %s
        group by `cart`.`id`;
    ''', [str(request.user.id)]))

    if len(cart_items) == 0:
        return redirect('cart:get_cart')

    #subtotal
    sub_total = 0
    for i in cart_items:
        if not Merchandise.objects.get(pk=i.merchandise_id).is_selling():
            return redirect_404
        sub_total += i.price * i.quantity

    # get payment method
    payment = Payment.objects.all()
    # get delivery method
    delivery = Delivery.objects.all()

    # check_out exceed
    if request.method == 'POST':
        #lấy địa chỉ giao hàng
        shipping_address_id = request.POST.get("pro-idaddress")
        # split order
        store_address = Cart.objects.raw(
            '''
            select distinct c.id, m.id_address
            from merchandise as m join cart as c 
            where c.id_user = %s and m.id=c.id_merchandise
            group by m.id_address;
        ''', str(request.user.id))

        # transaction
        try:
            with transaction.atomic():
                # create order with each store_address
                for i in store_address:
                    cart = Cart.objects.raw(
                        '''
                        select m.id, c.quantity, m.price
                        from cart as c join merchandise as m
                        where m.id = c.id_merchandise and
                            m.id_address = %s and c.id_user = %s
                    ''', [str(i.id_address),
                          str(request.user.id)])
                    new_order = Order.objects.create(
                        user_id=request.user.id,
                        address_id=shipping_address_id,
                        payment_id=request.POST.get("payment"),
                        delivery_id=request.POST.get("delivery"),
                        fee_delivery=0,
                        created_date=timezone.now())

                    # create Detail of each order
                    for item in cart:
                        DetailOrder.objects.create(
                            order_id=new_order.pk,
                            merchandise_id=item.id,
                            quantity=item.quantity,
                            total_price=item.price * item.quantity,
                            total_price_after_discount=item.price *
                            item.quantity)
                        # udate quantity_exists
                        merchandise = Merchandise.objects.get(pk=item.id)
                        merchandise.quantity_exists -= item.quantity
                        merchandise.save()

                    # create history_status gor current order
                    HistoryOrderStatus.objects.create(
                        order_id=new_order.pk,
                        order_status_id=1,
                        created_date=timezone.now(),
                        created_by=request.user)

                # delete all cart objects after check_out
                Cart.objects.filter(user=request.user.id).delete()

                send_notification_by_system(request.user,
                                            'Cảm ơn bạn đã đặt hàng.')
        except DatabaseError as error:
            print(error)

        return redirect('user:order')

    # get user address
    address = Address.objects.filter(user_id=request.user.id, delete_date=None)
    return render(
        request, 'order/check_out.html', {
            'cart': cart_items,
            'sub_total': sub_total,
            'payment': payment,
            'delivery': delivery,
            'address': address
        })