def render_slovo(slovo, file_format='pdf', tip_dokumenta=None): if not tip_dokumenta: tip_dokumenta = 2 try: trd = TipRenderovanogDokumenta.objects.get(id=tip_dokumenta) except TipRenderovanogDokumenta.DoesNotExist: log.fatal( f'Nije pronadjen tip renderovanog dokumenta: id={tip_dokumenta}') return odrednice = Odrednica.objects.filter( rec__startswith=slovo[0].lower()).filter( status_id__in=trd.statusi.values_list('id', flat=True)) odrednice = odrednice.order_by( Collate('sortable_rec', 'utf8mb4_croatian_ci'), 'rbr_homonima') rendered_odrednice = [render_one(o) for o in odrednice] context = {'odrednice': rendered_odrednice, 'slovo': slovo.upper()} if file_format == 'pdf': return render_to_pdf(context, 'render/pdf/slovo.html', trd, opis=f'слово {slovo[0].upper()}') elif file_format == 'docx': return render_to_docx(context, 'render/docx/slovo.html', trd, opis=f'слово {slovo[0].upper()}') else: return None
def render_recnik(file_format='pdf', tip_dokumenta=None): if not tip_dokumenta: tip_dokumenta = 2 try: trd = TipRenderovanogDokumenta.objects.get(id=tip_dokumenta) except TipRenderovanogDokumenta.DoesNotExist: log.fatal( f'Nije pronadjen tip renderovanog dokumenta: id={tip_dokumenta}') return slova = [] log.info('Generisanje odrednica...') for s in AZBUKA: odrednice = Odrednica.objects.filter(rec__startswith=s).filter( status_id__in=trd.statusi.values_list('id', flat=True)) odrednice = odrednice.order_by( Collate('sortable_rec', 'utf8mb4_croatian_ci'), 'rbr_homonima') slova.append({ 'slovo': s.upper(), 'odrednice': [render_one(o) for o in odrednice] }) context = {'slova': slova} log.info(f'Generisanje fajla, tip: {file_format}...') if file_format == 'pdf': return render_to_pdf(context, 'render/pdf/recnik.html', trd) elif file_format == 'docx': return render_to_docx(context, 'render/docx/recnik.html', trd) else: return None
def test_collate_order_by_cs(self): collation = connection.features.test_collations.get('cs') if not collation: self.skipTest( 'This backend does not support case-sensitive collations.') qs = Author.objects.order_by(Collate('alias', collation)) self.assertSequenceEqual(qs, [self.author2, self.author1])
def test_collate_filter_ci(self): collation = connection.features.test_collations.get('ci') if not collation: self.skipTest( 'This backend does not support case-insensitive collations.') qs = Author.objects.filter(alias=Collate(Value('a'), collation)) self.assertEqual(qs.count(), 2)
def test_op_class_descending_collation(self): collation = connection.features.test_collations.get("non_default") if not collation: self.skipTest("This backend does not support case-insensitive collations.") index_name = "test_op_class_descending_collation" index = Index( Collate( OpClass(Lower("field"), name="text_pattern_ops").desc(nulls_last=True), collation=collation, ), name=index_name, ) with connection.schema_editor() as editor: editor.add_index(TextFieldModel, index) self.assertIn( "COLLATE %s" % editor.quote_name(collation), str(index.create_sql(TextFieldModel, editor)), ) with editor.connection.cursor() as cursor: cursor.execute(self.get_opclass_query, [index_name]) self.assertCountEqual(cursor.fetchall(), [("text_pattern_ops", index_name)]) table = TextFieldModel._meta.db_table constraints = self.get_constraints(table) self.assertIn(index_name, constraints) self.assertEqual(constraints[index_name]["orders"], ["DESC"]) with connection.schema_editor() as editor: editor.remove_index(TextFieldModel, index) self.assertNotIn(index_name, self.get_constraints(table))
class ShortOdrednicaListAlpha(generics.ListAPIView): permission_classes = [permissions.IsAuthenticated] queryset = Odrednica.objects.all().order_by( Collate('rec', 'utf8mb4_croatian_ci')) serializer_class = ShortOdrednicaSerializer filter_backends = [DjangoFilterBackend] filter_fields = ['rec', 'rod', 'vreme_kreiranja', 'obradjivac_id']
def test_invalid_collation(self): tests = [ None, '', 'et-x-icu" OR ', '"schema"."collation"', ] msg = "Invalid collation name: %r." for value in tests: with self.subTest(value), self.assertRaisesMessage( ValueError, msg % value): Collate(F('alias'), value)
def test_language_collation_order_by(self): collation = connection.features.test_collations.get('swedish_ci') if not collation: self.skipTest('This backend does not support language collations.') author3 = Author.objects.create(alias='O', name='Jones') author4 = Author.objects.create(alias='Ö', name='Jones') author5 = Author.objects.create(alias='P', name='Jones') qs = Author.objects.order_by(Collate(F('alias'), collation), 'name') self.assertSequenceEqual( qs, [self.author1, self.author2, author3, author5, author4], )
def test_language_collation_order_by(self): collation = connection.features.test_collations.get("swedish_ci") if not collation: self.skipTest("This backend does not support language collations.") author3 = Author.objects.create(alias="O", name="Jones") author4 = Author.objects.create(alias="Ö", name="Jones") author5 = Author.objects.create(alias="P", name="Jones") qs = Author.objects.order_by(Collate(F("alias"), collation), "name") self.assertSequenceEqual( qs, [self.author1, self.author2, author3, author5, author4], )