class RESTModelCoreResourcePermissionsMixin(RESTObjectPermissionsMixin): pk_name = 'pk' permission = PermissionsSet( # HTTP permissions head=CoreReadAllowed(), options=CoreReadAllowed(), post=CoreCreateAllowed(), get=CoreReadAllowed(), put=CoreUpdateAllowed(), patch=CoreUpdateAllowed(), delete=CoreDeleteAllowed(), # Serializer permissions create_obj=CoreCreateAllowed(), read_obj=CoreReadAllowed(), update_obj=CoreUpdateAllowed(), delete_obj=CoreDeleteAllowed(), # Other permissions **{ DEFAULT_PERMISSION: CoreAllowed(), } ) def _get_perm_obj_or_none(self, pk=None): pk = pk or self.kwargs.get(self.pk_name) if pk: return get_object_or_none(self.core.model, pk=pk) else: return None
class ReadonlyDetailModelFormView(DetailModelFormView): show_save_and_continue = False permission = PermissionsSet(get=CoreReadAllowed(), **{ DEFAULT_PERMISSION: CoreAllowed(), }) def is_readonly(self): return True
class DefaultCoreViewMixin(DefaultViewMixin): core = None view_type = None title = None permission = PermissionsSet(get=CoreReadAllowed(), **{ DEFAULT_PERMISSION: CoreAllowed(), }) def __init__(self): super(DefaultCoreViewMixin, self).__init__() self.site_name = self.core.site_name self.menu_groups = self.core.get_menu_groups() @classmethod def __init_core__(cls, core, pattern): cls.core = core cls.pattern = pattern def dispatch(self, request, *args, **kwargs): self.core.init_ui_request(request) return super(DefaultCoreViewMixin, self).dispatch(request, *args, **kwargs) def get_title(self): return self.title @property def view_name(self): return '%s-%s' % (self.view_type, '-'.join(self.menu_groups)) def get_context_data(self, **kwargs): context_data = super(DefaultCoreViewMixin, self).get_context_data(**kwargs) extra_context_data = { 'core_permission': self.core.permission, 'permission': self.permission, 'view': self } extra_context_data.update(context_data) return extra_context_data
class DetailModelFormView(GetCoreObjViewMixin, DefaultCoreModelFormView): template_name = 'is_core/generic_views/detail_form.html' form_template = 'is_core/forms/model_detail_form.html' view_type = 'detail' messages = { 'success': _('The %(name)s "%(obj)s" was changed successfully.'), 'error': _('Please correct the error below.') } pk_name = 'pk' permission = PermissionsSet(get=CoreReadAllowed() | CoreUpdateAllowed(), post=CoreUpdateAllowed(), **{ DEFAULT_PERMISSION: CoreAllowed(), }) @property def detail_verbose_name(self): return self.model._ui_meta.detail_verbose_name def get_title(self): return (self.title or self.detail_verbose_name % { 'verbose_name': self.model._meta.verbose_name, 'verbose_name_plural': self.model._meta.verbose_name_plural, 'obj': self.get_obj(True) }) def is_readonly(self): return not self.has_permission('post') def link(self, arguments=None, **kwargs): if arguments is None: arguments = (self.kwargs[self.pk_name], ) return super().link(arguments=arguments, **kwargs) # TODO: get_obj should not be inside core get_obj and _get_perm_obj_or_404 should have same implementation def _get_perm_obj_or_404(self, pk=None): """ If is send parameter pk is returned object according this pk, else is returned object from get_obj method, but it search only inside filtered values for current user, finally if object is still None is returned according the input key from all objects. If object does not exist is raised Http404 """ if pk: obj = get_object_or_none(self.core.model, pk=pk) else: try: obj = self.get_obj(False) except Http404: obj = get_object_or_none(self.core.model, **self.get_obj_filters()) if not obj: raise Http404 return obj def _get_export_types(self): return self.core.get_ui_detail_export_types( self.request) if self.export_types is None else self.export_types def _get_export_fields(self): return list( self.core.get_ui_detail_export_fields(self.request, self.get_obj(True))) def _generate_rest_detail_export_fieldset(self): return ModelFlatRESTFields.create_from_flat_list( self._get_export_fields(), self.model) def get_context_data(self, form=None, inline_form_views=None, **kwargs): context_data = super().get_context_data( form=form, inline_form_views=inline_form_views, **kwargs) if self._get_export_types() and self._get_export_fields(): context_data.update({ 'export_types': get_export_types_with_content_type(self._get_export_types()), 'rest_detail_export_fieldset': self._generate_rest_detail_export_fieldset(), 'api_url': self.core.get_api_detail_url(self.request, self.get_obj(True)) }) return context_data