def populate(self):
     config = self.__get_newsblog_config()
     if not config:
         return
     try:
         view_name = self.request.resolver_match.view_name
     except AttributeError:
         return
     user = getattr(self.request, 'user', None)
     if user and view_name:
         if view_name == '{0}:article-detail'.format(config.namespace):
             article = get_object_from_request(Article, self.request)
         else:
             article = None
         menu = self.toolbar.get_or_create_menu('newsblog-app',
                                                config.get_app_title())
         delete_article_perm = user.has_perm(
             'aldryn_newsblog.delete_article')
         if delete_article_perm and article:
             url = get_admin_url(
                 'aldryn_newsblog_article_publish_instant_article', [
                     article.pk,
                 ])
             menu.add_break()
             menu.add_modal_item(_('Publish Instant Article'), url=url)
Example #2
0
    def populate(self):
        config = self.get_app_config(EventsConfig)

        if not config:
            return

        event = get_object_from_request(Event, self.request)

        if self.request.user:
            user = self.request.user

            chg_event_perm = user.has_perm('aldryn_events.change_event')
            add_event_perm = user.has_perm('aldryn_events.add_event')
            del_event_perm = user.has_perm('aldryn_events.delete_event')
            chg_config_perm = user.has_perm(
                'aldryn_events.change_eventsconfig')
            add_config_perm = user.has_perm('aldryn_events.add_eventsconfig')

            event_perms = [chg_event_perm, add_event_perm, del_event_perm]
            config_perms = [chg_config_perm, add_config_perm]

            if any(event_perms + config_perms):
                menu = self.toolbar.get_or_create_menu('events-app',
                                                       _('Events'))

                if chg_config_perm:
                    url = admin_reverse('aldryn_events_eventsconfig_change',
                                        args=[
                                            config.pk,
                                        ])
                    menu.add_modal_item(_('Edit App Configuration'), url=url)

                if chg_config_perm and any(event_perms):
                    menu.add_break()

                if chg_event_perm:
                    url = admin_reverse('aldryn_events_event_changelist')
                    menu.add_sideframe_item(_('Events list'), url=url)

                if add_event_perm:
                    url = admin_reverse('aldryn_events_event_add')
                    menu.add_modal_item(_('Add event'), url=url)

                if chg_event_perm and event:
                    url = admin_reverse('aldryn_events_event_change',
                                        args=[
                                            event.pk,
                                        ])
                    menu.add_modal_item(_('Edit event'), url=url, active=True)

                if del_event_perm and event:
                    redirect_url = self.get_on_delete_redirect_url(event)
                    url = admin_reverse('aldryn_events_event_delete',
                                        args=[
                                            event.pk,
                                        ])
                    menu.add_modal_item(_('Delete event'),
                                        url=url,
                                        on_close=redirect_url)
 def test_get_obj_from_empty_request(self):
     """ Test that we get None if the request doesn't contain an object. """
     request = self.request_factory.get(reverse('simple:simple-root'))
     request.LANGUAGE_CODE = 'en'
     request.current_page = self.page
     request.user = self.user
     request.resolver_match = resolve(request.path)
     simple = get_object_from_request(Simple, request)
     self.assertIsNone(simple)
 def test_get_obj_from_empty_request(self):
     """ Test that we get None if the request doesn't contain an object. """
     request = self.request_factory.get(reverse('simple:simple-root'))
     request.LANGUAGE_CODE = 'en'
     request.current_page = self.page
     request.user = self.user
     request.resolver_match = resolve(request.path)
     simple = get_object_from_request(Simple, request)
     self.assertIsNone(simple)
Example #5
0
    def populate(self):
        config = self.get_app_config(EventsConfig)

        if not config:
            return

        event = get_object_from_request(Event, self.request)

        if self.request.user:
            user = self.request.user

            chg_event_perm = user.has_perm('aldryn_events.change_event')
            add_event_perm = user.has_perm('aldryn_events.add_event')
            del_event_perm = user.has_perm('aldryn_events.delete_event')
            chg_config_perm = user.has_perm(
                'aldryn_events.change_eventsconfig')
            add_config_perm = user.has_perm('aldryn_events.add_eventsconfig')

            event_perms = [chg_event_perm, add_event_perm, del_event_perm]
            config_perms = [chg_config_perm, add_config_perm]

            if any(event_perms + config_perms):
                menu = self.toolbar.get_or_create_menu(
                    'events-app', _('Events'))

                if chg_config_perm:
                    url = admin_reverse('aldryn_events_eventsconfig_change',
                                        args=[config.pk, ])
                    menu.add_modal_item(_('Edit App Configuration'), url=url)

                if chg_config_perm and any(event_perms):
                    menu.add_break()

                if chg_event_perm:
                    url = admin_reverse('aldryn_events_event_changelist')
                    menu.add_sideframe_item(_('Events list'), url=url)

                if add_event_perm:
                    url = admin_reverse('aldryn_events_event_add')
                    # preselect the current app config
                    url = '{0}?app_config={1}'.format(url, config.pk)
                    menu.add_modal_item(_('Add event'), url=url)

                if chg_event_perm and event:
                    url = admin_reverse('aldryn_events_event_change',
                                        args=[event.pk, ])
                    menu.add_modal_item(_('Edit event'), url=url, active=True)

                if del_event_perm and event:
                    redirect_url = self.get_on_delete_redirect_url(event)
                    url = admin_reverse('aldryn_events_event_delete',
                                        args=[event.pk, ])
                    menu.add_modal_item(_('Delete event'), url=url,
                                        on_close=redirect_url)
    def test_get_untranslated_obj_from_request(self):
        """ Test that we can get a non-translated object from the request. """
        slug_url = self.untranslated1.get_absolute_url()
        pk_url = slug_url.replace(
            '/{0}/'.format(self.untranslated1.slug),
            '/{0}/'.format(self.untranslated1.pk))

        for url in [slug_url, pk_url, ]:
            request = self.request_factory.get(url)
            request.LANGUAGE_CODE = 'en'
            request.current_page = self.page
            request.user = self.user
            request.resolver_match = resolve(request.path)
            untranslated = get_object_from_request(Untranslated, request)
            self.assertTrue(untranslated.pk, self.untranslated1.pk)
    def test_get_untranslated_obj_from_request(self):
        """ Test that we can get a non-translated object from the request. """
        slug_url = self.untranslated1.get_absolute_url()
        pk_url = slug_url.replace('/{0}/'.format(self.untranslated1.slug),
                                  '/{0}/'.format(self.untranslated1.pk))

        for url in [
                slug_url,
                pk_url,
        ]:
            request = self.request_factory.get(url)
            request.LANGUAGE_CODE = 'en'
            request.current_page = self.page
            request.user = self.user
            request.resolver_match = resolve(request.path)
            untranslated = get_object_from_request(Untranslated, request)
            self.assertTrue(untranslated.pk, self.untranslated1.pk)
    def test_get_obj_from_request(self):
        """ Test that we can get the object from the request. """
        self.simple1.set_current_language('en')
        slug_url = self.simple1.get_absolute_url('en')
        pk_url = slug_url.replace(
            '/{0}/'.format(self.simple1.slug),
            '/{0}/'.format(self.simple1.pk))

        for url in [slug_url, pk_url, ]:
            request = self.request_factory.get(url)
            request.LANGUAGE_CODE = 'en'
            request.current_page = self.page
            request.user = self.user
            request.resolver_match = resolve(request.path)

            simple = get_object_from_request(Simple, request)
            self.assertTrue(simple.pk, self.simple1.pk)
    def test_get_obj_from_request(self):
        """ Test that we can get the object from the request. """
        self.simple1.set_current_language('en')
        slug_url = self.simple1.get_absolute_url('en')
        pk_url = slug_url.replace('/{0}/'.format(self.simple1.slug),
                                  '/{0}/'.format(self.simple1.pk))

        for url in [
                slug_url,
                pk_url,
        ]:
            request = self.request_factory.get(url)
            request.LANGUAGE_CODE = 'en'
            request.current_page = self.page
            request.user = self.user
            request.resolver_match = resolve(request.path)

            simple = get_object_from_request(Simple, request)
            self.assertTrue(simple.pk, self.simple1.pk)
 def populate(self):
     config = self.__get_newsblog_config()
     if not config:
         return
     try:
         view_name = self.request.resolver_match.view_name
     except AttributeError:
         return
     user = getattr(self.request, 'user', None)
     if user and view_name:
         if view_name == '{0}:article-detail'.format(config.namespace):
             article = get_object_from_request(Article, self.request)
         else:
             article = None
         menu = self.toolbar.get_or_create_menu('newsblog-app',
                                                config.get_app_title())
         delete_article_perm = user.has_perm(
             'aldryn_newsblog.delete_article')
         if delete_article_perm and article:
             url = get_admin_url(
                 'aldryn_newsblog_article_publish_instant_article',
                 [article.pk, ])
             menu.add_break()
             menu.add_modal_item(_('Publish Instant Article'), url=url)
    def populate(self):
        self.page = get_page_draft(self.request.current_page)
        config = self.__get_services_config()
        if not config:
            # Do nothing if there is no services app_config to work with
            return

        user = getattr(self.request, 'user', None)
        try:
            view_name = self.request.resolver_match.view_name
        except AttributeError:
            view_name = None

        if user and view_name:
            language = get_language_from_request(self.request, check_path=True)

            # get existing admin menu
            admin_menu = self.toolbar.get_or_create_menu(ADMIN_MENU_IDENTIFIER)

            # add new Services item
            admin_menu.add_sideframe_item(_('Services'),
                                          url='/admin/js_services/service/',
                                          position=0)

            # If we're on an Service detail page, then get the service
            if view_name == '{0}:service-detail'.format(config.namespace):
                obj = get_object_from_request(Service, self.request)
            else:
                obj = None

            menu = self.toolbar.get_or_create_menu('services-app',
                                                   config.get_app_title())

            change_config_perm = user.has_perm(
                'js_services.change_servicesconfig')
            add_config_perm = user.has_perm('js_services.add_servicesconfig')
            config_perms = [change_config_perm, add_config_perm]

            change_service_perm = user.has_perm('js_services.change_service')
            delete_service_perm = user.has_perm('js_services.delete_service')
            add_service_perm = user.has_perm('js_services.add_service')
            service_perms = [
                change_service_perm,
                add_service_perm,
                delete_service_perm,
            ]

            if change_config_perm:
                url_args = {}
                if language:
                    url_args = {
                        'language': language,
                    }
                url = get_admin_url('js_services_servicesconfig_change', [
                    config.pk,
                ], **url_args)
                menu.add_modal_item(_('Configure addon'), url=url)

            if any(config_perms) and any(service_perms):
                menu.add_break()

            if change_service_perm:
                url_args = {}
                if config:
                    url_args = {'sections__id__exact': config.pk}
                url = get_admin_url('js_services_service_changelist',
                                    **url_args)
                menu.add_sideframe_item(_('Service list'), url=url)

            if add_service_perm:
                url_args = {
                    'sections': config.pk,
                    'owner': user.pk,
                }
                if language:
                    url_args.update({
                        'language': language,
                    })
                url = get_admin_url('js_services_service_add', **url_args)
                menu.add_modal_item(_('Add new service'), url=url)

            if change_service_perm and obj:
                url_args = {}
                if language:
                    url_args = {
                        'language': language,
                    }
                url = get_admin_url('js_services_service_change', [
                    obj.pk,
                ], **url_args)
                menu.add_modal_item(_('Edit this service'),
                                    url=url,
                                    active=True)

            if delete_service_perm and obj:
                redirect_url = self.get_on_delete_redirect_url(
                    obj, language=language)
                url = get_admin_url('js_services_service_delete', [
                    obj.pk,
                ])
                menu.add_modal_item(_('Delete this service'),
                                    url=url,
                                    on_close=redirect_url)

        if settings.USE_I18N:  # and not self._language_menu:
            if obj:
                self._language_menu = self.toolbar.get_or_create_menu(
                    LANGUAGE_MENU_IDENTIFIER, _('Language'), position=-1)
                self._language_menu.items = []
                languages = get_language_dict(self.current_site.pk)
                page_languages = self.page.get_languages()
                remove = []

                for code, name in get_language_tuple():
                    if code in obj.get_available_languages():
                        remove.append((code, name))
                        try:
                            url = obj.get_absolute_url(code)
                        except NoReverseMatch:
                            url = None
                        if url and code in page_languages:
                            self._language_menu.add_link_item(
                                name,
                                url=url,
                                active=self.current_lang == code)

                if self.toolbar.edit_mode_active:
                    add = [l for l in languages.items() if l not in remove]
                    copy = [
                        (code, name) for code, name in languages.items()
                        if code != self.current_lang and (code, name) in remove
                    ]

                    if (add or len(remove) > 1
                            or copy) and change_service_perm:
                        self._language_menu.add_break(ADD_OBJ_LANGUAGE_BREAK)

                        if add:
                            add_plugins_menu = self._language_menu.get_or_create_menu(
                                '{0}-add-trans'.format(
                                    LANGUAGE_MENU_IDENTIFIER),
                                _('Add Translation'))
                            for code, name in add:
                                url_args = {}
                                url = '%s?language=%s' % (get_admin_url(
                                    'js_services_service_change', [obj.pk], **
                                    url_args), code)
                                add_plugins_menu.add_modal_item(name, url=url)

                        if len(remove) > 1:
                            remove_plugins_menu = self._language_menu.get_or_create_menu(
                                '{0}-del-trans'.format(
                                    LANGUAGE_MENU_IDENTIFIER),
                                _('Delete Translation'))
                            for code, name in remove:
                                url = get_admin_url(
                                    'js_services_service_delete_translation',
                                    [obj.pk, code])
                                remove_plugins_menu.add_modal_item(name,
                                                                   url=url)

                        if copy:
                            copy_plugins_menu = self._language_menu.get_or_create_menu(
                                '{0}-copy-trans'.format(
                                    LANGUAGE_MENU_IDENTIFIER),
                                _('Copy all plugins'))
                            title = _('from %s')
                            question = _(
                                'Are you sure you want to copy all plugins from %s?'
                            )
                            url = get_admin_url(
                                'js_services_service_copy_language', [obj.pk])
                            for code, name in copy:
                                copy_plugins_menu.add_ajax_item(
                                    title % name,
                                    action=url,
                                    data={
                                        'source_language': code,
                                        'target_language': self.current_lang
                                    },
                                    question=question % name,
                                    on_success=self.toolbar.REFRESH_PAGE)
Example #12
0
def populate(self):
    config = self._NewsBlogToolbar__get_newsblog_config()
    if not config:
        # Do nothing if there is no NewsBlog app_config to work with
        return

    user = getattr(self.request, "user", None)
    try:
        view_name = self.request.resolver_match.view_name
    except AttributeError:
        view_name = None

    if user and view_name:
        language = get_language_from_request(self.request, check_path=True)

        # If we're on an Article detail page, then get the article
        if view_name == "{0}:article-detail".format(config.namespace):
            article = get_object_from_request(Article, self.request)
        else:
            article = None

        menu = self.toolbar.get_or_create_menu("newsblog-app",
                                               config.get_app_title())

        change_config_perm = user.has_perm(
            "aldryn_newsblog.change_newsblogconfig")
        add_config_perm = user.has_perm("aldryn_newsblog.add_newsblogconfig")
        config_perms = [change_config_perm, add_config_perm]

        change_article_perm = False
        userprofiles = user.userprofile_set.all()

        if user.is_superuser:
            change_article_perm = True
        else:
            for profile in userprofiles:
                if profile.app_config == config:
                    change_perm = Permission.objects.filter(
                        codename="change_article").first()
                    if change_perm in user.user_permissions.all():
                        change_article_perm = True
                        break

        add_article_perm = user.is_superuser if article else False
        delete_article_perm = user.is_superuser if article else False

        article_perms = [
            change_article_perm, add_article_perm, delete_article_perm
        ]

        if change_config_perm:
            url_args = {}
            if language:
                url_args = {"language": language}
            url = get_admin_url("aldryn_newsblog_newsblogconfig_change",
                                [config.pk], **url_args)
            menu.add_modal_item(_("Configure addon"), url=url)

        if any(config_perms) and any(article_perms):
            menu.add_break()

        if change_article_perm:
            url_args = {}
            if config:
                url_args = {"app_config__id__exact": config.pk}
            url = get_admin_url("aldryn_newsblog_article_changelist",
                                **url_args)
            menu.add_sideframe_item(_("Article list"), url=url)

        # if add_article_perm:
        #     url_args = {'app_config': config.pk, 'owner': user.pk, }
        #     if language:
        #         url_args.update({'language': language, })
        #     url = get_admin_url('aldryn_newsblog_article_add', **url_args)
        #     menu.add_modal_item(_('Add new article'), url=url)

        if change_article_perm and article:
            url_args = {}
            if language:
                url_args = {"language": language}
            url = get_admin_url("aldryn_newsblog_article_change", [article.pk],
                                **url_args)
            menu.add_modal_item(_("Edit this article"), url=url, active=True)

        if change_article_perm and article:
            if article.is_published:
                text = _("Unpublish Article")
                url = reverse("unpublish_article", args=[article.id])
            else:
                text = _("Publish Article")
                url = reverse("publish_article", args=[article.id])

            self.toolbar.add_button(text, url=url, side=self.toolbar.RIGHT)

        if delete_article_perm and article:
            redirect_url = self.get_on_delete_redirect_url(article,
                                                           language=language)
            url = get_admin_url("aldryn_newsblog_article_delete", [article.pk])
            menu.add_modal_item(_("Delete this article"),
                                url=url,
                                on_close=redirect_url)

        try:
            article_review = ArticleReview.objects.get(article=article)
            if not article_review.is_reviewed and user.is_superuser:
                url = reverse("review_article", args=[article.id])
                self.toolbar.add_button(_("Mark Reviewed"),
                                        url=url,
                                        side=self.toolbar.RIGHT)
        except ArticleReview.DoesNotExist:
            pass

        try:
            if user.is_superuser:
                url = (f"{admin_reverse('gsoc_blogposthistory_changelist')}"
                       f"?article__id__exact={article.id}")
                self.toolbar.add_sideframe_item(_("View History"), url=url)
        except Exception as e:
            pass
Example #13
0
    def populate(self):
        config = self.get_app_config(ThingsConfig)
        if not config:
            # Do nothing if there is no NewsBlog app_config to work with
            return

        user = getattr(self.request, 'user', None)
        try:
            view_name = self.request.resolver_match.view_name
        except AttributeError:
            view_name = None

        if not (user and view_name):
            return

        language = get_language_from_request(self.request, check_path=True)

        # If we're on an thing detail page, then get the thing
        if view_name == '{0}:thing-detail'.format(config.namespace):
            thing = get_object_from_request(Thing, self.request)
        else:
            thing = None

        menu = self.toolbar.get_or_create_menu('things-app',
                                               config.get_app_title())

        change_config_perm = user.has_perm('things.change_thingsconfig')

        add_thing_perm = user.has_perm('things.add_thing')
        change_thing_perm = user.has_perm('things.change_thing')
        delete_thing_perm = user.has_perm('things.delete_thing')
        thing_perms = [add_thing_perm, change_thing_perm,
                       delete_thing_perm, ]

        if change_config_perm:
            url_args = {}
            if language:
                url_args = {'language': language, }
            url = get_admin_url('things_thingsconfig_change',
                                [config.pk, ], **url_args)
            menu.add_modal_item(_('Configure addon'), url=url)

        if change_config_perm and any(thing_perms):
            menu.add_break()

        if change_thing_perm:
            url_args = {}
            if config:
                url_args = {'app_config__id__exact': config.pk}
            url = get_admin_url('things_thing_changelist',
                                **url_args)
            menu.add_sideframe_item(_('Thing list'), url=url)

        if add_thing_perm:
            url_args = {'app_config': config.pk, 'owner': user.pk, }
            if language:
                url_args.update({'language': language, })
            url = get_admin_url('things_thing_add', **url_args)
            menu.add_modal_item(_('Add new thing'), url=url)

        if change_thing_perm and thing:
            url_args = {}
            if language:
                url_args = {'language': language, }
            url = get_admin_url('things_thing_change',
                                [thing.pk, ], **url_args)
            menu.add_modal_item(_('Edit this thing'), url=url, active=True)

        if delete_thing_perm and thing:
            redirect_url = self.get_on_delete_redirect_url(thing)
            url = get_admin_url('things_thing_delete',
                                [thing.pk, ])
            menu.add_modal_item(_('Delete this thing'), url=url,
                                on_close=redirect_url)
Example #14
0
    def populate(self):
        config = self.__get_dashboards_app_config()
        if not config:
            # Do nothing if there is no Dashboards_app app_config to work with
            return

        user = getattr(self.request, 'user', None)
        try:
            view_name = self.request.resolver_match.view_name
        except AttributeError:
            view_name = None

        if user and view_name:
            language = get_language_from_request(self.request, check_path=True)

            # If we're on an Dashboard detail page, then get the dashboard
            if view_name == '{0}:dashboard-detail'.format(config.namespace):
                dashboard = get_object_from_request(Dashboard, self.request)
            else:
                dashboard = None

            menu = self.toolbar.get_or_create_menu('dashboards_app-app',
                                                   config.get_app_title())

            change_config_perm = user.has_perm(
                'dashboards_app.change_dashboards_appconfig')
            add_config_perm = user.has_perm(
                'dashboards_app.add_dashboards_appconfig')
            config_perms = [change_config_perm, add_config_perm]

            change_dashboard_perm = user.has_perm(
                'dashboards_app.change_dashboard')
            delete_dashboard_perm = user.has_perm(
                'dashboards_app.delete_dashboard')
            add_dashboard_perm = user.has_perm('dashboards_app.add_dashboard')
            dashboard_perms = [
                change_dashboard_perm,
                add_dashboard_perm,
                delete_dashboard_perm,
            ]

            if change_config_perm:
                url_args = {}
                if language:
                    url_args = {
                        'language': language,
                    }
                url = get_admin_url(
                    'dashboards_app_dashboards_appconfig_change', [
                        config.pk,
                    ], **url_args)
                menu.add_modal_item(_('Configure addon'), url=url)

            if any(config_perms) and any(dashboard_perms):
                menu.add_break()

            if change_dashboard_perm:
                url_args = {}
                if config:
                    url_args = {'app_config__id__exact': config.pk}
                url = get_admin_url('dashboards_app_dashboard_changelist',
                                    **url_args)
                menu.add_sideframe_item(_('Dashboard list'), url=url)

            if add_dashboard_perm:
                url_args = {
                    'app_config': config.pk,
                    'owner': user.pk,
                }
                if language:
                    url_args.update({
                        'language': language,
                    })
                url = get_admin_url('dashboards_app_dashboard_add', **url_args)
                menu.add_modal_item(_('Add new dashboard'), url=url)

            if change_dashboard_perm and dashboard:
                url_args = {}
                if language:
                    url_args = {
                        'language': language,
                    }
                url = get_admin_url('dashboards_app_dashboard_change', [
                    dashboard.pk,
                ], **url_args)
                menu.add_modal_item(_('Edit this dashboard'),
                                    url=url,
                                    active=True)

            if delete_dashboard_perm and dashboard:
                redirect_url = self.get_on_delete_redirect_url(
                    dashboard, language=language)
                url = get_admin_url('dashboards_app_dashboard_delete', [
                    dashboard.pk,
                ])
                menu.add_modal_item(_('Delete this dashboard'),
                                    url=url,
                                    on_close=redirect_url)
    def populate(self):
        config = self.__get_newsblog_config()
        if not config:
            # Do nothing if there is no NewsBlog app_config to work with
            return

        user = getattr(self.request, 'user', None)
        try:
            view_name = self.request.resolver_match.view_name
        except AttributeError:
            view_name = None

        if user and view_name:
            language = get_language_from_request(self.request, check_path=True)

            # If we're on an Article detail page, then get the article
            if view_name == '{0}:article-detail'.format(config.namespace):
                article = get_object_from_request(Article, self.request)
            else:
                article = None

            menu = self.toolbar.get_or_create_menu('newsblog-app',
                                                   config.get_app_title())

            change_config_perm = user.has_perm(
                'aldryn_newsblog.change_newsblogconfig')
            add_config_perm = user.has_perm(
                'aldryn_newsblog.add_newsblogconfig')
            config_perms = [change_config_perm, add_config_perm]

            change_article_perm = user.has_perm(
                'aldryn_newsblog.change_article')
            delete_article_perm = user.has_perm(
                'aldryn_newsblog.delete_article')
            add_article_perm = user.has_perm('aldryn_newsblog.add_article')
            article_perms = [change_article_perm, add_article_perm,
                             delete_article_perm, ]

            if change_config_perm:
                url_args = {}
                if language:
                    url_args = {'language': language, }
                url = get_admin_url('aldryn_newsblog_newsblogconfig_change',
                                    [config.pk, ], **url_args)
                menu.add_modal_item(_('Configure addon'), url=url)

            if any(config_perms) and any(article_perms):
                menu.add_break()

            if change_article_perm:
                url_args = {}
                if config:
                    url_args = {'app_config__id__exact': config.pk}
                url = get_admin_url('aldryn_newsblog_article_changelist',
                                    **url_args)
                menu.add_sideframe_item(_('Article list'), url=url)

            if add_article_perm:
                url_args = {'app_config': config.pk, 'owner': user.pk, }
                if language:
                    url_args.update({'language': language, })
                url = get_admin_url('aldryn_newsblog_article_add', **url_args)
                menu.add_modal_item(_('Add new article'), url=url)

            if change_article_perm and article:
                url_args = {}
                if language:
                    url_args = {'language': language, }
                url = get_admin_url('aldryn_newsblog_article_change',
                                    [article.pk, ], **url_args)
                menu.add_modal_item(_('Edit this article'), url=url,
                                    active=True)

            if delete_article_perm and article:
                redirect_url = self.get_on_delete_redirect_url(
                    article, language=language)
                url = get_admin_url('aldryn_newsblog_article_delete',
                                    [article.pk, ])
                menu.add_modal_item(_('Delete this article'), url=url,
                                    on_close=redirect_url)
Example #16
0
    def populate(self):
        config = self.__get_newsblog_config()
        if not config:
            # Do nothing if there is no NewsBlog app_config to work with
            return

        user = getattr(self.request, 'user', None)
        try:
            view_name = self.request.resolver_match.view_name
        except AttributeError:
            view_name = None

        if user and view_name:
            language = get_language_from_request(self.request, check_path=True)

            # If we're on an Article detail page, then get the article
            if view_name == '{0}:article-detail'.format(config.namespace):
                article = get_object_from_request(Article, self.request)
            else:
                article = None

            menu = self.toolbar.get_or_create_menu('newsblog-app',
                                                   config.get_app_title())

            change_config_perm = user.has_perm(
                'aldryn_newsblog.change_newsblogconfig')
            add_config_perm = user.has_perm(
                'aldryn_newsblog.add_newsblogconfig')
            config_perms = [change_config_perm, add_config_perm]

            change_article_perm = user.has_perm(
                'aldryn_newsblog.change_article')
            delete_article_perm = user.has_perm(
                'aldryn_newsblog.delete_article')
            add_article_perm = user.has_perm('aldryn_newsblog.add_article')
            article_perms = [
                change_article_perm,
                add_article_perm,
                delete_article_perm,
            ]

            if change_config_perm:
                url_args = {}
                if language:
                    url_args = {
                        'language': language,
                    }
                url = get_admin_url('aldryn_newsblog_newsblogconfig_change', [
                    config.pk,
                ], **url_args)
                menu.add_modal_item(_('Configure addon'), url=url)

            if any(config_perms) and any(article_perms):
                menu.add_break()

            if change_article_perm:
                url_args = {}
                if config:
                    url_args = {'app_config__id__exact': config.pk}
                url = get_admin_url('aldryn_newsblog_article_changelist',
                                    **url_args)
                menu.add_sideframe_item(_('Article list'), url=url)

            if add_article_perm:
                url_args = {
                    'app_config': config.pk,
                    'owner': user.pk,
                }
                if language:
                    url_args.update({
                        'language': language,
                    })
                url = get_admin_url('aldryn_newsblog_article_add', **url_args)
                menu.add_modal_item(_('Add new article'), url=url)

            if change_article_perm and article:
                url_args = {}
                if language:
                    url_args = {
                        'language': language,
                    }
                url = get_admin_url('aldryn_newsblog_article_change', [
                    article.pk,
                ], **url_args)
                menu.add_modal_item(_('Edit this article'),
                                    url=url,
                                    active=True)

            if delete_article_perm and article:
                redirect_url = self.get_on_delete_redirect_url(
                    article, language=language)
                url = get_admin_url('aldryn_newsblog_article_delete', [
                    article.pk,
                ])
                menu.add_modal_item(_('Delete this article'),
                                    url=url,
                                    on_close=redirect_url)
Example #17
0
def populate(self):
    config = self._NewsBlogToolbar__get_newsblog_config()
    if not config:
        # Do nothing if there is no NewsBlog app_config to work with
        return

    user = getattr(self.request, 'user', None)
    try:
        view_name = self.request.resolver_match.view_name
    except AttributeError:
        view_name = None

    if user and view_name:
        language = get_language_from_request(self.request, check_path=True)

        # If we're on an Article detail page, then get the article
        if view_name == '{0}:article-detail'.format(config.namespace):
            article = get_object_from_request(Article, self.request)
        else:
            article = None

        menu = self.toolbar.get_or_create_menu('newsblog-app',
                                               config.get_app_title())

        change_config_perm = user.has_perm(
            'aldryn_newsblog.change_newsblogconfig')
        add_config_perm = user.has_perm('aldryn_newsblog.add_newsblogconfig')
        config_perms = [change_config_perm, add_config_perm]

        change_article_perm = False
        userprofiles = user.userprofile_set.all()

        if user.is_superuser:
            change_article_perm = True
        else:
            for profile in userprofiles:
                if profile.app_config == config:
                    change_article_perm = True
                    break

        add_article_perm = user.is_superuser if article else False
        delete_article_perm = user.is_superuser if article else False

        article_perms = [
            change_article_perm,
            add_article_perm,
            delete_article_perm,
        ]

        if change_config_perm:
            url_args = {}
            if language:
                url_args = {
                    'language': language,
                }
            url = get_admin_url('aldryn_newsblog_newsblogconfig_change', [
                config.pk,
            ], **url_args)
            menu.add_modal_item(_('Configure addon'), url=url)

        if any(config_perms) and any(article_perms):
            menu.add_break()

        if change_article_perm:
            url_args = {}
            if config:
                url_args = {'app_config__id__exact': config.pk}
            url = get_admin_url('aldryn_newsblog_article_changelist',
                                **url_args)
            menu.add_sideframe_item(_('Article list'), url=url)

        # if add_article_perm:
        #     url_args = {'app_config': config.pk, 'owner': user.pk, }
        #     if language:
        #         url_args.update({'language': language, })
        #     url = get_admin_url('aldryn_newsblog_article_add', **url_args)
        #     menu.add_modal_item(_('Add new article'), url=url)

        if change_article_perm and article:
            url_args = {}
            if language:
                url_args = {
                    'language': language,
                }
            url = get_admin_url('aldryn_newsblog_article_change', [
                article.pk,
            ], **url_args)
            menu.add_modal_item(_('Edit this article'), url=url, active=True)

        if change_article_perm and article:
            if article.is_published:
                text = _('Unpublish Article')
                url = reverse('unpublish_article', args=[article.id])
            else:
                text = _('Publish Article')
                url = reverse('publish_article', args=[article.id])

            self.toolbar.add_button(text, url=url, side=self.toolbar.RIGHT)

        if delete_article_perm and article:
            redirect_url = self.get_on_delete_redirect_url(article,
                                                           language=language)
            url = get_admin_url('aldryn_newsblog_article_delete', [
                article.pk,
            ])
            menu.add_modal_item(_('Delete this article'),
                                url=url,
                                on_close=redirect_url)

        try:
            article_review = ArticleReview.objects.get(article=article)
            if not article_review.is_reviewed and user.is_superuser:
                url = reverse('review_article', args=[article.id])
                self.toolbar.add_button(_('Mark Reviewed'),
                                        url=url,
                                        side=self.toolbar.RIGHT)
        except ArticleReview.DoesNotExist:
            pass
Example #18
0
    def populate(self):
        config = self.get_app_config(ThingsConfig)
        if not config:
            # Do nothing if there is no NewsBlog app_config to work with
            return

        user = getattr(self.request, "user", None)
        try:
            view_name = self.request.resolver_match.view_name
        except AttributeError:
            view_name = None

        if not (user and view_name):
            return

        language = get_language_from_request(self.request, check_path=True)

        # If we're on an thing detail page, then get the thing
        if view_name == "{0}:thing-detail".format(config.namespace):
            thing = get_object_from_request(Thing, self.request)
        else:
            thing = None

        menu = self.toolbar.get_or_create_menu("things-app", config.get_app_title())

        change_config_perm = user.has_perm("things.change_thingsconfig")

        add_thing_perm = user.has_perm("things.add_thing")
        change_thing_perm = user.has_perm("things.change_thing")
        delete_thing_perm = user.has_perm("things.delete_thing")
        thing_perms = [add_thing_perm, change_thing_perm, delete_thing_perm]

        if change_config_perm:
            url_args = {}
            if language:
                url_args = {"language": language}
            url = get_admin_url("things_thingsconfig_change", [config.pk], **url_args)
            menu.add_modal_item(_("Configure addon"), url=url)

        if change_config_perm and any(thing_perms):
            menu.add_break()

        if change_thing_perm:
            url_args = {}
            if config:
                url_args = {"app_config__id__exact": config.pk}
            url = get_admin_url("things_thing_changelist", **url_args)
            menu.add_sideframe_item(_("Thing list"), url=url)

        if add_thing_perm:
            url_args = {"app_config": config.pk, "owner": user.pk}
            if language:
                url_args.update({"language": language})
            url = get_admin_url("things_thing_add", **url_args)
            menu.add_modal_item(_("Add new thing"), url=url)

        if change_thing_perm and thing:
            url_args = {}
            if language:
                url_args = {"language": language}
            url = get_admin_url("things_thing_change", [thing.pk], **url_args)
            menu.add_modal_item(_("Edit this thing"), url=url, active=True)

        if delete_thing_perm and thing:
            redirect_url = self.get_on_delete_redirect_url(thing)
            url = get_admin_url("things_thing_delete", [thing.pk])
            menu.add_modal_item(_("Delete this thing"), url=url, on_close=redirect_url)