def ready(self): # Connections may already exist before we are called. for conn in connections.all(): if conn.connection is not None: register_type_handlers(conn) connection_created.connect(register_type_handlers) CharField.register_lookup(Unaccent) TextField.register_lookup(Unaccent) CharField.register_lookup(SearchLookup) TextField.register_lookup(SearchLookup) CharField.register_lookup(TrigramSimilar) TextField.register_lookup(TrigramSimilar)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['tab'] = "info" context['comments'] = self.dining_list.diningcomment_set.order_by( '-pinned_to_top', 'timestamp').all() # Last visit context['last_visited'] = DiningCommentVisitTracker.get_latest_visit( user=self.request.user, dining_list=self.dining_list, update=True) from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) context[ 'number_of_allergies'] = self.dining_list.internal_dining_entries( ).filter( user__userdiningsettings__allergies__length__gte=1).count() if self.dining_list.claimed_by == self.request.user or self.dining_list.purchaser == self.request.user: context['can_change_settings'] = True if self.dining_list.claimed_by == self.request.user and self.dining_list.diners.count( ) < self.dining_list.min_diners: context['can_remove_list'] = True else: context['can_remove_list'] = False return context
def statistics_view(request): """View for statistics page """ user = str( request.user) # get name of an authorized user for queries to DB data = Order.objects data_dict = {} data_dict['users products'] = data.filter(user=user).count() data_dict['all products'] = data.count() # data_dict['users max price'] = max([query.price for query in data.filter(user=user)]) # data_dict['user max price'] = data.filter(user=user).aggregate(Max('price')).get('price__max') data_dict['user max price'] = data.filter(user=user).aggregate( max_price=Max('price')).get('max_price') # data_dict['avg all prices'] = round(mean([query.price for query in data.all()])) data_dict['avg all prices'] = round( data.aggregate(Avg('price')).get('price__avg')) CharField.register_lookup(Length) # register lookup # data_dict['sum users prices'] = sum([query.price for query in data.filter(user=user)]) data_dict['sum users prices'] = data.filter(user=user).aggregate( Sum('price')).get('price__sum') data_dict['complex query'] = sum([ data.filter(price__gt=50).count(), data.filter(user=user, name__length__gt=3).count() ]) if request.method == "POST": Order.objects.all().update(price=F('price') + 1) return redirect('statistics') return render(request, 'statistics.html', {'data': data_dict})
def do_search(request): CharField.register_lookup(Lower, "lower") designs = Designs.objects.filter(title__lower__contains=request.GET['q']) return render(request, "graphics/search.html", {'designs':designs})
def ready(self): super().ready() register(check_data_writable) register(check_mail_connection, deploy=True) register(check_celery, deploy=True) register(check_database, deploy=True) register(check_cache, deploy=True) register(check_settings, deploy=True) register(check_templates, deploy=True) register(check_site, deploy=True) register(check_perms, deploy=True) register(check_errors, deploy=True) register(check_version) monkey_patch_translate() init_error_collection() engine = settings.DATABASES["default"]["ENGINE"] if engine == "django.db.backends.postgresql": CharField.register_lookup(SearchLookup) TextField.register_lookup(SearchLookup) elif engine == "django.db.backends.mysql": CharField.register_lookup(MySQLSearchLookup) TextField.register_lookup(MySQLSearchLookup) else: raise Exception("Unsupported database: {}".format(engine))
def fix_datasets(self): from django.db.models import CharField from django.db.models.functions import Length from mcod.datasets.models import Dataset ds = Dataset.objects.filter(organization__is_removed=True) for d in ds: print( f"Dataset ({d.id}) is set as removed because organization ({d.organization.id}) is removed" ) d.is_removed = True d.save() ds = Dataset.objects.filter(organization__status="draft") for d in ds: if d.status == "published": print( f"Status of dataset ({d.id}) is changed to draft because organization ({d.organization.id}) is draft" ) # noqa d.status = "draft" d.save() print("Fixing slugs") CharField.register_lookup(Length, 'length') datasets = Dataset.objects.filter(title__length__gt=90) for d in datasets: Dataset.objects.filter(pk=d.id).update(slug=d.get_unique_slug())
def ready(self): setting_changed.connect(uninstall_if_needed) # Connections may already exist before we are called. for conn in connections.all(initialized_only=True): if conn.vendor == "postgresql": conn.introspection.data_types_reverse.update({ 3904: "django.contrib.postgres.fields.IntegerRangeField", 3906: "django.contrib.postgres.fields.DecimalRangeField", 3910: "django.contrib.postgres.fields.DateTimeRangeField", 3912: "django.contrib.postgres.fields.DateRangeField", 3926: "django.contrib.postgres.fields.BigIntegerRangeField", }) if conn.connection is not None: register_type_handlers(conn) connection_created.connect(register_type_handlers) CharField.register_lookup(Unaccent) TextField.register_lookup(Unaccent) CharField.register_lookup(SearchLookup) TextField.register_lookup(SearchLookup) CharField.register_lookup(TrigramSimilar) TextField.register_lookup(TrigramSimilar) CharField.register_lookup(TrigramWordSimilar) TextField.register_lookup(TrigramWordSimilar) CharField.register_lookup(TrigramStrictWordSimilar) TextField.register_lookup(TrigramStrictWordSimilar) MigrationWriter.register_serializer(RANGE_TYPES, RangeSerializer) IndexExpression.register_wrappers(OrderBy, OpClass, Collate)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) context['allergy_entries'] = self.dining_list.internal_dining_entries( ).filter(user__userdiningsettings__allergies__length__gte=1) return context
def test_transform(self): try: CharField.register_lookup(Ord) authors = Author.objects.annotate(first_initial=Left('name', 1)) self.assertCountEqual(authors.filter(first_initial__ord=ord('J')), [self.john]) self.assertCountEqual(authors.exclude(first_initial__ord=ord('J')), [self.elena, self.rhonda]) finally: CharField._unregister_lookup(Ord)
def test_transform(self): try: CharField.register_lookup(Lower) Author.objects.create(name='John Smith', alias='smithj') Author.objects.create(name='Rhonda') authors = Author.objects.filter(name__lower__exact='john smith') self.assertQuerysetEqual(authors.order_by('name'), ['John Smith'], lambda a: a.name) finally: CharField._unregister_lookup(Lower)
def test_transform(self): new_name = self.t1.name.upper() self.assertNotEqual(self.t1.name, new_name) Tag.objects.create(name=new_name) CharField.register_lookup(Lower) try: self.assertCountEqual( Tag.objects.order_by().distinct('name__lower'), [self.t1, self.t2, self.t3, self.t4, self.t5], ) finally: CharField._unregister_lookup(Lower)
def test_transform(self): query = Query(Author) CharField.register_lookup(Lower, 'lower') try: where = query.build_where(~Q(name__lower='foo')) finally: CharField._unregister_lookup(Lower, 'lower') lookup = where.children[0] self.assertIsInstance(lookup, Exact) self.assertIsInstance(lookup.lhs, Lower) self.assertIsInstance(lookup.lhs.lhs, SimpleCol) self.assertEqual(lookup.lhs.lhs.target, Author._meta.get_field('name'))
def ready(self): super().ready() register(check_data_writable) register(check_mail_connection, deploy=True) register(check_celery, deploy=True) register(check_cache, deploy=True) register(check_settings, deploy=True) register(check_templates, deploy=True) register(check_site, deploy=True) register(check_perms, deploy=True) register(check_errors, deploy=True) register(check_version) register(check_encoding) register(check_diskspace, deploy=True) init_error_collection() engine = settings.DATABASES["default"]["ENGINE"] if engine == "django.db.backends.postgresql": CharField.register_lookup(PostgreSQLSearchLookup) TextField.register_lookup(PostgreSQLSearchLookup) CharField.register_lookup(PostgreSQLSubstringLookup) TextField.register_lookup(PostgreSQLSubstringLookup) elif engine == "django.db.backends.mysql": CharField.register_lookup(MySQLSearchLookup) TextField.register_lookup(MySQLSearchLookup) CharField.register_lookup(MySQLSubstringLookup) TextField.register_lookup(MySQLSubstringLookup) else: raise Exception(f"Unsupported database: {engine}")
def ready(self): super().ready() register(check_data_writable) register(check_mail_connection, deploy=True) register(check_celery, deploy=True) register(check_cache, deploy=True) register(check_settings, deploy=True) register(check_templates, deploy=True) register(check_database, deploy=True) register(check_site) register(check_perms, deploy=True) register(check_errors, deploy=True) register(check_version, deploy=True) register(check_encoding) register(check_diskspace, deploy=True) init_error_collection() if using_postgresql(): CharField.register_lookup(PostgreSQLSearchLookup) TextField.register_lookup(PostgreSQLSearchLookup) CharField.register_lookup(PostgreSQLSubstringLookup) TextField.register_lookup(PostgreSQLSubstringLookup) else: CharField.register_lookup(MySQLSearchLookup) TextField.register_lookup(MySQLSearchLookup) CharField.register_lookup(MySQLSubstringLookup) TextField.register_lookup(MySQLSubstringLookup)
def test_upper_transform(self): try: CharField.register_lookup(Upper, 'upper') Author.objects.create(name='John Smith', alias='smithj') Author.objects.create(name='Rhonda') authors = Author.objects.filter(name__upper__exact='JOHN SMITH') self.assertQuerysetEqual( authors.order_by('name'), [ 'John Smith', ], lambda a: a.name ) finally: CharField._unregister_lookup(Upper, 'upper')
def test_length_transform(self): try: CharField.register_lookup(Length, 'length') Author.objects.create(name='John Smith', alias='smithj') Author.objects.create(name='Rhonda') authors = Author.objects.filter(name__length__gt=7) self.assertQuerysetEqual( authors.order_by('name'), [ 'John Smith', ], lambda a: a.name ) finally: CharField._unregister_lookup(Length, 'length')
def test_func_transform_bilateral(self): class UpperBilateral(Upper): bilateral = True try: CharField.register_lookup(UpperBilateral, 'upper') Author.objects.create(name='John Smith', alias='smithj') Author.objects.create(name='Rhonda') authors = Author.objects.filter(name__upper__exact='john smith') self.assertQuerysetEqual(authors.order_by('name'), [ 'John Smith', ], lambda a: a.name) finally: CharField._unregister_lookup(UpperBilateral, 'upper')
def test_trim_transform(self): Author.objects.create(name=' John ') Author.objects.create(name='Rhonda') tests = ( (LTrim, 'John '), (RTrim, ' John'), (Trim, 'John'), ) for transform, trimmed_name in tests: with self.subTest(transform=transform): try: CharField.register_lookup(transform) authors = Author.objects.filter(**{'name__%s' % transform.lookup_name: trimmed_name}) self.assertQuerysetEqual(authors, [' John '], lambda a: a.name) finally: CharField._unregister_lookup(transform)
def test_unsalted_pwd_users(self): """Lists users with unsalted password""" # Unsalted MD5/SHA1: print(User.objects.filter(password__startswith='md5$$').count()) print(User.objects.filter(password__startswith='sha1$$').count()) # Salted MD5/SHA1: print(User.objects.filter(password__startswith='md5$').exclude(password__startswith='md5$$').count()) print(User.objects.filter(password__startswith='sha1$').exclude(password__startswith='sha1$$').count()) # Crypt hasher: print(User.objects.filter(password__startswith='crypt$$').count()) from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) # Unsalted MD5 passwords might not have an 'md5$$' prefix: print(User.objects.filter(password__length=32).count())
def test_func_transform_bilateral(self): class UpperBilateral(Upper): bilateral = True try: CharField.register_lookup(UpperBilateral, 'upper') Author.objects.create(name='John Smith', alias='smithj') Author.objects.create(name='Rhonda') authors = Author.objects.filter(name__upper__exact='john smith') self.assertQuerysetEqual( authors.order_by('name'), [ 'John Smith', ], lambda a: a.name ) finally: CharField._unregister_lookup(UpperBilateral, 'upper')
def test_ord_transform(self): try: CharField.register_lookup(Ord, 'ord') Author.objects.create(name='John Smith', alias='smithj') Author.objects.create(name='Élena Jordan', alias='elena') Author.objects.create(name='Rhonda') authors = Author.objects.annotate(first_initial=Left('name', 1)) self.assertQuerysetEqual( authors.filter(first_initial__ord=ord('J')), ['John Smith'], lambda x: x.name) self.assertQuerysetEqual( authors.exclude(first_initial__ord=ord('J')), ['Élena Jordan', 'Rhonda'], lambda x: x.name, ordered=False, ) finally: CharField._unregister_lookup(Ord, 'ord')
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['comments'] = self.dining_list.diningcomment_set.order_by( '-pinned_to_top', 'timestamp').all() # Last visit context['last_visited'] = DiningCommentVisitTracker.get_latest_visit( user=self.request.user, dining_list=self.dining_list, update=True) from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) context[ 'number_of_allergies'] = self.dining_list.internal_dining_entries( ).filter( user__userdiningsettings__allergies__length__gte=1).count() return context
def forwards_func(apps, schema_editor): """Prepend first Registration.prefix to User.username""" CharField.register_lookup(Length, 'length') User = apps.get_model('auth', 'User') qs = User.objects.filter(is_superuser=False)\ .exclude(username__contains='@')\ .exclude(username__length=32) for user in qs: useraccount = user.useraccount_set.first() if not useraccount: continue username = '******' % (useraccount.registration.prefix, user.username) user.username = username user.save()
def ready(self): setting_changed.connect(uninstall_if_needed) # Connections may already exist before we are called. for conn in connections.all(): if conn.vendor == 'postgresql': conn.introspection.data_types_reverse.update({ 3802: 'django.contrib.postgres.fields.JSONField', 3904: 'django.contrib.postgres.fields.IntegerRangeField', 3906: 'django.contrib.postgres.fields.DecimalRangeField', 3910: 'django.contrib.postgres.fields.DateTimeRangeField', 3912: 'django.contrib.postgres.fields.DateRangeField', 3926: 'django.contrib.postgres.fields.BigIntegerRangeField', }) if conn.connection is not None: register_type_handlers(conn) connection_created.connect(register_type_handlers) CharField.register_lookup(Unaccent) TextField.register_lookup(Unaccent) CharField.register_lookup(SearchLookup) TextField.register_lookup(SearchLookup) CharField.register_lookup(TrigramSimilar) TextField.register_lookup(TrigramSimilar) MigrationWriter.register_serializer(RANGE_TYPES, RangeSerializer)
def ready(self): # Connections may already exist before we are called. for conn in connections.all(): if conn.vendor == 'postgresql': conn.introspection.data_types_reverse.update({ 3802: 'django.contrib.postgresql.fields.JSONField', 3904: 'django.contrib.postgresql.fields.IntegerRangeField', 3906: 'django.contrib.postgresql.fields.FloatRangeField', 3910: 'django.contrib.postgresql.fields.DateTimeRangeField', 3912: 'django.contrib.postgresql.fields.DateRangeField', 3926: 'django.contrib.postgresql.fields.BigIntegerRangeField', }) if conn.connection is not None: register_type_handlers(conn) connection_created.connect(register_type_handlers) CharField.register_lookup(Unaccent) TextField.register_lookup(Unaccent) CharField.register_lookup(SearchLookup) TextField.register_lookup(SearchLookup) CharField.register_lookup(TrigramSimilar) TextField.register_lookup(TrigramSimilar)
def ready(self): connection_created.connect(register_hstore_handler) CharField.register_lookup(Unaccent) TextField.register_lookup(Unaccent) CharField.register_lookup(SearchLookup) TextField.register_lookup(SearchLookup) CharField.register_lookup(TrigramSimilar) TextField.register_lookup(TrigramSimilar)
class SearchView(View): CharField.register_lookup(Lower, "lower") TextField.register_lookup(Lower, "lower") template_name = 'news/search.html' def get(self, request, *args, **kwargs): search_query = request.GET.get('search', '') print(search_query.lower()) founded_news = News.objects.filter( Q(title__lower__icontains=search_query) | Q(text__lower__icontains=search_query)) context = {'founded_news': founded_news} return render(self.request, self.template_name, context)
def add_lookups(self): from django.db.models import CharField, TextField from django_mysql.models.lookups import CaseSensitiveExact, Soundex, SoundsLike CharField.register_lookup(CaseSensitiveExact) CharField.register_lookup(SoundsLike) CharField.register_lookup(Soundex) TextField.register_lookup(CaseSensitiveExact) TextField.register_lookup(SoundsLike) TextField.register_lookup(Soundex)
def ready(self): # Connections may already exist before we are called. for conn in connections.all(): if conn.connection is not None: register_hstore_handler(conn) connection_created.connect(register_hstore_handler) CharField.register_lookup(Unaccent) TextField.register_lookup(Unaccent) CharField.register_lookup(SearchLookup) TextField.register_lookup(SearchLookup) CharField.register_lookup(TrigramSimilar) TextField.register_lookup(TrigramSimilar)
def add_lookups(self): from django.db.models import CharField, TextField from django_mysql.models.lookups import ( CaseSensitiveExact, Soundex, SoundsLike ) CharField.register_lookup(CaseSensitiveExact) CharField.register_lookup(SoundsLike) CharField.register_lookup(Soundex) TextField.register_lookup(CaseSensitiveExact) TextField.register_lookup(SoundsLike) TextField.register_lookup(Soundex)
def ready(self): # Connections may already exist before we are called. for conn in connections.all(): if conn.vendor == 'postgresql': conn.introspection.data_types_reverse.update({ 3802: 'django.contrib.postgres.fields.JSONField', 3904: 'django.contrib.postgres.fields.IntegerRangeField', 3906: 'django.contrib.postgres.fields.DecimalRangeField', 3910: 'django.contrib.postgres.fields.DateTimeRangeField', 3912: 'django.contrib.postgres.fields.DateRangeField', 3926: 'django.contrib.postgres.fields.BigIntegerRangeField', }) if conn.connection is not None: register_type_handlers(conn) connection_created.connect(register_type_handlers) CharField.register_lookup(Unaccent) TextField.register_lookup(Unaccent) CharField.register_lookup(SearchLookup) TextField.register_lookup(SearchLookup) CharField.register_lookup(TrigramSimilar) TextField.register_lookup(TrigramSimilar)
'field': 'name', 'create_missing': True }, } CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', 'LOCATION': '/var/tmp/django_cache', } } from django.db.models import Q, CharField, TextField from django.db.models.functions import Lower CharField.register_lookup(Lower, "lower") TextField.register_lookup(Lower, "lower") DEFAULT_CHARSET = 'UTF-8' MINUTES_AGO_HIT_COUNT = 15 IPV6_IP2LOCATION_PATH = "data/IP2LOCATION-LITE-DB1.BIN/IP2LOCATION-LITE-DB1.BIN" IPV4_IP2LOCATION_PATH = "data/IP2LOCATION-LITE-DB1.IPV6.BIN/IP2LOCATION-LITE-DB1.IPV6.BIN" IMPORT_EXPORT_USE_TRANSACTIONS = True MAX_PHOTO_SIZE = 10000000 MAX_PHOTO_SIZE_MB = int(MAX_PHOTO_SIZE / 1000 / 1000) FILE_UPLOAD_DIRECTORY_PERMISSIONS = 0o755 FILE_UPLOAD_PERMISSIONS = 0o644 #mailing
from django.apps import AppConfig
#************************************************ from django.db.models import Transform, Field class UpperCase(Transform): lookup_name = 'upper' bilateral = True def as_sql(self, compiler, connection): lhs, params = compiler.compile(self.lhs) return "UPPER(%s)" % lhs, params from django.db.models import CharField, TextField CharField.register_lookup(UpperCase) TextField.register_lookup(UpperCase) class AbsoluteValue(Transform): lookup_name = 'abs' def as_sql(self, compiler, connection): lhs, params = compiler.compile(self.lhs) return "ABS(%s)" % lhs, params @property def output_field(self): return FloatField() #*****************************8
def ready(self): connection_created.connect(register_hstore_handler) CharField.register_lookup(Unaccent) TextField.register_lookup(Unaccent)
class Levenshtein(Transform): lookup_name = 'levenshtein' def as_sql(self, qn, connection): lhs, lhs_params = qn.compile(self.lhs) rhs, rhs_params = self.process_rhs(qn, connection) params = lhs_params + rhs_params return 'levenshtein(%s, %s)' % (lhs), params @property def output_field(self): return PositiveIntegerField() class DoubleMetaphone(Transform): lookup_name = 'dmetaphone' def as_sql(self, qn, connection): lhs, params = qn.compile(self.lhs) return 'dmetaphone(%s)' % (lhs), params @property def output_field(self): return CharField() CharField.register_lookup(SoundsLike) CharField.register_lookup(Soundex) CharField.register_lookup(Difference) CharField.register_lookup(DoubleMetaphone)
def ready(self): CharField.register_lookup(Unaccent) TextField.register_lookup(Unaccent)
from .forms import * from django.contrib import messages from django.core.exceptions import ValidationError import random from django.core.paginator import Paginator, EmptyPage from django.http import HttpResponse import json #from datetime import timedelta #from django.utils.timezone import timezone #from django.db.models.signals import pre_save #from django.dispatch import receiver from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) categories = Category.objects.all().order_by('category') ###SEARCH PAGE def search(request): global categories query = request.GET.get('search') if query: related_articles = {} try: # if there is only one article with that name, then itwill return that article = MainArticle.objects.get( title__istartswith=query ) #_iexact exactly matches the word with case insensitive
Company, Workplace, Department, Position, Equipment, Employee, Commission, Event, DepartmentType, EventEmployee, EventType, AssessmentCard, ) CharField.register_lookup(Lower) class CompanyFilter(filters.FilterSet): class Meta: model = Company fields = ('active',) class WorkplaceFilter(filters.FilterSet): class Meta: model = Workplace fields = ('active', 'department') class DepartmentFilter(filters.FilterSet):
redis_host = os.getenv('{}_SERVICE_HOST'.format(redis_key_name), redis_service_name) redis_port = os.getenv('{}_SERVICE_PORT'.format(redis_key_name), '6379') redis_password = os.getenv('REDIS_PASSWORD') r = redis.StrictRedis(host=redis_host, port=redis_port, db=0, password=redis_password) r_history = redis.StrictRedis(host=redis_host, port=redis_port, db=1, password=redis_password) schema_manager = SchemaManager() configurator = Configurator() CharField.register_lookup(Length, 'length') def admin(request): if not redis_service_name: raise Exception( 'The REDIS_SERVICE_NAME environment variable must be defined. REDIS_SERVICE_NAME=redis.' ) # Get all keys in redis pending_requests = [] rkeys = r.scan()[1] rkeys = [byte.decode("utf-8") for byte in rkeys] # # logger.info('------rkeys-----') # logger.info(rkeys)