def post(self, request): node_ids = [int(id) for id in request.POST.getlist('node_ids')] print node_ids # Rearrange all nodes in the group following # the order set by node_ids. if request.POST.get('group_id'): group = Group.objects.get(pk=int(request.POST.get('group_id'))) nodes_in_group = group.nodes.all() else: group = None nodes_in_group = Node.objects.filter(group_id__isnull=True) for node in nodes_in_group: try: node.weight = node_ids.index(node.id) + 1 except ValueError: node.weight = 0 node.save() # Add to the group any other given node that was not yet on it. node_ids_in_group = [node.id for node in nodes_in_group] print node_ids_in_group for node_id in node_ids: if node_id not in node_ids_in_group: node = Node.objects.get(pk=node_id) node.group = group node.weight = node_ids.index(node.id) + 1 node.save() return HttpResponseAjax()
def process_response(self, request, response): if request.is_ajax(): if isinstance(response, HttpResponseRedirect) or \ isinstance(response, HttpResponsePermanentRedirect): return HttpResponseAjax( [commands.redirect(response['Location'])]) return response
def get(self, request, token): async_result = tasks.find(request, token) if async_result: if async_result.successful(): cmds = [commands.hide_progress()] if 'callback' in async_result.result: fn = async_result.result['callback']['fn'] # Transform 'fn' in a callable. if isinstance(fn, tuple) and len(fn) == 2: (path, static_method) = fn index = path.rfind('.') classname = path[index + 1:len(path)] module = __import__( path[0:index], fromlist=[classname]) klass = getattr(module, classname) callable = getattr(klass, static_method) else: callable = None # Call callable. if callable: cmds.extend(callable.__call__( request, async_result.result['result'], async_result.result['callback']['context'] )) async_result.forget() return HttpResponseAjax(cmds, request) elif async_result.failed(): async_result.forget() messages.error(request, DEFAULT_ERROR_MESSAGE) return HttpResponseAjax([ commands.hide_progress(), ], request) elif async_result.status == 'PROGRESS': return HttpResponseAjax([ commands.update_progress(async_result.result['value']), ], request) else: return HttpResponseAjax([ commands.update_progress(), ], request) else: messages.error(request, DEFAULT_ERROR_MESSAGE) return HttpResponseAjax([ commands.hide_progress(), ], request)
def post(self, request, node): try: node.delete() messages.success(request, _('The node has been deleted.')) except: messages.error(request, DEFAULT_ERROR_MESSAGE) return HttpResponseAjax([ commands.navigate(reverse('caches-browse')), ], request)
def post(self, request): ids = [int(id) for id in request.POST.getlist('ids')] for group in Group.objects.all(): try: group.weight = ids.index(group.id) + 1 except ValueError: group.weight = 0 group.save() return HttpResponseAjax()
def post(self, request, token): async_result = tasks.find(request, token) if async_result and async_result.task_id: revoke(async_result.task_id, terminate=True, signal='SIGKILL') messages.info(request, _('The task execution has been aborted.')) else: messages.error(request, DEFAULT_ERROR_MESSAGE) return HttpResponseAjax([ commands.hide_progress(), ], request)
def post(self, request): form = PasswordChangeForm(request.user, data=request.POST) if form.is_valid(): form.save() messages.success(request, _('Your password has been updated.')) return HttpResponseAjax([ commands.reload(request), ], request) else: messages.error(request, DEFAULT_FORM_ERROR_MESSAGE) return self._render(form)
def post(self, request, node): form = UpdateForm(data=request.POST, instance=node) if form.is_valid(): form.save() messages.success(request, DEFAULT_SUCCESS_MESSAGE) return HttpResponseAjax([ commands.navigate(reverse('caches-browse')), ], request) else: messages.error(request, DEFAULT_FORM_ERROR_MESSAGE) return self._render(form)
def post(self, request): form = GeneralForm(data=request.POST) if form.is_valid(): form.save() messages.success(request, DEFAULT_SUCCESS_MESSAGE) return HttpResponseAjax([ commands.navigate(reverse('settings-general')), ], request) else: messages.error(request, DEFAULT_FORM_ERROR_MESSAGE) return self._render(form=form)
def post(self, request, user): form = UpdateForm(user, data=request.POST, files=request.FILES) if form.is_valid(): form.save() messages.success(request, DEFAULT_SUCCESS_MESSAGE) return HttpResponseAjax([ commands.navigate(reverse('users-browse')), ], request) else: messages.error(request, DEFAULT_FORM_ERROR_MESSAGE) return self._render(form, user)
def post(self, request, group): try: group.delete() messages.success( request, _('The group has been deleted. Its nodes are no longer assigned ' 'to any group.')) except: messages.error(request, DEFAULT_ERROR_MESSAGE) return HttpResponseAjax([ commands.navigate(reverse('caches-browse')), ], request)
def post(self, request): form = LoginForm(data=request.POST) if form.is_valid(): # Log user in. auth.login(request, form.user) # Done! messages.info(request, _('Welcome back!')) return HttpResponseAjax([ commands.redirect(form.cleaned_data.get('destination')), ], request) else: return self._render(form)
def post(self, request): form = ProfilePreferencesForm(request.user, data=request.POST, files=request.FILES) if form.is_valid(): form.save() messages.success(request, DEFAULT_SUCCESS_MESSAGE) return HttpResponseAjax([ commands.reload(request), ], request) else: messages.error(request, DEFAULT_FORM_ERROR_MESSAGE) return self._render(form)
def post(self, request, user): form = PasswordResetConfirmationForm(user, data=request.POST) if form.is_valid(): # Save. form.save() # Done! messages.success(request, _('Your password has been updated.')) return HttpResponseAjax([ commands.navigate(reverse('user-login')), ], request) else: return self._render(form, request)
def _redirect(self, request, secure): protocol = secure and "https" or "http" url = "%s://%s%s" % (protocol, request.get_host(), request.get_full_path()) if settings.DEBUG and request.method == 'POST': raise RuntimeError( '''Django can't perform a redirect while maintaining POST data. Please structure your views so that redirects only occur during GETs.''') if request.is_ajax(): return HttpResponseAjax([commands.redirect(url)]) else: return HttpResponseRedirect(url)
def post(self, request, user): token = tasks.enqueue( request, DeleteTask(), [user.id], callback={ 'fn': ( 'varnish_bans_manager.core.views.users.Delete', 'callback', ), 'context': { 'destination': reverse('users-browse'), } }) return HttpResponseAjax([ commands.show_progress(token, title=_('Removing user...')), ], request)
def _launch_task(self, request, form, task, title, destination): token = tasks.enqueue( request, task, form.ids, callback={ 'fn': ( 'varnish_bans_manager.core.views.users.Bulk', '%s_callback' % form.cleaned_data.get('op'), ), 'context': { 'destination': destination, } }) return HttpResponseAjax([ commands.show_progress(token, title=title), ], request)
def post(self, request): form = PasswordResetForm(data=request.POST) if form.is_valid(): # Save. form.save(request) # Done! messages.success( request, _('An e-mail with password reset instructions has been ' 'delivered to %(email)s. Please, check your inbox and ' 'follow the instructions.') % {'email': form.user.email}) return HttpResponseAjax([ commands.navigate(reverse('user-login')), ], request) else: return self._render(form)
def wrapped(request, *args, **kwargs): result = fn(request, *args, **kwargs) if isinstance(result, HttpResponse): return result else: if request.is_ajax(): contents = render_to_string( result['template'], result['context'], context_instance=RequestContext(request)) return HttpResponseAjax([ set_content(contents), ], request) else: return render_to_response( result['template'], result['context'], context_instance=RequestContext(request))
def post(self, request): form = StatusForm(data=request.POST) if form.is_valid(): token = tasks.enqueue( request, StatusTask(), form.cleaned_data.get('cache'), callback={ 'fn': ( 'varnish_bans_manager.core.views.bans.Status', 'callback', ), 'context': {}, }) return HttpResponseAjax([ commands.show_progress(token, title=_('Fetching lists of bans...')), ], request) else: messages.error(request, DEFAULT_FORM_ERROR_MESSAGE) return self._render(form=form)
def post(self, request): form = self._form(request.user, data=request.POST) if form.is_valid(): token = tasks.enqueue( request, SubmitTask(), form.ban_submission, callback={ 'fn': ( 'varnish_bans_manager.core.views.bans.Submit', 'callback', ), 'context': { 'expression': form.expression, 'destination': self.destination, } }) return HttpResponseAjax([ commands.show_progress(token, title=_('Submitting ban...')), ], request) else: messages.error(request, DEFAULT_FORM_ERROR_MESSAGE) return self._render(form=form)