def convert_context_to_json(self, context): "Convert passed context into JSON string" blacklisted = ['object_list', 'object', 'is_paginated', 'paginator', 'page_obj', 'view'] data = dict([(key, value) for key, value in context.iteritems() if key not in blacklisted]) if 'object_list' in context: data_key = self.get_context_object_name(context['object_list']) or 'object_list' elif 'object' in context: data_key = self.get_context_object_name(context['object']) or 'object' data['$'+_('data')] = data_key data[data_key] = serialize(context.get(data_key)) try: # Add pagination information to data page_obj = context['page_obj'] data['$'+_('page')] = page_obj.number data['$'+_('pages')] = page_obj.paginator.num_pages if page_obj.has_previous(): data['$'+_('previous')] = self.get_page_link(page_obj.previous_page_number()) if page_obj.has_next(): data['$'+_('next')] = self.get_page_link(page_obj.next_page_number()) except (KeyError, AttributeError,): # If no pagination is in use, ignore it pass return json.dumps(data, cls=DjangoJSONEncoder, default=cleanup_json)
class PersonAdmin (admin.ModelAdmin): form = PersonAdminForm fieldsets = ( (None, {'fields': ('username', 'password', 'username_changes')}), (_('Personal info'), {'fields': ('email', 'display_name', 'bio', 'community')}), (_('Status'), {'fields': ('is_active', 'is_staff', 'is_superuser')}), (_('Important dates'), {'fields': ('last_login', 'date_joined')}), )
def persona_login(text='Sign in', color=None, next='', link_class='browserid-login persona-button', attrs=None, fallback_href='#'): if color: if 'persona-button' not in link_class: link_class += ' persona-button {0}'.format(color) else: link_class += ' ' + color return browserid_button(_(text), next, link_class, attrs, fallback_href)
def form_valid(self, form): messages.success(self.request, _('Key successfully created')) self.object = form.save(commit=False) self.object.user = self.request.user secret = self.object.generate_secret() self.object.save() context = self.get_context_data(form=form) context['secret'] = secret return self.render_to_response(context)
class KeyDeleteView(OwnerMixin, ContextMixin, DeleteView): model = Key template_name = 'account/keys/delete.html' context_object_name = 'key' slug_field = 'slug' slug_url_kwarg = 'key' page_title = _('Delete key') success_url = reverse_lazy('account:keys:list') def delete(self, request, *args, **kwargs): messages.success(request, _('Key successfully deleted')) return super(KeyDeleteView, self).delete(request, *args, **kwargs)
class KeyDetailView(OwnerMixin, HybridDetailView): model = Key template_name = 'account/keys/detail.html' context_object_name = 'key' slug_field = 'slug' slug_url_kwarg = 'key' page_title = _('Key history') def get_context_data(self, **kwargs): context = super(KeyDetailView, self).get_context_data(**kwargs) context['actions'] = self.object.history.all() return context
def convert_context_to_json(self, context): "Convert passed context into JSON string" data_key = self.context_object_name data = { '$'+_('data'): data_key, data_key: serialize(context.get(data_key)) } try: # Add pagination information to data page_obj = context['page_obj'] data['$'+_('page')] = page_obj.number data['$'+_('pages')] = page_obj.paginator.num_pages if page_obj.has_previous(): data['$'+_('previous')] = self.get_page_link(page_obj.previous_page_number()) if page_obj.has_next(): data['$'+_('next')] = self.get_page_link(page_obj.next_page_number()) except (KeyError, AttributeError,): # If no pagination is in use, ignore it pass return json.dumps(data, cls=DjangoJSONEncoder)
def convert_context_to_json(self, context): "Convert passed context into JSON string" blacklisted = [ 'object_list', 'object', 'is_paginated', 'paginator', 'page_obj', 'view' ] data = dict([(key, value) for key, value in context.iteritems() if key not in blacklisted]) if 'object_list' in context: data_key = self.get_context_object_name( context['object_list']) or 'object_list' elif 'object' in context: data_key = self.get_context_object_name( context['object']) or 'object' data['$' + _('data')] = data_key data[data_key] = serialize(context.get(data_key)) try: # Add pagination information to data page_obj = context['page_obj'] data['$' + _('page')] = page_obj.number data['$' + _('pages')] = page_obj.paginator.num_pages if page_obj.has_previous(): data['$' + _('previous')] = self.get_page_link( page_obj.previous_page_number()) if page_obj.has_next(): data['$' + _('next')] = self.get_page_link( page_obj.next_page_number()) except ( KeyError, AttributeError, ): # If no pagination is in use, ignore it pass return json.dumps(data, cls=DjangoJSONEncoder, default=cleanup_json)
class KeyCreateView(ContextMixin, CreateView): form_class = KeyForm model = Key template_name = 'account/keys/create.html' page_title = _('New key') def form_valid(self, form): messages.success(self.request, _('Key successfully created')) self.object = form.save(commit=False) self.object.user = self.request.user secret = self.object.generate_secret() self.object.save() context = self.get_context_data(form=form) context['secret'] = secret return self.render_to_response(context)
def persona_logout(text='Sign out', next='', link_class='browserid-logout', attrs=None): return browserid_button(_(text), next, link_class, attrs, reverse('browserid.logout'))
def delete(self, request, *args, **kwargs): messages.success(request, _('Key successfully deleted')) return super(KeyDeleteView, self).delete(request, *args, **kwargs)
class KeyListView(OwnerMixin, HybridListView): model = Key template_name = 'account/keys/list.html' context_object_name = 'keys' paginate_by = 10 page_title = _('My API keys')