Exemplo n.º 1
0
 def signup(self, request):
     """User sign up."""
     serializer = UserSignUpSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     user = serializer.save()
     data = UserModelSerializer(user).data
     return Response(data, status=status.HTTP_201_CREATED)
Exemplo n.º 2
0
 def singup(self, request):
     """Handle HTTP POST request"""
     serialized = UserSingUpSerializer(data=request.data)
     serialized.is_valid(raise_exception=True)
     user = serialized.save()
     data = UserModelSerializer(user).data
     return Response(data, status=status.HTTP_201_CREATED)
Exemplo n.º 3
0
 def login(self, request):
     """User login."""
     serializer = UserLoginSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     user, token = serializer.save()
     data = {'user': UserModelSerializer(user).data, 'access_token': token}
     return Response(data, status=status.HTTP_201_CREATED)
Exemplo n.º 4
0
 def post(self, request, *args, **kwargs):
     serializer = UserSingupSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     user = serializer.save()
     data = {
         'user': UserModelSerializer(user).data,
     }
     return Response(data, status=status.HTTP_201_CREATED)
Exemplo n.º 5
0
 def login(self, request):
     """Handle HTTP POST request"""
     serialized = UserLoginSerializer(data=request.data)
     serialized.is_valid(raise_exception=True)
     user, token = serialized.save()
     data = {
         'status': 'ok',
         'user': UserModelSerializer(user).data,
         'token': token
     }
     return Response(data, status=status.HTTP_201_CREATED)
Exemplo n.º 6
0
 def profile(self, request, *args, **kwargs):
     """Update profile data"""
     user = self.get_object()
     profile = user.profile
     partial = request.method == 'PATCH'
     serializer = ProfileModelSerializer(profile,
                                         data=request.data,
                                         partial=partial)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     data = UserModelSerializer(user).data
     return Response(data)
Exemplo n.º 7
0
    def login(self, request):
        """Login action """
        serializer = UserLoginSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user, token = serializer.save()

        data = {
            "status": "ok",
            "user": UserModelSerializer(user).data,
            "token": token,
        }
        return Response(data=data, status=status.HTTP_201_CREATED)
Exemplo n.º 8
0
class MembershipModelSerializer(serializers.ModelSerializer):
    """ Membership model serializer. """
    user = UserModelSerializer(read_only=True)
    invited_by = serializers.StringRelatedField()
    joined_at = serializers.DateTimeField(source="created", read_only=True)

    class Meta:
        """ Meta class. """
        model = Membership
        fields = (
            "user", "is_admin", "is_active",
            "used_invitations", "remaining_invitations",
            "invited_by",
            "rides_taken", "rides_offered",
            "joined_at"
        )
        read_only_fields = (
            "user",
            "used_invitations",
            "invited_by",
            "rides_taken", "rides_offered",
        )
Exemplo n.º 9
0
class MembershipModelSerializer(serializers.ModelSerializer):
    """Member model serializer."""

    user = UserModelSerializer(read_only=True)
    invited_by = serializers.StringRelatedField()
    joined_at = serializers.DateTimeField(source='created', read_only=True)

    class Meta:
        """Meta class."""

        model = Membership
        fields = ('user', 'is_admin', 'is_active', 'used_invitations',
                  'remaining_invitations', 'invited_by', 'rides_taken',
                  'rides_offered', 'joined_at')

        read_only_fields = (
            'user',
            'user_invitations',
            'invited_by',
            'rides_taken',
            'rides_offered',
        )
Exemplo n.º 10
0
class MembershipViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin,
                        mixins.DestroyModelMixin, viewsets.GenericViewSet):
    """Circle Membership View Set"""

    user = UserModelSerializer(read_only=True)
    serializer_class = MembershipModelSerializer

    def dispatch(self, request, *args, **kwargs):
        """Verify if circle exists"""
        slug_name = kwargs['slug_name']
        self.circle = get_object_or_404(Circle, slug_name=slug_name)
        return super(MembershipViewSet, self).dispatch(request, *args,
                                                       **kwargs)

    def get_permissions(self):
        """Asign permissions based on actions"""
        permissions = [IsAuthenticated]
        if self.action != 'create':
            permissions.append(IsActiveCircleMember)
        if self.action == 'invitations':
            permissions.append(IsSelfMember)
        return [p() for p in permissions]

    def get_queryset(self):
        """Returns circle members"""
        return Membership.objects.filter(circle=self.circle, is_active=True)

    def get_object(self):
        """Return Circle member by users username."""
        return get_object_or_404(Membership,
                                 user__username=self.kwargs['pk'],
                                 circle=self.circle,
                                 is_active=True)

    def perfom_destroy(self, instance):
        """Disable membership"""
        instantce.is_active = False
        instance.save()

    @action(detail=True, methods=['get'])
    def invitations(self, request, *args, **kwargs):
        """Retrieve Members invitations markdown
        return a list containing all the members that have used its invitations and other list with those invitations that have
        not being used yet"""
        member = self.get_object()
        invited_members = Membership.objects.filter(circle=self.circle,
                                                    invited_by=request.user,
                                                    is_active=True)

        unused_invitations = Invitation.objects.filter(
            circle=self.circle, issued_by=request.user,
            used=False).values_list('code')
        diff = member.remaining_invitations - len(unused_invitations)

        invitations = [x[0] for x in unused_invitations]
        for i in range(0, diff):
            invitations.append(
                Invitation.objects.create(issued_by=request.user,
                                          circle=self.circle).code)

        data = {
            'used_invitations':
            MembershipModelSerializer(invited_members, many=True).data,
            'invitations':
            invitations
        }
        return Response(data)

    def create(self, request, *args, **kwargs):
        """Handle creation for invitation code"""
        serializer = AddMemberSerializer(data=request.data,
                                         context={
                                             'circle': self.circle,
                                             'request': request
                                         })
        serializer.is_valid(raise_exception=True)
        member = serializer.save()

        data = self.get_serializer(member).data
        return Response(data, status=status.HTTP_201_CREATED)