def sale_add_view(request): get_permission(request) if request.method == 'POST': user = request.user sale_form = SaleForm(request.POST) if sale_form.is_valid(): sal = ProductSale() sal.promo = sale_form.cleaned_data['promo'] sal.sale = sale_form.cleaned_data['sale'] sal.save() sale_response = render_template('shopadmin_sale.html', {'sal': sal}, request) ctx = { 'result': 'success', 'sale_response': sale_response, 'message': message("Промокод добавлен!", request) } return JsonResponse(ctx) else: sale_form_response = render_template('shopadmin_sale_form.html', {'sale_form': sale_form}, request) return JsonResponse({ 'result': 'error', 'sale_form_response': sale_form_response })
def update(request, categorys, ctx): get_permission(request) category_response = render_template('shopadmin_category_main.html', {'category': categorys}, request) category_form_response = render_template('shopadmin_category_form.html', {'category_form': CategoryForm()}, request) EmptySizeFormSet = modelformset_factory(ProductSize, form=SizeForm, can_delete=True, extra=3) empty_size_formset = EmptySizeFormSet(queryset=ProductSize.objects.none(), prefix="empty_form") product_form_response = render_template('shopadmin_product_form.html', { 'empty_size_formset': empty_size_formset, 'product_form': ProductForm() }, request) products = Product.objects.all() main_product_response = render_template('shopadmin_product_main.html', {'products': products}, request) ctx.update({ 'main_product_response': main_product_response, 'product_form_response': product_form_response, 'category_form_response': category_form_response, 'category_response': category_response, })
def category_edit_view(request): get_permission(request) if request.method == 'GET': category_id = request.GET.get('category_id') user = request.user category = Category.objects.get(id=category_id) category_form_edit = CategoryForm(instance=category, self_cat_id=category_id) try: parent = category.parent.id except: parent = None category_form_edit_response = render_template( 'shopadmin_category_form_edit.html', {'category_form_edit': category_form_edit}, request) return JsonResponse({ 'result': 'success', 'category_form_edit_response': category_form_edit_response, 'parent': parent }) if request.method == 'POST': user = request.user category_form_edit = CategoryForm(request.POST) if category_form_edit.is_valid(): cat = Category.objects.get(id=request.POST.get('category_id')) category_form_edit.save(cat, user) categorys = Category.objects.all() ctx = { 'result': 'success', 'message': message("Категория изменена!", request) } update(request, categorys, ctx) return JsonResponse(ctx) else: category_form_edit_response = render_template( 'shopadmin_category_form_edit.html', {'category_form_edit': category_form_edit}, request) return JsonResponse({ 'result': 'error', 'category_form': category_form_edit_response })
def category_remove_view(request): get_permission(request) if request.method == 'GET': category_id = request.GET.get('category_id') user = request.user category = Category.objects.get(id=category_id) if category.author != user: return HttpResponseRedirect(reverse('permission')) if request.GET.get('method') == 'get_about_delete': related_category = category.get_descendants(include_self=True) for r in related_category: r.related_product = Product.objects.filter(category=r) category_related = render_template( 'shopadmin_category_related.html', { 'category': category, 'related_category': related_category }, request) return JsonResponse({ 'result': 'success', 'category_related': category_related, }) elif request.GET.get('method') == 'delete': category.delete() categorys = Category.objects.all() ctx = { 'result': 'success', 'message': message("Категория удалена!", request) } update(request, categorys, ctx) return JsonResponse(ctx)
def password_change_view(request): if request.user.profile.social: return redirect('permission') if request.POST: password_change_form = PasswordChangeForm(request.POST) if password_change_form.is_valid(): password = password_change_form.cleaned_data['password'] user = request.user user.set_password(password) user.save() update_session_auth_hash(request, user) return JsonResponse({ 'response': "Good", 'result': 'success', 'message': message('Пароль успешно изменен!', request), 'url': reverse('main'), }) else: response = render_template( 'PasswordReset/password_change_form.html', {'password_change_form': password_change_form}, request) return JsonResponse({'response': response, 'result': 'error'})
def category_add_view(request): get_permission(request) if request.method == 'POST': user = request.user category_form = CategoryForm(request.POST) if category_form.is_valid(): cat = Category() category_form.save(cat, user) categorys = Category.objects.all() ctx = { 'result': 'success', 'message': message("Категория добавлена!", request) } update(request, categorys, ctx) return JsonResponse(ctx) else: category_form_response = render_template( 'shopadmin_category_form.html', {'category_form': category_form}, request) return JsonResponse({ 'result': 'error', 'category_form': category_form_response })
def post(self, request, *args, **kwargs): session_token = self.request.session.get(INTERNAL_RESET_SESSION_TOKEN) uid = self.request.session.get(INTERNAL_RESET_SESSION_UIDB) self.user = self.get_user(uid) if self.user is not None: if self.token_generator.check_token(self.user, session_token): password_change_form = PasswordChangeForm(request.POST) if password_change_form.is_valid(): password = password_change_form.cleaned_data['password'] self.user.set_password(password) self.user.save() del self.request.session[INTERNAL_RESET_SESSION_TOKEN] del self.request.session[INTERNAL_RESET_SESSION_UIDB] update_session_auth_hash(request, self.user) login_user = authenticate(username=self.user.username, password=password) if login_user: login(request, login_user) return JsonResponse({ 'response': "Good", 'result': 'success', 'url': reverse('main'), }) else: response = render_template( 'PasswordReset/password_change_form.html', {'password_change_form': password_change_form}, request) return JsonResponse({ 'response': response, 'result': 'error' }) return HttpResponseRedirect(reverse('permission'))
def login_view(request): if request.POST: login_form = LoginForm(request.POST) if login_form.is_valid(): login_user = login_form.save() if login_user: login(request, login_user) return JsonResponse({ 'response': "Good", 'result': 'success', 'url': reverse('main'), }) else: response = render_template('LoginRegistration/login_form.html', {'login_form': login_form}, request) return JsonResponse({'response': response, 'result': 'error'})
def password_reset_handler_view(request): if request.POST: password_reset_form = PasswordResetForm(request.POST) if password_reset_form.is_valid(): email = password_reset_form.cleaned_data['email'] password_reset_form.save(request) return JsonResponse({ 'message': message( "На ваш почтовый адрес отправлена инструкция о восстановлении пароля!", request), 'result': 'success', 'url': '', }) else: response = render_template( 'PasswordReset/password_reset_form.html', {'password_reset_form': password_reset_form}, request) return JsonResponse({'response': response, 'result': 'error'})
def shopadmin_view(request): get_permission(request) method = request.GET.get('method') if method == 'doc_download': try: order = Order.objects.get(id=request.GET.get('id')) ret = pr_get_doc(order.pr_batch_name) if ret['ret_status'] == 'success': return JsonResponse({ 'h': reverse('main') + ret['url'], 'result': 'success' }) else: return JsonResponse({'result': 'error'}) except: return JsonResponse({'result': 'error'}) elif method == 'get_track': try: order = Order.objects.get(id=request.GET.get('id')) tracking = pr_tracking(order.pr_barcode) if tracking['ret_status'] == 'success': return JsonResponse({ 'status': tracking['status'], 'result': 'success' }) else: return JsonResponse({'result': 'error'}) except: return JsonResponse({'result': 'error'}) if request.GET.get('method') == 'get_order': try: order = Order.objects.get(id=request.GET.get('order_id')) order_response = render_template('order_expand.html', { 'admin': 'true', 'order': order }, request) return JsonResponse({ 'order_response': order_response, 'result': 'success' }) except: return JsonResponse({'result': 'error'}) if request.GET.get('method') == 'order_confirm_delivery': try: order = Order.objects.get(id=request.GET.get('order_id')) order.send_confirm = True order.save() return JsonResponse({'result': 'success'}) except: return JsonResponse({'result': 'error'}) orders = Order.objects.all() statusForm = StatusForm() product_form = ProductForm() products = Product.objects.all()[0:10] category_form = CategoryForm() category = Category.objects.all() sale_form = SaleForm() sale = ProductSale.objects.all() SizeFormSet = modelformset_factory(ProductSize, form=SizeForm) size_formset = SizeFormSet() EmptySizeFormSet = modelformset_factory(ProductSize, form=SizeForm, can_delete=True, extra=3) empty_size_formset = EmptySizeFormSet(queryset=ProductSize.objects.none(), prefix="empty_form") context = { 'product_form': product_form, 'sale_form': sale_form, 'product_form_edit': ProductForm(), 'products': products, 'photos': None, 'category': category, 'category_form': category_form, 'category_form_edit': CategoryForm(), 'related_category': None, 'orders': orders, 'statusForm': statusForm, 'size_formset': size_formset, 'empty_size_formset': empty_size_formset, 'sale': sale } getDataForNavBaR(context, request) return render(request, 'shopadmin.html', context)
def product_add_view(request): get_permission(request) if request.method == 'POST': user = request.user product_form = ProductForm(request.POST) type_post = request.POST.get('type_post') EmptySizeFormSet = modelformset_factory(ProductSize, form=SizeForm, can_delete=True) empty_size_formset = EmptySizeFormSet(request.POST, prefix="empty_form") if product_form.is_valid() and empty_size_formset.is_valid(): if type_post == "check_form": return JsonResponse({'result': 'success_check_form'}) elif type_post == "confirm_form": new_product = Product() product_form.save(new_product, request, user) empty_size_formset.save(commit=False) for object in empty_size_formset.new_objects: object.product = new_product object.save() products = Product.objects.all() main_product_response = render_template( 'shopadmin_product_main.html', {'products': products}, request) EmptySizeFormSet = modelformset_factory(ProductSize, form=SizeForm, can_delete=True, extra=3) empty_size_formset = EmptySizeFormSet( queryset=ProductSize.objects.none(), prefix="empty_form") photos = None product_form_response = render_template( 'shopadmin_product_form.html', { 'product_form': ProductForm(), 'empty_size_formset': empty_size_formset, 'photos': photos }, request) return JsonResponse({ 'product_form_response': product_form_response, 'main_product_response': main_product_response, 'result': 'success', 'message': message("Продукт добавлен!", request) }) else: photos = None product_form_response = render_template( 'shopadmin_product_form.html', { 'product_form': product_form, 'empty_size_formset': empty_size_formset, 'photos': photos }, request) return JsonResponse({ 'product_form_response': product_form_response, 'result': 'error' })
def product_edit_view(request): get_permission(request) if request.method == 'GET': product_id = request.GET.get('product_id') user = request.user product = Product.objects.get(id=product_id) product_form_edit = ProductForm(instance=product) photos = product.product_photo.all() SizeFormSet = modelformset_factory(ProductSize, form=SizeForm, can_delete=True, extra=1) size_formset = SizeFormSet(queryset=ProductSize.objects.filter( product=product)) product_form_edit_response = render_template( 'shopadmin_product_form_edit.html', { 'size_formset': size_formset, 'product': product, 'product_form_edit': product_form_edit, 'photos': photos }, request) return JsonResponse({ 'product_form_edit_response': product_form_edit_response, 'result': 'success' }) if request.method == 'POST': user = request.user product_form_edit = ProductForm(request.POST) type_post = request.POST.get('type_post') SizeFormSet = modelformset_factory(ProductSize, form=SizeForm, can_delete=True, extra=1) size_formset = SizeFormSet(request.POST) if product_form_edit.is_valid() and size_formset.is_valid(): if type_post == "check_form": return JsonResponse({'result': 'success_check_form'}) elif type_post == "confirm_form": size_formset.save(commit=False) for object in size_formset.deleted_objects: object.delete() for object in size_formset.changed_objects: object[0].save() for object in size_formset.new_objects: object.product = Product.objects.get( id=request.POST.get('product_id')) object.save() new_product = Product.objects.get( id=request.POST.get('product_id')) product_form_edit.save(new_product, request, user) products = Product.objects.all() main_product_response = render_template( 'shopadmin_product_main.html', {'products': products}, request) return JsonResponse({ 'main_product_response': main_product_response, 'result': 'success', 'message': message("Продукт изменен!", request) }) else: photos = None product = Product.objects.get(id=request.POST.get('product_id')) product_form_edit_response = render_template( 'shopadmin_product_form_edit.html', { 'size_formset': size_formset, 'product': product, 'product_form_edit': product_form_edit, 'photos': photos }, request) return JsonResponse({ 'product_form_edit_response': product_form_edit_response, 'result': 'error' })
def anonymous_profile_view(request): user = request.user if user.is_authenticated: return custom_redirect('profile', order_id=request.GET.get('order_id'), success_pay=request.GET.get('success_pay')) if request.method == 'GET': if request.GET.get('method') == 'get_order': try: order = Order.objects.get(id=request.GET.get('order_id')) order_response = render_template('order_expand.html', {'order': order}, request) return JsonResponse({ 'order_response': order_response, 'result': 'success' }) except: return JsonResponse({'result': 'error'}) if request.GET.get('method') == 'order_confirm_delivery': try: order = Order.objects.get(id=request.GET.get('order_id')) order.send_confirm = True order.save() return JsonResponse({'result': 'success'}) except: return JsonResponse({'result': 'error'}) context = {} context['message_act'] = 'false' context['success_pay'] = 'False' order_id = request.GET.get('order_id') if str(order_id) != 'None' and str( request.GET.get('success_pay')) == 'None': context['order_id'] = order_id response = status_pay(order_id) url = reverse('anonymous_profile') if response['ret_status'] != 'error': if response['status'] == 'authorized' or response[ 'status'] == 'acknowledged': url += '?success_pay=True&order_id=' + order_id context['success_pay'] = 'True' context.update({ 'redirect_url': url, }) getDataForNavBaR(context, request) return render(request, 'anonymous_profile.html', context) elif str(order_id) != 'None' and str( request.GET.get('success_pay')) == 'True': pay_order = Order.objects.get(id=order_id) response = status_pay(pay_order.id) if response['ret_status'] != 'error': if pay_order.pay == False and ( response['status'] == 'authorized' or response['status'] == 'acknowledged') and pay_order.status == 'registered': pay_order.pay = True pay_order.incr_buy_for_items() pay_order.status = 'ready_to_send' pay_order.save() notify_admin(request, pay_order) context['message_act'] = 'true' context['message'] = message('Заказ совершен!', request) context['success_pay'] = 'False' order_update(request) orders = Order.objects.filter( Q(sk=request.session.session_key) & Q(pay=True)) context.update({ 'orders': orders, }) getDataForNavBaR(context, request) return render(request, 'anonymous_profile.html', context)
def profile_view(request): user = request.user if not user.is_authenticated: return custom_redirect('anonymous_profile', order_id=request.GET.get('order_id'), success_pay=request.GET.get('success_pay')) if request.method == 'GET': if request.GET.get('method') == 'get_order': try: order = Order.objects.get(id=request.GET.get('order_id')) order_response = render_template('order_expand.html', {'order': order}, request) return JsonResponse({ 'order_response': order_response, 'result': 'success' }) except: return JsonResponse({'result': 'error'}) if request.GET.get('method') == 'order_confirm_delivery': try: order = Order.objects.get(id=request.GET.get('order_id')) order.send_confirm = True order.save() return JsonResponse({'result': 'success'}) except: return JsonResponse({'result': 'error'}) context = {} context['message_act'] = 'false' context['success_pay'] = 'False' order_id = request.GET.get('order_id') if str(order_id) != 'None' and str( request.GET.get('success_pay')) == 'None': context['order_id'] = order_id user_form = UserForm(instance=request.user) address_form = AddressForm(instance=request.user.profile) password_change_form = PasswordChangeForm() response = status_pay(order_id) url = reverse('profile') if response['ret_status'] != 'error': if response['status'] == 'authorized' or response[ 'status'] == 'acknowledged': url += '?success_pay=True&order_id=' + order_id context['success_pay'] = 'True' context.update({ 'redirect_url': url, 'user_form': user_form, 'address_form': address_form, 'with_save': "true", 'password_change_form': password_change_form }) getDataForNavBaR(context, request) return render(request, 'profile.html', context) elif str(order_id) != 'None' and str( request.GET.get('success_pay')) == 'True': pay_order = Order.objects.get(id=order_id) response = status_pay(pay_order.id) if response['ret_status'] != 'error': if pay_order.pay == False and ( response['status'] == 'authorized' or response['status'] == 'acknowledged' ) and pay_order.status == 'registered': pay_order.pay = True pay_order.incr_buy_for_items() pay_order.status = 'ready_to_send' pay_order.save() notify_admin(request, pay_order) context['message_act'] = 'true' context['message'] = message('Заказ совершен!', request) order_update(request) orders = Order.objects.filter(Q(user=user) & Q(pay=True)) user_form = UserForm(instance=request.user) address_form = AddressForm(instance=request.user.profile) password_change_form = PasswordChangeForm() context.update({ 'orders': orders, 'user_form': user_form, 'address_form': address_form, 'with_save': "true", 'password_change_form': password_change_form }) getDataForNavBaR(context, request) return render(request, 'profile.html', context) if request.method == 'POST': type_post = request.POST.get('type_post') if type_post == "user_form": user_form = UserForm(request.POST, instance=request.user) if user_form.is_valid(): user_form.save() user_change_form_response = render_template( 'user_change.html', {'user_form': user_form}, request) return JsonResponse({ 'user_change_form_response': user_change_form_response, 'result': 'success', 'message': message("Профиль изменен!", request) }) else: user_change_form_response = render_template( 'user_change.html', {'user_form': user_form}, request) return JsonResponse({ 'user_change_form_response': user_change_form_response, 'result': 'error' }) else: address_form = AddressForm(request.POST, instance=request.user.profile) if address_form.is_valid(): address_form.save() address_change_form_response = render_template( 'address_change.html', { 'address_form': address_form, 'with_save': "true" }, request) return JsonResponse({ 'address_change_form_response': address_change_form_response, 'result': 'success', 'message': message("Адрес изменен!", request) }) else: address_change_form_response = render_template( 'address_change.html', { 'address_form': address_form, 'with_save': "true" }, request) return JsonResponse({ 'address_change_form_response': address_change_form_response, 'result': 'error' })
def create_notification_view(request): if request.method == 'POST': user = request.user product_id = request.POST.get('product_id') size_id = request.POST.get('size_id') type = request.POST.get('type') noty_kind = request.POST.get('noty_kind') if type == 'notify_product': template = 'notification.html' else: template = 'notification_size.html' notification_form = NotificationForm(request.POST) if notification_form.is_valid(): new_notification = Notification() new_notification.email = notification_form.cleaned_data['email'] if type == 'notify_product': new_notification.product = Product.objects.get(id=product_id) new_notification.product_notify = True else: if noty_kind == 'noty_product': new_notification.product = Product.objects.get( id=product_id) new_notification.product_notify = True else: new_notification.product_size = ProductSize.objects.get( id=size_id) new_notification.product_notify = False if user.is_authenticated: new_notification.user = user else: new_notification.sk = str(request.session.session_key) new_notification.save() notification_form_response = render_template( template, { 'size_id': size_id, 'product_id': product_id, 'type': type, 'notification_form': NotificationForm() }, request) ctx = { 'result': 'success', 'message': message("Вы подписались!", request), 'notification_form_response': notification_form_response } return JsonResponse(ctx) else: notification_form_response = render_template( template, { 'size_id': size_id, 'product_id': product_id, 'type': type, 'notification_form': notification_form }, request) return JsonResponse({ 'result': 'error', 'notification_form_response': notification_form_response })
def to_order_view(request): if request.method == 'GET': method = request.GET.get('method') if method == "check_form": address_form = AddressForm(request.GET) if address_form.is_valid(): info = address_form.cleaned_data normalize = pr_address_normalize(info['country'], info['region'], info['area'], info['city'], info['street'], info['house'], info['room'], info['index']) if normalize['ret_status'] == 'success': delivery_total_ret = pr_pre_calc( normalize['body']['index']) delivery_total = delivery_total_ret['total_nds'] delivery_max_day = delivery_total_ret['max-days'] delivery_min_day = delivery_total_ret['min-days'] address_change_form_response = render_template( 'address_change.html', { 'address_form': address_form, 'with_save': "true" }, request) cart = getUserCart(request) if cart.items.count() == 0: return JsonResponse({'result': 'error'}) address_form.set_readonly() sale_code = request.GET.get('sale_code') sale_result = 0 try: sale = ProductSale.objects.get(promo=sale_code) val = cart.price_all * Decimal(sale.sale) / 100 delivery_and_cart_price = cart.price_all - val + delivery_total sale_result = sale.sale except: delivery_and_cart_price = cart.price_all + delivery_total order_body_response = render_template( 'order_body.html', { 'delivery_max_day': delivery_max_day, 'delivery_min_day': delivery_min_day, "delivery_and_cart_price": delivery_and_cart_price, 'delivery_price': delivery_total, 'cart': cart, 'address_form': address_form, 'with_save': "false", 'sale': sale_result }, request) return JsonResponse({ 'result': 'success', 'address_change_form_response': address_change_form_response, 'order_body_response': order_body_response }) else: if request.user.is_authenticated: with_save = "true" else: with_save = "false" address_change_form_response = render_template( 'address_change.html', { 'address_form': address_form, 'with_save': with_save }, request) return JsonResponse({ 'address_change_form_response': address_change_form_response, 'result': 'error' }) else: if request.user.is_authenticated: with_save = "true" else: with_save = "false" address_change_form_response = render_template( 'address_change.html', { 'address_form': address_form, 'with_save': with_save }, request) return JsonResponse({ 'address_change_form_response': address_change_form_response, 'result': 'error' }) elif method == "order_confirm": address_form = AddressForm(request.GET) if address_form.is_valid(): info = address_form.cleaned_data normalize = pr_address_normalize(info['country'], info['region'], info['area'], info['city'], info['street'], info['house'], info['room'], info['index']) if normalize['ret_status'] == 'success': delivery_total_ret = pr_pre_calc( normalize['body']['index']) delivery_total = delivery_total_ret['total_nds'] cart = getUserCart(request) if cart.items.count() == 0: return JsonResponse({'result': 'error'}) order = Order() order.save() address_form = AddressForm(request.GET, instance=order) if not address_form.is_valid(): return JsonResponse({'result': 'error'}) address_form.save() order.order_from_cart(cart) order.delivery_price = delivery_total sale_code = request.GET.get('sale_code') try: sale = ProductSale.objects.get(promo=sale_code) val = order.price_all * Decimal(sale.sale) / 100 order.delivery_cart_price = delivery_total + order.price_all - val if sale.promo == 'delivery': order.delivery_cart_price = order.price_all order.sale = sale.sale order.promo = sale.promo except: order.delivery_cart_price = delivery_total + order.price_all order.save() cart.remove_all_from_cart(delete=False) if request.user.is_authenticated == False: from_ = str(request.session.session_key) order.sk = from_ order.save() user_id = order.sk else: user_id = order.user.id from_ = str(order.user.id) responce = register_pay(request, order.id, order.delivery_cart_price, user_id, order.name, order.email, order.phone) if responce['ret_status'] == 'success': order.status = 'registered' order.archive() order.save() return JsonResponse({ 'result': 'success', 'url': responce['redirect_url'] + "?session=" + responce['session'] }) else: cart.cart_from_order(order) order.delete() return JsonResponse({ 'result': 'error', 'message_act': 'true', 'message': message('Произошла ошибка, попробуйте еще раз!', request), }) else: return JsonResponse({'result': 'error'}) else: return JsonResponse({'result': 'error'}) elif method == "calc": address_form = AddressForm(request.GET) info = address_form.data normalize = pr_address_normalize(info['country'], info['region'], info['area'], info['city'], info['street'], info['house'], info['room'], info['index']) if normalize['ret_status'] == 'success': delivery_total_ret = pr_pre_calc(normalize['body']['index']) delivery_total = delivery_total_ret['total_nds'] return JsonResponse({ 'result': 'success', "delivery_total": delivery_total }) else: return JsonResponse({'result': 'error', "delivery_total": "0"}) elif method == "check_sale": sale_code = request.GET.get('sale_code') try: sale = ProductSale.objects.get(promo=sale_code) return JsonResponse({ 'result': 'success', "sale": str(sale.sale) }) except: return JsonResponse({'result': 'error', "sale": "0"})