Ņō
OLc           @   sŪ   d  Z  yG d d k l Z l Z l Z d   Z e d  Z d   Z d   Z	 WnZ e
 j
 oN d d k l Z d d k l Z d	   Z e d
  Z d   Z d   Z	 n Xd S(   sd   wrapper around Django 1.1+ aggregate query functions, with alternative implementation for Django 1.0iĸĸĸĸ(   t   Sumt   Countt   Maxc         C   s6   |  i  d t |   d } | d  j o | S| Sd  S(   Nt   result(   t	   aggregateR   t   None(   t   querysett   columnt   defaultR   (    (    s3   /var/www/Pootle/local_apps/pootle_misc/aggregate.pyt
   max_column   s    c         C   sO   h  } | o t  d  | d <n x | D] } t |  | | <q( W|  i |   S(   Nt   idt   count(   R   R    R   (   R   t   columnsR   t   arg_dictR   (    (    s3   /var/www/Pootle/local_apps/pootle_misc/aggregate.pyt
   sum_column!   s     c            s;   |  i     i d t     } t   f d   | D  S(   NR   c         3   s'   x  |  ] } |   | d  f Vq Wd S(   R   N(    (   t   .0t   item(   R   (    s3   /var/www/Pootle/local_apps/pootle_misc/aggregate.pys	   <genexpr>-   s   	 (   t   valuest   annotateR   t   dict(   R   R   R   (    (   R   s3   /var/www/Pootle/local_apps/pootle_misc/aggregate.pyt   group_by_count+   s    !c         C   s+   |  i  d t |   i d  i d |  S(   NR   s   -count(   R   R   t   order_byR   (   R   R   t   fields(    (    s3   /var/www/Pootle/local_apps/pootle_misc/aggregate.pyt   group_by_sort/   s    (   t   dictsum(   t   ObjectDoesNotExistc         C   sI   y( |  i  d |  i | d t d SWn t t f j
 o | SXd  S(   Nt   -t   flati    (   R   t   values_listt   Truet
   IndexErrorR   (   R   R   R   (    (    s3   /var/www/Pootle/local_apps/pootle_misc/aggregate.pyR	   8   s    (c         C   sP   h  } x | D] } d | | <q Wt  t |  i |   |  } |  i   | d <| S(   Ni    R   (   t   reduceR   R   R   (   R   R   R   t   initialR   R   (    (    s3   /var/www/Pootle/local_apps/pootle_misc/aggregate.pyR   ?   s     c         C   sJ   h  } x= |  i  | d t D]& } | i | d  | | c d 7<q W| S(   NR   i    i   (   R   R   t
   setdefault(   R   R   R   R   (    (    s3   /var/www/Pootle/local_apps/pootle_misc/aggregate.pyR   H   s     c         C   s   |  i  d |  i   } g  } xA | i   D]3 } |  i d | d  i   | d <| i |  q+ W| i d d   d t  | S(   NR
   R   t   keyc         S   s   |  d  S(   R   (    (   t   x(    (    s3   /var/www/Pootle/local_apps/pootle_misc/aggregate.pyt   <lambda>U   s    t   reverse(   R   t   distinctt   iteratort   filterR   t   appendt   sortR   (   R   R   R   t   itemsR   R   (    (    s3   /var/www/Pootle/local_apps/pootle_misc/aggregate.pyR   O   s      N(   t   __doc__t   django.db.modelsR    R   R   R	   t   FalseR   R   R   t   ImportErrort   pootle_misc.utilR   t   django.core.exceptionsR   (    (    (    s3   /var/www/Pootle/local_apps/pootle_misc/aggregate.pyt   <module>   s   	
				