Esempio n. 1
0
    def post(self, request, username):
        u_redirect = email_and_search(request, 'recovery_confirm')
        if u_redirect is not None:
            return u_redirect

        context = generate_context(request)

        pass_key_confirm = request.POST.get('pass_key_confirm')
        user = get_user_model().objects.get(username=username)
        pass_key = VarificationCode.objects.get(user=user)

        context['username'] = username

        if pass_key_confirm == str(pass_key):
            request.session['_username'] = username
            return redirect('password_change')

        if pass_key.attempt_amount > 2:
            pass_key.delete()
            messages.error(request, 'Too many attempts')
            return redirect('recovery')

        pass_key.attempt_amount += 1
        pass_key.save()
        messages.error(request, 'Invalid code')

        return render(request, 'account/recovery-confirm.html', context)
Esempio n. 2
0
    def get(self, request):
        context = generate_context(request)
        user = request.user

        f_items = models.FavoriteItem.objects.filter(user=user)
        context['f_items'] = f_items

        return render(request, 'account/wishlist.html', context)
Esempio n. 3
0
    def get(self, request, order_id):
        context = generate_context(request)
        order = models.Order.objects.get(id=order_id)

        if order.user != request.user:
            return redirect('orders')

        context['order'] = order
        return render(request, 'order/order.html', context)
Esempio n. 4
0
    def get(self, request):
        context = generate_context(request)
        try:
            coupon = models.Coupon.objects.get(
                name=request.session.get('_coupon'))
        except models.Coupon.DoesNotExist:
            coupon = None
        context['coupon'] = coupon

        return render(request, 'cart/cart.html', context)
Esempio n. 5
0
    def get(self, request, item_id):
        print(request.user.has_perm('account.delete_shop_user'))
        context = generate_context(request)
        item = models.Item.objects.get(pk=item_id)
        comments = models.Comment.objects.filter(item=item)
        form = forms.CommentForm()

        context['item'] = item
        context['comments'] = comments
        context['form'] = form
        return render(request, 'catalog/comments.html', context)
Esempio n. 6
0
    def get(self, request):
        logout(request)
        context = generate_context(request)

        registration_form = forms.RegistrationForm()
        login_form = forms.LoginForm()

        context['registration_form'] = registration_form
        context['login_form'] = login_form

        return render(request, 'account/registration.html', context)
Esempio n. 7
0
    def get(self, request, searching=None, order=None, page=1):
        context = generate_context(request)
        try:
            ordering = ORDERING[order]
        except KeyError:
            ordering = 'name'

        categories = models.Category.objects.all().order_by('name')
        context['categories'] = categories

        context['ordering'] = ordering

        context['searching'] = searching

        if searching is not None and searching != '' and searching != 'None':
            # Фильтрация и упорядочивание поиска
            filtered_items = list(models.Item.objects.filter(name__icontains=searching).order_by(ordering))
            categories_qs = models.Category.objects.filter(name__icontains=searching)
            brands_qs = models.Brand.objects.filter(name__icontains=searching)
            categories_items = list(models.Item.objects.filter(category__in=categories_qs).order_by(ordering))
            brands_items = list(models.Item.objects.filter(brand__in=brands_qs).order_by(ordering))
            for item in brands_items + categories_items:
                if item not in filtered_items:
                    filtered_items.append(item)
        else:
            filtered_items = models.Item.objects.filter(featured=True)

        paginator = Paginator(filtered_items, 12)
        # Paginating
        try:
            items = paginator.page(page)
            context['items'] = items
            context['page'] = page
            context['next_page'] = page + 1
            context['previous_page'] = page - 1

        except PageNotAnInteger:
            items = paginator.page(1)
            context['items'] = items
            context['page'] = 1
            context['next_page'] = 2

        except EmptyPage:
            items = paginator.page(paginator.num_pages)
            context['items'] = items
            context['page'] = paginator.num_pages
            context['previous_page'] = paginator.num_pages - 1

        language = request.headers['Accept-Language'][:2]
        context['language'] = language

        return render(request, 'catalog/catalog.html', context)
Esempio n. 8
0
    def get(self, request):
        context = generate_context(request)

        form = forms.ChangePasswordForm()
        username = request.session.get('_username')

        if username is None:
            return redirect('registration')

        context['form'] = form
        context['username'] = username

        return render(request, 'account/password-change.html', context)
Esempio n. 9
0
    def post(self, request):
        u_redirect = email_and_search(request, 'recovery_confirm')
        if u_redirect is not None:
            return u_redirect

        context = generate_context(request)

        form = forms.ChangePasswordForm(request.POST)
        username = request.session.get('_username')

        context['form'] = form
        context['username'] = username

        if form.is_valid():
            new_password = form.cleaned_data['password']
            user = get_user_model().objects.get(username=username)
            user.set_password(new_password)
            user.save()
            return redirect('registration')

        return render(request, 'account/password-change.html', context)
Esempio n. 10
0
    def get(self, request, item_id):
        context = generate_context(request)

        item = models.Item.objects.get(id=item_id)
        context['item'] = item

        items = models.Item.objects.filter(featured=True)[:4]
        context['items'] = items

        context['marks'] = [5, 4, 3, 2, 1]

        comments = models.Comment.objects.filter(item=item)

        context_comments = []
        for comment in comments:
            if comment.comment is not None:
                context_comments.append(comment)

        context['comments'] = context_comments[:2]

        context['comments_amount'] = len(context_comments[:2])

        return render(request, 'catalog/item.html', context)
Esempio n. 11
0
    def post(self, request):
        u_redirect = email_and_search(request, 'registration')
        if u_redirect is not None:
            return u_redirect

        context = generate_context(request)

        login_form = forms.LoginForm(request.POST)
        registration_form = forms.RegistrationForm(request.POST)

        context['registration_form'] = registration_form
        context['login_form'] = login_form

        if login_form.is_valid():  # login
            data = login_form.cleaned_data
            user = authenticate(username=data['user_login'],
                                password=data['user_password'])
            if user is not None:
                login(request, user)
                return redirect('profile')
            else:
                messages.error(request, 'Incorrect password')

        if registration_form.is_valid():  # registration
            data = registration_form.cleaned_data
            try:
                user = get_user_model().objects.create_user(
                    username=data['username'],
                    email=data['mail'],
                    password=data['password'])
                user.save()
                return redirect('profile')
            except IntegrityError:
                messages.error(request,
                               'Пользователь с такими данными уже существует')

        return render(request, 'account/registration.html', context)
Esempio n. 12
0
 def get(self, request):
     context = generate_context(request)
     user = request.user
     form = forms.ProfileForm(instance=user)
     context['form'] = form
     return render(request, 'account/profile.html', context)
Esempio n. 13
0
    def get(self, request):
        context = generate_context(request)
        orders = Order.objects.filter(user=request.user)

        context['orders'] = orders
        return render(request, 'account/orders.html', context)
Esempio n. 14
0
 def get(self, request):
     context = generate_context(request)
     return render(request, 'order/checkout.html', context)
Esempio n. 15
0
 def get(self, request):
     context = generate_context(request)
     return render(request, 'account/recovery.html', context)
Esempio n. 16
0
 def get(self, request, username):
     context = generate_context(request)
     context['username'] = username
     return render(request, 'account/recovery-confirm.html', context)