Login

upload handler decorators

Author:
Rozza
Posted:
July 11, 2008
Language:
Python
Version:
.96
Tags:
admin decorator file uploadhandler
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. Easy file upload handler by mattdw 6 years, 8 months ago
  2. Upload progress handler using cache framework by ebartels 7 years, 1 month ago
  3. ImageField for Google App Engine by davepeck 5 years, 6 months ago
  4. Admin Image Widget by baumer1122 6 years, 9 months ago
  5. Human readable file names decorator by maxk 4 years, 1 month 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.