Пример #1
0
    def enterAn_rest_inline_decl(
            self, ctx: ZmeiLangParser.An_rest_inline_declContext):
        name = ctx.an_rest_inline_name().getText()
        field = self.rest_config.fields_index[name]

        inline_model = field.get_rest_inline_model()
        if not inline_model:
            raise ValidationException(
                'field "{}" can not be used as inline'.format(field.name))

        serializer_name = self.rest_config.serializer_name + '_Inline' + inline_model.class_name

        new_config = RestSerializerConfig(serializer_name, inline_model, field)

        self.rest_config.inlines[name] = new_config

        self.rest_config.extension_serializers.append(new_config)

        self.rest_config.field_imports.append(
            FieldDeclaration(
                '{}.models'.format(inline_model.application.app_name),
                inline_model.class_name))

        self.rest_config.field_declarations.append(
            (field.name, '{}Serializer(many={}, read_only={})'.format(
                serializer_name, repr(field.is_many),
                repr(field.name in self.rest_config.read_only_fields))))

        self.rest_config_stack.append(self.rest_config)
        self.rest_config = new_config
Пример #2
0
    def get_rest_field(self):
        sizes_prepared = []

        for size in self.sizes:

            filters = []

            for filter in size.filters:
                if filter.name not in ('crop', 'upscale'):
                    raise ValidationException('Unknown image filter: {}'.format(filter.name))
                filters.append(filter.name)

                sizes_prepared.append('"{}": Size({}, {}, crop={}, upscale={})'.format(
                size.name, size.width, size.height, 'crop' in filters, 'upscale' in filters
            ))

        sizes_prepared = '{\n%s\n}' % (', \n'.join(sizes_prepared))

        return FieldDeclaration(
            [
                ('cratis_filer.serializers', 'ThumbnailImageField'),
                ('cratis_filer.utils', 'Size')
             ],
            'ThumbnailImageField({})'.format(sizes_prepared)
        )
Пример #3
0
 def get_rest_field(self):
     return FieldDeclaration(
         [
             ('cratis_filer.serializers', 'FileSerializer')
          ],
         'FileSerializer()'
     )
Пример #4
0
    def get_model_field(self):
        args = self.prepare_field_arguemnts({'related_name': self.related_name or '+'})

        return FieldDeclaration(
            [('django.db', 'models')],
            'models.ForeignKey("{}", {})'.format(self.related_class, gen_args(args, ['on_delete']))
        )
Пример #5
0
    def get_model_field(self):
        args = self.prepare_field_arguemnts({
            'upload_to':
            self.options or f'image_upload/{self.model.ref}/{self.name}',
        })

        return FieldDeclaration([('django.db', 'models')],
                                'models.ImageField({})'.format(gen_args(args)))
Пример #6
0
    def get_model_field(self):
        args = self.prepare_field_arguemnts({'max_length': self.max_length})

        if self.choices:
            args['choices'] = self.choices

        return FieldDeclaration([('django.db', 'models')],
                                'models.CharField({})'.format(gen_args(args)))
Пример #7
0
    def type_declarations(self):

        declarations = []

        if self.inline_type == 'tabular':
            if self.target_model.sortable:
                declarations.append(
                    FieldDeclaration('suit.admin', 'SortableTabularInline'))
            else:
                declarations.append(
                    FieldDeclaration('django.contrib.admin', 'TabularInline'))
        elif self.inline_type == 'polymorphic':
            if self.target_model.sortable:
                declarations.append(
                    FieldDeclaration('cratis_admin.admin',
                                     'StackedPolymorphicSortableInline'))
            else:
                declarations.append(
                    FieldDeclaration('polymorphic.admin',
                                     'StackedPolymorphicInline'))
        else:
            if self.target_model.sortable:
                declarations.append(
                    FieldDeclaration('suit.admin', 'SortableStackedInline'))
            else:
                declarations.append(
                    FieldDeclaration('django.contrib.admin', 'StackedInline'))

        if self.target_model.translatable:
            declarations.append(
                FieldDeclaration('modeltranslation.admin',
                                 'TranslationInlineModelAdmin'))

        return declarations
Пример #8
0
    def get_model_field(self):
        args = self.prepare_field_arguemnts({
            'related_name': '+'
        })

        return FieldDeclaration(
            [('filer.fields.image', 'FilerImageField')],
            'FilerImageField(on_delete=models.SET_NULL, {})'.format(gen_args(args))
        )
Пример #9
0
    def get_model_field(self):
        args = self.prepare_field_arguemnts()

        if self.choices:
            args['choices'] = self.choices

        return FieldDeclaration([('django.db', 'models')],
                                'models.IntegerField({})'.format(
                                    gen_args(args)))
Пример #10
0
    def get_model_field(self):
        args = self.prepare_field_arguemnts({'default': self.default})

        if 'null' in args:
            del args['null']

        return FieldDeclaration([('django.db', 'models')],
                                'models.BooleanField({})'.format(
                                    gen_args(args)))
Пример #11
0
    def get_rest_field(self):
        args = self.prepare_field_arguemnts()

        return FieldDeclaration(
            [
                ('cratis_filer.serializers', 'ImageFolderSerializer'),
                ('cratis_filer.utils', 'Size')
             ],
            'ImageFolderSerializer({}, {})'.format(self.sizes, gen_args(args))
        )
Пример #12
0
    def get_model_field(self):
        args = self.prepare_field_arguemnts({'related_name': self.related_name or '+'})

        if 'null' in args:
            del args['null']

        return FieldDeclaration(
            [('django.db', 'models')],
            'models.ManyToManyField("{}", {})'.format(self.related_class, gen_args(args, ['on_delete']))
        )
Пример #13
0
    def enterAn_rest_annotate_count(
            self, ctx: ZmeiLangParser.An_rest_annotate_countContext):
        field = ctx.an_rest_annotate_count_field().getText()
        kind = 'count'

        if ctx.an_rest_annotate_count_alias():
            alias = ctx.an_rest_annotate_count_alias().getText()
        else:
            alias = '{}_{}'.format(field, kind)

        self.rest_config.field_declarations.append(
            (alias, 'serializers.IntegerField()'))
        self.rest_config.field_names.append(alias)
        self.rest_config.annotations.append(
            "{}=Count('{}', distinct=True)".format(alias, field))
        self.rest_config.field_imports.append(
            FieldDeclaration('django.db.models', 'Count'))
Пример #14
0
    def get_model_field(self):
        imports = [('django.db', 'models')]

        own_args = {
            'max_digits': 15,
            'decimal_places': 2,
        }

        if self.positive:
            imports.append(('django.core.validators', 'MinValueValidator'))
            imports.append(('decimal', 'Decimal'))
            own_args['validators'] = '[MinValueValidator(Decimal("0.00"))]'

        args = self.prepare_field_arguemnts(own_args)

        return FieldDeclaration(
            imports, 'models.DecimalField({})'.format(
                gen_args(args, raw_args=['validators'])))
Пример #15
0
    def get_model_field(self):
        max_len = 0

        # TODO: get validation back
        # for field_name in self.field_names:
        #
        #     if field_name not in self.model.all_and_inherited_fields_map:
        #         raise ValidationException(
        #             'Slug field "{}" can not find field "{}" in the model'.format(self.name, field_name))
        #
        #     target_field = self.model.all_and_inherited_fields_map[field_name]
        #
        #     if not isinstance(target_field, TextFieldDef):
        #         raise ValidationException(
        #             'Slug field "{}" target field is not of type text()'.format(self.name))
        #
        #     max_len += target_field.max_length

        args = self.prepare_field_arguemnts({'max_length': 100})

        return FieldDeclaration([('django.db', 'models')],
                                'models.SlugField({})'.format(gen_args(args)))
Пример #16
0
    def get_model_field(self):
        args = self.prepare_field_arguemnts()

        return FieldDeclaration([('ckeditor.fields', 'RichTextField')],
                                'RichTextField({})'.format(gen_args(args)))
Пример #17
0
    def get_model_field(self):
        args = self.prepare_field_arguemnts()

        return FieldDeclaration([('django.db', 'models')],
                                'models.FloatField({})'.format(gen_args(args)))