def put(self, request, pk, format=None): activity = self.get_object(pk) serializer = ActivitySerializer(activity, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def list(self, request, *args, **kwargs): queryset = self.get_queryset() serializer = ActivitySerializer( queryset, context={"request": request}, many=True ) return Response(serializer.data, status=status.HTTP_200_OK)
class UnitSerializer(serializers.ModelSerializer): video = VideoSerializer(required=False, allow_null=True) activities = ActivitySerializer(many=True, required=False, allow_null=True) class Meta: model = Unit fields = ('id', 'title', 'video', 'content', 'activities', 'side_notes', 'position',)
class PostSerializer(serializers.ModelSerializer): user_name = serializers.ReadOnlyField(source='user.username') profile = ProfileSerializer(read_only=True) profileId = serializers.PrimaryKeyRelatedField(write_only=True, queryset=Profile.objects.all(), source='profile') comments = CommentSerializer(many=True, read_only=True, source='comment_set') activity = ActivitySerializer(read_only=True) activityId = serializers.PrimaryKeyRelatedField(write_only=True, queryset=Activity.objects.all(), source='activity') # activities = ActivitySerializer(many=True, read_only=True, source='activity_set') # interest = InterestSerializer(many=True, read_only=True, source='interest_set') # interest = InterestSerializer(read_only=True) # interestsId = serializers.PrimaryKeyRelatedField(write_only=True, queryset=Interest.objects.all(), source='interest') # user = UserSerializer(no_required=True) # user = UserSerializer(source='user.username') # user = serializers.PrimaryKeyRelatedField(write_only=True, queryset=User.objects.all(), required=True) # profile = serializers.ReadOnlyField(source='s') # user = serializers.PrimaryKeyRelatedField( # many=False, # queryset=User.objects.all(), source='user.username', # ) user = UserSerializer() # def create(self, validated_data): # profile_data = validated_data.pop('user') # user = User.objects.create(**profile_data) # # for profile_data in profile_data: # Post.objects.create(user=user, **validated_data) # return user class Meta: model = Post fields = '__all__'
class LeadSerializer(serializers.HyperlinkedModelSerializer): user = serializers.StringRelatedField() activities = ActivitySerializer(read_only=True, many=True) class Meta: model = Lead fields = ['id', 'user', 'activities', 'lead_contact']
def test_activitiy_serializer_saved_from_JSON(self): """ Test that serializer will take activity data and save it. Take into account measurements save! """ json = { "activities": [ { "owner": "0", "name": "testing json", "measurements": [{ "name": "time spent", "type": "int", "value": "10" }, { "name": "lines of code was written", "type": "int", "value": "20" }] }, { "owner": "0", "name": "testing json 2", "measurements": [{ "name": "time spent", "type": "int", "value": "10" }] } ] } for data in json['activities']: data['user'] = User.objects.get_by_natural_key('testingUser').id measurements_amount = len(Measurement.objects.all()) serializer = ActivitySerializer(data=data) self.assertEqual(serializer.is_valid(), True, 'serializer is invalid') serializer.save() self.assertEqual( measurements_amount, Measurement.objects.count()-len(data['measurements']), 'not all measurements were created' )
def get(self, request, format=None): project_id = request.data[ "project_id"] if "project_id" in request.data else None participation = UserParticipation.objects.get(user=request.user.id, project=project_id) activities = Activity.objects.filter(participation=participation) paginator = Paginator(activities, self.page_size) page = request.GET.get('page') try: res = paginator.page(page) except PageNotAnInteger: res = paginator.page(1) except EmptyPage: res = paginator.page(paginator.num_pages) serializer = ActivitySerializer(res, many=True) return Response(serializer.data)
class PostCommunitySerializer(serializers.ModelSerializer): user_name = serializers.ReadOnlyField(source='user.username') profile = ProfileSerializer(read_only=True) profileId = serializers.PrimaryKeyRelatedField( write_only=True, queryset=Profile.objects.all(), source='profile') commentscommunities = CommentCommunitySerializer( many=True, read_only=True, source='commentcommunity_set') activity = ActivitySerializer(read_only=True) activityId = serializers.PrimaryKeyRelatedField( write_only=True, queryset=Activity.objects.all(), source='activity') # activities = ActivitySerializer(many=True, read_only=True, source='activity_set') # interest = InterestSerializer(many=True, read_only=True, source='interest_set') class Meta: model = PostCommunity fields = '__all__'
class NewCommunitySerializer(serializers.ModelSerializer): # interests = serializers.HyperlinkedRelatedField(many=True, view_name='interest-detail', read_only=True) profile = ProfileSerializer(read_only=True) profileId = serializers.PrimaryKeyRelatedField( write_only=True, queryset=Profile.objects.all(), source='profile') activity = ActivitySerializer(read_only=True) activityId = serializers.PrimaryKeyRelatedField( write_only=True, queryset=Activity.objects.all(), source='activity') joinusers = JoinUserSerializer(many=True, read_only=True, source='joinuser_set') posts = PostSerializer(many=True, read_only=True, source='post_set') postscommunities = PostCommunitySerializer(many=True, read_only=True, source='postcommunity_set') # jUsersId = serializers.PrimaryKeyRelatedField(write_only=True, queryset=JoinUser.objects.all(), source='comments') # # interest = serializers.StringRelatedField(many=True) # interestsId = serializers.PrimaryKeyRelatedField(write_only=True, queryset=Interest.objects.all(), source='interest') class Meta: model = NewCommunity fields = '__all__'
def get_activities(self, instance): querryset = Activity.objects.filter(classroom=instance.id) serializers = ActivitySerializer(querryset, many=True) return serializers.data
def get(self, request, pk, format=None): activity = self.get_object(pk) activity = ActivitySerializer(activity) return Response(activity.data)
def post(self, request, format=None): serializer = ActivitySerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): noErrors = True serializers = [] brokenSerializers = [] for data in request.data['activities']: user = request.user participation = UserParticipation.objects.get(user=user, project=None) entity, created = Entity.objects.get_or_create(name=data['name']) entity.save() data['participation'] = participation.id data['entity'] = entity.id serializer = ActivitySerializer(data=data) noErrors = noErrors and serializer.is_valid() if serializer.is_valid(): serializer.save() serializers.append(serializer.data) else: brokenSerializers.append(serializer) us = User.objects.get(username=request.user) usr = users.objects.filter(user=us).values('time') now = datetime.datetime.now().replace(tzinfo=None) if usr[0]['time']: c = datetime.datetime.now().replace( tzinfo=None) - usr[0]['time'].replace(tzinfo=None) diff = divmod(c.days * 86400 + c.seconds, 60) if diff[0] > 60: gitid = users.objects.filter(user=us).values('githubid') bitid = users.objects.filter(user=us).values('bitbucket') svnid = users.objects.filter(user=us).values('svn') urls = users.objects.filter(user=us).values('urls') if gitid[0]['githubid']: git(gitid[0]['githubid']) if bitid[0]['bitbucket']: bit(bitid[0]['bitbucket']) if svnid[0]['svn'] and urls[0]['urls']: svna(svnid[0]['svn'], urls[0]['urls']) elif svnid[0]['svn']: pubsvn(svnid[0]['svn']) users.objects.filter(user=us).update(time=now) else: users.objects.filter(user=us).update(time=now) gitid = users.objects.filter(user=us).values('githubid') bitid = users.objects.filter(user=us).values('bitbucket') svnid = users.objects.filter(user=us).values('svn') urls = users.objects.filter(user=us).values('urls') if gitid[0]['githubid']: git(gitid[0]['githubid']) if bitid[0]['bitbucket']: bit(bitid[0]['bitbucket']) if svnid[0]['svn'] and urls[0]['urls']: svna(svnid[0]['svn'], urls[0]['urls']) else: pubsvn(svnid[0]['svn']) if noErrors: return Response({'activities': serializers}, status=status.HTTP_201_CREATED) else: print("Error when inserting new data accured with this tuples:" + repr(brokenSerializers)) return Response({'activities': serializer}, status=status.HTTP_400_BAD_REQUEST)