예제 #1
0
파일: views.py 프로젝트: Ankrate/dp
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)
예제 #2
0
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)