def index_admin(request, extra_context=None): """ Display the main admin index page, which lists all of the installed apps that have been registered in this site. """ APPS_EXIBIR = { 'Account': ['User'], 'Egresso': ['Egresso', 'Formacao'], 'Empresa': ['Empresa'], 'Oportunidade': ['Oportunidade'], 'Curso': ['Curso', 'NivelCurso'], 'Questionario': ['Questao', 'Questionario'] } app_list = admin_site.get_app_list(request) app_list = [app for app in app_list if app['name'] in APPS_EXIBIR] for app in app_list: app_name = app['name'] app['models'] = [m for m in app['models'] if m['object_name'] in APPS_EXIBIR[app_name]] context = { **admin_site.each_context(request), 'title': admin_site.index_title, 'app_list': app_list, **(extra_context or {}), } request.current_app = admin_site.name return TemplateResponse(request, 'admin/index.html', context)
def test_as_list_structure_default_options(self): settings.AUTO_CREATE_APP_GROUP = True request = self.factory.get(reverse('admin:index')) request.user = self.superuser result = AppGroup.objects.as_list(request, False) self.assertEqual(len(result), 1) app = result[0] app_model = app['models'][0] original_app_list = site.get_app_list(request) original_app = [ oa for oa in original_app_list if oa['app_label'] == 'auth' ][0] original_app_model = [ oam for oam in original_app['models'] if oam['object_name'] == 'User' ][0] # The newly created app has no matches in the original app list. self.assertEqual(app['name'], self.app_group.name) self.assertEqual(app['app_label'], self.app_group.slug) # Attributes that are in the original structure as well. self.assertDictEqual(app_model['perms'], original_app_model['perms']) for key in ['name', 'object_name', 'admin_url', 'add_url']: self.assertEqual(app_model[key], original_app_model[key]) # Attributes copied from the original app to the model, just for reference as to where the app originally # belonged to. for key in ['app_label', 'app_url', 'has_module_perms']: self.assertEqual(app_model[key], original_app[key]) settings.AUTO_CREATE_APP_GROUP = False
def get(self, request, **kwargs): """Get the form to add a new authorized Spotify Account.""" player = kwargs.get("spotify", None) render_data = { "view_name": self.view_name, "opts": self.instance_type._meta, "has_view_permission": site.has_permission(request), "site_header": site.site_header, "site_url": site.site_url, "app_label": self.instance_type._meta.app_label, "original": player, "is_popup": False, "is_nav_sidebar_enabled": site.enable_nav_sidebar, "available_apps": site.get_app_list(request), } if player is not None: render_data["form"] = SpotifyTokenForm( initial={ "client_id": player.client_id, "client_secret": player.client_secret, } # noqa ) else: render_data["form"] = SpotifyTokenForm() return render(request, self.template_name, render_data)
def get(self, request, **kwargs): """GET the view.""" spotify = kwargs.get("spotify") render_data = { "view_name": self.view_name, "opts": self.instance_type._meta, "has_view_permission": site.has_permission(request), "site_header": site.site_header, "site_url": site.site_url, "app_label": self.instance_type._meta.app_label, "original": spotify, "is_popup": False, "is_nav_sidebar_enabled": site.enable_nav_sidebar, "available_apps": site.get_app_list(request), } return render(request, self.template_name, render_data)
def dashboard_app_list(context): try: request = context["request"] except KeyError: raise ImproperlyConfigured( "Django admin index requires 'django.template.context_processors.request' to be configured." ) # Get the new app_list. app_list = AppGroup.objects.as_list( request, settings.show_remaining_apps(request.user.is_superuser)) # Use default app_list if there were no groups and no "misc" section. if not app_list: app_list = site.get_app_list(request) return app_list
def dashboard(request): if hasattr(request, "user") and hasattr(request, "path"): # noqa # Get the new app_list. app_list = AppGroup.objects.as_list( request, settings.show_remaining_apps(request.user.is_superuser) ) # Use default app_list if there were no groups and no "misc" section. if not app_list: app_list = site.get_app_list(request) return { "dashboard_app_list": app_list, "admin_index_settings": settings.as_dict(), } return {}
def get(self, request): """ Returns a json representing the menu voices in a format eaten by the js menu. Raised ImproperlyConfigured exceptions can be viewed in the browser console """ self.app_list = site.get_app_list(request) self.apps_dict = self.create_app_list_dict() # no menu provided items = self.get_menu(request) if not items: voices = self.get_default_voices() else: voices = [] for item in items: self.add_voice(voices, item) return JsonResponse(voices, safe=False)
def post(self, request, **kwargs): """ Post the OAuth credentials to add a new authorized Spotify Account. :param request: the request :param kwargs: keyword arguments :return: either a render of the authorize page on error or a redirect to the authorization url otherwise """ form = SpotifyTokenForm(request.POST) if form.is_valid(): spotify_auth_code, _ = Player.objects.get_or_create( client_id=form.cleaned_data.get("client_id")) spotify_auth_code.client_secret = form.cleaned_data.get( "client_secret") spotify_auth_code.redirect_uri = request.build_absolute_uri( reverse("admin:add_token")) spotify_auth_code.save() spotify_oauth = redirect( spotify_auth_code.auth.get_authorize_url()) spotify_oauth.set_cookie(COOKIE_CLIENT_ID, spotify_auth_code.client_id) return spotify_oauth player = kwargs.get("spotify", None) render_data = { "view_name": self.view_name, "opts": self.instance_type._meta, "has_view_permission": site.has_permission(request), "site_header": site.site_header, "site_url": site.site_url, "app_label": self.instance_type._meta.app_label, "original": player, "is_popup": False, "is_nav_sidebar_enabled": site.enable_nav_sidebar, "available_apps": site.get_app_list(request), "form": form, } return render(request, self.template_name, render_data)
def as_list(self, request, include_remaining=True): # Convert to convienent dict model_dicts = {} if django.VERSION < (1, 9): from .compat.django18 import get_app_list original_app_list = get_app_list(site, request) else: original_app_list = site.get_app_list(request) for app in original_app_list: for model in app['models']: key = '{}.{}'.format(app['app_label'], model['object_name'].lower()) # noqa model_dict = model.copy() model_dict.update({ 'app_label': app['app_label'], 'app_name': app['name'], 'app_url': app['app_url'], 'has_module_perms': app['has_module_perms'], 'active': request.path.startswith(model_dict['admin_url']), }) model_dicts[key] = model_dict added = [] # Create new list based on our groups, using the model_dicts constructed above. # noqa result = [] app_list = self.prefetch_related('models', 'applink_set') active_app = request.path == reverse('admin:index') for app in app_list: models = [] active = False for model in app.models.all(): key = '{}.{}'.format(model.app_label, model.model) o = model_dicts.get(key) if o: models.append(o) added.append(key) if o['active']: active = True for app_link in app.applink_set.all(): models.append({ 'name': app_link.name, 'app_label': app.slug, 'admin_url': app_link.link, 'active': request.path.startswith(app_link.link), }) active = request.path.startswith(app_link.link) if models: result.append({ 'name': app.name, 'app_label': app.slug, 'models': sorted(models, key=lambda m: m['name']), 'active': active, }) if active: active_app = True other = [model_dicts[k] for k in model_dicts if k not in added] if settings.AUTO_CREATE_APP_GROUP: for model in other: app_group, created = AppGroup.objects.get_or_create( slug=model['app_label'], defaults={'name': model['app_name']}) contenttype = ContentTypeProxy.objects.get( app_label=model['app_label'], model=model['object_name'].lower()) app_group.models.add(contenttype) elif other and include_remaining: result.append({ 'name': _('Miscellaneous'), 'app_label': 'misc', 'models': sorted(other, key=lambda m: m['name']), 'active': not active_app, }) return result
def as_list(self, request, include_remaining=True): # Convert to convienent dict model_dicts = {} original_app_list = site.get_app_list(request) for app in original_app_list: for model in app["models"]: key = "{}.{}".format(app["app_label"], model["object_name"].lower()) # noqa model_dict = model.copy() # If the user lacks create/read/update permissions, these # variables are None in the model_dict if model_dict.get("admin_url"): active = request.path.startswith(model_dict["admin_url"]) elif model_dict.get("add_url"): active = request.path.startswith(model_dict["add_url"]) else: active = False model_dict.update({ "app_label": app["app_label"], "app_name": app["name"], "app_url": app["app_url"], "has_module_perms": app["has_module_perms"], "active": active, }) model_dicts[key] = model_dict added = [] # Create new list based on our groups, using the model_dicts constructed above. # noqa result = [] app_list = self.prefetch_related("models", "applink_set") active_app = request.path == reverse("admin:index") for app in app_list: models = [] active = False for model in app.models.all(): key = "{}.{}".format(model.app_label, model.model) o = model_dicts.get(key) if o: models.append(o) added.append(key) if o["active"]: active = True for app_link in app.applink_set.all(): models.append({ "name": app_link.name, "app_label": app.slug, "admin_url": app_link.link, "active": request.path.startswith(app_link.link), }) active = request.path.startswith(app_link.link) if models: result.append({ "name": app.name, "app_label": app.slug, "models": sorted(models, key=lambda m: m["name"]), "active": active, }) if active: active_app = True other = [model_dicts[k] for k in model_dicts if k not in added] if settings.AUTO_CREATE_APP_GROUP: new_apps = False for model in other: app_group, created = AppGroup.objects.get_or_create( slug=model["app_label"], defaults={"name": model["app_name"]}) if created: new_apps = True contenttype = ContentTypeProxy.objects.get( app_label=model["app_label"], model=model["object_name"].lower()) app_group.models.add(contenttype) # If apps are created, rerender the list. if new_apps: return self.as_list(request, include_remaining) elif other and include_remaining: result.append({ "name": _("Miscellaneous"), "app_label": "misc", "models": sorted(other, key=lambda m: m["name"]), "active": not active_app, }) return result