Beispiel #1
0
    def __init__(self, options):
        """
        Create a new instance of a widget with the given options.
        """
        # widget options
        if options is None:
            options = {}
        self.options = options

        # acl
        if hasattr(self, 'model'):
            self.acl = Acl.of(self.model)
        else:
            self.acl = Acl.default(None)
Beispiel #2
0
def get_edit_reference(request,
                       instance,
                       property_names,
                       help_text=None,
                       shortcut=False):
    """
    Return reference information about editing the given list of properties for the
    given object instance.
    """
    if settings.CUBANE_FRONTEND_EDITING:
        if request is not None and request.user is not None and (
                request.user.is_staff or request.user.is_superuser):
            if instance is not None and property_names:
                # make sure that the user has access to the instance
                if Acl.of(instance.__class__).can_edit_instance(
                        request, instance):
                    # try to extract help text from form
                    if help_text is None and len(property_names) == 1:
                        if hasattr(instance.__class__, 'get_form'):
                            form = instance.__class__.get_form()
                            if form:
                                field = form.declared_fields.get(
                                    property_names[0])
                                if field:
                                    help_text = field.help_text

                    # construct reference
                    return '%s%s|%s|%s%s' % ('!' if shortcut else '',
                                             model_to_hash(instance.__class__),
                                             instance.pk,
                                             ':'.join(property_names), '|%s' %
                                             help_text if help_text else '')

    return None
Beispiel #3
0
    def db_get(self, request, model_name, pk):
        """
        As part of the REST API, return a specific instance of the given model
        with the given pk.
        """
        # get model
        try:
            model = get_class_from_string(model_name)
        except:
            return self._db_error('Unknown model.')

        # get instance
        try:
            qs = model.objects.filter(pk=pk)
            instance = Acl.of(model).filter(request, qs)[0]
        except:
            return self._db_error('Unknown pk.')

        # result
        return {
            'success': True,
            'message': 'OK',
            'result': model_to_dict(instance,
                                    exclude_many_to_many=True,
                                    json=True)
        }
Beispiel #4
0
    def images(self, request):
        """
        Return a list of images.
        """
        if 'cubane.media' in settings.INSTALLED_APPS:
            from cubane.media.models import Media
            images = Media.objects.filter(is_image=True)
            images = Acl.of(Media).filter(request, images)
        else:
            images = []

        return images
Beispiel #5
0
    def db_get_all(self, request, model_name):
        """
        As part of the REST API, return a list of all model instances.
        """
        # get model
        try:
            model = get_class_from_string(model_name)
        except:
            return self._db_error('Unknown model.')

        # get list
        qs = model.objects.all()
        instances = Acl.of(model).filter(request, qs)
        result = [
            model_to_dict(instance, exclude_many_to_many=True, json=True)
            for instance in instances
        ]

        # result
        return {'success': True, 'message': 'OK', 'result': result}
Beispiel #6
0
    def add(self, model, links, title=None, slug=None):
        """
        Add the given list of links based on the given model.
        """
        if title is None:
            title = model._meta.verbose_name_plural.title()

        if slug is None:
            slug = slugify(title)

        links = Acl.of(links.model).filter(self.request, links)

        self.links.append({
            'title':
            title,
            'type':
            model.__name__,
            'slug':
            slug,
            'links': [{
                'id': x.pk,
                'title': '%s' % x
            } for x in links]
        })
Beispiel #7
0
    def links(self, request):
        """
        Return a list of links (e.g. pages, documents etc).
        """
        items = []

        if 'cubane.cms' in settings.INSTALLED_APPS:
            from cubane.cms import get_page_model
            from cubane.cms.views import get_cms

            # pages
            page_model = get_page_model()
            pages = page_model.objects.filter(disabled=False)
            pages = Acl.of(page_model).filter(request, pages)
            items.append({
                'title':
                page_model._meta.verbose_name_plural.title(),
                'type':
                page_model.__name__,
                'slug':
                slugify(page_model._meta.verbose_name_plural),
                'links': [{
                    'id': x.id,
                    'title': x.title
                } for x in pages]
            })

            # child pages
            cms = get_cms()
            for model in cms.get_child_page_models():
                child_pages = model.objects.filter(disabled=False)
                child_pages = Acl.of(model).filter(request, child_pages)
                items.append({
                    'title':
                    model._meta.verbose_name_plural.title(),
                    'type':
                    model.__name__,
                    'slug':
                    slugify(model._meta.verbose_name_plural),
                    'links': [{
                        'id': x.id,
                        'title': x.title
                    } for x in child_pages]
                })

            # call back into CMS class to receive more links
            link_builder = LinkBuilder(request)
            cms.on_object_links(link_builder)
            items.extend(link_builder.links)

        if 'cubane.media' in settings.INSTALLED_APPS:
            from cubane.media.models import Media
            media = Media.objects.filter(is_image=False)
            media = Acl.of(Media).filter(request, media)
            items.append({
                'title':
                'Documents',
                'type':
                'Media',
                'slug':
                'documents',
                'links': [{
                    'id': x.id,
                    'title': x.caption
                } for x in media]
            })

        return {'items': items, 'links': None}