Beispiel #1
0
def users_by_skill(request, skill_id):
    try:
        persons = [p for p in Person.objects.all() if len(p.skills.filter(id=skill_id)) >= 1]
    except Skill.DoesNotExist as e:
        return Response({'error': str(e)})

    if request.method == "GET":
        serializer = PersonSerializer(persons, many=True)
        return Response(serializer.data)
Beispiel #2
0
def user_detail(request, user_id):
    try:
        person = Person.objects.get(id=user_id)
    except Person.DoesNotExist as e:
        return Response({'error': str(e)})

    if request.method == 'GET':
        serializer = PersonSerializer(person)
        return Response(serializer.data)
Beispiel #3
0
def user_friends(request, user_id):
    try:
        person = Person.objects.get(id=user_id)
    except User.DoesNotExist as e:
        return Response({'error': str(e)})

    if request.method == 'GET':
        friends = person.friends.filter(to_people__from_person=person)
        serializer = PersonSerializer(friends, many=True)
        return Response(serializer.data)
Beispiel #4
0
def current_user_friends(request):
    try:
        person = Person.objects.get(user=request.user)
    except Exception as e:
        return Response({'error': str(e)})

    if request.method == 'GET':
        friends = person.friends.filter(to_friend__from_person=person)
        serializer = PersonSerializer(friends, many=True)
        return Response(serializer.data)

    if request.method == 'PUT':
        friend_id = request.data.get("person_id")
        Friends.objects.get_or_create(from_person=person, to_person=Person.objects.get(id=friend_id))
        return Response({"Created": True}, status=status.HTTP_200_OK)

    if request.method == 'DELETE':
        friend_id = request.data.get("person_id")
        friend = Friends.objects.get(from_person=person, to_person=Person.objects.get(id=friend_id))
        friend.delete()
        return Response({"Deleted": True}, status=status.HTTP_200_OK)
    def update(self, data: dict):
        id = data.get("card_id", None)
        if not id:
            raise exceptions.ValidationError(detail="card id is required")
        try:
            person = Person.objects.get(card_id=id)
        except Person.DoesNotExist:
            raise exceptions.ValidationError(
                detail=f"Person with id {id} don't exist")

        if person.actual_role == 'E':
            serializer = PersonManagerSerializer(person, data=data)
            if serializer.is_valid(raise_exception=True):
                person = serializer.save()
                return PersonSerializer(person).data

        elif person.actual_role == 'T':
            try:
                professor = Professor.objects.get(card_id=id)
            except Professor.DoesNotExist:
                raise exceptions.ValidationError(
                    detail=f"Professor with id {id} don't exist")
            serializer = ProfessorManagerSerializer(professor, data=data)
            if serializer.is_valid(raise_exception=True):
                professor = serializer.save()
                return ProfessorSerializer(professor).data

        elif person.actual_role not in ('A', 'T', 'E'):
            try:
                student = Student.objects.get(card_id=id)
            except Student.DoesNotExist:
                raise exceptions.ValidationError(
                    detail=f"Student with id {id} don't exist")
            serializer = StudentManagerSerializer(student, data=data)
            if serializer.is_valid(raise_exception=True):
                student = serializer.save()
                return StudentSerializer(student).data
        raise exceptions.ValidationError(detail="This action is not allowed")
Beispiel #6
0
def list_of_users(request):
    if request.method == 'GET':
        users = Person.objects.all()
        serializer = PersonSerializer(users, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        u_serializer = UserSerializer(data=request.data.get("user"))
        if u_serializer.is_valid():
            u_serializer.save()
            data = request.data
            rating = Rating.objects.create()
            data['user_id'] = u_serializer.data.get("id")
            data['rating_id'] = rating.id
            p_serializer = PersonSerializer(data=data)
            if p_serializer.is_valid():
                p_serializer.save()
                return Response(p_serializer.data, status=status.HTTP_201_CREATED)
            return Response(p_serializer.errors, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Beispiel #7
0
def user_top(request):
    if request.method == "GET":
        persons = Person.objects.all().order_by('-mark')[:10]
        serializer = PersonSerializer(persons, many=True)
        return Response(serializer.data)
Beispiel #8
0
def current_user(request):
    try:
        person = Person.objects.get(user=request.user)
        user = User.objects.get(id=person.user.id)
    except Exception as e:
        return Response({'error': str(e)})

    if request.method == 'GET':
        if request.method == 'GET':
            serializer = PersonSerializer(person)
            return Response(serializer.data)

    elif request.method == 'PUT':
        data = request.data
        if data.get("user"):
            user_data = data.pop("user")
            user_data["username"] = user_data.get("username") or user.username
            serializer = UserSerializer(instance=user, data=user)
            if serializer.is_valid():
                serializer.save()
            else:
                return Response({'error': serializer.errors})
        data["user_id"] = user.id
        data["gender"] = data.get("gender") or person.gender
        data["birth_date"] = data.get("birth_date") or person.birth_date
        data["availability"] = data.get("availability") or person.availability
        data["reviews"] = data.get("reviews") or person.reviews

        serializer = PersonSerializer(instance=person, data=data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response({'error': serializer.errors})

    elif request.method == 'DELETE':
        person.delete()
        user.delete()
        return Response({'deleted': True})