def update_output(cls, execution_id, stdout, stderr): """ Append to stdout & stderr. Use concatenation to efficiently update the fields. """ query = Execution.objects.filter(id=execution_id) query.update(stdout=functions.Concat('stdout', models.Value(stdout or '')), stderr=functions.Concat('stderr', models.Value(stderr or '')))
def busca(request): termo = request.GET.get('termo') if termo is None: messages.add_message(request, messages.ERROR, 'A página de busca precisa do parametro "termo"') return redirect('contatos') if not termo: return redirect('contatos') campos = functions.Concat('nome', Value(' '), 'sobrenome') contatos = Contato.objects.annotate(nome_completo=campos).filter( Q(nome_completo__icontains=termo) | Q(telefone__icontains=termo), mostrar=True) paginator = Paginator(contatos, 20) page_params = request.GET.get('pagina') contatos = paginator.get_page(page_params) return render(request, 'contatos/busca.html', { 'contatos': contatos, 'mostrar_busca': True })
def get_queryset(self, request): return super().get_queryset(request).annotate(_name=functions.Concat( models.F('cellar'), models.Value(' '), functions.LPad( functions.Cast(models.F('number'), output_field=models.CharField( max_length=2)), 2, models.Value('0')), output_field=models.CharField(max_length=5)))
def set_fid(queryset, path, stdout): model = queryset.model._meta.label qs = queryset.filter(fid=None) base_url = "{}{}".format(settings.FUNKWHALE_URL, path) stdout.write( "* Assigning federation ids to {} entries (path: {})".format(model, base_url) ) new_fid = functions.Concat(Value(base_url), F("uuid"), output_field=CharField()) total = qs.update(fid=new_fid) stdout.write(" * {} entries updated".format(total))
def lookup_by_model(prefix, value, comparison): query = models.Q(**{f"{prefix}__union_field__{comparison}": value}) annotation = { f"{prefix}__union_field": functions.Concat( models.F(f"{prefix}__lookup_field1"), models.Value(" "), models.F(f"{prefix}__lookup_field2"), ) } return query, annotation
def with_author(self): return self.select_related( 'author', 'category', ).annotate( author_name=functions.Concat( 'author__first_name', models.Value(' '), 'author__last_name', ), ).order_by( '-published_at', )
def get_queryset(self): queryset = UserModel.objects\ .prefetch_related( Prefetch('groups', queryset=Group.objects.all()) )\ .annotate(full_name=functions.Concat( 'first_name', Value(' '), 'last_name', output=CharField(), )) search = self.request.GET.get('search', None) if search: queryset = queryset.filter(full_name__icontains=search) return queryset
class PhoneAdmin(TrackingModelAdmin, ShowCountryMixin, ShowDeletedMixin, admin.ModelAdmin): list_display = ('number_intl', 'profile_link', 'country_code', 'display_country', 'type', 'is_deleted') list_select_related = ('profile__user', ) search_fields = ('number', 'country', 'profile__first_name', 'profile__last_name') list_filter = ('type', 'deleted_on', CountryMentionedOnlyFilter) fieldsets = ( (None, { 'fields': ( 'profile', 'number', 'country', 'type', 'comments', ) }), (_('Important dates'), { 'fields': TrackingModelAdmin.fields }), ) fields = None raw_id_fields = ('profile', ) radio_fields = {'type': admin.VERTICAL} inlines = [ VisibilityInLine, ] def number_intl(self, obj): return obj.number.as_international number_intl.short_description = _("number") number_intl.admin_order_field = 'number' def profile_link(self, obj): return format_html('<a href="{url}">{name}</a>', url=obj.profile.get_admin_url(), name=obj.profile) profile_link.short_description = _("profile") profile_link.admin_order_field = dbf.Concat('profile__first_name', V(","), 'profile__last_name', V(","), 'profile__user__username') def country_code(self, obj): return obj.number.country_code country_code.short_description = _("country code")
def generate_actor_urls(part, stdout): field = "{}_url".format(part) stdout.write("* Update {} for local actors...".format(field)) queryset = federation_models.Actor.objects.local().filter(**{field: None}) base_url = "{}/federation/actors/".format(settings.FUNKWHALE_URL) new_field = functions.Concat( Value(base_url), F("preferred_username"), Value("/{}".format(part)), output_field=CharField(), ) queryset.update(**{field: new_field})
def get_person_fields(request): personas = list( PersonaNatural.objects.exclude( perfil_datos__datos_usuario__is_superuser=True).annotate( text=functions.Concat(F('nombre'), Value(' '), F( 'apellidos'), Value(' ('), F('ci'), Value(')'))).values( 'id', 'nombre', 'apellidos', 'email_address', 'ci', 'text', )) data = dict() data['personas'] = personas return JsonResponse(data=data, status=200)
def as_sql(self, compiler, connection): """ Returns tuple: sql, params -- where %s is used in place of caller/user-supplied values in the SQL, and `params` is that list of values. In this case, we want a Concat function that will produce the desired composite value from the subparts and separator defined in the main field. Fortunately, Django has an existing Concat function expression class we can use. We just have to turn *everything* into an `Expression` class first. """ sep_exp = models.expressions.Value(self.target.separator) exps_nested = [[f.get_col(self.alias), sep_exp] for f in self.target.partfields] exps_flattened = [item for sublist in exps_nested for item in sublist] return_exp = functions.Concat(*exps_flattened[:-1]) return return_exp.as_sql(compiler, connection)
def __init__(self): super().__init__( fields={ # example: model field # Order by order creation date 'created': 'created', # example: expression on related model # Order by user full name 'user': functions.Concat( models.F('user__first_name'), models.Value(' '), models.F('user__last_name'), ), # example: aggregate expression 'total_quantity': aggregates.Sum(models.F('order_lines__quantity')), })
def concat(*args): """ Concatenates the values of all given fields or expressions. """ return functions.Concat(*args)
def handle(self, *args, **options): self.verbosity = options['verbosity'] mapped_cities = (Whereabouts.objects.annotate( lookup=dbf.Concat('name', V('###'), 'state', V('###'), 'country', output_field=CharField())).filter( type=LocationType.CITY).values_list( 'lookup', flat=True)) city_list = (Place.all_objects.annotate(city_lookup=dbf.Concat( dbf.Upper('city'), V('###'), Case(When(country__in=countries_with_mandatory_region(), then=dbf.Upper('state_province')), default=V('')), V('###'), 'country', output_field=CharField())).exclude(city='').exclude( city_lookup__in=mapped_cities)) success_counter = 0 mapped_set = set() for place in city_list: if place.city_lookup not in mapped_set: city_location = geocode_city( place.city, state_province=place.subregion.latin_name or place.subregion.latin_code, country=place.country.code) if city_location: whereabouts = Whereabouts.objects.create( type=LocationType.CITY, name=place.city.upper(), state=(place.state_province.upper() if place.country in countries_with_mandatory_region() else ''), country=place.country, bbox=LineString(city_location.bbox['southwest'], city_location.bbox['northeast'], srid=SRID), center=Point(city_location.xy, srid=SRID), ) if self.verbosity >= 2: self.stdout.write( make_style( fg='green')(f"+ Mapped {whereabouts!r}")) success_counter += 1 else: if self.verbosity >= 2: region = f"R:{place.subregion.iso_code}, " if place.subregion.pk else "" self.stdout.write( f"- {place.city} ({region}{place.country}) could not be mapped" ) mapped_set.add(place.city_lookup) else: city_location = None if self.verbosity >= 3: self.stdout.write( f"* {place.city} ({place.country}) skipped (already processed in this session)" ) if city_location is not None and city_location.remaining_api_calls < 500: self.stdout.write( self.style.ERROR( "Daily geocoding requests limit exhausted. Please continue on a different day!" )) break if self.verbosity >= 1: self.stdout.write( make_style(opts=('bold', ), fg='white')(f"[MAPPED {success_counter} CITIES]"))
class PlaceAdmin(TrackingModelAdmin, ShowCountryMixin, ShowDeletedMixin, admin.ModelAdmin): list_display = ( 'id', 'city', 'postcode', 'state_province', 'display_country', 'display_location', # 'max_host', 'max_night', 'contact_before', 'available', 'in_book', 'owner_link', 'confirmed_on', 'checked_by__name', 'is_deleted', 'modified', ) list_display_links = ( 'id', 'city', 'state_province', 'display_country', ) search_fields = ( 'address', 'city', 'postcode', 'country', 'state_province', 'owner__first_name', 'owner__last_name', 'owner__user__email', ) list_filter = ( 'confirmed_on', 'checked_on', 'in_book', 'available', PlaceHasLocationFilter, 'deleted_on', CountryMentionedOnlyFilter, ) fieldsets = ( (None, { 'fields': ( 'owner', 'country', 'state_province', ('city', 'closest_city'), 'postcode', 'address', 'location', ) }), (_('Conditions'), { 'fields': ( 'description', 'short_description', ('max_guest', 'max_night', 'contact_before'), 'conditions', 'available', 'in_book', ('tour_guide', 'have_a_drink'), 'sporadic_presence', 'family_members', ) }), (_('Permissions'), { 'fields': ('authorized_users', ) }), (_('Important dates'), { 'fields': TrackingModelAdmin.fields }), ) fields = None formfield_overrides = { PointField: { 'widget': AdminMapboxGlWidget }, } raw_id_fields = ( 'owner', 'authorized_users', ) filter_horizontal = ('family_members', ) inlines = [ VisibilityInLine, ] def display_location(self, obj): return (' '.join([ '{point.y:.4f} {point.x:.4f}'.format( point=obj.location), '{symbol}{precision}'.format( symbol=chr(8982), precision=obj.location_confidence or 0) ]) if obj.location else None) display_location.short_description = _("location") def owner_link(self, obj): return format_html('<a href="{url}">{name}</a>', url=obj.owner.get_admin_url(), name=obj.owner) owner_link.short_description = _("owner") owner_link.admin_order_field = dbf.Concat('owner__first_name', V(","), 'owner__last_name', V(","), 'owner__user__username') def checked_by__name(self, obj): try: return obj.checked_by.username except AttributeError: return '-' checked_by__name.short_description = _("approved by") checked_by__name.admin_order_field = 'checked_by__username' class FamilyMemberRepr(Profile): class Meta: ordering = ['first_name', 'last_name', 'id'] proxy = True def __str__(self): return "(p:%05d, u:%05d) %s" % (self.pk, self.user_id if self.user_id else 0, super().__str__()) @lru_cache(maxsize=None) def get_queryset(self, request): qs = super().get_queryset(request).select_related( 'owner__user', 'checked_by') qs = qs.defer('owner__description') try: if not self.single_object_view: qs = qs.defer('description', 'short_description', 'address') except Exception: pass return qs def get_field_queryset(self, db, db_field, request): if db_field.name == 'family_members': return PlaceAdmin.FamilyMemberRepr.objects.select_related('user') return super().get_field_queryset(db, db_field, request) def change_view(self, request, object_id, form_url='', extra_context=None): self.single_object_view = True return super().change_view(request, object_id, form_url=form_url, extra_context=extra_context) def add_view(self, request, form_url='', extra_context=None): self.single_object_view = True return super().add_view(request, form_url=form_url, extra_context=extra_context) def changelist_view(self, request, extra_context=None): self.single_object_view = False return super().changelist_view(request, extra_context=extra_context)