Пример #1
0
class ModelEditView(DetailView):
    queryset = MetaModel.get_non_primitive()
    template_name = 'metamodel/model_edit.html'

    def get_context_data(self, **kwargs):
        context = super(ModelEditView, self).get_context_data(**kwargs)

        form = MetaModelForm(instance=self.object)
        context['form'] = form

        return context

    def post(self, request, pk, *args, **kwargs):
        meta_model = self.get_object()

        form = MetaModelForm(request.POST, instance=meta_model)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(
                reverse('metamodel_model_meta',
                        kwargs={'pk': self.get_object().pk}))
        else:
            self.object = meta_model
            context = self.get_context_data(**kwargs)
            context['form'] = form
            return self.render_to_response(context)
Пример #2
0
class ModelDeleteView(DetailView):
    queryset = MetaModel.get_non_primitive()
    template_name = 'metamodel/model_delete.html'

    def post(self, request, pk, *args, **kwargs):
        meta_model = self.get_object()
        meta_model.delete()

        return HttpResponseRedirect(reverse('metamodel_model_list'))
Пример #3
0
class ModelAddInstance(DetailView):
    queryset = MetaModel.get_non_primitive()
    template_name = 'metamodel/model_add_instance.html'

    def is_popup(self):
        return bool(self.request.GET.get('popup', False))

    def get_context_data(self, **kwargs):
        context = super(ModelAddInstance, self).get_context_data(**kwargs)
        context['form'] = self.object.get_form()()
        is_popup = self.is_popup()

        form_action = reverse('metamodel_model_add_instance',
                              kwargs={'pk': self.object.pk})

        if is_popup:
            form_action += '?popup=1'

        context['hide_nav'] = is_popup
        context['form_action'] = form_action
        return context

    def post(self, request, pk, *args, **kwargs):
        meta_model = self.get_object()

        form = meta_model.get_form()(request.POST, request.FILES)

        if form.is_valid():
            instance_model = InstanceModel()
            instance_model.model = meta_model

            instance_model.save(initial=True)
            instance_model.update_fields(form.cleaned_data,
                                         request.POST,
                                         creator_id=request.user.id)

            if self.is_popup():
                return HttpResponseRedirect(
                    reverse('metamodel_instance_popup_redirect',
                            kwargs={'pk': instance_model.pk}))
            else:
                messages.success(
                    request,
                    u'<a href={0}>{1}</a> creada correctamente'.format(
                        reverse('metamodel_instance_detail',
                                kwargs={'pk': instance_model.pk}),
                        str(instance_model)))
                return HttpResponseRedirect(
                    reverse('metamodel_model_detail',
                            kwargs={'pk': meta_model.pk}))
        else:
            self.object = meta_model
            context = self.get_context_data(**kwargs)
            context['form'] = form
            return self.render_to_response(context)
Пример #4
0
class ModelMetaView(DetailView):
    queryset = MetaModel.get_non_primitive()
    template_name = 'metamodel/model_meta.html'

    def post(self, request, pk, *args, **kwargs):
        meta_model = self.get_object()

        for field in meta_model.fields.all():
            if field.name in request.POST:
                field.ordering = int(request.POST[field.name])
                field.save()

        return HttpResponseRedirect(
            reverse('metamodel_model_meta', kwargs={'pk': meta_model.pk}))
Пример #5
0
class ModelDetailView(DetailView):
    queryset = MetaModel.get_non_primitive()
    template_name = 'metamodel/model_detail.html'
Пример #6
0
class ModelListView(ListView):
    queryset = MetaModel.get_non_primitive()
    template_name = 'metamodel/model_list.html'
Пример #7
0
class MetaModelAddFieldView(DetailView):
    queryset = MetaModel.get_non_primitive()
    template_name = 'metamodel/model_add_field.html'

    def get(self, request, *args, **kwargs):
        if request.is_ajax():
            model_type = request.GET['model']
            nullable = request.GET['nullable'] == 'true'
            multiple = request.GET['multiple'] == 'true'

            meta_model = self.get_object()

            requires_default = True

            if nullable or multiple:
                requires_default = False

            if not meta_model.instancemodel_set.all():
                requires_default = False

            default_choices = None

            if model_type:
                meta_model = MetaModel.objects.get(pk=model_type)
                if meta_model.is_primitive():
                    default_choices = meta_model.html_input_type()
                else:
                    default_choices = [
                        (e.pk, str(e))
                        for e in meta_model.instancemodel_set.all()
                    ]

            return HttpResponse(json.dumps([requires_default,
                                            default_choices]))

        else:
            return super(MetaModelAddFieldView,
                         self).get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(MetaModelAddFieldView, self).get_context_data(**kwargs)

        form = MetaModelAddFieldForm()
        context['form'] = form

        return context

    def post(self, request, pk, *args, **kwargs):
        meta_model = self.get_object()

        form = MetaModelAddFieldForm(data=request.POST, files=request.FILES)

        if form.is_valid():
            meta_field = form.instance
            meta_field.parent = meta_model

            if meta_field.requires_default_value_for_saving():
                if meta_field.model.name == 'FileField':
                    default_value = request.FILES['default'].name
                    cleaned_default = meta_field.clean_value(default_value)

                    if hasattr(settings, 'METAMODEL'):
                        path = os.path.join(
                            settings.METAMODEL.get('MEDIA_PATH', ''),
                            cleaned_default)
                    else:
                        path = cleaned_default

                    cleaned_default = default_storage.save(
                        path, request.FILES['default'])
                else:
                    default_value = request.POST.get('default')
                    cleaned_default = meta_field.clean_value(default_value)

                meta_field.save(default=cleaned_default)
            else:
                meta_field.save()

            return HttpResponseRedirect(
                reverse('metamodel_model_meta', kwargs={'pk': meta_model.pk}))

        self.object = meta_model
        context = self.get_context_data(**kwargs)
        context['form'] = form
        return self.render_to_response(context)