изменен способ получения случайности (тест)

This commit is contained in:
Sergei Erjemin (Сергей Еремин) 2020-11-21 16:47:29 +03:00
parent 6c506ed251
commit 1d7460ea4d

View File

@ -38,17 +38,21 @@ def for_dq(dq):
if dq.kImages_id is None: if dq.kImages_id is None:
if len(tags) != 0: if len(tags) != 0:
try: try:
tagged_image = TbImages.objects.filter(tags__name__in=tags).order_by('?')[0] # tagged_image = TbImages.objects.filter(tags__name__in=tags).order_by('?').first()
to_template.update({'IMAGE': tagged_image.imFile}) tagged_image = TbImages.objects.filter(tags__name__in=tags)
random.shuffle(list(tagged_image))
to_template.update({'IMAGE': tagged_image[0].imFile})
except IndexError: except IndexError:
pass pass
else: else:
to_template.update({'IMAGE': dq.kImages.imFile}) to_template.update({'IMAGE': dq.kImages.imFile})
dq.iViewCounter += 1 dq.iViewCounter += 1
dq.save() dq.save()
dq_next = TbDictumAndQuotes.objects.exclude(id=dq.id).order_by('?').first() # dq_next = TbDictumAndQuotes.objects.exclude(id=dq.id).order_by('?').first()
to_template.update({"NEXT": dq_next.id}) dq_next = TbDictumAndQuotes.objects.exclude(id=dq.id)
to_template.update({"NEXT_TXT": pytils.translit.slugify(dq_next.szContent.lower()[:120])}) random.shuffle(list(dq_next))
to_template.update({"NEXT": dq_next[0].id})
to_template.update({"NEXT_TXT": pytils.translit.slugify(dq_next[0].szContent.lower()[:120])})
return to_template return to_template
@ -71,13 +75,13 @@ def index(request):
# if not request.user.is_authenticated(): # if not request.user.is_authenticated():
# return HttpResponseRedirect("/access") # return HttpResponseRedirect("/access")
template = "index.html" # шаблон template = "index.html" # шаблон
dq_ = TbDictumAndQuotes.objects.order_by('?') dq_ = TbDictumAndQuotes.objects
if request.GET.get('tag'): if request.GET.get('tag'):
dq = dq_.filter(kAuthor__tags__slug__in=[request.GET['tag']]).first() dq = dq_.filter(kAuthor__tags__slug__in=[request.GET['tag']]).order_by('?').first()
if dq is None: if dq is None:
dq = dq_.filter(tags__slug__in=[request.GET['tag']]).first() dq = dq_.filter(tags__slug__in=[request.GET['tag']]).order_by('?').first()
if dq is None: if dq is None:
dq = dq_.first() dq = dq_.order_by('?').first()
else: else:
dq = dq_.first() dq = dq_.first()
to_template = for_dq(dq) to_template = for_dq(dq)