def get_ticket_lists(self, event_slug): event = Event.objects.get( slug=event_slug ) row = namedtuple('Row', 'ticket_type,current_tier,next_tier,at_the_door') rows = [] valid_ticket_types = event.tickettype_set.filter(tickettier__isnull=False).distinct() for ticket_type in valid_ticket_types: current_tier = ( TicketTier.objects.best_tier(ticket_type, error_as_none=True) or TicketTier.objects.filter( Q(access_code=None) | Q(access_code=''), ticket_type=ticket_type, end_date__gte=get_now() ).earliest('start_date') ) if current_tier: next_tier = TicketTier.objects.next_tier(ticket_type, current_tier) at_the_door = TicketTier.objects.filter(ticket_type=ticket_type).latest('price') rows.append( row(ticket_type, current_tier, next_tier, at_the_door)._asdict() ) return {'rows': rows}
def is_expirable(self): if self.is_active: return False one_day_ago = get_now() - datetime.timedelta(days=1) if self.updated_at < one_day_ago: return True return False
def best_tier(self, ticket_type, access_code=None, error_as_none=False): now = get_now() access_code_query = Q(access_code=None) | Q(access_code='') if access_code: access_code_query |= Q(access_code__iexact=access_code) tiers = self.filter( access_code_query, ticket_type=ticket_type, start_date__lte=now, end_date__gte=now ).annotate( ticket_count=Count('ticket') ).filter( ticket_count__lt=F('capacity') ) if tiers.count(): return tiers.earliest('price') if error_as_none: return None raise TicketTier.NoTiersAvailableError()
def quick_abandon(modeladmin, request, queryset): updated = queryset.filter( is_paid_override=False, is_abandoned=False, is_pending=False, created_at__lt=get_now() - datetime.timedelta(hours=1) ).update( is_abandoned=True ) messages.info(request, "{0} ticket object(s) abandoned".format(updated))
def in_time(self, obj): now = get_now() if obj.start_date < now < obj.end_date: delta = obj.end_date - now delta = timedelta(delta.days, delta.seconds) state = 'closes in {}'.format(delta) elif obj.start_date > now: delta = obj.start_date - now delta = timedelta(delta.days, delta.seconds) state = 'opens in {}'.format(delta) elif obj.end_date < now: state = 'closed' else: state = '???' return state
def next_tier(self, ticket_type, current_tier): now = get_now() query = Q(price__gt=current_tier.price) | Q(start_date__gte=current_tier.start_date) access_code_query = Q(access_code=None) | Q(access_code='') tiers = self.filter( query, access_code_query, ticket_type=ticket_type, end_date__gte=now, ).exclude( pk=current_tier.pk ).annotate( ticket_count=Count('ticket') ).filter( ticket_count__lt=F('capacity') ) if tiers.count(): return tiers.earliest('price')
def now(): return get_now() - timedelta(seconds=10)
def is_available(self): return ( self.start_date <= get_now() <= self.end_date and self.remaining > 0 )