Пример #1
0
    def sa_add(self):
        action = self.request.matched_route.name
        bc = breadcrumbs(self.tname, get_table_verbose_name(self.table),
                         action, self.pk)
        dbsession = self.request.dbsession
        try:
            obj = get_obj(dbsession, self.table, self.pk)
        except (NoResultFound, KeyError):
            raise HTTPNotFound
        form = SacrudForm(obj=obj, dbsession=dbsession,
                          request=self.request, table=self.table)()

        def options_for_response(form):
            return dict(
                form=form.render(), pk=self.pk, obj=obj, breadcrumbs=bc
            )

        if 'form.submitted' in self.request.params:
            controls = self.request.POST.items()
            pstruct = peppercorn.parse(controls)

            if '__formid__' in pstruct:
                try:
                    deserialized = form.validate_pstruct(pstruct).values()
                except deform.ValidationFailure as e:
                    return options_for_response(e)
                data = {k: preprocessing_value(v)
                        for d in deserialized
                        for k, v in d.items()}
            else:
                # if not peppercon format
                data = pstruct

            try:
                if action == PYRAMID_SACRUD_UPDATE:
                    obj = self.crud.update(self.pk, data)
                    flash_action = 'updated'
                else:
                    obj = self.crud.create(data)
                    flash_action = 'created'
                name = obj.__repr__()
                dbsession.flush()
            except SacrudMessagedException as e:
                self.flash_message(e.message, status=e.status)
                return self.get_response(options_for_response(form),
                                         SACRUD_EDIT_TEMPLATE)
            except Exception as e:
                transaction.abort()
                logging.exception("Something awful happened!")
                raise e
            transaction.commit()
            self.flash_message(_ps(
                u"You ${action} object of ${name}",
                mapping={'action': flash_action, 'name': escape(name or '')}
            ))
            return HTTPFound(
                location=self.request.route_url(PYRAMID_SACRUD_LIST,
                                                table=self.tname))
        return self.get_response(options_for_response(form),
                                 SACRUD_EDIT_TEMPLATE)
Пример #2
0
 def test_get_obj_with_bad_pk(self):
     self.assertEqual(None, get_obj(self.session, User, None))
     self.assertEqual(None, get_obj(self.session, User, ''))
     self.assertEqual(None, get_obj(self.session, User, {}))
Пример #3
0
 def test_get_obj_with_bad_pk(self):
     self.assertEqual(None, get_obj(self.session, User, None))
     self.assertEqual(None, get_obj(self.session, User, ''))
     self.assertEqual(None, get_obj(self.session, User, {}))
Пример #4
0
    def sa_add(self):
        bc = breadcrumbs(self.tname, get_table_verbose_name(self.table),
                         'sa_create')
        if self.pk:
            bc = breadcrumbs(self.tname,
                             get_table_verbose_name(self.table),
                             'sa_update',
                             id=self.pk)
        dbsession = self.request.dbsession
        try:
            obj = get_obj(dbsession, self.table, self.pk)
        except (NoResultFound, KeyError):
            raise HTTPNotFound
        form = SacrudForm(obj=obj,
                          dbsession=dbsession,
                          request=self.request,
                          table=self.table)()

        def get_responce(form):

            return dict(form=form.render(),
                        pk=self.pk,
                        obj=obj,
                        breadcrumbs=bc,
                        pk_to_list=pk_to_list)

        if 'form.submitted' in self.request.params:
            controls = self.request.POST.items()
            pstruct = peppercorn.parse(controls)
            if '__formid__' in pstruct:
                try:
                    deserialized = form.validate_pstruct(pstruct).values()
                except deform.ValidationFailure as e:
                    return get_responce(e)
                data = {
                    k: preprocessing_value(v)
                    for d in deserialized for k, v in d.items()
                }
            else:
                # if not peppercon format
                data = pstruct

            try:
                if self.pk:
                    obj = self.crud.update(self.pk, data)
                else:
                    obj = self.crud.create(data)
                name = obj.__repr__()
                dbsession.flush()
            except SacrudMessagedException as e:
                self.flash_message(e.message, status=e.status)
                return get_responce(form)
            except Exception as e:
                transaction.abort()
                logging.exception("Something awful happened!")
                raise e
            transaction.commit()

            if self.pk:
                self.flash_message(
                    _ps(u"You updated object of ${name}",
                        mapping={'name': escape(name or '')}))
            else:
                self.flash_message(
                    _ps("You created new object of ${name}",
                        mapping={'name': escape(name or '')}))
            return HTTPFound(
                location=self.request.route_url('sa_list', table=self.tname))
        return get_responce(form)