<s> """<STR_LIT>""" <EOL> import json <EOL> from django . db . models import Q <EOL> from rest_framework import serializers <EOL> from geokey . core . serializers import FieldSelectorSerializer <EOL> from geokey . categories . serializers import CategorySerializer <EOL> from geokey . subsets . serializers import SubsetSerializer <EOL> from geokey . contributions . models import Location <EOL> from . models import Project <EOL> class ProjectSerializer ( FieldSelectorSerializer ) : <EOL> """<STR_LIT>""" <EOL> num_locations = serializers . SerializerMethodField ( ) <EOL> categories = serializers . SerializerMethodField ( ) <EOL> contribution_info = serializers . SerializerMethodField ( ) <EOL> user_info = serializers . SerializerMethodField ( ) <EOL> geographic_extent = serializers . SerializerMethodField ( ) <EOL> subsets = serializers . SerializerMethodField ( ) <EOL> class Meta : <EOL> model = Project <EOL> depth = <NUM_LIT:1> <EOL> fields = ( '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:status>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> read_only_fields = ( '<STR_LIT:id>' , '<STR_LIT:name>' ) <EOL> def get_subsets ( self , project ) : <EOL> """<STR_LIT>""" <EOL> serializer = SubsetSerializer ( project . subsets . all ( ) , many = True ) <EOL> return serializer . data <EOL> def get_categories ( self , project ) : <EOL> """<STR_LIT>""" <EOL> serializer = CategorySerializer ( <EOL> project . categories . all ( ) . exclude ( fields = None ) , many = True ) <EOL> return serializer . data <EOL> def get_geographic_extent ( self , project ) : <EOL> """<STR_LIT>""" <EOL> if project . geographic_extent is not None : <EOL> return json . loads ( project . geographic_extent . json ) <EOL> else : <EOL> return None <EOL> def get_num_locations ( self , project ) : <EOL> """<STR_LIT>""" <EOL> locations = Location . objects . filter ( <EOL> Q ( private = False ) | <EOL> Q ( private_for_project = project ) ) . count ( ) <EOL> return locations <EOL> def get_num_contributions ( self , project ) : <EOL> """<STR_LIT>""" <EOL> return project . observations . exclude ( <EOL> status = '<STR_LIT>' ) . exclude ( status = '<STR_LIT>' ) . count ( ) <EOL> def get_user_contributions ( self , project ) : <EOL> """<STR_LIT>""" <EOL> user = self . context . get ( '<STR_LIT:user>' ) <EOL> if not user . is_anonymous ( ) : <EOL> return project . observations . filter ( creator = user ) . count ( ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def get_contribution_info ( self , project ) : <EOL> """<STR_LIT>""" <EOL> drafts = <NUM_LIT:0> <EOL> pending_personal = <NUM_LIT:0> <EOL> personal = <NUM_LIT:0> <EOL> pending_all = None <EOL> user = self . context . get ( '<STR_LIT:user>' ) <EOL> if not user . is_anonymous ( ) : <EOL> personal = project . observations . filter ( creator = user ) . count ( ) <EOL> pending_personal = project . observations . filter ( <EOL> creator = user , status = '<STR_LIT>' ) . count ( ) <EOL> drafts = project . observations . filter ( <EOL> creator = user , status = '<STR_LIT>' ) . count ( ) <EOL> if project . can_moderate ( user ) : <EOL> pending_all = project . observations . filter ( <EOL> status = '<STR_LIT>' ) . count ( ) <EOL> return { <EOL> '<STR_LIT>' : self . get_num_contributions ( project ) , <EOL> '<STR_LIT>' : personal , <EOL> '<STR_LIT>' : pending_all , <EOL> '<STR_LIT>' : pending_personal , <EOL> '<STR_LIT>' : drafts <EOL> } <EOL> def get_user_info ( self , project ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT>' : project . is_admin ( self . context . get ( '<STR_LIT:user>' ) ) , <EOL> '<STR_LIT>' : project . can_contribute ( self . context . get ( '<STR_LIT:user>' ) ) , <EOL> '<STR_LIT>' : project . is_involved ( self . context . get ( '<STR_LIT:user>' ) ) , <EOL> '<STR_LIT>' : project . can_moderate ( self . context . get ( '<STR_LIT:user>' ) ) <EOL> } </s>
