def about(request): form = EmailFormLight(request.POST or None) if form.is_valid(): phone = form.cleaned_data.get('phone') name = form.cleaned_data.get('name') callback, created = EmailModel.objects.get_or_create(phone=phone, name=name) context = { 'form': form, } return render(request, 'home/about.html', context)
def detailed(request, pk): cats = Categories.objects.filter(parent_id=0) try: obj = get_object_or_404(Products, id=int(pk)) bread_sub2 = Categories.objects.get(id=obj.cat.first().parent_id) bread_sub1 = Categories.objects.get(id=bread_sub2.parent_id) except Exception as e: raise Http404 try: aver = Rating.objects.get(object_id=pk) except: aver = 0 # Похожие товары try: similar_products = Products.objects.filter(cat=obj.cat.first().id) except: similar_products = Products.objects.order_by('?')[:8] comments = Comment.objects.filter_by_instance(obj) comments = Comment.objects.filter_by_instance(obj) initial_data = { 'content_type': obj.get_content_type, 'object_id': obj.id, } form = CommentForm(request.POST or None, initial=initial_data) user_string = None # Allow leave comments only for authenticated users if form.is_valid() and request.user.is_authenticated: if request.user.is_authenticated: user_string = request.user elif form.cleaned_data.get('user') is not None: user_string = form.cleaned_data.get('user') else: user_string = 'ANONIMUS' c_type = form.cleaned_data.get('content_type') content_type = ContentType.objects.get(model=c_type) obj_id = form.cleaned_data.get('object_id') content_data = form.cleaned_data.get('content') parent_obj = None try: parent_id = request.POST.get('parent_id') except: parent_id = None if parent_id: parent_qs = Comment.objects.filter(id=parent_id) if parent_qs.exists(): parent_obj = parent_qs.first() new_comment, created = Comment.objects.get_or_create( content_type=content_type, object_id=obj_id, content=strip_tags(content_data), parent=parent_obj, user=strip_tags(user_string), ) url = new_comment.content_object.get_absolute_url() send_mail( 'Ducatoparts.ru новый комментарий', f'На дукато партс оставили новый комментарий на странице {url}, Текст: {content_data}', '*****@*****.**', settings.SHOP_EMAILS_MANAGERS, fail_silently=False, ) return HttpResponseRedirect(url) else: pass #comments count stuff def check_comment_count(): count = comments.count() % 10 comment_word = 'КОММЕНТАРИЕВ' if count == 1: comments_word = 'КОММЕНТАРИЙ' elif count > 1 and count < 5: comment_word = 'КОММЕНТАРИЯ' elif count >= 5: comment_word = 'КОММЕНТАРИЕВ' return comment_word # Форма звонка Вася e_form = EmailFormLight(request.POST or None) #Article founder starts here search_list = search_splitter(obj.name) articles = Blogs.objects.all() art_w_list = [] for i, article in enumerate(articles): we = get_weight(article.text, search_list) if we > 0.02: art_w_list.append({ 'article': article, 'weight': we}) if i > 10: break #Добавляем аналоги analogs = Products.objects.filter(cat_n=obj.cat_n).exclude(id=obj.id) if not analogs: analogs = None context = { 'object': obj, 'categories': cats, 'cars': show_cars(), 'comments': comments, 'comment_count_word': check_comment_count(), 'comment_form': form, 'email_form': e_form, 'bread_sub1': bread_sub1, 'bread_sub2': bread_sub2, 'similar_products': similar_products, 'aver': aver, 'articles': art_w_list, 'analogs': analogs, } return render(request, 'products/product.html', context)
def detailed(request, pk): cats = Categories.objects.filter(parent_id=0) obj = Products.objects.get(id=pk) # Redefine function inside to returning lists of files in the directories def get_image_path(obj): working_dir = settings.STATICFILES_DIRS[1] files = os.listdir(os.path.join(working_dir, obj.cat_n))[:10] img_list = [] for f in files: img_list.append(os.path.join(obj.cat_n, f)) setattr(obj, 'image_path', img_list) return obj comments = Comment.objects.filter_by_instance(obj) comments = Comment.objects.filter_by_instance(obj) initial_data = { 'content_type': obj.get_content_type, 'object_id': obj.id, } form = CommentForm(request.POST or None, initial=initial_data) user_string = None if form.is_valid(): if request.user.is_authenticated: user_string = request.user elif form.cleaned_data.get('user') is not None: user_string = form.cleaned_data.get('user') else: user_string = 'ANONIMUS' c_type = form.cleaned_data.get('content_type') content_type = ContentType.objects.get(model=c_type) obj_id = form.cleaned_data.get('object_id') content_data = form.cleaned_data.get('content') parent_obj = None try: parent_id = request.POST.get('parent_id') print(parent_id) except: parent_id = None if parent_id: parent_qs = Comment.objects.filter(id=parent_id) if parent_qs.exists(): parent_obj = parent_qs.first() new_comment, created = Comment.objects.get_or_create( content_type=content_type, object_id=obj_id, content=strip_tags(content_data), parent=parent_obj, user=strip_tags(user_string), ) url = new_comment.content_object.get_absolute_url() send_mail( 'Ducatoparts.ru новый комментарий', f'На дукато партс оставили новый комментарий на странице {url}', '*****@*****.**', ['*****@*****.**', '*****@*****.**'], fail_silently=False, ) return HttpResponseRedirect(url) #comments count stuff def check_comment_count(): count = comments.count() % 10 comment_word = 'КОММЕНТАРИЕВ' if count == 1: comments_word = 'КОММЕНТАРИЙ' elif count > 1 and count < 5: comment_word = 'КОММЕНТАРИЯ' elif count >= 5: comment_word = 'КОММЕНТАРИЕВ' return comment_word # Форма звонка Вася e_form = EmailFormLight(request.POST or None) if e_form.is_valid(): phone = e_form.cleaned_data.get('phone') name = e_form.cleaned_data.get('name') callback, created = EmailModel.objects.get_or_create(phone=phone, name=name) # Похожие товары context = { 'object': get_image_path(obj), 'categories': cats, 'cars': show_cars(), 'comments': comments, 'comment_count_word': check_comment_count(), 'comment_form': form, 'email_form': e_form, } return render(request, 'products/product.html', context)