Beispiel #1
0
    def get(self, request, *args, **kwargs):
        username = request.GET["username"]
        username_request = request.GET["username_request"]

        user = User.objects.filter(username=username, is_active=True)
        user = user.first()
        print(user.date_joined)
        if user:
            user_json = {
                'name':
                "{0} {1}".format(user.first_name, user.last_name),
                'profile_picture':
                get_profile_url(user),
                'cover_photo':
                get_img_url_from_model(UserCoverPhoto, Q(user=user)),
                'skills': [
                    user_skill.area.area
                    for user_skill in UserSkill.objects.filter(
                        user__username=username, user__is_active=True)
                ],
                'date_joined':
                user.date_joined,
                'followers':
                UserRelationship.objects.filter(user_to__username=username,
                                                status=1).count(),
                'following':
                UserRelationship.objects.filter(user_from__username=username,
                                                status=1).count(),
                'follow_this_user':
                UserRelationship.objects.filter(
                    user_from__username=username_request,
                    user_to__username=username,
                    status=1).exists(),
                'follow_you':
                UserRelationship.objects.filter(
                    user_from__username=username,
                    user_to__username=username_request,
                    status=1).exists()
            }
        else:
            user_json = {
                'name': "Cuenta inactiva",
                'profile_picture': '',
                'cover_photo': '',
                'skills': [],
                'date_joined': '',
                'followers': 0,
                'following': 0,
                'follow_this_user': False,
                'follow_you': False
            }

            return Response(user_json, status=404)
        return Response(user_json, status=200)
Beispiel #2
0
 def review_serializer(review):
     return {
         'id': review.pk,
         'responsibility': review.responsibility,
         'respect': review.respect,
         'communication': review.communication,
         'opinion': review.opinion,
         'user_from': {
             'username': review.user_from.username,
             'name': '{0} {1}'.format(review.user_from.first_name, review.user_from.last_name),
             'profile_profile': get_profile_url(review.user_from)
         },
         'created': review.created
     }
Beispiel #3
0
 def get_follow_notification_json(notification):
     return {
         'receiver': notification.recipient.username,
         'userFrom': {
             'username':
             notification.actor.username,
             'name':
             '{0} {1}'.format(notification.actor.first_name,
                              notification.actor.last_name),
             'profile_picture':
             get_profile_url(notification.actor)
         },
         'action': notification.verb,
         'field': 'follow',
         'created': notification.created
     }
Beispiel #4
0
 def get_reaction_notification_json(notification):
     return {
         'receiver': notification.recipient.username,
         'reaction': notification.obj.reaction,
         'userFrom': {
             'username':
             notification.actor.username,
             'name':
             '{0} {1}'.format(notification.actor.first_name,
                              notification.actor.last_name),
             'profile_picture':
             get_profile_url(notification.actor)
         },
         'action': notification.verb,
         'barter': notification.target.serializer(),
         'field': 'reaction',
         'created': notification.created
     }
Beispiel #5
0
    def get(self, request, *args, **kwargs):
        try:
            user_about = UserAbout.objects.get(
                user__username=request.GET["username"], user__is_active=True)
            print(user_about.bio)
            return Response(user_about.serializer(), status=200)
        except:
            user = User.objects.filter(username=request.GET["username"],
                                       is_active=True)
            user = user.first()
            if not user:
                return Response({'Detail': 'User not found'}, status=404)

            user_about_json = {
                'user': user.username,
                'name': '{0} {1}'.format(user.first_name, user.last_name),
                'profile_picture': get_profile_url(user),
                'bio': '',
                'birthday': '',
                'gender': ''
            }
            return Response(user_about_json, status=200)
Beispiel #6
0
    def get(self, request, *args, **kwargs):
        messages = Message.objects.filter(
            Q(conversation__owner_id=request.user.pk)
            | Q(conversation__opponent_id=request.user.pk)).distinct(
                "conversation").order_by('conversation', '-created')
        messages_list = []
        messages = list(
            reversed(sorted(messages, key=lambda message: message.created)))
        for message in messages:
            msg_dictionary = message.serializer()
            opponent = self.get_opponent(request.user, message)
            msg_dictionary["opponent"] = {
                'username': opponent.username,
                'name': '{0} {1}'.format(opponent.first_name,
                                         opponent.last_name),
                'profile_picture': get_profile_url(opponent),
            }
            msg_dictionary["id"] = message.pk
            msg_dictionary["unread_messages"] = Message.objects.filter(
                conversation=message.conversation,
                read=False).exclude(sender=request.user).count()

            messages_list.append(msg_dictionary)
        return Response(messages_list, status=200)
Beispiel #7
0
    def post(self, request, *args, **kwargs):
        def get_query():
            query = Q(recipient=follow_status.user_to,
                      actor_object_id=request.user.pk,
                      target_object_id=follow_status.user_to.pk,
                      verb='Te siguió',
                      nf_type='followed_by_user')
            return query

        username_from = request.data["username_from"]
        username_to = request.data["username_to"]
        target = request.data["target"]
        notification_json = {}
        try:
            follow_status = UserRelationship.objects.get(
                user_from__username=username_from,
                user_to__username=username_to)
            if follow_status.status != 2:
                if follow_status.status == 1:
                    follow_status.status = 0

                else:
                    follow_status.status = 1
                    get_notification_and_mark_as_unread(get_query())
                    notification_json = {
                        'user_to': username_to,
                        'user_from': {
                            'username':
                            request.user.username,
                            'name':
                            '{0} {1}'.format(request.user.first_name,
                                             request.user.last_name),
                            'profile_picture':
                            get_profile_url(request.user)
                        },
                        'action': 'Te siguió',
                        'created': datetime.now(),
                        'field': 'follow'
                    }

                follow_status.save()
        except:
            user_from = User.objects.filter(username=username_from,
                                            is_active=True)
            user_to = User.objects.filter(username=username_to, is_active=True)
            user_from, user_to = user_from.first(), user_to.first()
            if user_to and user_from:
                follow_status = UserRelationship.objects.create(
                    user_from=user_from, user_to=user_to, status=1)
                notify.send(request.user,
                            recipient=follow_status.user_to,
                            actor=request.user,
                            obj=follow_status,
                            target=follow_status.user_to,
                            verb='Te siguió',
                            nf_type='followed_by_user')
            else:
                return Response({'Detail': 'User unavailable'}, status=406)

        if target == 'self':
            following = UserRelationship.objects.filter(
                user_from__username=username_from, status=1).count()
            followers = UserRelationship.objects.filter(
                user_to__username=username_from, status=1).count()
        else:
            following = UserRelationship.objects.filter(
                user_from__username=username_to, status=1).count()
            followers = UserRelationship.objects.filter(
                user_to__username=username_to, status=1).count()

        return Response({
            'following': following,
            'followers': followers,
            'follow_this_user': follow_status.status,
            'notification': notification_json
        })