Exemplo n.º 1
0
    def post(self, request, **kwargs):
        """Handle POST requests which change threads"""
        post_data = self.request.POST

        threads = self.get_queryset()

        # If there are no threads in this queryset, throw a 404
        if threads.count() == 0:
            raise Http404

        userthreads = UserThread.objects.filter(user=self.request.user,
                                                thread__pk__in=threads)

        changes = {}

        # Allow changing the `read` status of the thread
        if 'read' in post_data:
            if str_to_bool(post_data['read']):
                changes['last_read_at'] = now()
                changes['read'] = True
            else:
                changes['last_read_at'] = None
                changes['read'] = False

        if 'status' in post_data:
            if post_data['status'] == 'archived':
                changes['status'] = 'archived'
            elif post_data['status'] == 'active':
                changes['status'] = 'active'

        if 'subscribed' in post_data:
            if str_to_bool(post_data['subscribed']):
                changes['subscribed_email'] = True
            else:
                changes['subscribed_email'] = False

        if len(changes) > 0:
            rows = userthreads.update(**changes)
            status_code = 200
            success = True
        else:
            # If no changes were made, return a 400 status code
            rows = 0
            status_code = 400
            success = False

        return HttpResponse(json.dumps({
            'success': success,
            'rows': rows
        }),
                            content_type='application/json',
                            status=status_code)
Exemplo n.º 2
0
    def post(self, request, **kwargs):
        """Handle POST requests which change threads"""
        post_data = self.request.POST

        threads = self.get_queryset()

        # If there are no threads in this queryset, throw a 404
        if threads.count() == 0:
            raise Http404

        userthreads = UserThread.objects.filter(
            user=self.request.user, thread__pk__in=threads)

        changes = {}

        # Allow changing the `read` status of the thread
        if 'read' in post_data:
            if str_to_bool(post_data['read']):
                changes['last_read_at'] = now()
                changes['read'] = True
            else:
                changes['last_read_at'] = None
                changes['read'] = False

        if 'status' in post_data:
            if post_data['status'] == 'archived':
                changes['status'] = 'archived'
            elif post_data['status'] == 'active':
                changes['status'] = 'active'

        if 'subscribed' in post_data:
            if str_to_bool(post_data['subscribed']):
                changes['subscribed_email'] = True
            else:
                changes['subscribed_email'] = False

        if len(changes) > 0:
            rows = userthreads.update(**changes)
            status_code = 200
            success = True
        else:
            # If no changes were made, return a 400 status code
            rows = 0
            status_code = 400
            success = False

        return HttpResponse(
            json.dumps({'success': success, 'rows': rows}),
            content_type='application/json',
            status=status_code
        )
Exemplo n.º 3
0
    def test_str_to_bool(self):
        """Test str_to_bool() function"""
        self.assertTrue(str_to_bool("yes"))
        self.assertTrue(str_to_bool("true"))
        self.assertTrue(str_to_bool("TRUE"))
        self.assertTrue(str_to_bool("T"))
        self.assertTrue(str_to_bool("1"))

        self.assertFalse(str_to_bool("NO"))
        self.assertFalse(str_to_bool("false"))
        self.assertFalse(str_to_bool("0"))

        self.assertIsNone(str_to_bool("maybe"))
    def test_str_to_bool(self):
        """Test str_to_bool() function"""
        self.assertTrue(str_to_bool("yes"))
        self.assertTrue(str_to_bool("true"))
        self.assertTrue(str_to_bool("TRUE"))
        self.assertTrue(str_to_bool("T"))
        self.assertTrue(str_to_bool("1"))

        self.assertFalse(str_to_bool("NO"))
        self.assertFalse(str_to_bool("false"))
        self.assertFalse(str_to_bool("0"))

        self.assertIsNone(str_to_bool("maybe"))
Exemplo n.º 5
0
    def get_queryset(self):
        """Get only threads for a user, and get some related data in query."""
        get_data = self.request.GET
        threads = self.model.public.by_user(self.request.user)

        # Run the queryset through `SortableListMixin` to allow ordering
        threads = self.order_queryset(threads)

        if 'status' in get_data:
            threads = threads.extra(
                where=[
                    "connectmessages_userthread.status = %s"
                ],
                params=(get_data['status'],)
            )

        # Filter by group(s)
        if 'group' in get_data:
            group_ids = [
                int(pk) for pk in get_data.get('group', '').split(u',')
                if pk.isdigit() and int(pk) >= 0
            ]
            threads = threads.filter(group__pk__in=group_ids)

        # Check to see if the GET variable `since` exists and
        # confirm it's an interger
        if get_data.get('since', '').isdigit():
            threads = threads.filter(
                modified_at__gte=make_aware(
                    datetime.fromtimestamp(
                        int(get_data['since'])),
                    get_current_timezone()
                )
            )

        # Allow to query by ID number
        if 'id' in get_data:
            # Generate a list of positive integer from thread string
            threads_ids = [
                int(pk) for pk in get_data.get('id', '').split(u',')
                if pk.isdigit() and int(pk) >= 0
            ]
            threads = threads.filter(pk__in=threads_ids)

        if 'read' in get_data:
            threads = threads.extra(
                where=[
                    "connectmessages_userthread.read = %s"
                ],
                params=(str_to_bool(get_data['read']),)
            )

        return threads
Exemplo n.º 6
0
    def get_queryset(self):
        """Get only threads for a user, and get some related data in query."""
        get_data = self.request.GET
        threads = self.model.public.by_user(self.request.user)

        # Run the queryset through `SortableListMixin` to allow ordering
        threads = self.order_queryset(threads)

        if 'status' in get_data:
            threads = threads.extra(
                where=["connectmessages_userthread.status = %s"],
                params=(get_data['status'], ))

        # Filter by group(s)
        if 'group' in get_data:
            group_ids = [
                int(pk) for pk in get_data.get('group', '').split(u',')
                if pk.isdigit() and int(pk) >= 0
            ]
            threads = threads.filter(group__pk__in=group_ids)

        # Check to see if the GET variable `since` exists and
        # confirm it's an interger
        if get_data.get('since', '').isdigit():
            threads = threads.filter(modified_at__gte=make_aware(
                datetime.fromtimestamp(int(get_data['since'])),
                get_current_timezone()))

        # Allow to query by ID number
        if 'id' in get_data:
            # Generate a list of positive integer from thread string
            threads_ids = [
                int(pk) for pk in get_data.get('id', '').split(u',')
                if pk.isdigit() and int(pk) >= 0
            ]
            threads = threads.filter(pk__in=threads_ids)

        if 'read' in get_data:
            threads = threads.extra(
                where=["connectmessages_userthread.read = %s"],
                params=(str_to_bool(get_data['read']), ))

        return threads