<s> from __future__ import absolute_import <EOL> from django . db . models import Q <EOL> from sentry . api . serializers import register , serialize , Serializer <EOL> from sentry . models import ( <EOL> Project , ProjectBookmark , ProjectOption , ProjectStatus , UserOption <EOL> ) <EOL> STATUS_LABELS = { <EOL> ProjectStatus . VISIBLE : '<STR_LIT>' , <EOL> ProjectStatus . HIDDEN : '<STR_LIT>' , <EOL> ProjectStatus . PENDING_DELETION : '<STR_LIT>' , <EOL> ProjectStatus . DELETION_IN_PROGRESS : '<STR_LIT>' , <EOL> } <EOL> @ register ( Project ) <EOL> class ProjectSerializer ( Serializer ) : <EOL> def get_attrs ( self , item_list , user ) : <EOL> if user . is_authenticated ( ) and item_list : <EOL> bookmarks = set ( ProjectBookmark . objects . filter ( <EOL> user = user , <EOL> project_id__in = [ i . id for i in item_list ] , <EOL> ) . values_list ( '<STR_LIT>' , flat = True ) ) <EOL> user_options = { <EOL> ( u . project_id , u . key ) : u . value <EOL> for u in UserOption . objects . filter ( <EOL> Q ( user = user , project__in = item_list , key = '<STR_LIT>' ) | <EOL> Q ( user = user , key = '<STR_LIT>' , project__isnull = True ) <EOL> ) <EOL> } <EOL> default_subscribe = int ( user_options . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> else : <EOL> bookmarks = set ( ) <EOL> user_options = { } <EOL> default_subscribe = False <EOL> reviewed_callsigns = { <EOL> p . project_id : p . value <EOL> for p in ProjectOption . objects . filter ( <EOL> project__in = item_list , <EOL> key = '<STR_LIT>' , <EOL> ) <EOL> } <EOL> result = { } <EOL> for item in item_list : <EOL> result [ item ] = { <EOL> '<STR_LIT>' : item . id in bookmarks , <EOL> '<STR_LIT>' : bool ( user_options . get ( <EOL> ( item . id , '<STR_LIT>' ) , <EOL> default_subscribe , <EOL> ) ) , <EOL> '<STR_LIT>' : reviewed_callsigns . get ( item . id ) , <EOL> } <EOL> return result <EOL> def serialize ( self , obj , attrs , user ) : <EOL> from sentry import features <EOL> feature_list = [ ] <EOL> for feature in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if features . has ( '<STR_LIT>' + feature , obj , actor = user ) : <EOL> feature_list . append ( feature ) <EOL> status_label = STATUS_LABELS . get ( obj . status , '<STR_LIT>' ) <EOL> return { <EOL> '<STR_LIT:id>' : str ( obj . id ) , <EOL> '<STR_LIT>' : obj . slug , <EOL> '<STR_LIT:name>' : obj . name , <EOL> '<STR_LIT>' : obj . public , <EOL> '<STR_LIT>' : attrs [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : obj . callsign , <EOL> '<STR_LIT>' : obj . color , <EOL> '<STR_LIT>' : bool ( attrs [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : obj . date_added , <EOL> '<STR_LIT>' : obj . first_event , <EOL> '<STR_LIT>' : feature_list , <EOL> '<STR_LIT:status>' : status_label , <EOL> } <EOL> class ProjectWithOrganizationSerializer ( ProjectSerializer ) : <EOL> def get_attrs ( self , item_list , user ) : <EOL> attrs = super ( ProjectWithOrganizationSerializer , self ) . get_attrs ( <EOL> item_list , user <EOL> ) <EOL> orgs = { <EOL> d [ '<STR_LIT:id>' ] : d <EOL> for d in serialize ( list ( set ( i . organization for i in item_list ) ) , user ) <EOL> } <EOL> for item in item_list : <EOL> attrs [ item ] [ '<STR_LIT>' ] = orgs [ str ( item . organization_id ) ] <EOL> return attrs <EOL> def serialize ( self , obj , attrs , user ) : <EOL> data = super ( ProjectWithOrganizationSerializer , self ) . serialize ( <EOL> obj , attrs , user <EOL> ) <EOL> data [ '<STR_LIT>' ] = attrs [ '<STR_LIT>' ] <EOL> return data <EOL> class SharedProjectSerializer ( Serializer ) : <EOL> def serialize ( self , obj , attrs , user ) : <EOL> from sentry import features <EOL> feature_list = [ ] <EOL> for feature in ( '<STR_LIT>' , ) : <EOL> if features . has ( '<STR_LIT>' + feature , obj , actor = user ) : <EOL> feature_list . append ( feature ) <EOL> return { <EOL> '<STR_LIT>' : obj . slug , <EOL> '<STR_LIT:name>' : obj . name , <EOL> '<STR_LIT>' : obj . callsign , <EOL> '<STR_LIT>' : obj . color , <EOL> '<STR_LIT>' : feature_list , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : obj . organization . slug , <EOL> '<STR_LIT:name>' : obj . organization . name , <EOL> } , <EOL> } </s>
