Exemple #1
0
    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()

        # Ensure the user is an active member of this group
        if not is_active_member(request.user.id, instance.post.group.id):
            return Response(status=status.HTTP_403_FORBIDDEN)

        # Ensure the user is the owner of the post
        if not instance.owner == request.user:
            return Response(status=status.HTTP_403_FORBIDDEN)

        # Mark the comment as deleted
        parent_post = Post.objects.get(pk=instance.post.id)
        original_categories = parent_post.get_all_categories()
        updated_time_stamp = datetime.utcnow()
        instance.is_deleted = True
        instance.date_deleted = updated_time_stamp
        instance.save()

        post = Post.objects.get(id=instance.post.id)
        post.newest_update = updated_time_stamp
        post.save()

        # Push the update to connected clients
        updated_post = Post.objects.get(pk=post.id)
        updated_categories = updated_post.get_all_categories()
        CategoryStatistics.update_statistics_for_post_change(updated_post.group, updated_post, original_categories, updated_categories)
        post_serializer = PostSerializer(updated_post)
        try_emit(ACTION_TYPES.POST_DATA_RECEIVED, post_serializer.data, '/posts')

        return Response(status=status.HTTP_204_NO_CONTENT)
Exemple #2
0
    def update(self, request, *args, **kwargs):
        partial = kwargs.pop('partial', False)
        instance = self.get_object()

        # ensure the current user is the owner
        if instance.owner != request.user:
            return Response(status=status.HTTP_403_FORBIDDEN)

        # Ensure the user is an active member of this group
        if not is_active_member(request.user.id, instance.group.id):
            return Response(status=status.HTTP_403_FORBIDDEN)

        original_categories = instance.get_all_categories()
        data = copy.deepcopy(request.data)
        data.pop('owner_id', None)
        data['owner_id'] =  request.user.id

        # # Load the data into the serializer
        serializer = self.get_serializer(instance, data=data, partial=partial)
        serializer.is_valid(raise_exception=True)

        try:
            self.perform_update(serializer)
        except Exception as e:
            raise e

        updated_instance = Post.objects.get(pk=instance.id)
        updated_serializer = self.get_serializer(updated_instance)
        updated_categories = updated_instance.get_all_categories()
        CategoryStatistics.update_statistics_for_post_change(updated_instance.group, updated_instance, original_categories, updated_categories)

        # Push the update to connected clients
        try_emit(ACTION_TYPES.POST_DATA_RECEIVED, updated_serializer.data, namespace='/posts')

        return Response(updated_serializer.data)
Exemple #3
0
    def create(self, request, *args, **kwargs):
        # Add the current user information to the new loop
        user = request.user

        # If creating new loops is restricted
        if (settings.RESTRICT_LOOP_CREATION):
            # and if the user has not directly been granted the ability to create loops...
            if not user.person.can_create_loops:
                allowed_loops = settings.RESTRICT_LOOP_CREATION_ALLOWED_LOOPS.split(
                    ',')
                # and if they aren't in any of the loops granted the ability
                if not user.groups.filter(id__in=allowed_loops).exists():
                    # then forbid the action
                    return Response(status=status.HTTP_403_FORBIDDEN)

        # Load the data into the serializer
        data = copy.deepcopy(request.data)
        data['founder_id'] = user.id
        serializer = self.get_serializer(data=data)
        serializer.is_valid(raise_exception=True)

        # Block creating a loop for any user with no tokens available
        # user_token = Tokens.objects.get(user__id=user.id)
        # if user_token.create_loop < 1:
        #   return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)

        self.perform_create(serializer)
        created_group = Group.objects.get(pk=serializer.data['id'])
        CategoryStatistics.calculate_statistics_for_group(created_group)

        # Report success
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data,
                        status=status.HTTP_201_CREATED,
                        headers=headers)
Exemple #4
0
    def create(self, request, *args, **kwargs):
        data = copy.deepcopy(request.data)
        data.pop('id', None)
        data['owner_id'] = request.user.id

        serializer = self.get_serializer(data=data)
        serializer.is_valid(raise_exception=True)

        # Ensure the user is an active member of this group
        post = Post.objects.get(pk=serializer.validated_data['post_id'])
        if not is_active_member(request.user.id, post.group.id):
            return Response(status=status.HTTP_403_FORBIDDEN)

        original_categories = post.get_all_categories()
        self.perform_create(serializer)
        created_comment = Comment.objects.get(pk=serializer.data['id'])
        Post.objects.filter(pk=created_comment.post_id).update(newest_update=created_comment.date_modified)
        post = Post.objects.get(pk=created_comment.post_id)
        updated_categories = post.get_all_categories()
        CategoryStatistics.update_statistics_for_post_change(post.group, post, original_categories, updated_categories)

        # log the creation event
        SimpleAction.log_simple_action(request.user, 'submit_comment')
        ReceivedCommentToPost.objects.create(user=request.user, comment=created_comment, is_notified=(post.owner != request.user))

        # Send notifications
        try:
            post_id = serializer.validated_data['post_id']
            django_rq.enqueue(send_comment_notification_with_worker, data=serializer.data, commenter=request.user, post_id=post_id)
        except Exception:
            print('Failed to send notifications. The task runner is probably not running')

        # Push the update to connected clients
        updated_post = Post.objects.get(pk=post.id)
        post_serializer = PostSerializer(updated_post)
        try_emit(ACTION_TYPES.POST_DATA_RECEIVED, post_serializer.data, '/posts')


        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
Exemple #5
0
    def create(self, request, *args, **kwargs):
        # Add the current user information to the new post
        data = copy.deepcopy(request.data)
        data.pop('id', None)
        data['owner_id'] =  request.user.id
        
        # Load the data into the serializer
        serializer = self.get_serializer(data=data)
        serializer.is_valid(raise_exception=True)

        # Ensure the user is an active member of this group
        group_id = serializer.validated_data['group_id']
        if not is_active_member(request.user.id, group_id):
            return Response(status=status.HTTP_403_FORBIDDEN)
        
        owner_id = serializer.validated_data['owner_id']
        membership = Membership.objects.get(user__id= owner_id, group__id= group_id)
        membership.user_engagement_section_dismissed = True
        membership.save()
        #Create the new object
        self.perform_create(serializer)
        new_post = Post.objects.get(pk=serializer.data['id'])
        updated_categories = new_post.get_all_categories()
        CategoryStatistics.update_statistics_for_post_change(new_post.group, new_post, None, updated_categories)

        # log the creation event
        SimpleAction.log_simple_action(request.user, 'submit_post')

        #try to send notifications
        try:
            django_rq.enqueue(send_post_notification_with_worker, data=serializer.data, poster=request.user)
        except Exception:
            print('Failed to send new post notifications. The task runner is probably not running')

        # Push the update to connected clients
        try_emit(ACTION_TYPES.POST_DATA_RECEIVED, serializer.data, namespace='/posts')

        # Report success
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
Exemple #6
0
    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        
        # Ensure the user is an active member of this group
        if not is_active_member(request.user.id, instance.group.id):
            return Response(status=status.HTTP_403_FORBIDDEN)

        # Ensure the user is the owner of the post
        if not instance.owner == request.user:
            return Response(status=status.HTTP_403_FORBIDDEN)

        # Mark the post as deleted
        instance.is_deleted = True
        instance.date_deleted = datetime.utcnow()
        instance.save()
        all_instance_categories = instance.get_all_categories()
        CategoryStatistics.update_statistics_for_post_change(instance.group, instance, all_instance_categories, None)

        # Push the update to connected clients
        updated_serializer = self.get_serializer(instance)
        try_emit(ACTION_TYPES.POST_DATA_REMOVED, updated_serializer.data, namespace='/posts')
        
        return Response(status=status.HTTP_204_NO_CONTENT)
Exemple #7
0
 def handle(self, *args, **options):
     if options['group']:
         group = Group.objects.get(pk=options['group'])
         CategoryStatistics.calculate_statistics_for_group(group)
     else:
         CategoryStatistics.calculate_statistics()