Esempio n. 1
0
    def __call__(self, request, target=None, slug=None):
        self.request = request

        # Fetch target?
        model = None
        if target:
            model = self.get_and_validate_target(target)
            if not model:
                return redirect(self.get_fallback_link())
        original_model = model

        # Crash if this is invalid request
        if not request.csrf.request_secure(request):
            messages.error(request, _("Action authorization is invalid."), self.admin.id)
            return redirect(self.get_fallback_link())

        # Do something
        message, url = self.action(model)
        messages.add_message(request, message.level, message, self.admin.id)
        if url:
            return redirect(url)
        return redirect(self.get_fallback_link())
Esempio n. 2
0
    def __call__(self, request, target=None, slug=None):
        self.request = request

        # Fetch target?
        model = None
        if target:
            model = self.get_and_validate_target(target)
            if not model:
                return redirect(self.get_fallback_link())
        original_model = model

        # Crash if this is invalid request
        if not request.csrf.request_secure(request):
            messages.error(request, _("Action authorization is invalid."),
                           self.admin.id)
            return redirect(self.get_fallback_link())

        # Do something
        message, url = self.action(model)
        messages.add_message(request, message.level, message, self.admin.id)
        if url:
            return redirect(url)
        return redirect(self.get_fallback_link())
Esempio n. 3
0
def redirect_message(request, level, message, owner=None):
    messages.add_message(request, level, message, owner)
    return redirect(reverse('index'))
Esempio n. 4
0
def redirect_message(request, level, message, owner=None):
    messages.add_message(request, level, message, owner)
    return redirect(reverse('index'))
Esempio n. 5
0
    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))
Esempio n. 6
0
    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))
Esempio n. 7
0
    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));
Esempio n. 8
0
    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));