def makeSession(self): uid = self._mkuid() session = DBSession() session.uid = uid store.add(session) store.commit() return session
def render_create(self, request): presets = json.loads(request.args.get('presets', [''])[0] or '{}') noEdit = json.loads(request.args.get('noedit', [''])[0] or '[]') template = templateLookup.get_template("/crud/form.mak") fakeObj = self.model() # XXX TODO - Take a counter argument from the request here, # so Javascript can product lots of these which don't conflict. fakeObj.fakeID = '*1' fakeObj.noEdit = noEdit for (k,v) in presets.iteritems(): setattr(fakeObj, k, v) # We do an add/rollback dance here to work around some strange Storm # behaviour, where references don't get a value unless their object # is added, even if it isn't committed. store.add(fakeObj) rv = helpers.renderTemplateObj(request, template, obj=self.crudModel(fakeObj)) store.rollback() return rv
def applyForm(objects, request): errors = [] actions = [] results = { 'created': [], 'updated': [], 'deleted': [], } for jsobj in objects: if not all(jsobj.get(k) for k in ('action', 'model')): raise ValueError("Invalid object: %s" % jsobj) try: model, crudClass = exposedStormClasses[jsobj['model']] except KeyError: errors.append((None, u"Unknown or unexposed model: %s" % jsobj['model'])) continue if jsobj['action'] == 'create': obj = model() obj.fakeID = 'n' + jsobj['id'] results['created'].append(obj) # We haven't added the object to the store, because it may not # satisfy constraints yet. Down below, when we know things are # okay, we go back and add it. else: try: objID = int(jsobj['id']) except ValueError: errors.append((None, u"Invalid id: %s" % jsobj['id'])) continue obj = store.get(model, objID) if obj is None: errors.append((None, u"Missing ID '%s' for model '%s'" % (jsobj['id'], jsobj['model']))) continue results['updated'].append(obj) for (key, val) in jsobj['fields'].iteritems(): try: attr = str(key) except UnicodeEncodeError: errors.append((None, u"Invalid field name '%s'" % attr)) continue # XXX TODO -- Access check goes here (or, uh, somewhere) # ... crud = crudClass(obj) actions.append( (crud, attr, val) ) if errors: return (False, errors) for (crud, attr, val) in actions: error = crud.save(attr, val, request) if error is not None: key = crud.getProxy(attr, request).fieldName() errors.append((key, error)) if errors: return (False, errors) for obj in results['created']: store.add(obj) return (True, results)
def applyForm(objects, request): errors = [] actions = [] results = { 'created': [], 'updated': [], 'deleted': [], } for jsobj in objects: if not all(jsobj.get(k) for k in ('action', 'model')): raise ValueError("Invalid object: %s" % jsobj) try: model = exposedStormClasses[jsobj['model']] except KeyError: errors.append((None, u"Unknown model: %s" % jsobj['model'])) continue try: model.__warp_crud__ except AttributeError: errors.append((None, u"Model %s has no crud class" % jsobj['model'])) continue if jsobj['action'] == 'create': obj = model() store.add(obj) obj.fakeID = '*' + jsobj['id'] results['created'].append(obj) else: try: objID = int(jsobj['id']) except ValueError: errors.append((None, u"Invalid id: %s" % jsobj['id'])) continue obj = store.get(model, objID) if obj is None: errors.append((None, u"Missing ID '%s' for model '%s'" % (jsobj['id'], jsobj['model']))) continue results['updated'].append(obj) for (key, val) in jsobj['fields'].iteritems(): try: attr = str(key) except UnicodeEncodeError: errors.append((None, u"Invalid field name '%s'" % attr)) continue # XXX TODO -- Access check goes here (or, uh, somewhere) # ... crud = model.__warp_crud__(obj) actions.append( (crud, attr, val) ) if errors: return (False, errors) for (crud, attr, val) in actions: error = crud.save(attr, val, request) if error is not None: key = crud.getProxy(attr, request).fieldName() errors.append((key, error)) if errors: return (False, errors) return (True, results)