Login

upload handler decorators

Author:
Rozza
Posted:
July 11, 2008
Language:
Python
Version:
.96
Score:
0 (after 0 ratings)

In an admin custom view I had the requirement to modify the upload handlers.

However, the @staff_member_required locked the Files upload handlers as it uses the request.POST - see Ticket 7654.

These decorators can be used before other decorators to allow setting of the upload handlers.

Usage example:

@upload_handlers_insert(0, QuotaUploadHandler)
@staff_member_required
def upload(request):
   pass
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def upload_handlers_insert(position, handler):
    """
    Decorator that allows you to insert an upload_handler
    Used in when other deocrators lock the handlers ie. @staff_member_required decorator
    """
    def _decorator(view_func):
        def _upload_handlers_insert_func(request, *args, **kwargs):
            request.upload_handlers.insert(position, handler())
            return view_func(request, *args, **kwargs)

        _upload_handlers_insert_func.__doc__ = view_func.__doc__
        _upload_handlers_insert_func.__dict__ = view_func.__dict__
        return _upload_handlers_insert_func

    return _decorator

def upload_handlers_append(handler):
    """
    Decorator that allows you to append an upload_handler
    Used in when other deocrators lock the handlers ie. @staff_member_required decorator
    """
    def _decorator(view_func):
        def _upload_handlers_append_func(request, *args, **kwargs):
            request.upload_handlers.append(handler())

            return view_func(request, *args, **kwargs)

        _upload_handlers_append_func.__doc__ = view_func.__doc__
        _upload_handlers_append_func.__dict__ = view_func.__dict__
        return _upload_handlers_insert_func

    return _decorator

def upload_handlers(handlers):
    """
    Decorator that allows you to set the upload_handlers
    Used in when other deocrators lock the handlers ie. @staff_member_required decorator
    """
    def _decorator(view_func):
        def _upload_handlers_func(request, *args, **kwargs):
            upload_handlers = []
            for handler in handlers:
                upload_handlers.append(handler())
            request.upload_handlers = handlers

            return view_func(request, *args, **kwargs)

        _upload_handlers_func.__doc__ = view_func.__doc__
        _upload_handlers_func.__dict__ = view_func.__dict__
        return _upload_handlers_insert_func

    return _decorator

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Rozza (on July 14, 2008):

Edited - should pass uninitiated class to the decorator - it will then handle the initiation and will return a fresh instance per request.

#

Please login first before commenting.