def get_generic_form(self, cls_name, form_cls, widgets={}, **kwargs):
     model_cls = get_class(cls_name)
     return modelform_factory(
         model_cls,
         exclude=[],
         form=form_cls,
         widgets=widgets)
def _handle_regions(regions, feincms_object, fail_silently=False):

    if not feincms_object:
        LOG.warning('Skipped {}' % regions.items())
        return None

    for region, widgets in six.iteritems(regions):
        i = 0
        for widget_cls, widget_attrs in six.iteritems(widgets):

            try:
                WidgetCls = get_class(widget_cls)
            except Exception as e:
                if not fail_silently:
                    raise Exception('Cannout load {} with {}'.format(
                        widget_cls, e))

            # TODO create form and validate options
            w_attrs = widget_attrs.get('attrs', {})
            w_attrs.update({
                'parent': feincms_object,
                'region': region,
                'ordering': w_attrs.get('ordering', i)
            })

            w_attrs['content_theme'] = WidgetContentTheme.objects.get(
                name=w_attrs.get('content_theme', 'default'),
                widget_class=WidgetCls.__name__)

            w_attrs['base_theme'] = _get_item(
                WidgetBaseTheme,
                w_attrs.get('base_theme', 'default'),
                "name")

            try:
                widget = WidgetCls(**w_attrs)
                widget.save(created=False)
            except Exception as e:
                if not fail_silently:
                    raise e
                else:
                    LOG.exception(e)

            else:
                for size, width in six.iteritems(
                        widget_attrs.get('dimensions', {})):

                    WidgetDimension(**{
                        'widget_id': widget.pk,
                        'widget_type': widget.content_type,
                        'size': size,
                        'width': width
                    }).save()

                i += 1
def get_widget_create_form(**kwargs):
    """
    widget = get_widget_from_id(widget_id)

    """
    model_cls = get_class(kwargs['cls_name'])

    form_class_base = getattr(
        model_cls, 'feincms_item_editor_form', WidgetCreateForm)

    WidgetModelForm = modelform_factory(model_cls,
                                        exclude=[],
                                        form=form_class_base,
                                        widgets=WIDGETS)

    return WidgetModelForm
def get_widget_create_form(**kwargs):
    """
    widget = get_widget_from_id(widget_id)

    """
    model_cls = get_class(kwargs['cls_name'])

    form_class_base = getattr(model_cls, 'feincms_item_editor_form',
                              WidgetCreateForm)

    WidgetModelForm = modelform_factory(model_cls,
                                        exclude=[],
                                        form=form_class_base,
                                        widgets=WIDGETS)

    return WidgetModelForm
Beispiel #5
0
def get_widget_update_form(**kwargs):
    """
    widget = get_widget_from_id(widget_id)

    """
    model_cls = get_class(kwargs['cls_name'])

    form_class_base = getattr(
        model_cls, 'feincms_item_editor_form', WidgetUpdateForm)

    default_widgets = WIDGETS
    default_widgets.update(getattr(model_cls, 'widgets', {}))

    WidgetModelForm = modelform_factory(model_cls,
                                        exclude=[],
                                        form=form_class_base,
                                        widgets=default_widgets)

    return WidgetModelForm
Beispiel #6
0
def get_widget_update_form(**kwargs):
    """
    widget = get_widget_from_id(widget_id)

    """
    model_cls = get_class(kwargs['cls_name'])

    form_class_base = getattr(model_cls, 'feincms_item_editor_form',
                              WidgetUpdateForm)

    default_widgets = WIDGETS
    default_widgets.update(getattr(model_cls, 'widgets', {}))

    WidgetModelForm = modelform_factory(model_cls,
                                        exclude=[],
                                        form=form_class_base,
                                        widgets=default_widgets)

    return WidgetModelForm
Beispiel #7
0
    def get_form(self, cls_name, **kwargs):

        if cls_name not in self._forms:

            model_cls = get_class(cls_name)

            form_class_base = getattr(model_cls, 'feincms_item_editor_form',
                                      WidgetForm)

            default_widgets = WIDGETS

            model_cls.init_widgets()

            default_widgets.update(getattr(model_cls, 'widgets', {}))

            self._forms[cls_name] = modelform_factory(model_cls,
                                                      exclude=[],
                                                      form=form_class_base,
                                                      widgets=default_widgets)

        return self._forms[cls_name]
    def get_form(self, cls_name, **kwargs):

        if cls_name not in self._forms:

            model_cls = get_class(cls_name)

            form_class_base = getattr(
                model_cls, 'feincms_item_editor_form', WidgetForm)

            default_widgets = WIDGETS

            model_cls.init_widgets()

            default_widgets.update(getattr(model_cls, 'widgets', {}))

            self._forms[cls_name] = modelform_factory(
                model_cls,
                exclude=[],
                form=form_class_base,
                widgets=default_widgets)

        return self._forms[cls_name]
def _handle_regions(regions, feincms_object):

    for region, widgets in six.iteritems(regions):
        for widget_cls, widget_attrs in six.iteritems(widgets):

            try:
                WidgetCls = get_class(widget_cls)
            except Exception as e:
                raise Exception('Cannout load {} with {}'.format(
                    widget_cls, e))

            # TODO create form and validate options
            w_attrs = widget_attrs.get('attrs', {})
            w_attrs.update({
                'parent': feincms_object,
                'region': region,
                'ordering': 0
            })

            w_attrs['content_theme'] = WidgetContentTheme.objects.get(
                name=w_attrs['content_theme'],
                widget_class=WidgetCls.__name__)
            w_attrs['base_theme'] = WidgetBaseTheme.objects.get(
                name=w_attrs['base_theme'])
            widget = WidgetCls(**w_attrs)
            widget.save(created=False)

            for size, width in six.iteritems(
                    widget_attrs.get('dimenssions', {})):

                WidgetDimension(**{
                    'widget_id': widget.pk,
                    'widget_type': widget.content_type,
                    'size': size,
                    'width': width
                }).save()
Beispiel #10
0
 def model_class(self):
     return get_class(self.kwargs["cls_name"])
Beispiel #11
0
 def get_generic_form(self, cls_name, form_cls, widgets={}, **kwargs):
     model_cls = get_class(cls_name)
     return modelform_factory(model_cls,
                              exclude=[],
                              form=form_cls,
                              widgets=widgets)
def load_data(data, fail_silently=True):

    root_page = None

    for username, user_attrs in six.iteritems(data.pop('auth.User', {})):

        if not User.objects.filter(
                username=username,
                email=user_attrs['mail']).exists():

            try:
                # create and login user
                User.objects.create_superuser(
                    username, user_attrs['mail'], user_attrs['password'])
            except Exception as e:
                if not fail_silently:
                    raise e
                else:
                    LOG.exception(e)

    for page_name, page_attrs in six.iteritems(data.pop('web.Page', {})):

        page_theme_name = page_attrs.pop('theme', '__first__')
        page_color_scheme_name = page_attrs.pop('color_scheme', '__first__')

        regions = page_attrs.pop('content', {})

        if not (PageTheme.objects.exists() or
                PageColorScheme.objects.exists()):
            raise Exception(
                "You havent any themes please install someone and run sync_all")

        page_attrs['theme'] = _get_item(PageTheme, page_theme_name, "name")

        page_attrs['color_scheme'] = _get_item(PageColorScheme,
                                               page_color_scheme_name, "name")

        parent = page_attrs.get('parent', None)

        if parent:
            if str(parent).isdigit():
                page_attrs['parent'] = _get_item(Page, parent)
            else:
                page_attrs['parent'] = _get_item(Page, parent, "slug")

        page, created = get_or_create(Page, fail_silently, **page_attrs)

        # TODO from attrs etc..
        root_page = page

        _handle_regions(regions, page, fail_silently)

    # generic stuff
    for cls_name, entries in six.iteritems(data):

        for entry, cls_attrs in six.iteritems(entries):

            cls = get_class(cls_name)

            regions = cls_attrs.pop('content', {})

            # load FK from
            # author: {'pk': 1, 'type': 'auth.User'}
            for attr, value in six.iteritems(cls_attrs):
                if isinstance(value, (dict, OrderedDict)):
                    cls_type = value.get('type', None)
                    if cls_type:
                        try:
                            cls_attrs[attr] = _get_item(get_class(
                                cls_type), value.get('pk'))
                        except Exception as e:
                            if not fail_silently:
                                raise Exception(
                                    'Cannot load FK {} not Found original exception {}'.format(cls_type, e))

            instance, created = get_or_create(cls, fail_silently, **cls_attrs)

            _handle_regions(regions, instance, fail_silently)

    return root_page
def create_new_site(run_syncall=False, with_user=True, request=None,
                    name='demo.yaml', url=None):
    """load all available scripts and try scaffold new site from them

    TODO(majklk): refactor and support for more cases

    """

    if run_syncall:
        from django.core import management
        management.call_command('sync_all', force=True)

    if url:
        try:
            BOOTSTRAP = _load_from_stream(requests.get(url).text)
        except Exception as e:
            raise e
    else:
        try:
            scripts = get_loaded_scripts()
            BOOTSTRAP = scripts[name]
        except KeyError:
            raise Exception('Cannot find {} in {} loaded from {}'.format(
                name, scripts, LEONARDO_BOOTSTRAP_DIR))

    root_page = None

    for username, user_attrs in six.iteritems(BOOTSTRAP.pop('auth.User', {})):

        # create and login user
        if with_user and not User.objects.exists():
            User.objects.create_superuser(
                username, user_attrs['mail'], user_attrs['password'])

            # login
            if request:
                auth_user = authenticate(
                    username=username, password=user_attrs['password'])
                login(request, auth_user)

    for page_name, page_attrs in six.iteritems(BOOTSTRAP.pop('web.Page', {})):

        page_theme_name = page_attrs.pop('theme', '__first__')
        page_color_scheme_name = page_attrs.pop('color_scheme', '__first__')

        regions = page_attrs.pop('content', {})

        if not (PageTheme.objects.exists() or
                PageColorScheme.objects.exists()):
            raise Exception("You havent any themes \
                please install someone and run sync_all")

        try:
            if page_theme_name == '__first__':
                theme = PageTheme.objects.first()
            else:
                theme = PageTheme.objects.get(name=page_theme_name)
        except PageTheme.DoesNotExist:
            raise Exception(
                "Page theme %s not found" % page_theme_name)
        except Exception as e:
            raise Exception(
                "Page theme find more than one PageTheme for %s not found" % page_theme_name)

        else:
            page_attrs['theme'] = theme

        try:
            if page_color_scheme_name == '__first__':
                color_scheme = PageColorScheme.objects.first()
            else:
                color_scheme = PageColorScheme.objects.get(
                    name__icontains=page_color_scheme_name)
        except PageColorScheme.DoesNotExist:
            raise Exception("Page Color Scheme %s "
                            "not found" % page_color_scheme_name)
        else:
            page_attrs['color_scheme'] = color_scheme

        page, created = Page.objects.get_or_create(**page_attrs)

        # TODO from attrs etc..
        root_page = page

        _handle_regions(regions, page)

    # generic stuff
    for cls_name, entries in six.iteritems(BOOTSTRAP):

        for entry, cls_attrs in six.iteritems(entries):

            cls = get_class(cls_name)

            regions = cls_attrs.pop('content', {})

            # load FK from
            # author: {'pk': 1, 'type': 'auth.User'}
            for attr, value in six.iteritems(cls_attrs):
                if isinstance(value, dict):
                    cls_type = value.get('type', None)
                    if cls_type:
                        try:
                            cls_attrs[attr] = get_class(
                                cls_type).objects.get(pk=value.get('pk'))
                        except Exception as e:
                            raise Exception(
                                'Cannot load FK {} not Found original exception {}'.format(cls_type, e))

            instance, created = cls.objects.get_or_create(**cls_attrs)

            _handle_regions(regions, instance)

    return root_page