14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License.
18
"""Administrative views for page editing and user management."""
18
Administrative views for page editing and user management.
25
26
from django import http
26
27
from django.core import urlresolvers
27
28
from django.core import validators
29
30
from google.appengine.api import memcache
30
31
from google.appengine.ext import db
36
def admin_required(func):
37
"""Ensure that the logged in user is an administrator."""
39
@functools.wraps(func)
40
def __wrapper(request, *args, **kwds):
41
"""Makes it possible for admin_required to be used as a decorator."""
42
if request.user_is_admin:
43
return func(request, *args, **kwds) # pylint: disable-msg=W0142
45
return utility.forbidden(
47
error_message='You must be an administrator to view this page.')
52
def super_user_required(func):
53
"""Ensure that the logged in user has editing privileges."""
55
@functools.wraps(func)
56
def __wrapper(request, *args, **kwds):
57
"""Makes it possible for super_user_required to be used as a decorator."""
58
if request.profile.is_superuser:
59
return func(request, *args, **kwds) # pylint: disable-msg=W0142
61
return utility.forbidden(
63
error_message='You must be a superuser to view this page.')
37
from views import publisher
40
def edit_instance(request, model_type, model_form_type,
41
edit_template, success_url, object_id, **kwargs):
42
# pylint: disable-msg=R0913
43
"""Generic method to handle editing objects with Django forms.
46
request: the http request
47
model_type: the class of object being edited
48
model_form_type: the form type to use for editing this object
49
edit_template: the template to use for editing the object
50
success_url: the URL to redirect the user to when the editing is succesful
51
object_id: the ID of the object to edit, or None if creating a new object
52
kwargs: additional data to be passed to the edit form
55
A HTTP response, either a redirect to the success_url or the edit form.
62
type_instance = model_type.get_by_id(int(object_id))
63
if type_instance is None:
64
return http.HttpResponseNotFound('No object exists with key %r',
67
form = model_form_type(data=request.POST or None, instance=type_instance)
70
kwargs['type_instance'] = type_instance
71
kwargs['editing'] = editing
74
return publisher.respond(request, edit_template, kwargs)
79
type_instance = form.save(commit=False)
80
except ValueError, err:
81
errors['__all__'] = unicode(err)
83
return publisher.respond(request, edit_template, kwargs)
85
if 'callback' in kwargs:
86
kwargs['callback'](type_instance, kwargs['params'])
90
return http.HttpResponseRedirect(success_url)
93
def edit_updated_page(page_id, message_id='', tab_name=''):
94
"""Issues a redirect to the edit form for page_id.
97
page_id: the id of the page that is being edited
98
message_id: the id of the message element to be displayed to the user once
100
tab_name: the name of the tab to default to when the page is reloaded
103
A http redirect to the edit form for page_id
106
url = urlresolvers.reverse('views.admin.edit_page', args=[str(page_id)])
108
url = '%s?m=%s' % (url, message_id)
110
url = '%s#%s' % (url, tab_name)
111
return http.HttpResponseRedirect(url)
114
@security.super_user_required
69
115
def index(request):
70
116
"""Show the root administrative page."""
71
return utility.respond(request, 'admin/index')
117
return publisher.respond(request, 'admin/index')
120
@security.super_user_required
75
121
def recently_modified(request):
76
122
"""Show the 10 most recently modified pages."""
77
123
pages = models.Page.all().order('modified').fetch(10)
78
return utility.respond(request, 'admin/recently_modified', {'pages': pages})
124
return publisher.respond(request, 'admin/recently_modified', {'pages': pages})
127
@security.super_user_required
82
128
def get_help(request):
83
129
"""Return a help page for the site maintainer."""
84
return utility.respond(request, 'admin/help')
130
return publisher.respond(request, 'admin/help')
87
133
def edit_acl(request):
565
611
args=[request.POST['email']])
566
612
return http.HttpResponseRedirect(url)
568
return utility.respond(request, 'admin/edit_user', {'title': 'Edit user'})
614
return publisher.respond(request, 'admin/edit_user', {'title': 'Edit user'})
570
616
profile = models.UserProfile.load(email, utility.get_domain(request))
572
return utility.page_not_found(request)
618
return publisher.page_not_found(request)
573
619
title = 'Edit user: %s (%s)' % (email, profile.domain)
575
return utility.edit_instance(
621
return edit_instance(
576
622
request, models.UserProfile, forms.UserEditForm, 'admin/edit_user',
577
623
urlresolvers.reverse('views.admin.index'), profile.key().id(), title=title,
627
@security.super_user_required
582
628
def bulk_edit_users(request):
583
629
"""Renders and processes a form to edit UserProfiles with a csv format.