diff --git a/ArticlesApp/funcs.py b/ArticlesApp/funcs.py new file mode 100644 index 0000000..6c5af77 --- /dev/null +++ b/ArticlesApp/funcs.py @@ -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 diff --git a/ArticlesApp/js_urls.py b/ArticlesApp/js_urls.py new file mode 100644 index 0000000..fa8b0f6 --- /dev/null +++ b/ArticlesApp/js_urls.py @@ -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'), + +] \ No newline at end of file diff --git a/ArticlesApp/js_views.py b/ArticlesApp/js_views.py new file mode 100644 index 0000000..8869b51 --- /dev/null +++ b/ArticlesApp/js_views.py @@ -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) \ No newline at end of file diff --git a/ArticlesApp/views.py b/ArticlesApp/views.py index dbc4c58..cb7cf5d 100644 --- a/ArticlesApp/views.py +++ b/ArticlesApp/views.py @@ -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)) diff --git a/TWB/urls.py b/TWB/urls.py index d043eed..7cd0a8b 100644 --- a/TWB/urls.py +++ b/TWB/urls.py @@ -31,6 +31,7 @@ urlpatterns += i18n_patterns( path('reference_data/', include('ReferenceDataApp.js_urls')), path('', include('ArticlesApp.urls_translate')), + path('', include('ArticlesApp.js_urls')), ) diff --git a/templates/pages/p_articles.html b/templates/pages/p_articles.html index e519e21..ed75480 100644 --- a/templates/pages/p_articles.html +++ b/templates/pages/p_articles.html @@ -12,7 +12,7 @@