def __call__(self, request, target=None, slug=None): self.request = request # Fetch target model = None if target: model = self.get_and_validate_target(target) self.original_name = self.get_target_name(model) if not model: return redirect(self.get_fallback_link()) original_model = model # Set filter users = model.make_queryset() total_users = users.count() if not total_users: messages.error(request, _('Policy "%(name)s" does not apply to any users.') % {'name': model.name}, self.admin.id) return redirect(reverse('admin_prune_users')) message = None if request.method == 'POST': deleted = 0 if request.csrf.request_secure(request): for user in users.iterator(): if user.is_protected(): messages.info(request, _('User "%(name)s" is protected and was not deleted.') % {'name': user.username}, self.admin.id) else: user.delete() deleted += 1 if deleted: messages.success(request, ungettext( 'One user has been deleted.', '%(deleted)d users have been deleted.', deleted ) % {'deleted': deleted}, self.admin.id) User.objects.resync_monitor() else: messages.info(request, _("No users have been deleted."), self.admin.id) return redirect(reverse('admin_prune_users')) else: message = Message(_("Request authorization is invalid. Please resubmit your form."), messages.ERROR) return render_to_response(self.get_template(), { 'admin': self.admin, 'action': self, 'request': request, 'link': self.get_link(model), 'fallback': self.get_fallback_link(), 'messages': messages.get_messages(request, self.admin.id), 'message': message, 'tabbed': self.tabbed, 'total_users': total_users, 'target': self.get_target_name(original_model), 'target_model': original_model, }, context_instance=RequestContext(request));
def __call__(self, request, target=None, slug=None): self.request = request # Fetch target model = None if target: model = self.get_and_validate_target(target) self.original_name = self.get_target_name(model) if not model: return redirect(self.get_fallback_link()) original_model = model message = None if request.method == 'POST': if request.csrf.request_secure(request): if model.attachment_set.count(): deleted = 0 for attachment in model.attachment_set.iterator(): attachment.delete() deleted += 1 messages.success(request, ungettext( 'Attachment type and one attachment has been deleted.', 'Attachment type and %(deleted)d attachments have been deleted.', deleted ) % {'deleted': deleted}, self.admin.id) else: messages.info(request, _("Attachment type has been deleted."), self.admin.id) model.delete() AttachmentType.objects.flush_cache() return redirect(reverse('admin_attachments_types')) else: message = Message(_("Request authorization is invalid. Please resubmit your form."), messages.ERROR) return render_to_response(self.get_template(), { 'admin': self.admin, 'action': self, 'request': request, 'link': self.get_link(model), 'fallback': self.get_fallback_link(), 'messages': messages.get_messages(request, self.admin.id), 'message': message, 'tabbed': self.tabbed, 'attachments_count': model.attachment_set.count(), 'target': self.get_target_name(original_model), 'target_model': original_model, }, context_instance=RequestContext(request));
def __call__(self, request, target=None, slug=None): self.request = request # Fetch target? model = None if target: model = self.get_and_validate_target(target) self.original_name = self.get_target_name(model) if not model: return redirect(self.get_fallback_link()) original_model = model # Get form type to instantiate FormType = self.get_form(model) #Submit form message = None if request.method == 'POST': form = self.get_form_instance(FormType, model, self.get_initial_data(model), True) if form.is_valid(): try: model, message = self.submit_form(form, model) if message.level != messages.ERROR: messages.add_message(request, message.level, message, self.admin.id) # Redirect back to right page try: if 'save_new' in request.POST and self.get_new_link: return redirect(self.get_new_link(model)) except AttributeError: pass try: if 'save_edit' in request.POST and self.get_edit_link: return redirect(self.get_edit_link(model)) except AttributeError: pass try: if self.get_submit_link: return redirect(self.get_submit_link(model)) except AttributeError: pass return redirect(self.get_fallback_link()) except ValidationError as e: message = Message(e.messages[0], messages.ERROR) else: message = Message(form.non_field_errors()[0], messages.ERROR) else: form = self.get_form_instance(FormType, model, self.get_initial_data(model)) # Render form return render_to_response(self.get_template(), self.add_template_variables({ 'admin': self.admin, 'action': self, 'request': request, 'link': self.get_link(model), 'fallback': self.get_fallback_link(), 'messages_log': messages.get_messages( request, self.admin.id), 'message': message, 'tabbed': self.tabbed, 'target': self.get_target_name(original_model), 'target_model': original_model, 'form': form, }), context_instance=RequestContext(request))
def __call__(self, request, page=0): """ Use widget as view """ self.request = request # Get basic list items items_total = self.get_items() # Set extra filters? try: items_total = self.select_items(items_total).count() except AttributeError: items_total = items_total.count() # Set sorting and paginating sorting_method = self.get_sorting() try: paginating_method = self.get_pagination(items_total, page) except Http404: return redirect(self.get_link()) # List items items = self.get_items() if not request.session.get(self.get_token('filter')): items = items.all() # Set extra filters? try: items = self.select_items(items) except AttributeError: pass # Sort them items = self.sort_items(items, sorting_method) # Set pagination if self.pagination: items = items[paginating_method['start']:paginating_method['stop']] # Prefetch related? try: items = self.prefetch_related(items) except AttributeError: pass # Default message message = None # See if we should make and handle search form search_form = None SearchForm = self.get_search_form() if SearchForm: if request.method == 'POST': # New search if request.POST.get('origin') == 'search': search_form = SearchForm(request.POST, request=request) if search_form.is_valid(): search_criteria = {} for field, criteria in search_form.cleaned_data.items( ): if len(criteria) > 0: search_criteria[field] = criteria if not search_criteria: message = Message( _("No search criteria have been defined.")) else: request.session[self.get_token( 'filter')] = search_criteria return redirect(self.get_link()) else: message = Message(_("Search form contains errors.")) message.level = messages.ERROR else: search_form = SearchForm(request=request) # Kill search if request.POST.get( 'origin' ) == 'clear' and self.is_filtering and request.csrf.request_secure( request): request.session[self.get_token('filter')] = None messages.info(request, _("Search criteria have been cleared."), self.admin.id) return redirect(self.get_link()) else: if self.is_filtering: search_form = SearchForm(request=request, initial=request.session.get( self.get_token('filter'))) else: search_form = SearchForm(request=request) # See if we sould make and handle tab form table_form = None TableForm = self.get_table_form(items) if TableForm: if request.method == 'POST' and request.POST.get( 'origin') == 'table': table_form = TableForm(request.POST, request=request) if table_form.is_valid(): message, redirect_link = self.table_action( items, table_form.cleaned_data) if redirect_link: messages.add_message(request, message.level, message, self.admin.id) return redirect(redirect_link) else: message = Message(table_form.non_field_errors()[0], messages.ERROR) else: table_form = TableForm(request=request) # See if we should make and handle list form list_form = None ListForm = self.get_actions_form(items) if ListForm: if request.method == 'POST' and request.POST.get( 'origin') == 'list': list_form = ListForm(request.POST, request=request) if list_form.is_valid(): try: form_action = getattr( self, 'action_' + list_form.cleaned_data['list_action']) message, redirect_link = form_action( items, [ int(x) for x in list_form.cleaned_data['list_items'] ]) if redirect_link: messages.add_message(request, message.level, message, self.admin.id) return redirect(redirect_link) except AttributeError: message = Message(_("Requested action is incorrect.")) else: if 'list_items' in list_form.errors: message = Message(self.nothing_checked_message) elif 'list_action' in list_form.errors: message = Message(_("Requested action is incorrect.")) else: message = Message(list_form.non_field_errors()[0]) message.level = messages.ERROR else: list_form = ListForm(request=request) # Little hax to keep counters correct items_shown = len(items) if items_total < items_shown: items_total = items_shown # Render list return render_to_response(self.get_template(), self.add_template_variables({ 'admin': self.admin, 'action': self, 'request': request, 'link': self.get_link(), 'messages_log': messages.get_messages( request, self.admin.id), 'message': message, 'sorting': self.sortables, 'sorting_method': sorting_method, 'pagination': paginating_method, 'list_form': list_form or None, 'search_form': search_form or None, 'table_form': table_form or None, 'items': items, 'items_total': items_total, 'items_shown': items_shown, }), context_instance=RequestContext(request))
def __call__(self, request, target=None, slug=None): self.request = request # Fetch target? model = None if target: model = self.get_and_validate_target(target) self.original_name = self.get_target_name(model) if not model: return redirect(self.get_fallback_link()) original_model = model # Get form type to instantiate FormType = self.get_form(model) #Submit form message = None if request.method == 'POST': form = self.get_form_instance(FormType, model, self.get_initial_data(model), True) if form.is_valid(): try: model, message = self.submit_form(form, model) if message.level != messages.ERROR: messages.add_message(request, message.level, message, self.admin.id) # Redirect back to right page try: if 'save_new' in request.POST and self.get_new_link: return redirect(self.get_new_link(model)) except AttributeError: pass try: if 'save_edit' in request.POST and self.get_edit_link: return redirect(self.get_edit_link(model)) except AttributeError: pass try: if self.get_submit_link: return redirect(self.get_submit_link(model)) except AttributeError: pass return redirect(self.get_fallback_link()) except ValidationError as e: message = Message(e.messages[0], messages.ERROR) else: message = Message(form.non_field_errors()[0], messages.ERROR) else: form = self.get_form_instance(FormType, model, self.get_initial_data(model)) # Render form return render_to_response(self.get_template(), self.add_template_variables({ 'admin': self.admin, 'action': self, 'request': request, 'link': self.get_link(model), 'fallback': self.get_fallback_link(), 'messages_log': messages.get_messages(request, self.admin.id), 'message': message, 'tabbed': self.tabbed, 'target': self.get_target_name(original_model), 'target_model': original_model, 'form': form, }), context_instance=RequestContext(request));
def __call__(self, request, page=0): """ Use widget as view """ self.request = request # Get basic list items items_total = self.get_items() # Set extra filters? try: items_total = self.select_items(items_total).count() except AttributeError: items_total = items_total.count() # Set sorting and paginating sorting_method = self.get_sorting() try: paginating_method = self.get_pagination(items_total, page) except Http404: return redirect(self.get_link()) # List items items = self.get_items() if not request.session.get(self.get_token('filter')): items = items.all() # Set extra filters? try: items = self.select_items(items) except AttributeError: pass # Sort them items = self.sort_items(items, sorting_method); # Set pagination if self.pagination: items = items[paginating_method['start']:paginating_method['stop']] # Prefetch related? try: items = self.prefetch_related(items) except AttributeError: pass # Default message message = None # See if we should make and handle search form search_form = None SearchForm = self.get_search_form() if SearchForm: if request.method == 'POST': # New search if request.POST.get('origin') == 'search': search_form = SearchForm(request.POST, request=request) if search_form.is_valid(): search_criteria = {} for field, criteria in search_form.cleaned_data.items(): if len(criteria) > 0: search_criteria[field] = criteria if not search_criteria: message = Message(_("No search criteria have been defined.")) else: request.session[self.get_token('filter')] = search_criteria return redirect(self.get_link()) else: message = Message(_("Search form contains errors.")) message.level = messages.ERROR else: search_form = SearchForm(request=request) # Kill search if request.POST.get('origin') == 'clear' and self.is_filtering and request.csrf.request_secure(request): request.session[self.get_token('filter')] = None messages.info(request, _("Search criteria have been cleared."), self.admin.id) return redirect(self.get_link()) else: if self.is_filtering: search_form = SearchForm(request=request, initial=request.session.get(self.get_token('filter'))) else: search_form = SearchForm(request=request) # See if we sould make and handle tab form table_form = None TableForm = self.get_table_form(items) if TableForm: if request.method == 'POST' and request.POST.get('origin') == 'table': table_form = TableForm(request.POST, request=request) if table_form.is_valid(): message, redirect_link = self.table_action(items, table_form.cleaned_data) if redirect_link: messages.add_message(request, message.level, message, self.admin.id) return redirect(redirect_link) else: message = Message(table_form.non_field_errors()[0], messages.ERROR) else: table_form = TableForm(request=request) # See if we should make and handle list form list_form = None ListForm = self.get_actions_form(items) if ListForm: if request.method == 'POST' and request.POST.get('origin') == 'list': list_form = ListForm(request.POST, request=request) if list_form.is_valid(): try: form_action = getattr(self, 'action_' + list_form.cleaned_data['list_action']) message, redirect_link = form_action(items, [int(x) for x in list_form.cleaned_data['list_items']]) if redirect_link: messages.add_message(request, message.level, message, self.admin.id) return redirect(redirect_link) except AttributeError: message = Message(_("Requested action is incorrect.")) else: if 'list_items' in list_form.errors: message = Message(self.nothing_checked_message) elif 'list_action' in list_form.errors: message = Message(_("Requested action is incorrect.")) else: message = Message(list_form.non_field_errors()[0]) message.level = messages.ERROR else: list_form = ListForm(request=request) # Little hax to keep counters correct items_shown = len(items) if items_total < items_shown: items_total = items_shown # Render list return render_to_response(self.get_template(), self.add_template_variables({ 'admin': self.admin, 'action': self, 'request': request, 'link': self.get_link(), 'messages_log': messages.get_messages(request, self.admin.id), 'message': message, 'sorting': self.sortables, 'sorting_method': sorting_method, 'pagination': paginating_method, 'list_form': list_form or None, 'search_form': search_form or None, 'table_form': table_form or None, 'items': items, 'items_total': items_total, 'items_shown': items_shown, }), context_instance=RequestContext(request));