<s> from datetime import timedelta <EOL> from decimal import Decimal as D <EOL> from decimal import ROUND_UP <EOL> from django . db . models import Avg , Count , Sum <EOL> from django . utils . timezone import now <EOL> from django . views . generic import TemplateView <EOL> from oscar . apps . promotions . models import AbstractPromotion <EOL> from oscar . core . compat import get_user_model <EOL> from oscar . core . loading import get_model <EOL> ConditionalOffer = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Voucher = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Basket = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> StockAlert = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Product = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Order = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Line = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> User = get_user_model ( ) <EOL> class IndexView ( TemplateView ) : <EOL> """<STR_LIT>""" <EOL> def get_template_names ( self ) : <EOL> if self . request . user . is_staff : <EOL> return [ '<STR_LIT>' , ] <EOL> else : <EOL> return [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def get_context_data ( self , ** kwargs ) : <EOL> ctx = super ( IndexView , self ) . get_context_data ( ** kwargs ) <EOL> ctx . update ( self . get_stats ( ) ) <EOL> return ctx <EOL> def get_active_site_offers ( self ) : <EOL> """<STR_LIT>""" <EOL> return ConditionalOffer . objects . filter ( <EOL> end_datetime__gt = now ( ) , offer_type = ConditionalOffer . SITE ) <EOL> def get_active_vouchers ( self ) : <EOL> """<STR_LIT>""" <EOL> return Voucher . objects . filter ( end_datetime__gt = now ( ) ) <EOL> def get_number_of_promotions ( self , abstract_base = AbstractPromotion ) : <EOL> """<STR_LIT>""" <EOL> total = <NUM_LIT:0> <EOL> for cls in abstract_base . __subclasses__ ( ) : <EOL> if cls . _meta . abstract : <EOL> total += self . get_number_of_promotions ( cls ) <EOL> else : <EOL> total += cls . objects . count ( ) <EOL> return total <EOL> def get_open_baskets ( self , filters = None ) : <EOL> """<STR_LIT>""" <EOL> if filters is None : <EOL> filters = { } <EOL> filters [ '<STR_LIT:status>' ] = Basket . OPEN <EOL> return Basket . objects . filter ( ** filters ) <EOL> def get_hourly_report ( self , hours = <NUM_LIT> , segments = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> time_now = now ( ) . replace ( minute = <NUM_LIT:0> , second = <NUM_LIT:0> ) <EOL> start_time = time_now - timedelta ( hours = hours - <NUM_LIT:1> ) <EOL> orders_last_day = Order . objects . filter ( date_placed__gt = start_time ) <EOL> order_total_hourly = [ ] <EOL> for hour in range ( <NUM_LIT:0> , hours , <NUM_LIT:2> ) : <EOL> end_time = start_time + timedelta ( hours = <NUM_LIT:2> ) <EOL> hourly_orders = orders_last_day . filter ( date_placed__gt = start_time , <EOL> date_placed__lt = end_time ) <EOL> total = hourly_orders . aggregate ( <EOL> Sum ( '<STR_LIT>' ) <EOL> ) [ '<STR_LIT>' ] or D ( '<STR_LIT>' ) <EOL> order_total_hourly . append ( { <EOL> '<STR_LIT>' : end_time , <EOL> '<STR_LIT>' : total <EOL> } ) <EOL> start_time = end_time <EOL> max_value = max ( [ x [ '<STR_LIT>' ] for x in order_total_hourly ] ) <EOL> divisor = <NUM_LIT:1> <EOL> while divisor < max_value / <NUM_LIT:50> : <EOL> divisor *= <NUM_LIT:10> <EOL> max_value = ( max_value / divisor ) . quantize ( D ( '<STR_LIT:1>' ) , rounding = ROUND_UP ) <EOL> max_value *= divisor <EOL> if max_value : <EOL> segment_size = ( max_value ) / D ( '<STR_LIT>' ) <EOL> for item in order_total_hourly : <EOL> item [ '<STR_LIT>' ] = int ( item [ '<STR_LIT>' ] / segment_size ) <EOL> y_range = [ ] <EOL> y_axis_steps = max_value / D ( str ( segments ) ) <EOL> for idx in reversed ( range ( segments + <NUM_LIT:1> ) ) : <EOL> y_range . append ( idx * y_axis_steps ) <EOL> else : <EOL> y_range = [ ] <EOL> for item in order_total_hourly : <EOL> item [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> ctx = { <EOL> '<STR_LIT>' : order_total_hourly , <EOL> '<STR_LIT>' : max_value , <EOL> '<STR_LIT>' : y_range , <EOL> } <EOL> return ctx <EOL> def get_stats ( self ) : <EOL> datetime_24hrs_ago = now ( ) - timedelta ( hours = <NUM_LIT> ) <EOL> orders = Order . objects . filter ( ) <EOL> orders_last_day = orders . filter ( date_placed__gt = datetime_24hrs_ago ) <EOL> open_alerts = StockAlert . objects . filter ( status = StockAlert . OPEN ) <EOL> closed_alerts = StockAlert . objects . filter ( status = StockAlert . CLOSED ) <EOL> total_lines_last_day = Line . objects . filter ( <EOL> order__in = orders_last_day ) . count ( ) <EOL> stats = { <EOL> '<STR_LIT>' : orders_last_day . count ( ) , <EOL> '<STR_LIT>' : total_lines_last_day , <EOL> '<STR_LIT>' : orders_last_day . aggregate ( <EOL> Avg ( '<STR_LIT>' ) <EOL> ) [ '<STR_LIT>' ] or D ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : orders_last_day . aggregate ( <EOL> Sum ( '<STR_LIT>' ) <EOL> ) [ '<STR_LIT>' ] or D ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : self . get_hourly_report ( hours = <NUM_LIT> ) , <EOL> '<STR_LIT>' : User . objects . filter ( <EOL> date_joined__gt = datetime_24hrs_ago , <EOL> ) . count ( ) , <EOL> '<STR_LIT>' : self . get_open_baskets ( { <EOL> '<STR_LIT>' : datetime_24hrs_ago <EOL> } ) . count ( ) , <EOL> '<STR_LIT>' : Product . objects . count ( ) , <EOL> '<STR_LIT>' : open_alerts . count ( ) , <EOL> '<STR_LIT>' : closed_alerts . count ( ) , <EOL> '<STR_LIT>' : self . get_active_site_offers ( ) . count ( ) , <EOL> '<STR_LIT>' : self . get_active_vouchers ( ) . count ( ) , <EOL> '<STR_LIT>' : self . get_number_of_promotions ( ) , <EOL> '<STR_LIT>' : User . objects . count ( ) , <EOL> '<STR_LIT>' : self . get_open_baskets ( ) . count ( ) , <EOL> '<STR_LIT>' : orders . count ( ) , <EOL> '<STR_LIT>' : Line . objects . filter ( order__in = orders ) . count ( ) , <EOL> '<STR_LIT>' : orders . aggregate ( <EOL> Sum ( '<STR_LIT>' ) <EOL> ) [ '<STR_LIT>' ] or D ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : orders . order_by ( <EOL> '<STR_LIT:status>' <EOL> ) . values ( '<STR_LIT:status>' ) . annotate ( freq = Count ( '<STR_LIT:id>' ) ) <EOL> } <EOL> return stats </s>
