<s> import datetime <EOL> from django . conf import settings <EOL> from django . core . cache import cache <EOL> from django . db import models <EOL> from django . db . models import Sum <EOL> from django . template import Context , loader <EOL> from django . utils . datastructures import SortedDict <EOL> from django . utils . translation import ugettext_lazy as _lazy <EOL> import commonware . log <EOL> from olympia import amo <EOL> from olympia . amo . models import ManagerBase , ModelBase , skip_cache <EOL> from olympia . access . models import Group <EOL> from olympia . amo . helpers import absolutify <EOL> from olympia . amo . urlresolvers import reverse <EOL> from olympia . amo . utils import cache_ns_key , send_mail <EOL> from olympia . addons . models import Addon , Persona <EOL> from olympia . devhub . models import ActivityLog <EOL> from olympia . editors . sql_model import RawSQLModel <EOL> from olympia . translations . fields import save_signal , TranslatedField <EOL> from olympia . users . models import UserForeignKey , UserProfile <EOL> from olympia . versions . models import version_uploaded <EOL> user_log = commonware . log . getLogger ( '<STR_LIT>' ) <EOL> class CannedResponse ( ModelBase ) : <EOL> name = TranslatedField ( ) <EOL> response = TranslatedField ( short = False ) <EOL> sort_group = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> type = models . PositiveIntegerField ( <EOL> choices = amo . CANNED_RESPONSE_CHOICES . items ( ) , db_index = True , default = <NUM_LIT:0> ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' <EOL> def __unicode__ ( self ) : <EOL> return unicode ( self . name ) <EOL> models . signals . pre_save . connect ( save_signal , sender = CannedResponse , <EOL> dispatch_uid = '<STR_LIT>' ) <EOL> class AddonCannedResponseManager ( ManagerBase ) : <EOL> def get_queryset ( self ) : <EOL> qs = super ( AddonCannedResponseManager , self ) . get_queryset ( ) <EOL> return qs . filter ( type = amo . CANNED_RESPONSE_ADDON ) <EOL> class AddonCannedResponse ( CannedResponse ) : <EOL> objects = AddonCannedResponseManager ( ) <EOL> class Meta : <EOL> proxy = True <EOL> class EventLog ( models . Model ) : <EOL> type = models . CharField ( max_length = <NUM_LIT> ) <EOL> action = models . CharField ( max_length = <NUM_LIT> ) <EOL> field = models . CharField ( max_length = <NUM_LIT> , blank = True ) <EOL> user = models . ForeignKey ( UserProfile ) <EOL> changed_id = models . IntegerField ( ) <EOL> added = models . CharField ( max_length = <NUM_LIT> , blank = True ) <EOL> removed = models . CharField ( max_length = <NUM_LIT> , blank = True ) <EOL> notes = models . TextField ( blank = True ) <EOL> created = models . DateTimeField ( auto_now_add = True ) <EOL> class Meta : <EOL> db_table = u'<STR_LIT>' <EOL> @ staticmethod <EOL> def new_editors ( ) : <EOL> action = amo . LOG . GROUP_USER_ADDED <EOL> group = Group . objects . get ( name = '<STR_LIT>' ) <EOL> items = ( ActivityLog . objects . for_group ( group ) <EOL> . filter ( action = action . id ) <EOL> . order_by ( '<STR_LIT>' ) [ : <NUM_LIT:5> ] ) <EOL> return [ dict ( user = i . arguments [ <NUM_LIT:1> ] , <EOL> created = i . created ) <EOL> for i in items ] <EOL> class ViewQueue ( RawSQLModel ) : <EOL> id = models . IntegerField ( ) <EOL> addon_name = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> addon_slug = models . CharField ( max_length = <NUM_LIT:30> ) <EOL> addon_status = models . IntegerField ( ) <EOL> addon_type_id = models . IntegerField ( ) <EOL> admin_review = models . BooleanField ( ) <EOL> is_site_specific = models . BooleanField ( ) <EOL> external_software = models . BooleanField ( ) <EOL> binary = models . BooleanField ( ) <EOL> binary_components = models . BooleanField ( ) <EOL> is_restartless = models . BooleanField ( ) <EOL> is_jetpack = models . BooleanField ( ) <EOL> source = models . CharField ( max_length = <NUM_LIT:100> ) <EOL> is_webextension = models . BooleanField ( ) <EOL> latest_version = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> _file_platform_ids = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> has_info_request = models . BooleanField ( ) <EOL> has_editor_comment = models . BooleanField ( ) <EOL> _application_ids = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> waiting_time_days = models . IntegerField ( ) <EOL> waiting_time_hours = models . IntegerField ( ) <EOL> waiting_time_min = models . IntegerField ( ) <EOL> listed = True <EOL> def base_query ( self ) : <EOL> return { <EOL> '<STR_LIT>' : SortedDict ( [ <EOL> ( '<STR_LIT:id>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , """<STR_LIT>""" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:source>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , """<STR_LIT>""" ) , <EOL> ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ] ) , <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> """<STR_LIT>""" , <EOL> """<STR_LIT>""" <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( '<STR_LIT>' if self . listed else '<STR_LIT>' ) , <EOL> ] , <EOL> '<STR_LIT>' : '<STR_LIT:id>' } <EOL> @ property <EOL> def file_platform_ids ( self ) : <EOL> return self . _explode_concat ( self . _file_platform_ids ) <EOL> @ property <EOL> def application_ids ( self ) : <EOL> return self . _explode_concat ( self . _application_ids ) <EOL> @ property <EOL> def is_traditional_restartless ( self ) : <EOL> return self . is_restartless and not self . is_jetpack <EOL> @ property <EOL> def sources_provided ( self ) : <EOL> return bool ( self . source ) <EOL> @ property <EOL> def flags ( self ) : <EOL> props = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' , _lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , _lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> _lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:info>' , _lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , _lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> _lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , _lazy ( '<STR_LIT>' ) ) , <EOL> ) <EOL> return [ ( cls , title ) for ( prop , cls , title ) in props <EOL> if getattr ( self , prop ) ] <EOL> class ViewFullReviewQueue ( ViewQueue ) : <EOL> def base_query ( self ) : <EOL> q = super ( ViewFullReviewQueue , self ) . base_query ( ) <EOL> q [ '<STR_LIT>' ] . extend ( [ '<STR_LIT>' % amo . STATUS_BETA , <EOL> '<STR_LIT>' % ( <EOL> amo . STATUS_NOMINATED , <EOL> amo . STATUS_LITE_AND_NOMINATED ) ] ) <EOL> return q <EOL> class ViewPendingQueue ( ViewQueue ) : <EOL> def base_query ( self ) : <EOL> q = super ( ViewPendingQueue , self ) . base_query ( ) <EOL> q [ '<STR_LIT>' ] . extend ( [ '<STR_LIT>' % amo . STATUS_UNREVIEWED , <EOL> '<STR_LIT>' % amo . STATUS_PUBLIC ] ) <EOL> return q <EOL> class ViewPreliminaryQueue ( ViewQueue ) : <EOL> def base_query ( self ) : <EOL> q = super ( ViewPreliminaryQueue , self ) . base_query ( ) <EOL> q [ '<STR_LIT>' ] . extend ( [ '<STR_LIT>' % amo . STATUS_UNREVIEWED , <EOL> '<STR_LIT>' % ( <EOL> amo . STATUS_LITE , <EOL> amo . STATUS_UNREVIEWED ) ] ) <EOL> return q <EOL> class ViewFastTrackQueue ( ViewQueue ) : <EOL> def base_query ( self ) : <EOL> q = super ( ViewFastTrackQueue , self ) . base_query ( ) <EOL> q [ '<STR_LIT>' ] . extend ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % amo . STATUS_UNREVIEWED , <EOL> '<STR_LIT>' % ( <EOL> amo . STATUS_LITE , amo . STATUS_UNREVIEWED , <EOL> amo . STATUS_NOMINATED , <EOL> amo . STATUS_LITE_AND_NOMINATED ) ] ) <EOL> return q <EOL> class ViewAllList ( RawSQLModel ) : <EOL> id = models . IntegerField ( ) <EOL> addon_name = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> addon_slug = models . CharField ( max_length = <NUM_LIT:30> ) <EOL> guid = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> version_date = models . DateTimeField ( ) <EOL> _author_ids = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> _author_usernames = models . CharField ( ) <EOL> review_date = models . DateField ( ) <EOL> review_version_num = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> addon_status = models . IntegerField ( ) <EOL> latest_version = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> admin_review = models . BooleanField ( ) <EOL> is_deleted = models . BooleanField ( ) <EOL> listed = True <EOL> def base_query ( self ) : <EOL> return { <EOL> '<STR_LIT>' : SortedDict ( [ <EOL> ( '<STR_LIT:id>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] ) , <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> """<STR_LIT>""" , <EOL> """<STR_LIT>""" , <EOL> '<STR_LIT>' , <EOL> """<STR_LIT>""" , <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( '<STR_LIT>' if self . listed else '<STR_LIT>' ) , <EOL> """<STR_LIT>""" . format ( '<STR_LIT:1>' if self . listed else '<STR_LIT:0>' ) , <EOL> '<STR_LIT>' % ( <EOL> amo . STATUS_NULL , amo . STATUS_DISABLED ) <EOL> ] , <EOL> '<STR_LIT>' : '<STR_LIT:id>' } <EOL> @ property <EOL> def authors ( self ) : <EOL> ids = self . _explode_concat ( self . _author_ids ) <EOL> usernames = self . _explode_concat ( self . _author_usernames , cast = unicode ) <EOL> return list ( set ( zip ( ids , usernames ) ) ) <EOL> class ViewUnlistedFullReviewQueue ( ViewFullReviewQueue ) : <EOL> listed = False <EOL> class ViewUnlistedPendingQueue ( ViewPendingQueue ) : <EOL> listed = False <EOL> class ViewUnlistedPreliminaryQueue ( ViewPreliminaryQueue ) : <EOL> listed = False <EOL> class ViewUnlistedAllList ( ViewAllList ) : <EOL> listed = False <EOL> class PerformanceGraph ( ViewQueue ) : <EOL> id = models . IntegerField ( ) <EOL> yearmonth = models . CharField ( max_length = <NUM_LIT:7> ) <EOL> approval_created = models . DateTimeField ( ) <EOL> user_id = models . IntegerField ( ) <EOL> total = models . IntegerField ( ) <EOL> def base_query ( self ) : <EOL> request_ver = amo . LOG . REQUEST_VERSION . id <EOL> review_ids = [ str ( r ) for r in amo . LOG_REVIEW_QUEUE if r != request_ver ] <EOL> return { <EOL> '<STR_LIT>' : SortedDict ( [ <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ] ) , <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( review_ids ) ] , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> class EditorSubscription ( ModelBase ) : <EOL> user = models . ForeignKey ( UserProfile ) <EOL> addon = models . ForeignKey ( Addon ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' <EOL> def send_notification ( self , version ) : <EOL> user_log . info ( '<STR_LIT>' % <EOL> ( self . user . email , self . addon . pk ) ) <EOL> context = Context ( { <EOL> '<STR_LIT:name>' : self . addon . name , <EOL> '<STR_LIT:url>' : absolutify ( reverse ( '<STR_LIT>' , args = [ self . addon . pk ] , <EOL> add_prefix = False ) ) , <EOL> '<STR_LIT>' : version . version , <EOL> '<STR_LIT>' : absolutify ( reverse ( '<STR_LIT>' , <EOL> args = [ self . addon . pk ] , <EOL> add_prefix = False ) ) , <EOL> '<STR_LIT>' : settings . SITE_URL , <EOL> } ) <EOL> subject = '<STR_LIT>' % self . addon . name <EOL> template = loader . get_template ( '<STR_LIT>' ) <EOL> send_mail ( subject , template . render ( Context ( context ) ) , <EOL> recipient_list = [ self . user . email ] , <EOL> from_email = settings . EDITORS_EMAIL , <EOL> use_blacklist = False ) <EOL> def send_notifications ( signal = None , sender = None , ** kw ) : <EOL> if sender . is_beta : <EOL> return <EOL> subscribers = sender . addon . editorsubscription_set . all ( ) <EOL> if not subscribers : <EOL> return <EOL> for subscriber in subscribers : <EOL> subscriber . send_notification ( sender ) <EOL> subscriber . delete ( ) <EOL> version_uploaded . connect ( send_notifications , dispatch_uid = '<STR_LIT>' ) <EOL> class ReviewerScore ( ModelBase ) : <EOL> user = models . ForeignKey ( UserProfile , related_name = '<STR_LIT>' ) <EOL> addon = models . ForeignKey ( Addon , blank = True , null = True , related_name = '<STR_LIT:+>' ) <EOL> score = models . SmallIntegerField ( ) <EOL> note_key = models . SmallIntegerField ( choices = amo . REVIEWED_CHOICES . items ( ) , <EOL> default = <NUM_LIT:0> ) <EOL> note = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' <EOL> ordering = ( '<STR_LIT>' , ) <EOL> @ classmethod <EOL> def get_key ( cls , key = None , invalidate = False ) : <EOL> namespace = '<STR_LIT>' <EOL> if not key : <EOL> cache_ns_key ( namespace , invalidate ) <EOL> return <EOL> else : <EOL> ns_key = cache_ns_key ( namespace , invalidate ) <EOL> return '<STR_LIT>' % ( ns_key , key ) <EOL> @ classmethod <EOL> def get_event ( cls , addon , status , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> queue = '<STR_LIT>' <EOL> if status in [ amo . STATUS_UNREVIEWED , amo . STATUS_LITE ] : <EOL> queue = '<STR_LIT>' <EOL> elif status in [ amo . STATUS_NOMINATED , amo . STATUS_LITE_AND_NOMINATED ] : <EOL> queue = '<STR_LIT>' <EOL> elif status == amo . STATUS_PUBLIC : <EOL> queue = '<STR_LIT>' <EOL> if ( addon . type in [ amo . ADDON_EXTENSION , amo . ADDON_PLUGIN , <EOL> amo . ADDON_API ] and queue ) : <EOL> return getattr ( amo , '<STR_LIT>' % queue ) <EOL> elif addon . type == amo . ADDON_DICT and queue : <EOL> return getattr ( amo , '<STR_LIT>' % queue ) <EOL> elif addon . type in [ amo . ADDON_LPAPP , amo . ADDON_LPADDON ] and queue : <EOL> return getattr ( amo , '<STR_LIT>' % queue ) <EOL> elif addon . type == amo . ADDON_PERSONA : <EOL> return amo . REVIEWED_PERSONA <EOL> elif addon . type == amo . ADDON_SEARCH and queue : <EOL> return getattr ( amo , '<STR_LIT>' % queue ) <EOL> elif addon . type == amo . ADDON_THEME and queue : <EOL> return getattr ( amo , '<STR_LIT>' % queue ) <EOL> else : <EOL> return None <EOL> @ classmethod <EOL> def award_points ( cls , user , addon , status , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> event = cls . get_event ( addon , status , ** kwargs ) <EOL> score = amo . REVIEWED_SCORES . get ( event ) <EOL> try : <EOL> vq = ViewQueue . objects . get ( <EOL> addon_slug = addon . slug , <EOL> ) <EOL> if vq . waiting_time_days > amo . REVIEWED_OVERDUE_LIMIT : <EOL> days_over = vq . waiting_time_days - amo . REVIEWED_OVERDUE_LIMIT <EOL> bonus = days_over * amo . REVIEWED_OVERDUE_BONUS <EOL> score = score + bonus <EOL> except ViewQueue . DoesNotExist : <EOL> pass <EOL> if score : <EOL> cls . objects . create ( user = user , addon = addon , score = score , <EOL> note_key = event ) <EOL> cls . get_key ( invalidate = True ) <EOL> user_log . info ( <EOL> ( u'<STR_LIT>' % ( <EOL> score , user , amo . REVIEWED_CHOICES [ event ] , addon . id ) ) <EOL> . encode ( '<STR_LIT:utf-8>' ) ) <EOL> return score <EOL> @ classmethod <EOL> def award_moderation_points ( cls , user , addon , review_id , undo = False ) : <EOL> """<STR_LIT>""" <EOL> event = ( amo . REVIEWED_ADDON_REVIEW if not undo else <EOL> amo . REVIEWED_ADDON_REVIEW_POORLY ) <EOL> score = amo . REVIEWED_SCORES . get ( event ) <EOL> cls . objects . create ( user = user , addon = addon , score = score , note_key = event ) <EOL> cls . get_key ( invalidate = True ) <EOL> user_log . info ( <EOL> u'<STR_LIT>' % ( <EOL> score , user , amo . REVIEWED_CHOICES [ event ] , review_id ) ) <EOL> @ classmethod <EOL> def get_total ( cls , user ) : <EOL> """<STR_LIT>""" <EOL> key = cls . get_key ( '<STR_LIT>' % user . id ) <EOL> val = cache . get ( key ) <EOL> if val is not None : <EOL> return val <EOL> val = ( ReviewerScore . objects . no_cache ( ) . filter ( user = user ) <EOL> . aggregate ( total = Sum ( '<STR_LIT>' ) ) <EOL> . values ( ) ) [ <NUM_LIT:0> ] <EOL> if val is None : <EOL> val = <NUM_LIT:0> <EOL> cache . set ( key , val , None ) <EOL> return val <EOL> @ classmethod <EOL> def get_recent ( cls , user , limit = <NUM_LIT:5> , addon_type = None ) : <EOL> """<STR_LIT>""" <EOL> key = cls . get_key ( '<STR_LIT>' % user . id ) <EOL> val = cache . get ( key ) <EOL> if val is not None : <EOL> return val <EOL> val = ReviewerScore . objects . no_cache ( ) . filter ( user = user ) <EOL> if addon_type is not None : <EOL> val . filter ( addon__type = addon_type ) <EOL> val = list ( val [ : limit ] ) <EOL> cache . set ( key , val , None ) <EOL> return val <EOL> @ classmethod <EOL> def get_breakdown ( cls , user ) : <EOL> """<STR_LIT>""" <EOL> key = cls . get_key ( '<STR_LIT>' % user . id ) <EOL> val = cache . get ( key ) <EOL> if val is not None : <EOL> return val <EOL> sql = """<STR_LIT>""" <EOL> with skip_cache ( ) : <EOL> val = list ( ReviewerScore . objects . raw ( sql , [ user . id ] ) ) <EOL> cache . set ( key , val , None ) <EOL> return val <EOL> @ classmethod <EOL> def get_breakdown_since ( cls , user , since ) : <EOL> """<STR_LIT>""" <EOL> key = cls . get_key ( '<STR_LIT>' % ( user . id , since . isoformat ( ) ) ) <EOL> val = cache . get ( key ) <EOL> if val is not None : <EOL> return val <EOL> sql = """<STR_LIT>""" <EOL> with skip_cache ( ) : <EOL> val = list ( ReviewerScore . objects . raw ( sql , [ user . id , since ] ) ) <EOL> cache . set ( key , val , <NUM_LIT> ) <EOL> return val <EOL> @ classmethod <EOL> def _leaderboard_query ( cls , since = None , types = None , addon_type = None ) : <EOL> """<STR_LIT>""" <EOL> query = ( cls . objects <EOL> . values_list ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> . annotate ( total = Sum ( '<STR_LIT>' ) ) <EOL> . exclude ( user__groups__name__in = ( '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> . order_by ( '<STR_LIT>' ) ) <EOL> if since is not None : <EOL> query = query . filter ( created__gte = since ) <EOL> if types is not None : <EOL> query = query . filter ( note_key__in = types ) <EOL> if addon_type is not None : <EOL> query = query . filter ( addon__type = addon_type ) <EOL> return query <EOL> @ classmethod <EOL> def get_leaderboards ( cls , user , days = <NUM_LIT:7> , types = None , addon_type = None ) : <EOL> """<STR_LIT>""" <EOL> key = cls . get_key ( '<STR_LIT>' % user . id ) <EOL> val = cache . get ( key ) <EOL> if val is not None : <EOL> return val <EOL> week_ago = datetime . date . today ( ) - datetime . timedelta ( days = days ) <EOL> leader_top = [ ] <EOL> leader_near = [ ] <EOL> query = cls . _leaderboard_query ( since = week_ago , types = types , <EOL> addon_type = addon_type ) <EOL> scores = [ ] <EOL> user_rank = <NUM_LIT:0> <EOL> in_leaderboard = False <EOL> for rank , row in enumerate ( query , <NUM_LIT:1> ) : <EOL> user_id , name , total = row <EOL> scores . append ( { <EOL> '<STR_LIT>' : user_id , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : rank , <EOL> '<STR_LIT>' : int ( total ) , <EOL> } ) <EOL> if user_id == user . id : <EOL> user_rank = rank <EOL> in_leaderboard = True <EOL> if not in_leaderboard : <EOL> leader_top = scores [ : <NUM_LIT:5> ] <EOL> else : <EOL> if user_rank <= <NUM_LIT:5> : <EOL> leader_top = scores [ : <NUM_LIT:5> ] <EOL> else : <EOL> leader_top = scores [ : <NUM_LIT:3> ] <EOL> leader_near = [ scores [ user_rank - <NUM_LIT:2> ] , scores [ user_rank - <NUM_LIT:1> ] ] <EOL> try : <EOL> leader_near . append ( scores [ user_rank ] ) <EOL> except IndexError : <EOL> pass <EOL> val = { <EOL> '<STR_LIT>' : leader_top , <EOL> '<STR_LIT>' : leader_near , <EOL> '<STR_LIT>' : user_rank , <EOL> } <EOL> cache . set ( key , val , None ) <EOL> return val <EOL> @ classmethod <EOL> def all_users_by_score ( cls ) : <EOL> """<STR_LIT>""" <EOL> query = cls . _leaderboard_query ( ) <EOL> scores = [ ] <EOL> for row in query : <EOL> user_id , name , total = row <EOL> user_level = len ( amo . REVIEWED_LEVELS ) - <NUM_LIT:1> <EOL> for i , level in enumerate ( amo . REVIEWED_LEVELS ) : <EOL> if total < level [ '<STR_LIT>' ] : <EOL> user_level = i - <NUM_LIT:1> <EOL> break <EOL> if user_level < <NUM_LIT:0> : <EOL> level = '<STR_LIT>' <EOL> else : <EOL> level = amo . REVIEWED_LEVELS [ user_level ] [ '<STR_LIT:name>' ] <EOL> scores . append ( { <EOL> '<STR_LIT>' : user_id , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : int ( total ) , <EOL> '<STR_LIT>' : level , <EOL> } ) <EOL> prev = None <EOL> for score in reversed ( scores ) : <EOL> if score [ '<STR_LIT>' ] == prev : <EOL> score [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> prev = score [ '<STR_LIT>' ] <EOL> return scores <EOL> class EscalationQueue ( ModelBase ) : <EOL> addon = models . ForeignKey ( Addon ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' <EOL> class RereviewQueue ( ModelBase ) : <EOL> addon = models . ForeignKey ( Addon ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' <EOL> @ classmethod <EOL> def flag ( cls , addon , event , message = None ) : <EOL> cls . objects . get_or_create ( addon = addon ) <EOL> if message : <EOL> amo . log ( event , addon , addon . current_version , <EOL> details = { '<STR_LIT>' : message } ) <EOL> else : <EOL> amo . log ( event , addon , addon . current_version ) <EOL> class RereviewQueueThemeManager ( ManagerBase ) : <EOL> def __init__ ( self , include_deleted = False ) : <EOL> ManagerBase . __init__ ( self ) <EOL> self . include_deleted = include_deleted <EOL> def get_queryset ( self ) : <EOL> qs = super ( RereviewQueueThemeManager , self ) . get_queryset ( ) <EOL> if self . include_deleted : <EOL> return qs <EOL> else : <EOL> return qs . exclude ( theme__addon__status = amo . STATUS_DELETED ) <EOL> class RereviewQueueTheme ( ModelBase ) : <EOL> theme = models . ForeignKey ( Persona ) <EOL> header = models . CharField ( max_length = <NUM_LIT> , blank = True , default = '<STR_LIT>' ) <EOL> footer = models . CharField ( max_length = <NUM_LIT> , blank = True , default = '<STR_LIT>' ) <EOL> dupe_persona = models . ForeignKey ( Persona , null = True , <EOL> related_name = '<STR_LIT>' ) <EOL> unfiltered = RereviewQueueThemeManager ( include_deleted = True ) <EOL> objects = RereviewQueueThemeManager ( ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' <EOL> def __str__ ( self ) : <EOL> return str ( self . id ) <EOL> @ property <EOL> def header_path ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . theme . _image_path ( self . header or self . theme . header ) <EOL> @ property <EOL> def footer_path ( self ) : <EOL> """<STR_LIT>""" <EOL> footer = self . footer or self . theme . footer <EOL> return footer and self . theme . _image_path ( footer ) or '<STR_LIT>' <EOL> @ property <EOL> def header_url ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . theme . _image_url ( self . header or self . theme . header ) <EOL> @ property <EOL> def footer_url ( self ) : <EOL> """<STR_LIT>""" <EOL> footer = self . footer or self . theme . footer <EOL> return footer and self . theme . _image_url ( footer ) or '<STR_LIT>' <EOL> class ThemeLock ( ModelBase ) : <EOL> theme = models . OneToOneField ( '<STR_LIT>' ) <EOL> reviewer = UserForeignKey ( ) <EOL> expiry = models . DateTimeField ( ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' </s>
