articles page
This commit is contained in:
SDE
2023-10-05 19:21:27 +03:00
parent f850ef5c31
commit b89b9ed017
6 changed files with 116 additions and 17 deletions

35
ArticlesApp/funcs.py Normal file
View File

@@ -0,0 +1,35 @@
from .models import *
def get_articles(art_kwargs, request_Data=None, from_el=None, to_el=None):
if request_Data:
if not from_el and 'from_el' in request_Data:
from_el = request_Data['from_el']
if not to_el and 'to_el' in request_Data:
to_el = request_Data['to_el']
arts = ArticleModel.objects.filter(**art_kwargs).order_by('-createDT')
el_count = arts.count()
if from_el and to_el:
arts = arts[from_el:to_el]
elif from_el:
arts = arts[from_el:]
elif to_el:
arts = arts[:to_el]
else:
to_el = 3
arts = arts[:3]
last_block = False
if not to_el or to_el >= el_count:
last_block = True
Dict = {
'articles': arts,
'last_block': last_block
}
return Dict

10
ArticlesApp/js_urls.py Normal file
View File

@@ -0,0 +1,10 @@
# coding=utf-8
from django.urls import path
# from AuthApp.js_views import *
# from AuthApp.import_funcs import *
from .js_views import *
urlpatterns = [
path('get_articles_block/', get_articles_block_ajax, name='get_articles_block_ajax'),
]

53
ArticlesApp/js_views.py Normal file
View File

@@ -0,0 +1,53 @@
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/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)

View File

@@ -5,6 +5,7 @@ from .models import *
from datetime import datetime, date
from django.http import Http404, HttpResponse
from django.template import loader
from .funcs import *
# from django.contrib.auth.decorators import login_required
# from BaseModels.search_optimization.ld_json.ld_article_news import get_ld_article_news
@@ -76,11 +77,7 @@ def ArticlesPageView(request, year=None):
raise Http404
arts = ArticleModel.objects.filter(**kwargs).order_by('-createDT')
Dict = {
'articles': arts
}
Dict = get_articles(art_kwargs=kwargs)
t = loader.get_template('pages/p_articles.html')
return HttpResponse(t.render(Dict, request))