Exemplo n.º 1
0
def search(request: Any) -> QuerySet:
    text = SearchForm(request.GET)
    if text.is_valid():
        # spell checker
        # correct_word = checker.Spellchecker(text)
        # items = Item.objects.filter(name=correct_word)
        return Item.objects.filter(
            Q(name__contains=text.cleaned_data.get('search'))
            | Q(description__contains=text.cleaned_data.get('search'))
            | Q(category__contains=text.cleaned_data.get('search')))
Exemplo n.º 2
0
    def get_context_data(self, **kwargs):
        try:
            text = SearchForm()
            # context = super(SearchListView, self).get_context_data(**kwargs)  # get the default context data
            text = SearchForm(self.request.GET)
            if text.is_valid():
                items = service.search(text.cleaned_data.get('search'))
            context = {}
            context['text'] = text
            context['object_list'] = items

            return context
        except DBFailedExceptionServiceToViews as e:
            messages.warning(self.request, e.msg)
            loger.warning(e.msg)
            return redirect('/login_redirect')
Exemplo n.º 3
0
def index(request: Any) -> HttpResponse:
    try:
        if service.len_of_super() == 0:
            return redirect('/super_user')
        return render(request, 'homepage_guest.html', {'text': SearchForm()})
    except DBFailedExceptionServiceToViews as e:
        messages.warning(request, e.msg)
        loger.warning(e.msg)
        return redirect('/login_redirect')
Exemplo n.º 4
0
def view_discounts(request, pk):
    try:
        discounts = service.get_discounts_serach(pk)
        text = SearchForm()
        context = {'text': text, 'discounts': discounts}
        return render(request, 'trading_system/view_discounts.html', context)
    except DBFailedExceptionServiceToViews as e:
        messages.warning(request, e.msg)
        loger.warning(e.msg)
        return redirect('/login_redirect')
Exemplo n.º 5
0
def show_cart(request: Any) -> HttpResponse:
    if request.user.is_authenticated:
        if "store_owners" in request.user.groups.values_list(
                'name', flat=True
        ) or "store_managers" in request.user.groups.values_list('name',
                                                                 flat=True):
            base_template_name = 'store/homepage_store_owner.html'
        else:
            base_template_name = 'homepage_member.html'
        return render(
            request, 'cart.html', {
                'user_name': request.user.username,
                'text': SearchForm(),
                'base_template_name': base_template_name
            })
Exemplo n.º 6
0
def login_redirect(request: Any) -> Union[HttpResponseRedirect, HttpResponse]:
    if request.user.is_authenticated:
        if "store_owners" in request.user.groups.values_list(
                'name', flat=True
        ) or "store_managers" in request.user.groups.values_list('name',
                                                                 flat=True):

            return redirect(
                '/store/home_page_owner/', {
                    'text': SearchForm(),
                    'user_name': request.user.username,
                    'owner_id': request.user.pk,
                })
        else:

            return render(request, 'homepage_member.html', {
                'text': SearchForm(),
                'user_name': request.user.username
            })

    # return render(request, 'homepage_member.html',
    #               {'text': text, 'user_name': user_name})

    return render(request, 'homepage_guest.html', {'text': SearchForm()})
Exemplo n.º 7
0
def def_super_user(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            u = form.save()
            u.is_superuser = True
            u.is_staff = True
            u.save()
            messages.success(request, 'add super-user ' + str(u.username))
            logev.info('add superuser successfully')
            return render(request, 'homepage_guest.html',
                          {'text': SearchForm()})

        else:
            return redirect('/super_user')

    else:
        return render(request, 'trading_system/add_super_user.html',
                      {'form': UserCreationForm()})
Exemplo n.º 8
0
def make_cart_list(request: Any) -> Union[HttpResponseRedirect, HttpResponse]:
    try:
        # if not (request.user.is_authenticated):
        # 	return makeGuestCart(request)
        items_bought = []
        if request.method == 'POST':
            form = CartForm(request.user, make_guest_cart(request),
                            request.POST)
            shipping_form = ShippingForm(request.POST)
            supply_form = PayForm(request.POST)

            if shipping_form.is_valid() and supply_form.is_valid():
                # shipping
                country = shipping_form.cleaned_data.get('country')
                city = shipping_form.cleaned_data.get('city')
                zip1 = shipping_form.cleaned_data.get('zip')
                address = shipping_form.cleaned_data.get('address')
                name = shipping_form.cleaned_data.get('name')

                shipping_details = {
                    'country': country,
                    'city': city,
                    'zip': zip1,
                    'address': address,
                    'name': name
                }

                # card

                card_number = supply_form.cleaned_data.get('card_number')
                month = supply_form.cleaned_data.get('month')
                year = supply_form.cleaned_data.get('year')
                holder = supply_form.cleaned_data.get('holder')
                cvc = supply_form.cleaned_data.get('cvc')
                id1 = supply_form.cleaned_data.get('id')

                card_details = {
                    'card_number': card_number,
                    'month': month,
                    'year': year,
                    'holder': holder,
                    'cvc': cvc,
                    'id': id1
                }
            else:
                err = '' + str(shipping_form.errors) + str(supply_form.errors)
                messages.warning(request, 'error in :  ' + err)
                loger.warning('buy from cart failed')
                return redirect('/login_redirect')

            if form.is_valid():
                list_of_items = []
                total_amount = 0
                items = form.cleaned_data.get('items')
                for item_id in items:
                    quantity_to_buy = 1
                    try:
                        quantity_to_buy = request.POST.get('quantity' +
                                                           item_id)
                        total_amount += int(quantity_to_buy)
                    # print('q----------------id:----' + str(item.id) + '------------' + quantity_to_buy)
                    except:
                        messages.warning(request, 'problem with quantity ')
                        loger.warning('buy from cart failed')

                    list_of_items.append({
                        'item_id': int(item_id),
                        'amount': int(quantity_to_buy)
                    })
                res, res_before = service.apply_discounts_for_cart(
                    list_of_items)
                for item_id in form.cleaned_data.get('items'):
                    amount_in_db = service.get_quantity(item_id)
                    if amount_in_db > 0:
                        item1 = Item.objects.get(id=item_id)
                        # quantity_to_buy = 1
                        # try:
                        # 	quantity_to_buy = request.POST.get('quantity' + str(item1.id))
                        # # print('q----------------id:----' + str(item.id) + '------------' + quantity_to_buy)
                        # except:
                        # 	messages.warning(request, 'problem with quantity ')
                        # 	loger.warning('buy from cart failed')

                        valid, total, total_after_discount, messages_ = service.buy_logic(
                            int(item_id), int(quantity_to_buy), amount_in_db,
                            request.user.is_authenticated,
                            request.user.username, shipping_details,
                            card_details, True, total_amount)
                        if not valid:
                            messages.warning(
                                request, 'can`t buy item : ' + str(item_id))
                            messages.warning(request,
                                             'reason : ' + str(messages_))
                            loger.warning('buy from cart failed')
                            return redirect('/login_redirect')

                    if request.user.is_authenticated:
                        cart = Cart.objects.get(customer=request.user)
                        cart.items.remove(item1)
                    else:
                        cart_g = request.session['cart']
                        cart_g['items_id'].remove(Decimal(item_id))
                        request.session['cart'] = cart_g

                messages.success(
                    request, ' total after discount : ' + str(res) +
                    " instead of: " + str(res_before))
                logev.info('buy form cart successfully')
                return redirect('/login_redirect')
            else:
                err = '' + str(form.errors)
                messages.warning(request, 'error in :  ' + err)
                loger.warning('buy from cart failed')
                return redirect('/login_redirect')

        else:
            list_ = []
            if not request.user.is_authenticated:
                base_template_name = 'homepage_guest.html'
                list_ = make_guest_cart(request)
            else:
                if "store_owners" in request.user.groups.values_list(
                        'name', flat=True):
                    base_template_name = 'store/homepage_store_owner.html'
                else:
                    base_template_name = 'homepage_member.html'
                list_ = []

            form = CartForm(request.user, list_)
            q_list = QForm(request.user, list_)
            text = SearchForm()
            user_name = request.user.username
            items_of_user = []
            if request.user.is_authenticated:
                carts = Cart.objects.filter(customer=request.user)
                for cart in carts:
                    items_of_user += list(cart.items.all())
            context = {
                'user_name': user_name,
                'text': text,
                'form': form,
                'base_template_name': base_template_name,
                'qua': q_list,
                'card': PayForm(),
                'shipping': ShippingForm(),
                'items_list': items_of_user + list_
            }
            return render(request, 'trading_system/cart_test.html', context)
    except DBFailedExceptionServiceToViews as e:
        messages.warning(request, e.msg)
        loger.warning(e.msg)
        return redirect('/login_redirect')