<s> import datetime <EOL> from django . contrib . sites . models import Site <EOL> from django . db import models <EOL> try : <EOL> from django . utils . timezone import now <EOL> except ImportError : <EOL> now = datetime . datetime . now <EOL> class AdManager ( models . Manager ) : <EOL> """<STR_LIT>""" <EOL> def get_random_ad ( self , ad_zone , ad_category = None ) : <EOL> """<STR_LIT>""" <EOL> qs = self . get_queryset ( ) . filter ( start_showing__lte = now ( ) , <EOL> stop_showing__gte = now ( ) , <EOL> zone__slug = ad_zone , <EOL> sites = Site . objects . get_current ( ) . pk <EOL> ) . select_related ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> if ad_category : <EOL> qs = qs . filter ( category__slug = ad_category ) <EOL> try : <EOL> ad = qs . order_by ( '<STR_LIT:?>' ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return None <EOL> return ad </s>
