decorator to add author to extra_fields in snippets 635

 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
from functools import wraps

def extra_author_field(view):
    """
    This decorator should be used in conjunction with the newforms
    create_update views. It adds "request.user" in the "extra_fields" dict.
    """
    @wraps(view)
    def wrapper(*args, **kwargs):
        request = args[0]
        kwargs.setdefault('extra_fields', {}).update({'author' : request.user})
        return view(*args, **kwargs)
    return wrapper



#### urls.py (extract only) ####

"""This decorator can be used as shown below"""

url(r'^add/$',
    # here we use both the permission_required and my new extra_author_field decorator
    permission_required('news.add_news')(extra_author_field(create_object)),
    {
        'model' : News,
        'model_form' : NewsForm,
        'login_required' : True,
    },
),

More like this

  1. create_update for newforms (ModelForm) by Rozza 5 years, 3 months ago
  2. Arbitrary length formset by Rupe 3 years, 9 months ago
  3. ModelForm-based create_update generic views by carljm 4 years, 11 months ago
  4. Allow any view (probably a generic view) to accept captured URL variables into extra_context. by orblivion 2 years, 7 months ago
  5. FieldsetForm by Ciantic 6 years, 2 months ago

Comments

(Forgotten your password?)