Ejemplo n.º 1
0
def test_timed_context_manager(discard):
    with timed('dummy') as service:
        time.sleep(0.008)
        assert get_services() == [service]

    assert get_services()[0].duration == 8
    assert get_services()[0].name == 'dummy'
Ejemplo n.º 2
0
def complex_view(request):
    home_service = TimedService('first', 'First service')
    home_service.start()

    time.sleep(0.003)

    home_service.end()

    with timed('second', 'Second service'):
        time.sleep(0.005)

    return HttpResponse('This page is empty')
Ejemplo n.º 3
0
def index(request):
    home_service = TimedService('first', 'First service')
    home_service.start()

    time.sleep(0.3)

    home_service.end()

    with timed('second', 'Second service'):
        time.sleep(0.5)

    return HttpResponse('This page shows a list of most recent posts.')
Ejemplo n.º 4
0
    def post(self, request):
        post_service = TimedService('post')
        post_service.start()

        if not request.FILES:
            data = {'error': True, 'error_text': 'file not posted'}
            return self.render_to_response(self.get_context_data(**data))

        form = self.form_cls(request.POST, request.FILES)
        if not form.is_valid():
            data = {'error': True, 'error_text': 'form not valid'}
            return self.render_to_response(self.get_context_data(**data))

        if not self.process_post_data(form.cleaned_data):
            data = {'error': True, 'error_text': 'что-то пошло не так'}
            return self.render_to_response(self.get_context_data(**data))

        postfile = request.FILES['file']
        filename, file_extension = os.path.splitext(postfile.name)
        if file_extension not in ('.xls', '.xlsx'):
            data = {
                'form': form,
                'error': True,
                'error_text': 'Поддерживаются только *.xls, *.xlsx'
            }
            return self.render_to_response(self.get_context_data(**data))

        fs_storage = FileSystemStorage()
        filename = fs_storage.save(postfile.name, postfile)
        uploaded_file_path = fs_storage.path(filename)

        ret = self.create_async_task(file_extension, uploaded_file_path)

        data = {
            'message': 'Загрузка запущена',
            'task_id': ret.id,
            'task_type': self.task_type
        }
        post_service.end()
        with timed('render'):
            return self.render_to_response(self.get_context_data(**data))
Ejemplo n.º 5
0
def index(request):
    home_service = TimedService('select')
    home_service.start()

    orders = Order.objects.all()
    items = []
    for order in orders:
        data = {
            'order': order,
            'count': {},
            'count_all': OrderItem.objects.filter(order=order).count()
        }
        for i in range(5):
            data['count'][i] = OrderItem.objects.\
                filter(order=order, status=i).count()

        items.append(data)
    home_service.end()
    context = {'items': items}

    with timed('render'):
        return render(request, 'books/index.html', context)