def list(self, request, *args, **kwargs): error_forbidden = {'detail': 'Forbidden resource'} error_notfound = {'detail': 'Resource not found'} # check anonymous auth_token = self.request.auth user_id = auth_token.user_id user_ = User.objects.filter(id=user_id) if len(user_) > 1: return Response(error_notfound, status=status.HTTP_404_NOT_FOUND) if len(user_) < 1: return Response(error_forbidden, status=status.HTTP_403_FORBIDDEN) user = user_[0] connections = user.connections.all() friends_id = [user.id] for connection in connections: friends_id.append(connection.to_user.id) username_contain = self.request.query_params.get('contain', '') strangers = User.objects.exclude(id__in=friends_id) strangers = strangers.filter(username__contains=username_contain)[:50] serializer = UserDetailsSerializer(strangers, many=True) return Response(serializer.data)
class SurveySerializer(serializers.ModelSerializer): questions = QuestionSerializer(many=True, required=False) user = UserDetailsSerializer(read_only=True, ) target = TargetSerializer() answer_set = AnswerSerializer(write_only=True, required=False) class Meta: model = Survey fields = ('id', 'answer_set', 'questions', 'name', 'uuid', 'realAnswers', 'lang', 'sex', 'user', 'target') depth = 2 # we can set this to get all realation extra_kwargs = { 'realAnswers': { 'write_only': True }, } # Default `create` and `update` behavior... def create(self, validated_data): ModelClass = self.Meta.model instance = ModelClass._default_manager.create( user=self.context['request'].user, **validated_data) return instance def update(self, instance, validated_data): answer_set_data = None if 'answer_set' in validated_data.keys(): answer_set_data = validated_data.pop('answer_set') # set_trace() # This is put to debug. info = model_meta.get_field_info(instance) # set_trace() # This is put to debug. # Simply set each attribute on the instance, and then save it. # Note that unlike `.create()` we don't need to treat many-to-many # relationships as being a special case. During updates we already # have an instance pk for the relationships to be associated with. for attr, value in validated_data.items(): if attr in info.relations and info.relations[attr].to_many: field = getattr(instance, attr) field.set(value) else: setattr(instance, attr, value) instance.save() if answer_set_data: user_answer = Answer( name=answer_set_data['name'], answers=answer_set_data['answers'], total_correct=answer_set_data['total_correct']) user_answer.save() instance.answer_set.add(user_answer) return instance
class ProgramSerializer(serializers.ModelSerializer): user = UserDetailsSerializer(required=False) class Meta: model = Program fields = ('id', 'name', 'uuid', 'user') depth = 2 # we can set this to get all realation extra_kwargs = { 'password': { 'write_only': True }, 'uuid': { 'read_only': True }, 'user': { 'read_only': True }, } # Default `create` and `update` behavior... def create(self, validated_data): user = validated_data.pop('user') ModelClass = self.Meta.model instance = ModelClass._default_manager.create( user=self.context['request'].user, **validated_data) return instance def update(self, instance, validated_data): # set_trace() # This is put to debug. info = model_meta.get_field_info(instance) # set_trace() # This is put to debug. # Simply set each attribute on the instance, and then save it. # Note that unlike `.create()` we don't need to treat many-to-many # relationships as being a special case. During updates we already # have an instance pk for the relationships to be associated with. for attr, value in validated_data.items(): if attr in info.relations and info.relations[attr].to_many: field = getattr(instance, attr) field.set(value) else: setattr(instance, attr, value) instance.save() return instance
def list(self, request, *args, **kwargs): error_forbidden = {'detail': 'Forbidden resource'} error_notfound = {'detail': 'Resource not found'} # check anonymous auth_token = self.request.auth user_id = auth_token.user_id user_ = User.objects.filter(id=user_id) if len(user_) > 1: return Response(error_notfound, status=status.HTTP_404_NOT_FOUND) if len(user_) < 1: return Response(error_forbidden, status=status.HTTP_403_FORBIDDEN) user = user_[0] connections = user.connections.all() friends = [] for connection in connections: friends.append(connection.to_user) serializer = UserDetailsSerializer(friends, many=True) return Response(serializer.data)
def list(self, request, *args, **kwargs): error_forbidden = {'detail': 'Forbidden resource'} error_notfound = {'detail': 'Resource not found'} # check anonymous auth_token = self.request.auth user_id = auth_token.user_id user_ = User.objects.filter(id=user_id) if len(user_) > 1: return Response(error_notfound, status=status.HTTP_404_NOT_FOUND) if len(user_) < 1: return Response(error_forbidden, status=status.HTTP_403_FORBIDDEN) user = user_[0] # check user in conversation conversation = self.get_object() participant = Participant.objects.filter(user=user, conversation=conversation) if not participant: return Response(error_forbidden, status=status.HTTP_403_FORBIDDEN) participants = Participant.objects.filter(conversation=conversation) participants_user = [] for participant in participants: participants_user.append(participant.user) connections = user.connections.all() friends = [] for connection in connections: friends.append(connection.to_user) # get friends out of conversation friends_outside = [] for friend in friends: if friend not in participants_user: friends_outside.append(friend) serializer = UserDetailsSerializer(friends_outside, many=True) return Response(serializer.data)
class ResumeSerializer(serializers.ModelSerializer): pictures = serializers.SerializerMethodField() skills = serializers.SerializerMethodField() educations = serializers.SerializerMethodField() musics = serializers.SerializerMethodField() user = UserDetailsSerializer(read_only=True) experiences = serializers.SerializerMethodField() class Meta: model = Resume fields = ('id', 'name', 'abstract', 'introduce', 'lang', 'pictures', 'skills', 'experiences', 'educations', 'user', 'musics') def get_pictures(self, obj): queryset = obj.picture_set.all() serializer = PictureSerializer(queryset, many=True) return serializer.data def get_skills(self, obj): queryset = obj.skill_set.all() serializer = SkillSerializer(queryset, many=True) return serializer.data def get_educations(self, obj): queryset = obj.education_set.all() serializer = EducationSerializer(queryset, many=True) return serializer.data def get_experiences(self, obj): queryset = obj.experience_set.all() serializer = ExperienceSerializer(queryset, many=True) return serializer.data def get_musics(self, obj): queryset = obj.music_set.all() serializer = MusicSerializer(queryset, many=True) return serializer.data
def list(self, request, *args, **kwargs): error_forbidden = {'detail': 'Forbidden resource'} error_notfound = {'detail': 'Resource not found'} instance = self.get_object() # check anonymous auth_token = self.request.auth user_id = auth_token.user_id user_ = User.objects.filter(id=user_id) if len(user_) > 1: return Response(error_notfound, status=status.HTTP_404_NOT_FOUND) if len(user_) < 1: return Response(error_forbidden, status=status.HTTP_403_FORBIDDEN) user = user_[0] # get conversation if instance is None: return Response(error_notfound, status=status.HTTP_404_NOT_FOUND) # get participants participants = Participant.objects.filter(conversation=instance) participants_id = [] for participant in participants: participants_id.append(participant.user.id) outsiders = User.objects.exclude(id__in=participants_id)[:50] serializer = UserDetailsSerializer(outsiders, many=True) # check superuser if user.is_superuser: return Response(serializer.data) # check participating for participant in participants: if participant.user.id == user.id: return Response(serializer.data) return Response(error_forbidden, status=status.HTTP_403_FORBIDDEN)
def get_user_details(self, obj): serializer = UserDetailsSerializer(obj.user) return serializer.data