Project

General

Profile

0001-utils-add-an-helper-function-to-cut-an-iterable-as-b.patch

Benjamin Dauvergne, 23 March 2015 04:28 PM

Download (1.65 KB)

View differences:

Subject: [PATCH 1/4] utils: add an helper function to cut an iterable as batch
 iterable of fixed sizes

 src/authentic2/utils.py | 10 ++++++++++
 1 file changed, 10 insertions(+)
src/authentic2/utils.py
1 1
import random
2 2
import time
3 3
import logging
4 4
import urllib
5 5
import six
6 6
import urlparse
7 7
from functools import wraps
8
from itertools import islice, chain
8 9

  
9 10
from importlib import import_module
10 11

  
11 12
from django.conf import settings
12 13
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
13 14
from django.core.exceptions import ImproperlyConfigured
14 15
from django.core import urlresolvers
15 16
from django.http.request import QueryDict
......
434 435

  
435 436
def csrf_token_check(request, form):
436 437
    '''Check a request for CSRF cookie validation, and add an error to the form
437 438
       if check fails.
438 439
    '''
439 440
    if form.is_valid() and not getattr(request, 'csrf_processing_done', False):
440 441
        msg = _('The form was out of date, please try again.')
441 442
        form._errors[forms.forms.NON_FIELD_ERRORS] = ErrorList([msg])
443

  
444
def batch(iterable, size):
445
    '''Batch an iterable as an iterable of iterables of at most size element
446
       long.
447
    '''
448
    sourceiter = iter(iterable)
449
    while True:
450
        batchiter = islice(sourceiter, size)
451
        yield chain([batchiter.next()], batchiter)
442
-