<s> from django . forms import widgets <EOL> from django . contrib . auth . models import User <EOL> from rest_framework import serializers <EOL> from onadata . apps . api . models import Project <EOL> from onadata . apps . logger . models import Instance <EOL> from onadata . libs . permissions import get_object_users_with_permissions <EOL> from onadata . libs . serializers . fields . boolean_field import BooleanField <EOL> from onadata . libs . serializers . fields . json_field import JsonField <EOL> from onadata . libs . serializers . tag_list_serializer import TagListSerializer <EOL> from onadata . libs . utils . decorators import check_obj <EOL> class ProjectSerializer ( serializers . HyperlinkedModelSerializer ) : <EOL> projectid = serializers . ReadOnlyField ( source = '<STR_LIT:id>' ) <EOL> url = serializers . HyperlinkedIdentityField ( <EOL> view_name = '<STR_LIT>' , lookup_field = '<STR_LIT>' ) <EOL> owner = serializers . HyperlinkedRelatedField ( <EOL> view_name = '<STR_LIT>' , <EOL> source = '<STR_LIT>' , <EOL> lookup_field = '<STR_LIT:username>' , <EOL> queryset = User . objects . all ( ) ) <EOL> created_by = serializers . HyperlinkedRelatedField ( <EOL> view_name = '<STR_LIT>' , <EOL> lookup_field = '<STR_LIT:username>' , <EOL> read_only = True ) <EOL> metadata = JsonField ( required = False ) <EOL> starred = serializers . SerializerMethodField ( '<STR_LIT>' ) <EOL> users = serializers . SerializerMethodField ( '<STR_LIT>' ) <EOL> forms = serializers . SerializerMethodField ( '<STR_LIT>' ) <EOL> public = BooleanField ( source = '<STR_LIT>' ) <EOL> tags = TagListSerializer ( read_only = True ) <EOL> num_datasets = serializers . SerializerMethodField ( ) <EOL> last_submission_date = serializers . SerializerMethodField ( ) <EOL> class Meta : <EOL> model = Project <EOL> exclude = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def update ( self , instance , validated_data ) : <EOL> metadata = JsonField . to_json ( validated_data . get ( '<STR_LIT>' ) ) <EOL> if self . partial and metadata : <EOL> if not isinstance ( instance . metadata , dict ) : <EOL> instance . metadata = { } <EOL> instance . metadata . update ( metadata ) <EOL> validated_data [ '<STR_LIT>' ] = instance . metadata <EOL> return super ( ProjectSerializer , self ) . update ( instance , validated_data ) <EOL> def create ( self , validated_data ) : <EOL> if '<STR_LIT>' in self . context : <EOL> created_by = self . context [ '<STR_LIT>' ] . user <EOL> return Project . objects . create ( <EOL> name = validated_data . get ( '<STR_LIT:name>' ) , <EOL> organization = validated_data . get ( '<STR_LIT>' ) , <EOL> created_by = created_by , <EOL> metadata = validated_data . get ( '<STR_LIT>' ) , ) <EOL> def get_project_permissions ( self , obj ) : <EOL> return get_object_users_with_permissions ( obj , serializable = True ) <EOL> @ check_obj <EOL> def get_project_forms ( self , obj ) : <EOL> xforms_details = obj . projectxform_set . values ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> return [ { '<STR_LIT:name>' : form [ '<STR_LIT>' ] , '<STR_LIT:id>' : form [ '<STR_LIT>' ] } <EOL> for form in xforms_details ] <EOL> @ check_obj <EOL> def get_num_datasets ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> return obj . projectxform_set . count ( ) <EOL> @ check_obj <EOL> def get_last_submission_date ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> xform_ids = obj . projectxform_set . values_list ( '<STR_LIT>' , flat = True ) <EOL> last_submission = Instance . objects . order_by ( '<STR_LIT>' ) . filter ( xform_id__in = xform_ids ) . values_list ( '<STR_LIT>' , <EOL> flat = True ) <EOL> last_submission = list ( last_submission ) <EOL> return last_submission and last_submission [ <NUM_LIT:0> ] <EOL> def is_starred_project ( self , obj ) : <EOL> request = self . context [ '<STR_LIT>' ] <EOL> user = request . user <EOL> user_stars = obj . user_stars . all ( ) <EOL> if user in user_stars : <EOL> return True <EOL> return False </s>
