import json

from django.shortcuts import render

from uuid import uuid1
from .models import *
from django.contrib import auth
from django.http import HttpResponse, Http404, JsonResponse
from django.template import loader, RequestContext
from django.contrib.auth.decorators import login_required
from BaseModels.mailSender import techSendMail
from django.utils.translation import gettext as _
from datetime import datetime
from django.template.loader import render_to_string
from django.urls import reverse
# from .forms import *
from .funcs import *

def get_articles_block_ajax(request):

    if request.method != 'POST':
        raise Http404

    try:

        data = request.POST.dict()
        if not data and request.body:
            data = json.loads(request.body)

        art_kwargs = {}

        Dict = get_articles(art_kwargs=art_kwargs, request_Data=data)
        if 'errors' in Dict:
            return JsonResponse(Dict, status=400)

        html = render_to_string('blocks/articles/b_news_elements.html', Dict, request=request)

        res_Dict = {
            'html': html,
            'last_block': Dict['last_block']
            # 'form': RouteForm(initial=data)
        }

        return JsonResponse(res_Dict)

    except Exception as e:

        errors_Dict = {
            'errors': {
                'all__': f'ошибка в запросе = {str(e)}'
            }
        }
        return JsonResponse(errors_Dict, status=400)