コード例 #1
0
    def handle(self, *args, **options):
        threshold = 10  # 10より小さいときのみ更新
        blog_num_of_views_range = (10, 50)
        image_num_of_views_range = (10, 50)
        image_num_of_downloads_range = (10, 30)

        if not options['date']:
            otapick.print_console('date option is required.')
            return

        try:
            fromDate = datetime.strptime(options['date'], '%Y/%m/%d')
        except:
            otapick.print_console('date format is wrong.')
            return

        today = timezone.now()

        blogs = Blog.objects.filter(post_date__range=(fromDate, today))
        images = Image.objects.filter(publisher__post_date__range=(fromDate,
                                                                   today))

        for blog in blogs:
            if not blog.num_of_views > threshold:
                # num_of_views
                fake_blog_num_of_views = random.randint(
                    *blog_num_of_views_range)
                otapick.increment_num_of_views(blog=blog,
                                               num=fake_blog_num_of_views)

            otapick.print_console(f'add fake score「{blog.title}」!!')

        for image in images:
            if not image.num_of_views > threshold:
                # num_of_views
                fake_image_num_of_views = random.randint(
                    *image_num_of_views_range)
                otapick.increment_num_of_views(image=image,
                                               num=fake_image_num_of_views)

            if not image.num_of_downloads > threshold:
                # num_of_downloads
                fake_image_num_of_downloads = random.randint(
                    *image_num_of_downloads_range)
                otapick.increment_num_of_downloads(
                    image, image.publisher, num=fake_image_num_of_downloads)

            otapick.print_console(
                f'add fake score「{image.publisher}」({image.order}) !!')
コード例 #2
0
    def put(self, request, *args, **kwargs):
        group_id = self.kwargs.get('group_id')
        blog_ct = self.kwargs.get('blog_ct')

        # inform of blog view
        if 'action' in request.data and request.data['action'] == 'view':
            if 'key' in request.data and request.data[
                    'key'] == otapick.VIEW_KEY:
                blogs = Blog.objects.filter(
                    publishing_group__group_id=group_id, blog_ct=blog_ct)
                if blogs.exists():
                    blog = blogs.first()
                    otapick.increment_num_of_views(blog=blog, num=1)
                    return Response({'status': 'success'}, status.HTTP_200_OK)
                else:
                    return Response({'status': 'blog_not_found'},
                                    status.HTTP_200_OK)
            else:
                return Response({'status': 'unjust_key'}, status.HTTP_200_OK)

        # inform of image download for mobile
        elif 'action' in request.data and request.data[
                'action'] == 'download' and 'image_order' in request.data:
            if 'key' in request.data and request.data[
                    'key'] == otapick.DOWNLOAD_KEY:
                blogs = Blog.objects.filter(
                    publishing_group__group_id=group_id, blog_ct=blog_ct)
                if blogs.exists():
                    blog = blogs.first()
                    images = Image.objects.filter(
                        publisher=blog, order=request.data['image_order'])
                    if images.exists():
                        image = images.first()
                        otapick.increment_num_of_downloads(image, blog, num=1)
                        otapick.edit_num_of_most_downloads(blog)
                        return Response({'status': 'success'},
                                        status.HTTP_200_OK)
                    else:
                        return Response({'status': 'image_not_found'},
                                        status.HTTP_200_OK)
                else:
                    return Response({'status': 'blog_not_found'},
                                    status.HTTP_200_OK)
            else:
                return Response({'status': 'unjust_key'}, status.HTTP_200_OK)
        else:
            return Response({'status': 'failed'}, status.HTTP_200_OK)
コード例 #3
0
    def post(self, request, *args, **kwargs):
        group_id = self.kwargs.get('group_id')
        blog_ct = self.kwargs.get('blog_ct')
        order_list = request.data  # [0, 2, 3]

        try:
            if len(order_list) <= 0:
                return Response({'status': 'data_error'}, status.HTTP_200_OK)

            blogs = Blog.objects.filter(publishing_group__group_id=group_id,
                                        blog_ct=blog_ct)
            if blogs.exists():
                blog = blogs.first()
                if Image.objects.filter(publisher=blog,
                                        order__in=order_list).exists():
                    images = Image.objects.filter(publisher=blog,
                                                  order__in=order_list)

                    # rewrite num_of_downloads
                    otapick.increment_num_of_downloads(images, blog, num=1)
                    otapick.edit_num_of_most_downloads(blog)

                    if len(images) == 1:
                        return FileResponse(images[0].picture,
                                            as_attachment=True)
                    else:
                        response = HttpResponse(content_type='application/zip')
                        file_zip = zipfile.ZipFile(response, 'w')
                        for upload_image in images:
                            file_zip.writestr(
                                os.path.basename(upload_image.picture.name),
                                upload_image.picture.read())

                        zip_name = 'otapick_' + \
                            str(group_id) + '_' + str(blog_ct) + '.zip'
                        response[
                            'Content-Disposition'] = 'attachment; filename="{}"'.format(
                                zip_name)
                        return response
                else:
                    return Response({'status': 'image_not_found'},
                                    status.HTTP_200_OK)
            else:
                return Response({'status': 'blog_not_found'},
                                status.HTTP_200_OK)
        except:
            return Response({'status': 'failed'}, status.HTTP_200_OK)
コード例 #4
0
    def post(self, request, *args, **kwargs):
        group_id = self.kwargs.get('group_id')
        blog_ct = self.kwargs.get('blog_ct')
        order = self.kwargs.get('order')

        try:
            images = Image.objects.filter(
                publisher__publishing_group__group_id=group_id,
                publisher__blog_ct=blog_ct,
                order=order)
            if images.exists():
                image = images.first()
                # rewrite num_of_downloads
                otapick.increment_num_of_downloads(image,
                                                   image.publisher,
                                                   num=1)
                otapick.edit_num_of_most_downloads(image.publisher)
                return FileResponse(image.picture, as_attachment=True)
            else:
                return Response({'status': 'image_not_found'},
                                status.HTTP_200_OK)
        except:
            return Response({'status': 'failed'}, status.HTTP_200_OK)