Ejemplo n.º 1
0
def searchInterest(request):
    q = request.GET.get('q', 0)
    if q == '' or q is None or q == ' ':
        sqs = Interest.objects.all()
    else:
        sqs = Interest.objects.filter(name__contains=q).all()
    serializer = InterestSerializer(sqs, many=True)
    return Response(data=serializer.data, status=status.HTTP_200_OK)
Ejemplo n.º 2
0
    def test_get_interest_detail_auth(self):
        request = self.factory.get("/interest/")
        response = get_response(request,
                                user=self.user,
                                interest_id=self.interest.pk)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, InterestSerializer(self.interest).data)
Ejemplo n.º 3
0
    def test_get_interests_auth(self):
        request = self.factory.get("/interest/")
        response = get_response(request, user=self.user)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data.keys(),
                         {"count", "next", "previous", "results"})
        self.assertEqual(len(response.data.get("results")),
                         len(self.interests))
        self.assertEqual(
            response.data.get("results"),
            InterestSerializer(self.interests, many=True).data,
        )
Ejemplo n.º 4
0
class UserInterestSerializer(serializers.ModelSerializer):
    interests = InterestSerializer(read_only=True, many=True)

    class Meta:
        model = User
        exclude = [
            'password', 'is_superuser', 'is_active', 'is_staff', 'last_login',
            'groups', 'user_permissions'
        ]

    def to_representation(self, instance):
        data = super(UserInterestSerializer, self).to_representation(instance)
        data.update({'major': DepartmentSerializer(instance.major).data})
        return data
Ejemplo n.º 5
0
def searchInterestByTag(request):
    q = request.GET.get('q', 0)
    interestTags = request.GET.get('tags', '')
    if interestTags != '':
        interestTags = interestTags.split(',')
        interestTags = [int(id) for id in interestTags]
    else:
        interestTags = []
    if q == '' or q is None or q == ' ':
        sqs = Interest.objects.filter(tags__in=interestTags).all()
    else:
        sqs = Interest.objects.filter(tags__in=interestTags).filter(
            name__contains=q).all()
    serializer = InterestSerializer(sqs, many=True)
    return Response(data=serializer.data, status=status.HTTP_200_OK)
Ejemplo n.º 6
0
def get_save_interests(request):
    # import ipdb; ipdb.set_trace()
    token_header = request.META.get("HTTP_AUTHORIZATION")[7:][:-1]
    token = Token.objects.get(key=token_header)
    profile = Profile.objects.get(user_id=token.user_id)

    if (request.data['items']):
        interests = Interest.objects.filter(id__in=request.data['items'])
        profile.answered += len(request.data['items'])
        profile.save()
        for interest in interests:
            profile.interests.add(interest)
            profile_interest = ProfileInterests.objects.get(
                interest_id=interest.id, profile_id=profile.id)
            profile_interest.importance = request.data['importance']
            profile_interest.save()

    interests_buff = profile.interests.all().values_list("interest_type_id",
                                                         flat=True)
    types_available = InterestType.objects.exclude(id__in=interests_buff)

    if types_available:
        interest_type = random.choice(types_available)
        interests = Interest.objects.filter(interest_type=interest_type)
        serializers = InterestSerializer(interests, many=True)
        return Response(
            {
                "type": interest_type.name,
                "description": interest_type.description,
                "interests": serializers.data
            },
            status=status.HTTP_200_OK)
    else:
        return Response({
            "status": "Ok",
            "msg": "As perguntas terminaram!"
        },
                        status=status.HTTP_200_OK)
Ejemplo n.º 7
0
class UserSerializer(serializers.ModelSerializer):
    """Serializer with current match."""

    net_id = serializers.CharField(source="person.net_id")
    majors = MajorSerializer(source="person.majors", many=True)
    hometown = serializers.CharField(source="person.hometown")
    profile_pic_url = serializers.CharField(source="person.profile_pic_url")
    facebook_url = serializers.CharField(source="person.facebook_url")
    instagram_username = serializers.CharField(
        source="person.instagram_username")
    graduation_year = serializers.CharField(source="person.graduation_year")
    pronouns = serializers.CharField(source="person.pronouns")
    purposes = PurposeSerializer(source="person.purposes", many=True)
    availability = SerializerMethodField("get_availability")
    locations = LocationSerializer(source="person.locations", many=True)
    interests = InterestSerializer(source="person.interests", many=True)
    groups = GroupSerializer(source="person.groups", many=True)
    prompts = SerializerMethodField("get_prompts")
    has_onboarded = serializers.BooleanField(source="person.has_onboarded")
    pending_feedback = serializers.BooleanField(
        source="person.pending_feedback")
    current_match = serializers.SerializerMethodField("get_current_match")
    deleted = serializers.BooleanField(source="person.soft_deleted")
    blocked_users = SerializerMethodField("get_blocked_users")
    is_paused = serializers.BooleanField(source="person.is_paused")
    pause_expiration = serializers.DateTimeField(
        source="person.pause_expiration")
    last_active = serializers.DateTimeField(source="person.last_active")

    def get_availability(self, user):
        if user.person.availability is None:
            return []
        availability = json.loads(user.person.availability)
        return availability

    def get_current_match(self, user):
        blocked_ids = user.person.blocked_users.values_list("id", flat=True)
        matches = Match.objects.filter(Q(user_1=user) | Q(
            user_2=user)).order_by("-created_date")
        if (len(matches) == 0 or matches[0].user_1.id in blocked_ids
                or matches[0].user_2.id in blocked_ids):
            return None
        return MatchSerializer(matches.first(), user=user).data

    def get_prompts(self, user):
        prompt_questions = user.person.prompt_questions.all()
        prompt_answers = user.person.prompt_answers
        if prompt_answers is None:
            return []
        prompt_answers = json.loads(prompt_answers)
        prompts = []
        for question_index in range(len(prompt_questions)):
            prompts.append({
                "id":
                prompt_questions[question_index].id,
                "question_name":
                prompt_questions[question_index].question_name,
                "question_placeholder":
                prompt_questions[question_index].question_placeholder,
                "answer":
                prompt_answers[question_index],
            })
        return prompts

    def get_blocked_users(self, user):
        return map(lambda u: u.id, user.person.blocked_users.all())

    class Meta:
        model = User
        fields = (
            "id",
            "net_id",
            "first_name",
            "last_name",
            "majors",
            "hometown",
            "profile_pic_url",
            "facebook_url",
            "instagram_username",
            "graduation_year",
            "pronouns",
            "purposes",
            "availability",
            "locations",
            "interests",
            "groups",
            "prompts",
            "has_onboarded",
            "deleted",
            "pending_feedback",
            "current_match",
            "blocked_users",
            "is_paused",
            "pause_expiration",
            "last_active",
        )
        read_only_fields = fields
class SimpleUserSerializer(serializers.ModelSerializer):
    """Serializer for all users view."""

    net_id = serializers.CharField(source="person.net_id")
    profile_pic_url = serializers.CharField(source="person.profile_pic_url")
    majors = MajorSerializer(source="person.majors", many=True)
    hometown = serializers.CharField(source="person.hometown")
    graduation_year = serializers.CharField(source="person.graduation_year")
    pronouns = serializers.CharField(source="person.pronouns")
    interests = InterestSerializer(source="person.interests", many=True)
    groups = GroupSerializer(source="person.groups", many=True)
    prompts = serializers.SerializerMethodField("get_prompts")
    pending_feedback = serializers.BooleanField(source="person.pending_feedback")
    is_blocked = serializers.SerializerMethodField("get_blocked")

    def get_blocked(self, user):
        request_user = self.context.get("request_user")
        if request_user is not None and hasattr(request_user, "person"):
            return user.id in request_user.person.blocked_users.values_list(
                "id", flat=True
            )
        return None

    def get_prompts(self, user):
        prompt_questions = user.person.prompt_questions.all()
        prompt_answers = user.person.prompt_answers
        if prompt_answers is None:
            return []
        prompt_answers = json.loads(prompt_answers)
        prompts = []
        for question_index in range(len(prompt_questions)):
            prompts.append(
                {
                    "id": prompt_questions[question_index].id,
                    "question_name": prompt_questions[question_index].question_name,
                    "question_placeholder": prompt_questions[
                        question_index
                    ].question_placeholder,
                    "answer": prompt_answers[question_index],
                }
            )
        return prompts

    class Meta:
        model = User
        fields = (
            "id",
            "net_id",
            "first_name",
            "last_name",
            "profile_pic_url",
            "majors",
            "hometown",
            "graduation_year",
            "pronouns",
            "interests",
            "groups",
            "prompts",
            "pending_feedback",
            "is_blocked",
        )
        read_only_fields = fields