def get_object_data(self, **kwargs): """List of object fields to display. Choice fields values are expanded to readable choice label. """ logger.debug("UserProfile {}".format(self.object)) return super().get_object_data()
def get_absolute_url(self, model_object, model_name, url_name): '''get the absolute URL for the model object.''' model_path = self.get_model_path(model_name) try: return reverse(self.get_named_url(model_name, url_name), args=self.get_model_key(model_object, model_path)) except NoReverseMatch as exc: logger.debug('No reverse for url {} with keys {}'.format(self.get_named_url(model_name, url_name), self.get_model_key(model_object, model_path))) raise exc pass
def register(self, module): """Register a site module. Can be called on the package level. """ tagvalues = "\n".join(["%s: %s" % (attr, str(getattr(module, attr))) for attr in dir(module) if attr in ['create', 'menu', 'name', 'label'] ]) # tagvalues = "\n".join(["%s" % (attr) for attr in dir(module) if attr not in ['urls'] ]) logger.debug("module {} registered.\ndir : {}".format(module.label, tagvalues )) self._registry[module.label] = module self._modules[module.name] = module pass
def context_processor(request): """Add current site module and site module list to the template context.""" if not hasattr(request, 'user'): raise ValueError('site module context processor requires "django.contrib.auth.context_processors.auth"' 'to be in TEMPLATE_CONTEXT_PROCESSORS in your settings file.') module = None if request.resolver_match: module = module_registry.get_module(request.resolver_match.app_name) if module is None: logger.info('You need to add SiteModuleMixin to AppConfig instance of "{}"'.format(request.resolver_match.app_name)) attrs = ", ".join( "{}={}".format(attr, getattr( request.resolver_match, attr, "None")) for attr in [ 'url_name', 'app_name', 'namespace', 'route', 'viewName']) logger.debug("request.resolver_match ==> {}".format(attrs)) pass logger.debug("module ==> {}".format("None" if module is None else module.name)) pass else: logger.debug("request.resolver_match is None") pass modules = module_registry.available_modules(request.user) logger.debug("n-modules {}: {} in {}".format(len(modules), module.name if module is not None else "NONE", ",".join([module.name for module in modules]))) return { 'modules': modules, 'current_module': module, }
def get_context_data(self, **kwargs): """Additional context data for team list view. :keyword add_url: Link to the add view :keyword join_url: Link to the team member create view (aka joining a team) """ context = super(TeamListView, self).get_context_data(**kwargs) # opts = self.object_list.model._meta join_name = self.get_named_url('join') try: context['join_url'] = reverse(join_name) pass except NoReverseMatch: logger.debug("TeamListView::get_context_data join_url '{}' doesn't have reverse.".format(join_name)) pass return context
def has_update_permission(self, request, obj): """Object chane permission check. If view had a `viewset`, the `viewset.has_update_permission` used. If true, view will show `Update` link to the Update view. """ if self.viewset is not None: return self.viewset.has_update_permission(request, obj) # default lookup for the django permission opts = self.model._meta codename = get_permission_codename('change', opts) update_perm = '{}.{}'.format(opts.app_label, codename) logger.debug('DetailModelView.has_update_permission: {}'.format(update_perm)) if request.user.has_perm(update_perm): return True return request.user.has_perm(update_perm, obj=obj)
def get_context_data(self, **kwargs): """Additional context data for list view. :keyword add_url: Link to the add view """ kwargs.setdefault('form_template_pack', SITE_TEMPLATE_PACKS) opts = self.object_list.model._meta module = module_registry.get_module(opts.app_label) create_name = module.get_named_url(opts.model_name, 'create') try: if self.has_add_permission(self.request): kwargs['create_url'] = reverse(create_name) pass pass except NoReverseMatch: logger.debug( "DataTableMixin::get_context_data create_name '{}' doesn't have reverse." .format(create_name)) pass return super(ListModelView, self).get_context_data(**kwargs)
def get_context_data(self, **kwargs): """Additional context data for detail view. :keyword object_data: List of fields and values of the object :keyword update_url: Link to the update view :keyword delete_url: Link to the delete view """ kwargs.setdefault('form_template_pack', SITE_TEMPLATE_PACKS) opts = self.model._meta kwargs['object_data'] = self.get_object_data() if self.has_update_permission(self.request, self.object): try: kwargs['update_url'] = self.get_absolute_url(self.object, 'update') logger.debug("detail.get_context_data: update_url {}".format(kwargs['update_url'])) pass except NoReverseMatch: logger.debug("update_url {} does not exist.".format(self.get_named_url('update')) ) pass pass else: logger.debug("detail.get_context_data: no update_url due to perm.") pass if self.has_delete_permission(self.request, self.object): try: kwargs['delete_url'] = self.get_absolute_url(self.object, 'delete') logger.debug("detail.get_context_data: delete_url {}".format(kwargs['update_url'])) pass except NoReverseMatch: logger.debug("delete_url {} does not exist.".format(self.get_named_url('delete')) ) pass pass return super(DetailModelView, self).get_context_data(**kwargs)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) user = self.object.user context['user'] = user logger.debug("Profile {}, User {}".format(self.object, user)) return context