def test_query_extract(books): assert list( Book.objects.annotate(author=JSONExtract('data', 'author')).order_by( 'author').values_list('author', flat=True)) == [ 'Rowling', 'Tolkien', ] assert Book.objects.annotate( year=JSONExtract('data', 'publication', 'year')).filter( year=1997).count() == 1 assert Book.objects.annotate( year=JSONExtract('data', 'publication', 'year')).filter( year=1998).count() == 0
def render(self, form_data: dict) -> Tuple[str, str, str]: qs = OrderPosition.objects.filter( order__event=self.event, item_id__in=form_data['items'] ).prefetch_related( 'answers', 'answers__question' ).select_related('order', 'item', 'variation', 'addon_to') if not form_data.get('include_addons'): qs = qs.filter(addon_to__isnull=True) if form_data.get('include_pending'): qs = qs.filter(order__status__in=[Order.STATUS_PAID, Order.STATUS_PENDING]) else: qs = qs.filter(order__status__in=[Order.STATUS_PAID]) if form_data.get('order_by') == 'name': qs = qs.order_by('attendee_name_cached', 'order__code') elif form_data.get('order_by') == 'code': qs = qs.order_by('order__code') elif form_data.get('order_by', '').startswith('name:'): part = form_data['order_by'][5:] qs = qs.annotate( resolved_name=Coalesce('attendee_name_parts', 'addon_to__attendee_name_parts', 'order__invoice_address__name_parts') ).annotate( resolved_name_part=JSONExtract('resolved_name', part) ).order_by( 'resolved_name_part' ) outbuffer = render_pdf(self.event, qs, OPTIONS[form_data.get('rendering', 'one')]) return 'badges.pdf', 'application/pdf', outbuffer.read()
def _get_queryset(self, cl, form_data): cqs = Checkin.objects.filter( position_id=OuterRef('pk'), list_id=cl.pk ).order_by().values('position_id').annotate( m=Max('datetime') ).values('m') qs = OrderPosition.objects.filter( order__event=self.event, ).annotate( last_checked_in=Subquery(cqs), auto_checked_in=Exists( Checkin.objects.filter(position_id=OuterRef('pk'), list_id=cl.pk, auto_checked_in=True) ) ).prefetch_related( 'answers', 'answers__question', 'addon_to__answers', 'addon_to__answers__question' ).select_related('order', 'item', 'variation', 'addon_to', 'order__invoice_address', 'voucher', 'seat') if not cl.all_products: qs = qs.filter(item__in=cl.limit_products.values_list('id', flat=True)) if cl.subevent: qs = qs.filter(subevent=cl.subevent) if form_data['sort'] == 'name': qs = qs.order_by( Coalesce( NullIf('attendee_name_cached', Value('')), NullIf('addon_to__attendee_name_cached', Value('')), NullIf('order__invoice_address__name_cached', Value('')), 'order__code' ) ) elif form_data['sort'] == 'code': qs = qs.order_by('order__code') elif form_data['sort'].startswith('name:'): part = form_data['sort'][5:] qs = qs.annotate( resolved_name=Case( When(attendee_name_cached__ne='', then='attendee_name_parts'), When(addon_to__attendee_name_cached__isnull=False, addon_to__attendee_name_cached__ne='', then='addon_to__attendee_name_parts'), default='order__invoice_address__name_parts', ) ).annotate( resolved_name_part=JSONExtract('resolved_name', part) ).order_by( 'resolved_name_part' ) if form_data.get('attention_only'): qs = qs.filter(Q(item__checkin_attention=True) | Q(order__checkin_attention=True)) if not cl.include_pending: qs = qs.filter(order__status=Order.STATUS_PAID) else: qs = qs.filter(order__status__in=(Order.STATUS_PAID, Order.STATUS_PENDING)) return qs
def render(self, form_data): merger = PdfFileMerger() qs = OrderPosition.objects.filter( order__event__in=self.events ).prefetch_related( 'answers', 'answers__question' ).select_related('order', 'item', 'variation', 'addon_to') if form_data.get('include_pending'): qs = qs.filter(order__status__in=[Order.STATUS_PAID, Order.STATUS_PENDING]) else: qs = qs.filter(order__status__in=[Order.STATUS_PAID]) if form_data.get('order_by') == 'name': qs = qs.order_by('attendee_name_cached', 'order__code') elif form_data.get('order_by') == 'code': qs = qs.order_by('order__code') elif form_data.get('order_by', '').startswith('name:'): part = form_data['order_by'][5:] qs = qs.annotate( resolved_name=Coalesce('attendee_name_parts', 'addon_to__attendee_name_parts', 'order__invoice_address__name_parts') ).annotate( resolved_name_part=JSONExtract('resolved_name', part) ).order_by( 'resolved_name_part' ) o = PdfTicketOutput(Event.objects.none()) for op in qs: if not op.generate_ticket: continue if op.order.event != o.event: o = PdfTicketOutput(op.event) with language(op.order.locale, o.event.settings.region): layout = o.layout_map.get( (op.item_id, op.order.sales_channel), o.layout_map.get( (op.item_id, 'web'), o.default_layout ) ) outbuffer = o._draw_page(layout, op, op.order) merger.append(ContentFile(outbuffer.read())) outbuffer = BytesIO() merger.write(outbuffer) merger.close() outbuffer.seek(0) if self.is_multievent: return '{}_tickets.pdf'.format(self.events.first().organizer.slug), 'application/pdf', outbuffer.read() else: return '{}_tickets.pdf'.format(self.event.slug), 'application/pdf', outbuffer.read()
def render(self, form_data: dict) -> Tuple[str, str, str]: qs = OrderPosition.objects.filter( order__event=self.event, item_id__in=form_data['items']).prefetch_related( 'answers', 'answers__question').select_related('order', 'item', 'variation', 'addon_to') if not form_data.get('include_addons'): qs = qs.filter(addon_to__isnull=True) if form_data.get('include_pending'): qs = qs.filter( order__status__in=[Order.STATUS_PAID, Order.STATUS_PENDING]) else: qs = qs.filter(order__status__in=[Order.STATUS_PAID]) if form_data.get('date_from'): dt = make_aware( datetime.combine( dateutil.parser.parse(form_data['date_from']).date(), time(hour=0, minute=0, second=0)), self.event.timezone) qs = qs.filter( Q(subevent__date_from__gte=dt) | Q(subevent__isnull=True, order__event__date_from__gte=dt)) if form_data.get('date_to'): dt = make_aware( datetime.combine( dateutil.parser.parse(form_data['date_to']).date() + timedelta(days=1), time(hour=0, minute=0, second=0)), self.event.timezone) qs = qs.filter( Q(subevent__date_from__lt=dt) | Q(subevent__isnull=True, order__event__date_from__lt=dt)) if form_data.get('order_by') == 'name': qs = qs.order_by('attendee_name_cached', 'order__code') elif form_data.get('order_by') == 'code': qs = qs.order_by('order__code') elif form_data.get('order_by') == 'date': qs = qs.annotate(ed=Coalesce('subevent__date_from', 'order__event__date_from')).order_by( 'ed', 'order__code') elif form_data.get('order_by', '').startswith('name:'): part = form_data['order_by'][5:] qs = qs.annotate(resolved_name=Coalesce( 'attendee_name_parts', 'addon_to__attendee_name_parts', 'order__invoice_address__name_parts')).annotate( resolved_name_part=JSONExtract( 'resolved_name', part)).order_by('resolved_name_part') outbuffer = render_pdf(self.event, qs, OPTIONS[form_data.get('rendering', 'one')]) return 'badges.pdf', 'application/pdf', outbuffer.read()
def render(self, form_data): merger = PdfFileMerger() o = PdfTicketOutput(self.event) qs = OrderPosition.objects.filter( order__event=self.event).prefetch_related( 'answers', 'answers__question').select_related('order', 'item', 'variation', 'addon_to') if form_data.get('include_pending'): qs = qs.filter( order__status__in=[Order.STATUS_PAID, Order.STATUS_PENDING]) else: qs = qs.filter(order__status__in=[Order.STATUS_PAID]) if form_data.get('order_by') == 'name': qs = qs.order_by('attendee_name_cached', 'order__code') elif form_data.get('order_by') == 'code': qs = qs.order_by('order__code') elif form_data.get('order_by', '').startswith('name:'): part = form_data['order_by'][5:] qs = qs.annotate(resolved_name=Coalesce( 'attendee_name_parts', 'addon_to__attendee_name_parts', 'order__invoice_address__name_parts')).annotate( resolved_name_part=JSONExtract( 'resolved_name', part)).order_by('resolved_name_part') for op in qs: if op.addon_to_id and not self.event.settings.ticket_download_addons: continue if not op.item.admission and not self.event.settings.ticket_download_nonadm: continue with language(op.order.locale): buffer = BytesIO() p = o._create_canvas(buffer) layout = o.layout_map.get(op.item_id, o.default_layout) o._draw_page(layout, p, op, op.order) p.save() outbuffer = o._render_with_background(layout, buffer) merger.append(ContentFile(outbuffer.read())) outbuffer = BytesIO() merger.write(outbuffer) merger.close() outbuffer.seek(0) return '{}_tickets.pdf'.format( self.event.slug), 'application/pdf', outbuffer.read()
def _get_queryset(self, cl, form_data): cqs = Checkin.objects.filter( position_id=OuterRef('pk'), list_id=cl.pk).order_by().values('position_id').annotate( m=Max('datetime')).values('m') cqsin = cqs.filter(type=Checkin.TYPE_ENTRY) cqsout = cqs.filter(type=Checkin.TYPE_EXIT) qs = OrderPosition.objects.filter(order__event=self.event, ).annotate( last_checked_in=Subquery(cqsin), last_checked_out=Subquery(cqsout), auto_checked_in=Exists( Checkin.objects.filter( position_id=OuterRef('pk'), list_id=cl.pk, auto_checked_in=True))).prefetch_related( 'answers', 'answers__question', 'addon_to__answers', 'addon_to__answers__question').select_related( 'order', 'item', 'variation', 'addon_to', 'order__invoice_address', 'voucher', 'seat') if not cl.all_products: qs = qs.filter( item__in=cl.limit_products.values_list('id', flat=True)) if cl.subevent: qs = qs.filter(subevent=cl.subevent) if form_data.get('date_from'): dt = make_aware( datetime.combine( dateutil.parser.parse(form_data['date_from']).date(), time(hour=0, minute=0, second=0)), self.event.timezone) qs = qs.filter(subevent__date_from__gte=dt) if form_data.get('date_to'): dt = make_aware( datetime.combine( dateutil.parser.parse(form_data['date_to']).date() + timedelta(days=1), time(hour=0, minute=0, second=0)), self.event.timezone) qs = qs.filter(subevent__date_from__lt=dt) o = () if self.event.has_subevents and not cl.subevent: o = ('subevent__date_from', 'subevent__name') sort = form_data.get('sort') or 'name' if sort == 'name': qs = qs.order_by( *o, Coalesce( NullIf('attendee_name_cached', Value('')), NullIf('addon_to__attendee_name_cached', Value('')), NullIf('order__invoice_address__name_cached', Value('')), 'order__code')) elif sort == 'code': qs = qs.order_by(*o, 'order__code') elif sort.startswith('name:'): part = sort[5:] qs = qs.annotate(resolved_name=Case( When(attendee_name_cached__ne='', then='attendee_name_parts'), When(addon_to__attendee_name_cached__isnull=False, addon_to__attendee_name_cached__ne='', then='addon_to__attendee_name_parts'), default='order__invoice_address__name_parts', )).annotate(resolved_name_part=JSONExtract( 'resolved_name', part)).order_by(*o, 'resolved_name_part') if form_data.get('attention_only'): qs = qs.filter( Q(item__checkin_attention=True) | Q(order__checkin_attention=True)) if not cl.include_pending: qs = qs.filter(order__status=Order.STATUS_PAID) else: qs = qs.filter(order__status__in=(Order.STATUS_PAID, Order.STATUS_PENDING)) return qs